Refactor move tests from ebean-core to ebean-test

This commit is contained in:
rbygrave committed 2021-09-08 22:49:23 +12:00
1 parent 0b00a14fd3
commit eab56d323b
1650 files changed
+185 -147

No files matched your search

@@ -0,0 +1,33 @@
package org.tests.idkeys;
import io.avaje.moduuid.ModUUID;
import io.ebean.BaseTestCase;
import io.ebean.DB;
import org.junit.jupiter.api.Test;
import org.tests.model.basic.ECustomId;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class TestCustomId extends BaseTestCase {
@Test
public void insert() {
ECustomId bean = new ECustomId("fred");
DB.save(bean);
assertNotNull(bean.getId());
}
@Test
public void insert_when_hasValue() {
ECustomId bean = new ECustomId("gotId");
String explicitId = ModUUID.newShortId();
bean.setId(explicitId);
DB.save(bean);
ECustomId found = DB.find(ECustomId.class, explicitId);
assertEquals(found.getName(), bean.getName());
}
}
@@ -0,0 +1,93 @@
package org.tests.idkeys;
import io.ebean.BaseTestCase;
import io.ebean.Transaction;
import io.ebean.annotation.ForPlatform;
import io.ebean.annotation.Platform;
import io.ebean.config.dbplatform.IdType;
import io.ebeaninternal.api.SpiEbeanServer;
import org.junit.jupiter.api.Test;
import org.tests.idkeys.db.GenKeyIdentity;
import org.tests.idkeys.db.GenKeySequence;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class TestGeneratedKeys extends BaseTestCase {
@Test
@ForPlatform(Platform.H2) // readSequenceValue is H2 specific
public void testSequence() throws SQLException {
SpiEbeanServer server = spiEbeanServer();
if (idType() != IdType.SEQUENCE) {
// only run this test when SEQUENCE is being used
return;
}
try (Transaction tx = server.beginTransaction()) {
long sequenceStart = readSequenceValue(tx, GenKeySequence.SEQUENCE_NAME);
GenKeySequence al = new GenKeySequence();
al.setDescription("my description");
server.save(al);
long sequenceCurrent = readSequenceValue(tx, GenKeySequence.SEQUENCE_NAME);
assertNotNull(al.getId());
assertFalse(sequenceStart == sequenceCurrent);
assertEquals(sequenceStart + 20, sequenceCurrent);
}
}
private long readSequenceValue(Transaction tx, String sequence) throws SQLException {
Statement stm = null;
try {
stm = tx.connection().createStatement();
ResultSet rs = stm.executeQuery("select currval('" + sequence + "')");
rs.next();
return rs.getLong(1);
} finally {
if (stm != null) {
try {
stm.close();
} catch (SQLException e) {
}
}
}
}
@Test
public void testIdentity() throws SQLException {
if (idType() != IdType.IDENTITY) {
// only run this test when SEQUENCE is being used
return;
}
try (Transaction tx = server().beginTransaction()) {
GenKeyIdentity al = new GenKeyIdentity();
al.setDescription("my description");
server().save(al);
// For JDBC batching we won't get the id until after
// the batch has been flushed explicitly or via commit
//assertNotNull(al.getId());
tx.commit();
assertNotNull(al.getId());
}
}
}
@@ -0,0 +1,44 @@
package org.tests.idkeys;
import org.junit.jupiter.api.Test;
import org.tests.model.basic.TOne;
import io.ebean.BaseTestCase;
import static org.junit.jupiter.api.Assertions.*;
import java.sql.SQLException;
import java.util.List;
/**
* Test lazy loading
*/
public class TestLazyLoad extends BaseTestCase {
/**
* This test loads just a single property of the Entity AuditLog and later on access
* the description which should force a lazy load of this property
*/
@Test
public void testPartialLoad() throws SQLException {
TOne log = new TOne();
log.setName("test partial");
log.setDescription("log");
server().save(log);
assertNotNull(log.getId());
List<TOne> logs = server().find(TOne.class)
.select("id")
.where().eq("id", log.getId())
.findList();
assertNotNull(logs);
assertEquals(1, logs.size());
TOne logLazy = logs.get(0);
String description = logLazy.getDescription();
assertEquals(log.getDescription(), description);
}
}
@@ -0,0 +1,51 @@
package org.tests.idkeys;
import io.ebean.BaseTestCase;
import io.ebean.DB;
import org.junit.jupiter.api.Test;
import org.tests.model.basic.ESimple;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class TestSimpleIdInsert extends BaseTestCase {
@Test
public void test() {
ESimple e = new ESimple();
e.setName("name");
DB.save(e);
assertNotNull(e.getId());
}
// // This test fails with jdbc drivers that don't
// // support batch insert with getGeneratedKeys
// public void testJdbcBatch() {
//
// GlobalProperties.put("datasource.default", "hsqldb");
// GlobalProperties.put("ebean.classes", ESimple.class.getName());
//
// Transaction transaction = DB.beginTransaction();
// try {
// transaction.setBatchMode(true);
// transaction.setLogLevel(LogLevel.SQL);
// ESimple e = new ESimple();
// e.setName("name");
// DB.save(e);
//
// ESimple e2 = new ESimple();
// e2.setName("name2");
// DB.save(e2);
// transaction.commit();
//
// Assert.assertNotNull(e.getId());
// Assert.assertNotNull(e2.getId());
//
// } finally {
// DB.endTransaction();
// }
// }
}
@@ -0,0 +1,46 @@
package org.tests.idkeys.db;
import io.ebean.annotation.Identity;
import javax.persistence.Entity;
import javax.persistence.Id;
import static io.ebean.annotation.IdentityGenerated.BY_DEFAULT;
@Identity(generated = BY_DEFAULT, start = 1000, cache = 100)
@Entity
// Just let Ebean define Identity mechanism for testing across all DB types
//@SequenceGenerator(name = "AD_SEQ_NAME", sequenceName = "AD_SEQ")
public class AuditLog {
@Id
//@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "AD_SEQ_NAME")
private Long id;
private String description;
private String modifiedDescription;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getModifiedDescription() {
return modifiedDescription;
}
public void setModifiedDescription(String modifiedDescription) {
this.modifiedDescription = modifiedDescription == null ? null : "_" + modifiedDescription + "_";
}
}
@@ -0,0 +1,31 @@
package org.tests.idkeys.db;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class GenKeyIdentity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String description;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
@@ -0,0 +1,33 @@
package org.tests.idkeys.db;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class GenKeySequence {
public final static String SEQUENCE_NAME = "SEQ";
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_NAME")
private Long id;
private String description;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}