001: package com.bm.testsuite.mocked;
002:
003: import java.util.Arrays;
004: import java.util.Map;
005:
006: import javax.annotation.Resource;
007: import javax.ejb.EJB;
008: import javax.persistence.PersistenceContext;
009:
010: import org.jmock.Mock;
011:
012: import com.bm.creators.BeanCreationListener;
013: import com.bm.creators.MockedDIModuleCreator;
014: import com.bm.ejb3guice.inject.Ejb3Guice;
015: import com.bm.ejb3guice.inject.Injector;
016: import com.bm.ejb3guice.inject.Module;
017: import com.bm.ejb3guice.inject.Stage;
018: import com.bm.ejb3metadata.annotations.metadata.MetaDataCache;
019: import com.bm.testsuite.BaseTest;
020:
021: /**
022: * This class enables the testing of Seteless/Statefull Session beans and JBoss
023: * Service classes under Mock-Isolation control. All JSR 220 (EJB 3) fields will
024: * be injected as JMock objects.
025: *
026: * @author Daniel Wiese
027: *
028: * @param <T> -
029: * the type of the bena to test.
030: */
031: public abstract class MockedSessionBeanFixture<T> extends BaseTest {
032:
033: private final Class<T> beanUnderTestClass;
034:
035: private T beanToTest = null;
036:
037: private Map<Class<?>, Mock> controlls = null;
038:
039: /**
040: * Default constructor.
041: *
042: * @param beanToTest -
043: * the bean to test.
044: */
045: public MockedSessionBeanFixture(Class<T> beanToTest) {
046: this .beanUnderTestClass = beanToTest;
047: }
048:
049: /**
050: * Returns the mock controll to the given property.
051: *
052: * @param interfaze -
053: * the name of the property
054: * @return - the mock controll
055: */
056: protected Mock getMockControl(Class<?> interfaze) {
057: if (this .controlls != null) {
058: return this .controlls.get(interfaze);
059: } else {
060: return null;
061: }
062: }
063:
064: /**
065: * Returns the bean to test.
066: *
067: * @return - the bean to test
068: */
069: protected T getBeanToTest() {
070: return this .beanToTest;
071: }
072:
073: /**
074: * Sets a value for a field in the tested-bean instance.
075: *
076: * @author Daniel Wiese
077: * @since 02.05.2006
078: * @param fieldName -
079: * the name of the field
080: * @param toSet -
081: * the value to set
082: */
083: protected void setValueForField(String fieldName, Object toSet) {
084: this .setValueForField(this .beanToTest, fieldName, toSet);
085: }
086:
087: /**
088: * @see junit.framework.TestCase#setUp()
089: */
090: @Override
091: protected void setUp() throws Exception {
092: super .setUp();
093: MockedDIModuleCreator module = MetaDataCache
094: .getMockModuleCreator(beanUnderTestClass);
095: this .beanToTest = createBeanIstance(module);
096: this .controlls = module.getInterfacesAndMockControlls();
097: // register the mock controlls;
098: for (Mock current : controlls.values()) {
099: registerToVerify(current);
100: }
101: }
102:
103: @SuppressWarnings("unchecked")
104: private T createBeanIstance(Module module) {
105:
106: // final T back = Ejb3Utils.getNewInstance(toCreate);
107: Module[] mods = { module };
108: BeanCreationListener createdbeans = new BeanCreationListener();
109: Injector injector = Ejb3Guice.createInjector(Stage.PRODUCTION,
110: Arrays.asList(mods), Ejb3Guice.markerToArray(EJB.class,
111: Resource.class, PersistenceContext.class),
112: createdbeans);
113: final T instance = injector.getInstance(beanUnderTestClass);
114: return instance;
115: }
116:
117: /**
118: * @see junit.framework.TestCase#tearDown()
119: */
120: @Override
121: protected void tearDown() throws Exception {
122: super.tearDown();
123: this.beanToTest = null;
124: }
125:
126: }
|