Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing hibernate tests + nested plural associations + making the JpaVisitorTest run against both hibernate and eclispelink #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,10 @@ public static <T> Path<?> findPropertyPath(String propertyPath, Path startRoot,
classMetadata = metaModel.managedType(associationType);
LOG.log(Level.INFO, "Create a join between {0} and {1}.", new Object[]{previousClass, classMetadata.getJavaType().getName()});

if (root instanceof Join) {
root = root.get(mappedProperty);
} else {
if (root instanceof From) {
root = ((From) root).join(mappedProperty);
} else {
root = root.get(mappedProperty);
}
} else {
LOG.log(Level.INFO, "Create property path for type {0} property {1}.", new Object[]{classMetadata.getJavaType().getName(), mappedProperty});
Expand Down
107 changes: 0 additions & 107 deletions src/test/java/com/github/tennaito/rsql/jpa/AbstractVisitorTest.java

This file was deleted.

This file was deleted.

131 changes: 113 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,17 +24,28 @@
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.ObjTags;
import com.github.tennaito.rsql.jpa.entity.Person;
import com.github.tennaito.rsql.jpa.entity.Tag;
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;
Expand All @@ -46,14 +57,6 @@
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 +66,99 @@
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 = testEntityManagerBuilder.buildEntityManager("persistenceUnit-eclipse");
initialize(eclipseEntityManager);
final EntityManager hibernateEntityManager = testEntityManagerBuilder.buildEntityManager("persistenceUnit-hibernate");
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);

Tag tag = new Tag();
tag.setId(1L);
tag.setTag("TestTag");
entityManager.persist(tag);

ObjTags tags = new ObjTags();
tags.setId(1L);
tags.setTags(Arrays.asList(tag));
entityManager.persist(tags);

Department department = new Department();
department.setId(1L);
department.setName("Testing");
department.setCode("MI-MDW");
department.setHead(head);
department.setTags(tags);
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 Expand Up @@ -671,4 +756,14 @@ public void testSelectionUsingEmbeddedAssociationField() throws Exception {
List<Course> courses = entityManager.createQuery(query).getResultList();
assertEquals("Testing Course", courses.get(0).getName());
}

@Test
public void testNestedSelection() throws Exception {
Node rootNode = new RSQLParser().parse("tags.tags.tag=in=(TestTag)");
RSQLVisitor<CriteriaQuery<Department>, EntityManager> visitor = new JpaCriteriaQueryVisitor<Department>();
CriteriaQuery<Department> query = rootNode.accept(visitor, entityManager);

List<Department> departments = entityManager.createQuery(query).getResultList();
assertEquals("Testing", departments.get(0).getName());
}
}
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;
}


}
10 changes: 10 additions & 0 deletions src/test/java/com/github/tennaito/rsql/jpa/entity/Department.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;

/**
*
Expand All @@ -42,6 +43,8 @@ public class Department extends AbstractTestEntity {
@ManyToOne
private Person head;

@OneToOne
private ObjTags tags;

public String getCode() {
return code;
Expand All @@ -59,4 +62,11 @@ public void setHead(Person head) {
this.head = head;
}

public ObjTags getTags() {
return tags;
}

public void setTags(ObjTags tags) {
this.tags = tags;
}
}
Loading