-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests for the FieldOperations class.
Fixed a couple of issues - * OneToOne relationships were not being saved. * Imports were not added for the related type in OneToOne relations Also removed unwanted method parameters for creating OneToOne and ManyToOne relationships. These types do not have the concept of optional=false aka required relationships.
- Loading branch information
1 parent
e96e83c
commit 9c91a05
Showing
4 changed files
with
245 additions
and
6 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
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
237 changes: 237 additions & 0 deletions
237
javaee/tests/src/test/java/org/jboss/forge/addon/javaee/jpa/FieldOperationsTest.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,237 @@ | ||
package org.jboss.forge.addon.javaee.jpa; | ||
|
||
import com.google.common.collect.Lists; | ||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.arquillian.junit.Arquillian; | ||
import org.jboss.forge.addon.javaee.ProjectHelper; | ||
import org.jboss.forge.addon.parser.java.resources.JavaResource; | ||
import org.jboss.forge.addon.projects.Project; | ||
import org.jboss.forge.arquillian.AddonDependency; | ||
import org.jboss.forge.arquillian.Dependencies; | ||
import org.jboss.forge.arquillian.archive.ForgeArchive; | ||
import org.jboss.forge.furnace.repositories.AddonDependencyEntry; | ||
import org.jboss.forge.parser.java.Field; | ||
import org.jboss.forge.parser.java.JavaClass; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import javax.inject.Inject; | ||
import javax.persistence.*; | ||
import java.io.IOException; | ||
import java.util.Set; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
|
||
/** | ||
* Unit tests to verify the behavior of the {@link FieldOperations} class. | ||
*/ | ||
@RunWith(Arquillian.class) | ||
public class FieldOperationsTest | ||
{ | ||
|
||
@Deployment | ||
@Dependencies({ | ||
@AddonDependency(name = "org.jboss.forge.addon:javaee"), | ||
@AddonDependency(name = "org.jboss.forge.addon:maven") | ||
}) | ||
public static ForgeArchive getDeployment() | ||
{ | ||
return ShrinkWrap | ||
.create(ForgeArchive.class) | ||
.addClass(ProjectHelper.class) | ||
.addBeansXML() | ||
.addAsAddonDependencies( | ||
AddonDependencyEntry.create("org.jboss.forge.furnace.container:cdi"), | ||
AddonDependencyEntry.create("org.jboss.forge.addon:projects"), | ||
AddonDependencyEntry.create("org.jboss.forge.addon:javaee"), | ||
AddonDependencyEntry.create("org.jboss.forge.addon:maven") | ||
); | ||
} | ||
|
||
@Inject | ||
private ProjectHelper projectHelper; | ||
|
||
private Project project; | ||
|
||
private JavaResource entity; | ||
|
||
private JavaClass entityClass; | ||
|
||
@Inject | ||
private FieldOperations fieldOperations; | ||
|
||
@Before | ||
public void setUp() | ||
{ | ||
project = projectHelper.createJavaLibraryProject(); | ||
projectHelper.installJPA_2_0(project); | ||
try | ||
{ | ||
entity = projectHelper.createJPAEntity(project, "Customer"); | ||
entityClass = (JavaClass) entity.getJavaSource(); | ||
} | ||
catch (IOException ioEx) | ||
{ | ||
throw new IllegalStateException("Failed to setup the test suite.", ioEx); | ||
} | ||
} | ||
|
||
/** | ||
* Verifies that simple fields can be added to a class. | ||
*/ | ||
@Test | ||
public void testAddNewSimpleField() throws Exception | ||
{ | ||
String simpleTypeName = String.class.getSimpleName(); | ||
String qualifiedTypeName = String.class.getCanonicalName(); | ||
|
||
// Add a String field with a qualified type. | ||
String fieldName = "firstName"; | ||
fieldOperations.addFieldTo(entityClass, qualifiedTypeName, fieldName); | ||
|
||
// Is the field present? Is the name and type of the field correct? | ||
assertThat(entityClass.hasField(fieldName), is(true)); | ||
assertThat(entityClass.getField(fieldName).getName(), equalTo(fieldName)); | ||
assertThat(entityClass.getField(fieldName).getType(), equalTo(simpleTypeName)); | ||
// Is the type from java.lang imported? | ||
assertThat(entityClass.hasImport(qualifiedTypeName), is(false)); | ||
// Syntax errors? | ||
assertThat(entityClass.hasSyntaxErrors(), is(false)); | ||
|
||
// Add a String field with a simple type. | ||
fieldName = "lastName"; | ||
fieldOperations.addFieldTo(entityClass, simpleTypeName, fieldName); | ||
|
||
// Is the field present? Is the name and type of the field correct? | ||
assertThat(entityClass.hasField(fieldName), is(true)); | ||
assertThat(entityClass.getField(fieldName).getName(), equalTo(fieldName)); | ||
assertThat(entityClass.getField(fieldName).getType(), equalTo(simpleTypeName)); | ||
// Is the type from java.lang imported? | ||
assertThat(entityClass.hasImport(qualifiedTypeName), is(false)); | ||
// Syntax errors? | ||
assertThat(entityClass.hasSyntaxErrors(), is(false)); | ||
} | ||
|
||
@Test | ||
public void testAddOneToManyRel() throws Exception | ||
{ | ||
JavaResource rhsClass = projectHelper.createJPAEntity(project, "Account"); | ||
String qualifiedRhsType = rhsClass.getJavaSource().getCanonicalName(); | ||
String simpleRhsType = rhsClass.getJavaSource().getName(); | ||
String simpleCollectionType = Set.class.getSimpleName(); | ||
String qualifiedCollectionType = Set.class.getCanonicalName(); | ||
|
||
String fieldName = "accounts"; | ||
fieldOperations.newOneToManyRelationship(project, entity, fieldName, qualifiedRhsType, null, | ||
FetchType.LAZY, Lists.<CascadeType> newArrayList()); | ||
entityClass = (JavaClass) entity.getJavaSource(); | ||
|
||
// Is the field present? Is the name and type of the field and it's parameterized type correct? | ||
assertThat(entityClass.hasField(fieldName), is(true)); | ||
Field<JavaClass> field = entityClass.getField(fieldName); | ||
assertThat(field.getName(), equalTo(fieldName)); | ||
assertThat(field.getType(), equalTo(simpleCollectionType)); | ||
assertThat(field.getTypeInspector().getTypeArguments().get(0).getName(), equalTo(simpleRhsType)); | ||
|
||
// Is the @OneToMany annotation present ? | ||
assertThat(field.hasAnnotation(OneToMany.class), is(true)); | ||
|
||
// Is the collection type and generic type imported? | ||
assertThat(entityClass.hasImport(qualifiedRhsType), is(true)); | ||
assertThat(entityClass.hasImport(qualifiedCollectionType), is(true)); | ||
// Syntax errors? | ||
assertThat(entityClass.hasSyntaxErrors(), is(false)); | ||
} | ||
|
||
@Test | ||
public void testAddManyToManyRel() throws Exception | ||
{ | ||
JavaResource rhsClass = projectHelper.createJPAEntity(project, "Account"); | ||
String qualifiedRhsType = rhsClass.getJavaSource().getCanonicalName(); | ||
String simpleRhsType = rhsClass.getJavaSource().getName(); | ||
String simpleCollectionType = Set.class.getSimpleName(); | ||
String qualifiedCollectionType = Set.class.getCanonicalName(); | ||
|
||
String fieldName = "accounts"; | ||
fieldOperations.newManyToManyRelationship(project, entity, fieldName, qualifiedRhsType, null, | ||
FetchType.LAZY, Lists.<CascadeType> newArrayList()); | ||
entityClass = (JavaClass) entity.getJavaSource(); | ||
|
||
// Is the field present? Is the name and type of the field and it's parameterized type correct? | ||
assertThat(entityClass.hasField(fieldName), is(true)); | ||
Field<JavaClass> field = entityClass.getField(fieldName); | ||
assertThat(field.getName(), equalTo(fieldName)); | ||
assertThat(field.getType(), equalTo(simpleCollectionType)); | ||
assertThat(field.getTypeInspector().getTypeArguments().get(0).getName(), equalTo(simpleRhsType)); | ||
|
||
// Is the @ManyToMany annotation present ? | ||
assertThat(field.hasAnnotation(ManyToMany.class), is(true)); | ||
|
||
// Is the collection type and generic type imported? | ||
assertThat(entityClass.hasImport(qualifiedRhsType), is(true)); | ||
assertThat(entityClass.hasImport(qualifiedCollectionType), is(true)); | ||
// Syntax errors? | ||
assertThat(entityClass.hasSyntaxErrors(), is(false)); | ||
} | ||
|
||
@Test | ||
public void testAddManyToOneRel() throws Exception | ||
{ | ||
JavaResource rhsClass = projectHelper.createJPAEntity(project, "Store"); | ||
String qualifiedRhsType = rhsClass.getJavaSource().getCanonicalName(); | ||
String simpleRhsType = rhsClass.getJavaSource().getName(); | ||
|
||
String fieldName = "store"; | ||
fieldOperations.newManyToOneRelationship(project, entity, fieldName, qualifiedRhsType, null, | ||
FetchType.LAZY, false, Lists.<CascadeType> newArrayList()); | ||
entityClass = (JavaClass) entity.getJavaSource(); | ||
|
||
// Is the field present? Is the name and type of the field correct? | ||
assertThat(entityClass.hasField(fieldName), is(true)); | ||
Field<JavaClass> field = entityClass.getField(fieldName); | ||
assertThat(field.getName(), equalTo(fieldName)); | ||
assertThat(field.getType(), equalTo(simpleRhsType)); | ||
|
||
// Is the @ManyToOne annotation present ? | ||
assertThat(field.hasAnnotation(ManyToOne.class), is(true)); | ||
|
||
// Is the RHS type imported? | ||
assertThat(entityClass.hasImport(qualifiedRhsType), is(true)); | ||
|
||
// Syntax errors? | ||
assertThat(entityClass.hasSyntaxErrors(), is(false)); | ||
} | ||
|
||
@Test | ||
public void testAddOneToOneRel() throws Exception | ||
{ | ||
JavaResource rhsClass = projectHelper.createJPAEntity(project, "Store"); | ||
String qualifiedRhsType = rhsClass.getJavaSource().getCanonicalName(); | ||
String simpleRhsType = rhsClass.getJavaSource().getName(); | ||
|
||
String fieldName = "store"; | ||
fieldOperations.newOneToOneRelationship(project, entity, fieldName, qualifiedRhsType, null, | ||
FetchType.LAZY, false, Lists.<CascadeType> newArrayList()); | ||
entityClass = (JavaClass) entity.getJavaSource(); | ||
|
||
// Is the field present? Is the name and type of the field correct? | ||
assertThat(entityClass.hasField(fieldName), is(true)); | ||
Field<JavaClass> field = entityClass.getField(fieldName); | ||
assertThat(field.getName(), equalTo(fieldName)); | ||
assertThat(field.getType(), equalTo(simpleRhsType)); | ||
|
||
// Is the @OneToOne annotation present ? | ||
assertThat(field.hasAnnotation(OneToOne.class), is(true)); | ||
|
||
// Is the RHS type imported? | ||
assertThat(entityClass.hasImport(qualifiedRhsType), is(true)); | ||
|
||
// Syntax errors? | ||
assertThat(entityClass.hasSyntaxErrors(), is(false)); | ||
} | ||
|
||
} |
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