001: package org.vraptor.plugin.jpa;
002:
003: import java.lang.reflect.Field;
004:
005: import org.vraptor.AbstractTest;
006: import org.vraptor.component.ComponentType;
007: import org.vraptor.component.DefaultLogicMethod;
008:
009: /**
010: * @author Fabio Correia Kung
011: *
012: */
013: public class EntityManagerIntrospectorTest extends AbstractTest {
014:
015: private EntityManagerIntrospector introspector;
016:
017: private ComponentType wrongDependentType;
018:
019: private ComponentType dependentType;
020:
021: private ComponentType independentType;
022:
023: private ComponentType constructorDependentType;
024:
025: /*
026: * (non-Javadoc)
027: *
028: * @see junit.framework.TestCase#setUp()
029: */
030: protected void setUp() throws Exception {
031: super .setUp();
032: this .introspector = new EntityManagerIntrospector();
033: this .constructorDependentType = createComponentType(DependentByConstructor.class);
034: this .dependentType = createComponentType(EntityManagerDependent.class);
035: this .wrongDependentType = createComponentType(EntityManagerWrongDependent.class);
036: this .independentType = createComponentType(EntityManagerIndependent.class);
037:
038: }
039:
040: /**
041: * Test method for
042: * {@link org.vraptor.plugin.jpa.EntityManagerIntrospector#getEntityManagerField(org.vraptor.component.ComponentType)}.
043: */
044: public void testGetEntityManagerFieldForDependentComponent()
045: throws Exception {
046: Field expectedField = EntityManagerDependent.class
047: .getDeclaredField("entityManager");
048: Field field = this .introspector
049: .getEntityManagerField(dependentType);
050: assertEquals("Same field", expectedField, field);
051: }
052:
053: /**
054: * Test method for
055: * {@link org.vraptor.plugin.jpa.EntityManagerIntrospector#getEntityManagerField(org.vraptor.component.ComponentType)}.
056: */
057: public void testGetEntityManagerShouldGiveNullForIndependentComponent()
058: throws Exception {
059: Field field = this .introspector
060: .getEntityManagerField(independentType);
061: assertNull("hasn't the field", field);
062: }
063:
064: /**
065: * Test method for
066: * {@link org.vraptor.plugin.jpa.EntityManagerIntrospector#getEntityManagerField(org.vraptor.component.ComponentType)}.
067: */
068: public void testGetEntityManagerShouldGiveNullForMissingPersistenceContextAnnotation()
069: throws Exception {
070: Field field = this .introspector
071: .getEntityManagerField(wrongDependentType);
072: assertNull("mising @PersistenceContext", field);
073: }
074:
075: /**
076: * Test method for
077: * {@link org.vraptor.plugin.jpa.EntityManagerIntrospector#dependsOnEntityManager(org.vraptor.component.ComponentType)}.
078: */
079: public void testPersistenceContextAnnotatedFieldIsNotConstructorDependency() {
080: boolean depends = this .introspector
081: .dependsOnEntityManager(dependentType);
082: assertFalse(depends);
083: }
084:
085: /**
086: * Test method for
087: * {@link org.vraptor.plugin.jpa.EntityManagerIntrospector#dependsOnEntityManager(org.vraptor.component.ComponentType)}.
088: */
089: public void testIndependentComponentHasNotConstructorDependency() {
090: boolean depends = this .introspector
091: .dependsOnEntityManager(independentType);
092: assertFalse(depends);
093: }
094:
095: /**
096: * Test method for
097: * {@link org.vraptor.plugin.jpa.EntityManagerIntrospector#dependsOnEntityManager(org.vraptor.component.ComponentType)}.
098: */
099: public void testWrongDependentComponentHasNotConstructorDependency() {
100: boolean depends = this .introspector
101: .dependsOnEntityManager(wrongDependentType);
102: assertFalse(depends);
103: }
104:
105: /**
106: * Test method for
107: * {@link org.vraptor.plugin.jpa.EntityManagerIntrospector#dependsOnEntityManager(org.vraptor.component.ComponentType)}.
108: */
109: public void testComponentThatDependsOnEntityManagerByConstructor() {
110: boolean depends = this .introspector
111: .dependsOnEntityManager(constructorDependentType);
112: assertTrue(depends);
113: }
114:
115: /**
116: * Test method for
117: * {@link org.vraptor.plugin.jpa.EntityManagerIntrospector#hasPersistenceContext(org.vraptor.component.ComponentType)}.
118: */
119: public void testEntityManagerDependentComponentHasPersistenceContext() {
120: assertTrue(this .introspector
121: .hasPersistenceContext(dependentType));
122: }
123:
124: /**
125: * Test method for
126: * {@link org.vraptor.plugin.jpa.EntityManagerIntrospector#hasPersistenceContext(org.vraptor.component.ComponentType)}.
127: */
128: public void testEntityManagerIndependentComponentHasNotPersistenceContext() {
129: assertFalse(this .introspector
130: .hasPersistenceContext(independentType));
131: }
132:
133: /**
134: * Test method for
135: * {@link org.vraptor.plugin.jpa.EntityManagerIntrospector#hasPersistenceContext(org.vraptor.component.ComponentType)}.
136: */
137: public void testMissingPersistenceContextAnnotationComponentHasNotPersistenceContext() {
138: assertFalse(this .introspector
139: .hasPersistenceContext(wrongDependentType));
140: }
141:
142: /**
143: * Test method for
144: * {@link org.vraptor.plugin.jpa.EntityManagerIntrospector#hasPersistenceContext(org.vraptor.component.ComponentType)}.
145: */
146: public void testDependentByConstructorComponentHasNotPersistenceContext() {
147: assertFalse(this .introspector
148: .hasPersistenceContext(constructorDependentType));
149: }
150:
151: /**
152: * Test method for
153: * {@link org.vraptor.plugin.jpa.EntityManagerIntrospector#getPersistenceUnitName(org.vraptor.component.ComponentType)}.
154: *
155: * @throws Exception
156: */
157: public void testGetPersistenceUnitName() throws Exception {
158: assertEquals("default",
159: EntityManagerIntrospector.DEFAULT_PERSISTENCE_UNIT);
160:
161: String nameForDependent = this .introspector
162: .getPersistenceUnitName(dependentType);
163: assertEquals("default name",
164: EntityManagerIntrospector.DEFAULT_PERSISTENCE_UNIT,
165: nameForDependent);
166:
167: String nameForIndependent = this .introspector
168: .getPersistenceUnitName(independentType);
169: assertEquals("default name",
170: EntityManagerIntrospector.DEFAULT_PERSISTENCE_UNIT,
171: nameForIndependent);
172:
173: String nameWithoutAnnotation = this .introspector
174: .getPersistenceUnitName(wrongDependentType);
175: assertEquals("default name",
176: EntityManagerIntrospector.DEFAULT_PERSISTENCE_UNIT,
177: nameWithoutAnnotation);
178:
179: String customName = this .introspector
180: .getPersistenceUnitName(createComponentType(CustomUnitName.class));
181: assertEquals("customized name", "otherName", customName);
182: }
183:
184: /**
185: * Test method for
186: * {@link org.vraptor.plugin.jpa.EntityManagerIntrospector#isTransactionRequired(org.vraptor.component.LogicMethod)}.
187: *
188: * @throws Exception
189: */
190: public void testIsTransactionRequired() throws Exception {
191: DefaultLogicMethod requiresTransactionLogic = createLogicMethod(
192: WithTransaction.class, "logic");
193: DefaultLogicMethod missingRequiredAttributeLogic = createLogicMethod(
194: WithoutTransaction.class, "logic");
195: DefaultLogicMethod notAnnotatedLogic = createLogicMethod(
196: EntityManagerDependent.class, "logic");
197:
198: assertTrue(this .introspector
199: .isTransactionRequired(requiresTransactionLogic));
200: assertFalse(
201: "default value for required is false",
202: this .introspector
203: .isTransactionRequired(missingRequiredAttributeLogic));
204: assertFalse("default behavior is no transaction",
205: this.introspector
206: .isTransactionRequired(notAnnotatedLogic));
207: }
208:
209: }
|