001: package com.bm.creators;
002:
003: import java.util.HashMap;
004: import java.util.Map;
005: import java.util.Set;
006:
007: import javax.ejb.SessionContext;
008: import javax.ejb.TimerService;
009: import javax.persistence.EntityManager;
010: import javax.sql.DataSource;
011:
012: import org.jmock.Mock;
013: import org.jmock.core.CoreMock;
014: import org.jmock.core.Formatting;
015:
016: import com.bm.ejb3guice.inject.Binder;
017: import com.bm.ejb3guice.inject.Module;
018:
019: public class MockedDIModuleCreator implements Module {
020:
021: private Map<Class<?>, Mock> interfacesMockControlls = new HashMap<Class<?>, Mock>();
022:
023: private final Map<String, String> interface2implemantation;
024:
025: /**
026: * Constructor.
027: *
028: * @param manager
029: * the entity manager instance which should be used for the
030: * binding
031: */
032: public MockedDIModuleCreator() {
033: this .interface2implemantation = new HashMap<String, String>();
034: }
035:
036: /**
037: * Adds a map with interface impl. to the structure.
038: *
039: * @author Daniel Wiese
040: * @since Jul 19, 2007
041: * @param toAdd
042: * the map to add
043: */
044: public void addInteface2ImplMap(Map<String, String> toAdd) {
045: final Set<String> keySet = toAdd.keySet();
046: for (String interfaze : keySet) {
047: this .interface2implemantation.put(interfaze, toAdd
048: .get(interfaze));
049: }
050: }
051:
052: @SuppressWarnings("unchecked")
053: public void configure(Binder binder) {
054: // do the standard bindings
055: binder.bind(DataSource.class).toInstance(
056: createMock(DataSource.class));
057: binder.bind(EntityManager.class).toInstance(
058: createMock(EntityManager.class));
059: binder.bind(SessionContext.class).toInstance(
060: createMock(SessionContext.class));
061: binder.bind(TimerService.class).toInstance(
062: createMock(TimerService.class));
063:
064: for (String interfaze : interface2implemantation.keySet()) {
065: try {
066: Class<Object> interfazeCl = (Class<Object>) Thread
067: .currentThread().getContextClassLoader()
068: .loadClass(interfaze.replace('/', '.'));
069: binder.bind(interfazeCl).toInstance(
070: createMock(interfazeCl));
071: } catch (ClassNotFoundException e) {
072: throw new RuntimeException(
073: "Can't load Local/Remote interface "
074: + interfaze);
075: }
076: }
077:
078: }
079:
080: /**
081: * Liefert die map mit den interfaces und den mock controlls die erzeugt
082: * wurden
083: *
084: * @return die interfaces und die mock controlls
085: */
086: public Map<Class<?>, Mock> getInterfacesAndMockControlls() {
087: return interfacesMockControlls;
088: }
089:
090: @SuppressWarnings("unchecked")
091: public <T> T createMock(Class<T> forIterface) {
092: final String roleName = "mock"
093: + Formatting.classShortName(forIterface);
094: Mock newMock = new Mock(new CoreMock(forIterface, roleName));
095:
096: // create the proxy
097: final T proxy = (T) newMock.proxy();
098: interfacesMockControlls.put(forIterface, newMock);
099: return proxy;
100: }
101: }
|