001: /*
002: * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: */
007: package winstone.testCase;
008:
009: import java.util.Hashtable;
010:
011: import javax.naming.Context;
012: import javax.naming.InitialContext;
013: import javax.naming.NamingEnumeration;
014: import javax.naming.NamingException;
015:
016: import junit.framework.Test;
017: import junit.framework.TestCase;
018: import junit.framework.TestSuite;
019:
020: /**
021: * Automated tests for the JNDI provider component of Winstone
022: *
023: * @author <a href="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
024: * @version $Id: NamingTest.java,v 1.2 2006/02/28 07:32:49 rickknowles Exp $
025: */
026: public class NamingTest extends TestCase {
027: public static Test suite() {
028: return (new TestSuite(NamingTest.class));
029: }
030:
031: private InitialContext ic;
032:
033: /**
034: * Constructor for the junit test class for the JNDI service.
035: *
036: * @param name
037: * The name of the test case
038: */
039: public NamingTest(String name) {
040: super (name);
041: }
042:
043: /**
044: * Begins the setup of the test case
045: */
046: public void setUp() throws NamingException {
047: Hashtable env = new Hashtable();
048: env.put(Context.INITIAL_CONTEXT_FACTORY,
049: "winstone.jndi.java.javaURLContextFactory");
050: env.put(Context.URL_PKG_PREFIXES, "winstone.jndi");
051: this .ic = new InitialContext(env);
052: }
053:
054: /**
055: * Undoes any setup work for the test case
056: */
057: public void tearDown() throws NamingException {
058: this .ic.close();
059: this .ic = null;
060: }
061:
062: /**
063: * Performs an absolute context lookup
064: */
065: public void testAbsoluteContextLookup() throws NamingException {
066: Object context1 = this .ic.lookup("java:/comp/env");
067: assertNotNull("Lookup on java:/comp/env must be non-null",
068: context1);
069: assertTrue("Lookup on java:/comp/env must be a Context",
070: context1 instanceof Context);
071:
072: Object context2 = this .ic.lookup("java:/comp/env/");
073: assertNotNull("Lookup on java:/comp/env/ must be non-null",
074: context2);
075: assertTrue("Lookup on java:/comp/env/ must be a Context",
076: context2 instanceof Context);
077: }
078:
079: /**
080: * Performs an absolute lookup on the context
081: */
082: public void testAbsoluteLookup() throws NamingException {
083: Object value = this .ic.lookup("java:/comp/env");
084: assertNotNull("Lookup on java:/comp/env must be non-null",
085: value);
086: }
087:
088: /**
089: * Performs a relative lookup on the context
090: */
091: public void testRelativeLookup() throws NamingException {
092: Object value = this .ic.lookup("");
093: assertNotNull("Lookup on \"\" must be non-null", value);
094: }
095:
096: /**
097: * Performs a relative list on the context
098: */
099: public void testRelativeList() throws NamingException {
100: NamingEnumeration listing = this .ic.list("");
101: assertNotNull("Listing of current context must be non-null",
102: listing);
103: listing.close();
104: }
105:
106: /**
107: * Performs an absolute list on the context
108: */
109: public void testAbsoluteList() throws NamingException {
110: NamingEnumeration listing1 = this .ic.list("java:/comp/env");
111: assertNotNull("Listing of java:/comp/env must be non-null",
112: listing1);
113: listing1.close();
114: NamingEnumeration listing2 = this .ic.list("java:/comp/env/");
115: assertNotNull("Listing of java:/comp/env/ must be non-null",
116: listing2);
117: listing2.close();
118: }
119:
120: /**
121: * Performs an absolute list on the context
122: */
123: public void testCreateDestroyContexts() throws NamingException {
124: Context child = this .ic.createSubcontext("TestChildContext");
125: assertNotNull(
126: "Created subcontext TestChildContext must not be null",
127: child);
128: NamingEnumeration listing = child.list("");
129: assertTrue("Listing on new child context is empty", !listing
130: .hasMoreElements());
131: listing.close();
132: this .ic.destroySubcontext("java:/comp/env/TestChildContext");
133: }
134:
135: /**
136: * Attempts a simple bind
137: */
138: public void testSimpleBind() throws NamingException {
139: Context child = this .ic.createSubcontext("TestBindContext");
140: assertNotNull(
141: "Created subcontext TestBindContext must not be null",
142: child);
143: child.bind("bindInteger", new Integer(80));
144: Object lookupInt = this .ic
145: .lookup("TestBindContext/bindInteger");
146: assertNotNull(
147: "java:/comp/env/TestBindContext/bindInteger should be non-null",
148: lookupInt);
149: assertEquals("java:/comp/env/TestBindContext/bindInteger",
150: lookupInt, new Integer(80));
151: this .ic.destroySubcontext("java:/comp/env/TestBindContext");
152: }
153:
154: /**
155: * Attempts a rebind
156: */
157: public void testSimpleRebind() throws NamingException {
158: Context child = this .ic.createSubcontext("TestRebindContext");
159: assertNotNull(
160: "Created subcontext TestRebindContext must not be null",
161: child);
162: Context rebindChild = child.createSubcontext("ChildRebind");
163: assertNotNull(
164: "Created subcontext rebindChild must not be null",
165: rebindChild);
166: rebindChild.rebind(
167: "java:/comp/env/TestRebindContext/ChildRebind/integer",
168: new Integer(25));
169: rebindChild.close();
170: child.close();
171:
172: Object lookupInt = this .ic
173: .lookup("java:/comp/env/TestRebindContext/ChildRebind/integer");
174: assertNotNull(
175: "java:/comp/env/TestRebindContext/ChildRebind/integer should be non-null",
176: lookupInt);
177: assertEquals(
178: "java:/comp/env/TestRebindContext/ChildRebind/integer",
179: lookupInt, new Integer(25));
180:
181: this .ic.rebind("TestRebindContext/ChildRebind/integer",
182: new Integer(40));
183: Object lookupInt2 = this .ic
184: .lookup("TestRebindContext/ChildRebind/integer");
185: assertNotNull(
186: "TestRebindContext/ChildRebind/integer should be non-null",
187: lookupInt2);
188: assertEquals("TestRebindContext/ChildRebind/integer",
189: lookupInt2, new Integer(40));
190: Object lookupInt3 = this .ic
191: .lookup("java:/comp/env/TestRebindContext/ChildRebind/integer");
192: assertNotNull(
193: "java:/comp/env/TestRebindContext/ChildRebind/integer should be non-null",
194: lookupInt3);
195: assertEquals(
196: "java:/comp/env/TestRebindContext/ChildRebind/integer",
197: lookupInt3, new Integer(40));
198:
199: this .ic
200: .destroySubcontext("java:/comp/env/TestRebindContext/ChildRebind");
201: this .ic.destroySubcontext("java:/comp/env/TestRebindContext");
202: }
203: }
|