Merge pull request #2308 from FOCONIS/bugfix/fix-gen-key-sequence-test

fixing test entity GenKeySequence
This commit is contained in:
Rob Bygrave authored and GitHub committed 2021-09-20 11:59:15 +12:00
commit ad0befe90a
3 files changed
+82 -13

No files matched your search

@@ -8,36 +8,58 @@ 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 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 testSequence() throws SQLException {
public void testGenKeySeqA() throws SQLException {
assumeTrue(idType() == IdType.SEQUENCE);
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);
long sequenceStart = readSequenceValue(tx, GenKeySeqA.SEQUENCE_NAME);
GenKeySequence al = new GenKeySequence();
GenKeySeqA al = new GenKeySeqA();
al.setDescription("my description");
server.save(al);
long sequenceCurrent = readSequenceValue(tx, GenKeySequence.SEQUENCE_NAME);
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);
@@ -6,11 +6,16 @@ import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class GenKeySequence {
public final static String SEQUENCE_NAME = "SEQ";
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 = "SEQ_NAME")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQUENCE_NAME)
private Long id;
private String 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;
}
}