Merge remote-tracking branch 'origin/master' into bugfix/fix-gen-key-sequence-test

This commit is contained in:
Thomas Fellner committed 2021-09-14 11:29:00 +02:00
commit 45e235cb64
2057 files changed
+13934 -14678

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,113 @@
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.GenKeySeqA;
import org.tests.idkeys.db.GenKeySeqB;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
public class TestGeneratedKeys extends BaseTestCase {
@Test
@ForPlatform(Platform.H2) // readSequenceValue is H2 specific
public void testGenKeySeqA() throws SQLException {
assumeTrue(idType() == IdType.SEQUENCE);
SpiEbeanServer server = spiEbeanServer();
try (Transaction tx = server.beginTransaction()) {
long sequenceStart = readSequenceValue(tx, GenKeySeqA.SEQUENCE_NAME);
GenKeySeqA al = new GenKeySeqA();
al.setDescription("my description");
server.save(al);
long sequenceCurrent = readSequenceValue(tx, GenKeySeqA.SEQUENCE_NAME);
assertNotNull(al.getId());
assertFalse(sequenceStart == sequenceCurrent);
assertEquals(sequenceStart + 20, sequenceCurrent);
}
}
@Test
@ForPlatform(Platform.H2) // readSequenceValue is H2 specific
public void testGenKeySeqB() throws SQLException {
assumeTrue(idType() == IdType.SEQUENCE);
SpiEbeanServer server = spiEbeanServer();
try (Transaction tx = server.beginTransaction()) {
long sequenceStart = readSequenceValue(tx, GenKeySeqB.SEQUENCE_NAME);
GenKeySeqB al = new GenKeySeqB();
al.setDescription("my description");
server.save(al);
long sequenceCurrent = readSequenceValue(tx, GenKeySeqB.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 io.ebean.BaseTestCase;
import org.junit.jupiter.api.Test;
import org.tests.model.basic.TOne;
import java.sql.SQLException;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
/**
* 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,38 @@
package org.tests.idkeys.db;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class GenKeySeqA {
public final static String SEQUENCE_NAME = "GEN_KEY_A_SEQ_NAME";
/**
* {@link GeneratedValue#generator()} is not empty, but no {@code SequenceGenerator} is present.
* So the sequence is named like the generator: {@link #SEQUENCE_NAME}.
*/
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQUENCE_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;
}
}
@@ -0,0 +1,42 @@
package org.tests.idkeys.db;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
@Entity
public class GenKeySeqB {
public final static String SEQUENCE_NAME = "GEN_KEY_B_SEQ_NAME";
private final static String SEQ_GEN_NAME = "SEQ_GEN_NAME";
/**
* {@link GeneratedValue#generator()} links to {@link SequenceGenerator#name()}.
* The name of the sequence is {@link SequenceGenerator#sequenceName()}.
*/
@Id
@SequenceGenerator(name = SEQ_GEN_NAME, sequenceName = GenKeySeqB.SEQUENCE_NAME)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQ_GEN_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;
}
}