Skip to content

Commit

Permalink
jakartaee/persistence#417 - jUnit tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Tomáš Kraus <[email protected]>
  • Loading branch information
Tomas-Kraus committed Sep 13, 2023
1 parent a056e0e commit d7c56c9
Show file tree
Hide file tree
Showing 4 changed files with 305 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public Persistence32TableCreator() {
addTableDefinition(buildTypeTable());
addTableDefinition(buildPokemonTable());
addTableDefinition(buildPokemonTypeTable());
addTableDefinition(buildSyntaxEntityTable());
}

public static TableDefinition buildTypeTable() {
Expand All @@ -48,4 +49,15 @@ public static TableDefinition buildPokemonTypeTable() {
return table;
}

public static TableDefinition buildSyntaxEntityTable() {
TableDefinition table = new TableDefinition();
table.setName("PERSISTENCE32_SYNTAX_ENTITY");
table.addField(createNumericPk("ID"));
table.addField(createStringColumn("STR_VAL_1", 128, true));
table.addField(createStringColumn("STR_VAL_2", 128, true));
table.addField(createNumericColumn("INT_VAL_1", 15, true));
table.addField(createNumericColumn("INT_VAL_2", 15, true));
return table;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.eclipse.persistence.testing.models.jpa.persistence32;

import java.util.Collection;

import jakarta.persistence.CollectionTable;
import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.Table;

// JPQL query and Criteria query tests
@Entity
@Table(name="PERSISTENCE32_SYNTAX_ENTITY")
public class SyntaxEntity {

@Id
private long id;

@Column(name = "STR_VAL_1")
private String strVal1;
@Column(name = "STR_VAL_2")
private String strVal2;

@Column(name = "INT_VAL_1")
private Integer intVal1;
@Column(name = "INT_VAL_2")
private Integer intVal2;

@ElementCollection
@CollectionTable(name = "COLTABLE1", joinColumns = @JoinColumn(name = "ent_id"))
private Collection<String> colVal1;

public SyntaxEntity() {
}

public SyntaxEntity(Long id) {
this.id = id;
}

public SyntaxEntity(Long id, String strVal1, String strVal2, Integer intVal1, Integer intVal2) {
this.id = id;
this.strVal1 = strVal1;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getStrVal1() {
return strVal1;
}

public void setStrVal1(String strVal1) {
this.strVal1 = strVal1;
}

public String getStrVal2() {
return strVal2;
}

public void setStrVal2(String strVal2) {
this.strVal2 = strVal2;
}

public Integer getIntVal1() {
return intVal1;
}

public void setIntVal1(Integer intVal1) {
this.intVal1 = intVal1;
}

public Integer getIntVal2() {
return intVal2;
}

public void setIntVal2(Integer intVal2) {
this.intVal2 = intVal2;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>org.eclipse.persistence.testing.models.jpa.persistence32.Pokemon</class>
<class>org.eclipse.persistence.testing.models.jpa.persistence32.Type</class>
<class>org.eclipse.persistence.testing.models.jpa.persistence32.SyntaxEntity</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="jakarta.persistence.schema-generation.scripts.action" value="none"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
package org.eclipse.persistence.testing.tests.jpa.persistence32;

import java.util.List;

import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityTransaction;
import jakarta.persistence.TypedQuery;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.ParameterExpression;
import jakarta.persistence.criteria.Root;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl;
import org.eclipse.persistence.jpa.JpaEntityManagerFactory;
import org.eclipse.persistence.testing.framework.jpa.junit.JUnitTestCase;
import org.eclipse.persistence.testing.models.jpa.persistence32.Persistence32TableCreator;
import org.eclipse.persistence.testing.models.jpa.persistence32.SyntaxEntity;
import org.junit.Assert;

public class CriteriaBuilderTests extends JUnitTestCase {

// SyntaxEntity instances, array index is equal to ID
private static final SyntaxEntity[] ENTITIES = new SyntaxEntity[] {
null, // Skip array index 0
new SyntaxEntity(1L, "Left", null, null, null),
new SyntaxEntity(2L, "right", null, null, null),
new SyntaxEntity(3L, "LeftToken", "TokenRight", null, null)
};

private JpaEntityManagerFactory emf = null;

public static Test suite() {
TestSuite suite = new TestSuite();
suite.setName("CriteriaBuilderTests");
suite.addTest(new CriteriaBuilderTests("testSetup"));
suite.addTest(new CriteriaBuilderTests("testLeftIntLen"));
suite.addTest(new CriteriaBuilderTests("testLeftExprLen"));
suite.addTest(new CriteriaBuilderTests("testRightIntLen"));
suite.addTest(new CriteriaBuilderTests("testRightExprLen"));
suite.addTest(new CriteriaBuilderTests("testReplaceExprExpr"));
return suite;
}

public CriteriaBuilderTests() {
}

public CriteriaBuilderTests(String name) {
super(name);
setPuName(getPersistenceUnitName());
}

@Override
public String getPersistenceUnitName() {
return "persistence32";
}

@Override
public void setUp() {
super.setUp();
emf = getEntityManagerFactory(getPersistenceUnitName()).unwrap(EntityManagerFactoryImpl.class);
}

/**
* The setup is done as a test, both to record its failure, and to allow
* execution in the server.
*/
public void testSetup() {
new Persistence32TableCreator().replaceTables(JUnitTestCase.getServerSession(getPersistenceUnitName()));
clearCache();
try (EntityManager em = emf.createEntityManager()) {
EntityTransaction et = em.getTransaction();
try {
et.begin();
for (int i = 1; i < ENTITIES.length; i++) {
em.persist(ENTITIES[i]);
}
et.commit();
} catch (Exception e) {
et.rollback();
throw e;
}
}
}

public void testLeftIntLen() {
try (EntityManager em = emf.createEntityManager()) {
EntityTransaction et = em.getTransaction();
try {
et.begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<SyntaxEntity> cquery = cb.createQuery(SyntaxEntity.class);
Root<SyntaxEntity> root = cquery.from(SyntaxEntity.class);
ParameterExpression<String> strParam1 = cb.parameter(String.class, "strParam1");
cquery.where(cb.equal(root.get("strVal1"), cb.left(strParam1, 4)));
TypedQuery<SyntaxEntity> query = em.createQuery(cquery);
query.setParameter(strParam1, "Left substring to extract");
List<SyntaxEntity> result = query.getResultList();
Assert.assertEquals(1, result.size());
Assert.assertEquals(1L, result.get(0).getId());
et.commit();
} catch (Exception e) {
et.rollback();
throw e;
}
}
}

public void testLeftExprLen() {
try (EntityManager em = emf.createEntityManager()) {
EntityTransaction et = em.getTransaction();
try {
et.begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<SyntaxEntity> cquery = cb.createQuery(SyntaxEntity.class);
Root<SyntaxEntity> root = cquery.from(SyntaxEntity.class);
ParameterExpression<String> strParam1 = cb.parameter(String.class, "strParam1");
cquery.where(cb.equal(root.get("strVal1"), cb.left(strParam1, cb.literal(4))));
TypedQuery<SyntaxEntity> query = em.createQuery(cquery);
query.setParameter(strParam1, "Left substring to extract");
List<SyntaxEntity> result = query.getResultList();
Assert.assertEquals(1, result.size());
Assert.assertEquals(1L, result.get(0).getId());
et.commit();
} catch (Exception e) {
et.rollback();
throw e;
}
}
}

public void testRightIntLen() {
try (EntityManager em = emf.createEntityManager()) {
EntityTransaction et = em.getTransaction();
try {
et.begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<SyntaxEntity> cquery = cb.createQuery(SyntaxEntity.class);
Root<SyntaxEntity> root = cquery.from(SyntaxEntity.class);
ParameterExpression<String> strParam1 = cb.parameter(String.class, "strParam1");
cquery.where(cb.equal(root.get("strVal1"), cb.right(strParam1, 5)));
TypedQuery<SyntaxEntity> query = em.createQuery(cquery);
query.setParameter(strParam1, "Extract substring from right");
List<SyntaxEntity> result = query.getResultList();
Assert.assertEquals(1, result.size());
Assert.assertEquals(2L, result.get(0).getId());
et.commit();
} catch (Exception e) {
et.rollback();
throw e;
}
}
}

public void testRightExprLen() {
try (EntityManager em = emf.createEntityManager()) {
EntityTransaction et = em.getTransaction();
try {
et.begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<SyntaxEntity> cquery = cb.createQuery(SyntaxEntity.class);
Root<SyntaxEntity> root = cquery.from(SyntaxEntity.class);
ParameterExpression<String> strParam1 = cb.parameter(String.class, "strParam1");
cquery.where(cb.equal(root.get("strVal1"), cb.right(strParam1, cb.literal(5))));
TypedQuery<SyntaxEntity> query = em.createQuery(cquery);
query.setParameter(strParam1, "Extract substring from right");
List<SyntaxEntity> result = query.getResultList();
Assert.assertEquals(1, result.size());
Assert.assertEquals(2L, result.get(0).getId());
et.commit();
} catch (Exception e) {
et.rollback();
throw e;
}
}
}

public void testReplaceExprExpr() {
// Skip for Derby
if (emf.getServerSession().getPlatform().isDerby()) {
return;
}
try (EntityManager em = emf.createEntityManager()) {
EntityTransaction et = em.getTransaction();
try {
et.begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<SyntaxEntity> cquery = cb.createQuery(SyntaxEntity.class);
Root<SyntaxEntity> root = cquery.from(SyntaxEntity.class);
ParameterExpression<String> strParam1 = cb.parameter(String.class, "strParam1");
cquery.where(cb.equal(root.get("strVal1"), cb.replace(strParam1, cb.literal("Unknown"), cb.literal("Left"))));
TypedQuery<SyntaxEntity> query = em.createQuery(cquery);
query.setParameter(strParam1, "UnknownToken");
List<SyntaxEntity> result = query.getResultList();
Assert.assertEquals(1, result.size());
Assert.assertEquals(2L, result.get(0).getId());
et.commit();
} catch (Exception e) {
et.rollback();
throw e;
}
}
}

}

0 comments on commit d7c56c9

Please sign in to comment.