Skip to content

Commit

Permalink
added test to run only the complex composite id if exists on persiste…
Browse files Browse the repository at this point in the history
…nce unit
  • Loading branch information
Douglas Boleta committed Jun 23, 2017
1 parent 79d472b commit 189da92
Show file tree
Hide file tree
Showing 3 changed files with 596 additions and 539 deletions.
145 changes: 87 additions & 58 deletions src/test/java/com/github/tennaito/rsql/jpa/AbstractVisitorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,80 +28,109 @@
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.metamodel.EntityType;

import com.github.tennaito.rsql.jpa.entity.Teacher;
import org.junit.BeforeClass;

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.PersonDependent;
import com.github.tennaito.rsql.jpa.entity.Teacher;
import com.github.tennaito.rsql.jpa.entity.Title;

/**
* @author AntonioRabelo
*/
public abstract class AbstractVisitorTest<T> {

private static boolean loaded = false;
private static boolean loaded = false;

protected Class<T> entityClass;
protected EntityManager entityManager;

@BeforeClass
public static void setUpBefore() throws Exception {
if (!loaded) {

EntityManager entityManager = EntityManagerFactoryInitializer.getEntityManagerFactory().createEntityManager();
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();
loaded = true;
}
}
@BeforeClass
public static void setUpBefore() throws Exception {
if (!loaded) {

EntityManager entityManager = EntityManagerFactoryInitializer.getEntityManagerFactory()
.createEntityManager();
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);

if (hasEntity(PersonDependent.class)) {

PersonDependent dependent = new PersonDependent();
dependent.setAge(2);
dependent.getId().setName("son1");
dependent.getId().setPerson(head);
entityManager.persist(dependent);

}

entityManager.getTransaction().commit();
loaded = true;
}
}

public static boolean hasEntity(Class<?> clazz) {

Set<EntityType<?>> entities = EntityManagerFactoryInitializer.getEntityManagerFactory().getMetamodel()
.getEntities();

for (EntityType<?> entityType : entities) {

if (entityType.getJavaType().equals(clazz)) {
return true;
}

}

return false;
}
}
Loading

0 comments on commit 189da92

Please sign in to comment.