forked from eclipse-ee4j/eclipselink
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
jakartaee/persistence#417 - jUnit tests.
Signed-off-by: Tomáš Kraus <[email protected]>
- Loading branch information
1 parent
a056e0e
commit d7c56c9
Showing
4 changed files
with
305 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
.../src/main/java/org/eclipse/persistence/testing/models/jpa/persistence32/SyntaxEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
205 changes: 205 additions & 0 deletions
205
...st/java/org/eclipse/persistence/testing/tests/jpa/persistence32/CriteriaBuilderTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
|
||
} |