001: package com.mockrunner.test.ejb;
002:
003: import java.util.Properties;
004:
005: import javax.naming.Context;
006: import javax.naming.InitialContext;
007: import javax.naming.NameNotFoundException;
008:
009: import junit.framework.TestCase;
010:
011: import org.mockejb.jndi.MockContextFactory;
012:
013: import com.mockrunner.ejb.Configuration;
014: import com.mockrunner.ejb.JNDIUtil;
015: import com.mockrunner.mock.ejb.MockUserTransaction;
016: import com.mockrunner.test.ejb.TestJNDI.NullContext;
017:
018: public class JNDIUtilTest extends TestCase {
019: private Properties savedProperties;
020: private Context context;
021:
022: protected void setUp() throws Exception {
023: super .setUp();
024: savedProperties = new Properties();
025: TestJNDI.saveProperties(savedProperties);
026: MockContextFactory.setAsInitial();
027: context = new InitialContext();
028: }
029:
030: protected void tearDown() throws Exception {
031: super .tearDown();
032: MockContextFactory.revertSetAsInitial();
033: TestJNDI.restoreProperties(savedProperties);
034: }
035:
036: public void testInitMockContextFactory() throws Exception {
037: System.getProperties().remove(Context.INITIAL_CONTEXT_FACTORY);
038: JNDIUtil.initMockContextFactory();
039: assertEquals(MockContextFactory.class.getName(), System
040: .getProperty(Context.INITIAL_CONTEXT_FACTORY));
041: assertEquals("org.mockejb.jndi", System
042: .getProperty(Context.URL_PKG_PREFIXES));
043: System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "test");
044: JNDIUtil.initMockContextFactory();
045: assertEquals(MockContextFactory.class.getName(), System
046: .getProperty(Context.INITIAL_CONTEXT_FACTORY));
047: assertEquals("org.mockejb.jndi", System
048: .getProperty(Context.URL_PKG_PREFIXES));
049: System.setProperty(Context.URL_PKG_PREFIXES, "test");
050: JNDIUtil.initMockContextFactory();
051: assertEquals(MockContextFactory.class.getName(), System
052: .getProperty(Context.INITIAL_CONTEXT_FACTORY));
053: assertEquals("test", System
054: .getProperty(Context.URL_PKG_PREFIXES));
055: }
056:
057: public void testResetMockContextFactory() throws Exception {
058: System.getProperties().remove(Context.INITIAL_CONTEXT_FACTORY);
059: JNDIUtil.resetMockContextFactory();
060: assertNull(System.getProperty(Context.INITIAL_CONTEXT_FACTORY));
061: System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "test");
062: JNDIUtil.resetMockContextFactory();
063: assertEquals("test", System
064: .getProperty(Context.INITIAL_CONTEXT_FACTORY));
065: System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
066: MockContextFactory.class.getName());
067: JNDIUtil.resetMockContextFactory();
068: assertNull(System.getProperty(Context.INITIAL_CONTEXT_FACTORY));
069: System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "test");
070: System.setProperty(Context.URL_PKG_PREFIXES, "testURL");
071: JNDIUtil.initMockContextFactory();
072: JNDIUtil.resetMockContextFactory();
073: assertEquals("test", System
074: .getProperty(Context.INITIAL_CONTEXT_FACTORY));
075: assertEquals("testURL", System
076: .getProperty(Context.URL_PKG_PREFIXES));
077: }
078:
079: public void testGetContext() throws Exception {
080: Configuration configuration = new Configuration();
081: NullContext testContext = new NullContext();
082: configuration.setContext(testContext);
083: Context context = JNDIUtil.getContext(configuration);
084: assertSame(context, testContext);
085: configuration.setContext(null);
086: context = JNDIUtil.getContext(configuration);
087: assertNotSame(context, testContext);
088: }
089:
090: public void testBindUserTransaction() throws Exception {
091: Configuration configuration = new Configuration();
092: MockUserTransaction transaction = new MockUserTransaction();
093: configuration.setBindMockUserTransactionToJNDI(false);
094: JNDIUtil.bindUserTransaction(configuration, context,
095: transaction);
096: try {
097: context.lookup("javax.transaction.UserTransaction");
098: fail();
099: } catch (NameNotFoundException exc) {
100: //should throw exception
101: }
102: try {
103: context.lookup("java:comp/UserTransaction");
104: fail();
105: } catch (NameNotFoundException exc) {
106: //should throw exception
107: }
108: configuration.setBindMockUserTransactionToJNDI(true);
109: configuration.setUserTransactionJNDIName("myJNDIName");
110: JNDIUtil.bindUserTransaction(configuration, context,
111: transaction);
112: assertSame(context.lookup("myJNDIName"), transaction);
113: assertSame(context.lookup("javax.transaction.UserTransaction"),
114: transaction);
115: assertSame(context.lookup("java:comp/UserTransaction"),
116: transaction);
117: }
118: }
|