Skip to content

Commit

Permalink
Improving tests
Browse files Browse the repository at this point in the history
Tests are run against both Hibernate and EclipseLink JPA
  • Loading branch information
ivelkov committed Aug 15, 2017
1 parent 7458a9e commit 60d0d77
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 189 deletions.
107 changes: 0 additions & 107 deletions src/test/java/com/github/tennaito/rsql/jpa/AbstractVisitorTest.java

This file was deleted.

This file was deleted.

109 changes: 91 additions & 18 deletions src/test/java/com/github/tennaito/rsql/jpa/JpaVisitorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,38 @@
package com.github.tennaito.rsql.jpa;


import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.fail;
import com.github.tennaito.rsql.builder.BuilderTools;
import com.github.tennaito.rsql.jpa.entity.Course;
import com.github.tennaito.rsql.jpa.entity.CourseDetails;
import com.github.tennaito.rsql.jpa.entity.Department;
import com.github.tennaito.rsql.jpa.entity.Person;
import com.github.tennaito.rsql.jpa.entity.Teacher;
import com.github.tennaito.rsql.jpa.entity.Title;
import com.github.tennaito.rsql.misc.SimpleMapper;
import com.github.tennaito.rsql.parser.ast.ComparisonOperatorProxy;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.From;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.junit.Before;
import org.junit.Test;

import com.github.tennaito.rsql.builder.BuilderTools;
import com.github.tennaito.rsql.jpa.entity.Course;
import com.github.tennaito.rsql.misc.SimpleMapper;
import com.github.tennaito.rsql.parser.ast.ComparisonOperatorProxy;

import cz.jirutka.rsql.parser.RSQLParser;
import cz.jirutka.rsql.parser.ast.AbstractNode;
import cz.jirutka.rsql.parser.ast.ComparisonNode;
Expand All @@ -63,17 +65,88 @@
import cz.jirutka.rsql.parser.ast.Node;
import cz.jirutka.rsql.parser.ast.RSQLVisitor;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.fail;

/**
* @author AntonioRabelo
*/
public class JpaVisitorTest extends AbstractVisitorTest<Course> {
@RunWith(Parameterized.class)
public class JpaVisitorTest {

@Parameterized.Parameters
public static List<EntityManager[]> data() {
final TestEntityManagerBuilder testEntityManagerBuilder = new TestEntityManagerBuilder();
final EntityManager eclipseEntityManager = Persistence.createEntityManagerFactory("persistenceUnit-eclipse").createEntityManager();
final EntityManager hibernateEntityManager = Persistence.createEntityManagerFactory("persistenceUnit-hibernate").createEntityManager();
initialize(eclipseEntityManager);
initialize(hibernateEntityManager);
return Arrays.asList(new EntityManager[]{eclipseEntityManager}, new EntityManager[]{ hibernateEntityManager});
}

final static XorNode xorNode = new XorNode(new ArrayList<Node>());

@Before
public void setUp() throws Exception {
entityManager = EntityManagerFactoryInitializer.getEntityManagerFactory().createEntityManager();

private final EntityManager entityManager;

private Class<Course> entityClass;

public JpaVisitorTest(EntityManager entityManager) {
this.entityManager = entityManager;
entityClass = Course.class;
}

public static void initialize(EntityManager entityManager) {
entityManager.getTransaction().begin();

Title title1 = new Title();
title1.setId(1L);
title1.setName("Phd");
entityManager.persist(title1);

Title title2 = new Title();
title2.setId(2L);
title2.setName("Consultant");
entityManager.persist(title2);

Set<Title> titles = new HashSet<Title>();
titles.add(title1);
titles.add(title2);

Person head = new Person();
head.setId(1L);
head.setName("Some");
head.setSurname("One");
head.setTitles(titles);
entityManager.persist(head);

Department department = new Department();
department.setId(1L);
department.setName("Testing");
department.setCode("MI-MDW");
department.setHead(head);
entityManager.persist(department);

Teacher teacher = new Teacher();
teacher.setId(23L);
teacher.setSpecialtyDescription("Maths");
entityManager.persist(teacher);

Course c = new Course();
c.setId(1L);
c.setCode("MI-MDW");
c.setActive(true);
c.setCredits(10);
c.setName("Testing Course");
c.setDepartment(department);
c.setDetails(CourseDetails.of("test"));
c.getDetails().setTeacher(teacher);
c.setStartDate(new Date());
entityManager.persist(c);

entityManager.getTransaction().commit();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.tennaito.rsql.jpa;

import javax.persistence.EntityManager;
import javax.persistence.Persistence;

/**
* Created by ivelin on 8/15/17.
*/
public class TestEntityManagerBuilder {

public EntityManager buildEntityManager(String persistenceUnit) {
final EntityManager entityManager = Persistence.createEntityManagerFactory(persistenceUnit).createEntityManager();
return entityManager;
}


}
42 changes: 21 additions & 21 deletions src/test/resources/META-INF/persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="persistenceUnit">

<persistence-unit name="persistenceUnit-eclipse">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.github.tennaito.rsql.jpa.entity.Course</class>
<class>com.github.tennaito.rsql.jpa.entity.Department</class>
Expand All @@ -13,7 +14,7 @@
<class>com.github.tennaito.rsql.jpa.entity.Teacher</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:ProductDAOTest" />
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:EclipseProductDAOTest" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="eclipselink.logging.level" value="FINE" />
<property name="eclipselink.target-database" value="HSQL" />
Expand All @@ -24,23 +25,22 @@
<property name="eclipselink.logging.parameters" value="true" />
</properties>
</persistence-unit>

<!--
<persistence-unit name="persistenceUnit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.github.tennaito.rsql.jpa.entity.Course</class>
<class>com.github.tennaito.rsql.jpa.entity.Department</class>
<class>com.github.tennaito.rsql.jpa.entity.Person</class>
<class>com.github.tennaito.rsql.jpa.entity.Title</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:ProductDAOTest" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
-->

<persistence-unit name="persistenceUnit-hibernate">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.github.tennaito.rsql.jpa.entity.Course</class>
<class>com.github.tennaito.rsql.jpa.entity.Department</class>
<class>com.github.tennaito.rsql.jpa.entity.Person</class>
<class>com.github.tennaito.rsql.jpa.entity.Title</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:HibernateProductDAOTest" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>

</persistence>

0 comments on commit 60d0d77

Please sign in to comment.