001: /**
002: *
003: */package org.vraptor.plugin.jpa;
004:
005: import javax.persistence.EntityManager;
006:
007: import org.jmock.Mock;
008: import org.vraptor.AbstractTest;
009: import org.vraptor.component.BeanConstructor;
010: import org.vraptor.component.DefaultLogicMethod;
011: import org.vraptor.component.InvalidComponentException;
012: import org.vraptor.core.DefaultLogicDefinition;
013: import org.vraptor.introspector.BeanProvider;
014: import org.vraptor.scope.DefaultLogicRequest;
015:
016: /**
017: * @author Fabio Correia Kung
018: *
019: */
020: public class EntityManagerInjectorTest extends AbstractTest {
021:
022: private EntityManagerIntrospector introspector;
023:
024: private BeanProvider beanProvider;
025:
026: private EntityManager entityManager;
027:
028: @Override
029: protected void setUp() throws Exception {
030: super .setUp();
031: this .introspector = new EntityManagerIntrospector();
032: this .beanProvider = (BeanProvider) mock(BeanProvider.class)
033: .proxy();
034: this .entityManager = (EntityManager) mock(EntityManager.class)
035: .proxy();
036: }
037:
038: @Override
039: protected void tearDown() throws Exception {
040: super .tearDown();
041: this .introspector = null;
042: this .beanProvider = null;
043: this .entityManager = null;
044: }
045:
046: public void testInjectsEntityManagerOnDependendComponentCreation()
047: throws Exception {
048: EntityManagerDependent component = new EntityManagerDependent();
049: DefaultLogicRequest logicRequest = createLogicRequest(component);
050: logicRequest
051: .setLogicDefinition(new DefaultLogicDefinition(
052: component,
053: createComponentType(EntityManagerDependent.class),
054: null));
055:
056: // expects delegation
057: Mock constructorMock = mock(BeanConstructor.class);
058: BeanConstructor wrapped = (BeanConstructor) constructorMock
059: .proxy();
060: constructorMock.expects(once()).method("newInstance").with(
061: same(logicRequest), same(this .beanProvider)).will(
062: returnValue(component));
063:
064: EntityManagerInjector injector = new EntityManagerInjector(
065: wrapped, this .introspector);
066: injector.newInstance(logicRequest, this .beanProvider);
067:
068: assertSame("injected", component.getEntityManager(),
069: this .entityManager);
070: }
071:
072: public void testNotInjectEntityManagerIfMissingPersistenceContextAnnotation()
073: throws Exception {
074: EntityManagerWrongDependent component = new EntityManagerWrongDependent();
075: DefaultLogicRequest logicRequest = createLogicRequest(component);
076: logicRequest.setLogicDefinition(new DefaultLogicDefinition(
077: component,
078: createComponentType(EntityManagerWrongDependent.class),
079: null));
080:
081: // expects delegation
082: Mock constructorMock = mock(BeanConstructor.class);
083: BeanConstructor wrapped = (BeanConstructor) constructorMock
084: .proxy();
085: constructorMock.expects(once()).method("newInstance").with(
086: same(logicRequest), same(this .beanProvider)).will(
087: returnValue(component));
088:
089: EntityManagerInjector injector = new EntityManagerInjector(
090: wrapped, this .introspector);
091: injector.newInstance(logicRequest, this .beanProvider);
092:
093: assertNull("not injected", component.getEntityManager());
094: }
095:
096: public void testIgnoresNonDependentComponentCreation()
097: throws Exception {
098: EntityManagerIndependent component = new EntityManagerIndependent();
099: DefaultLogicRequest logicRequest = createLogicRequest(component);
100: logicRequest.setLogicDefinition(new DefaultLogicDefinition(
101: component,
102: createComponentType(EntityManagerIndependent.class),
103: null));
104:
105: // expects delegation
106: Mock constructorMock = mock(BeanConstructor.class);
107: BeanConstructor wrapped = (BeanConstructor) constructorMock
108: .proxy();
109: constructorMock.expects(once()).method("newInstance").with(
110: same(logicRequest), same(this .beanProvider)).will(
111: returnValue(component));
112:
113: EntityManagerInjector injector = new EntityManagerInjector(
114: wrapped, this .introspector);
115: injector.newInstance(logicRequest, this .beanProvider);
116:
117: // ok, no errors
118: }
119:
120: private DefaultLogicRequest createLogicRequest(Object component)
121: throws InvalidComponentException {
122: DefaultLogicMethod logicMethod = createLogicMethod(component
123: .getClass(), "logic");
124: DefaultLogicRequest logicRequest = createLogicRequest(
125: "dependent", "logic");
126: logicRequest.setLogicDefinition(new DefaultLogicDefinition(
127: component,
128: createComponentType(EntityManagerDependent.class),
129: logicMethod));
130: // assume there is already an EntityManager in LogicRequest,
131: // created by the JavaPersistenceInterceptor
132: logicRequest.getRequestContext().setAttribute(
133: EntityManager.class.getName(), this.entityManager);
134:
135: return logicRequest;
136: }
137:
138: }
|