1 /*
2 * Copyright (c) 2005, Henri Yandell
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * + Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * + Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * + Neither the name of the Simple-JNDI nor the names of its contributors may
15 * be used to endorse or promote products derived from this software without
16 * specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30 package org.osjava.sj.memory;
31
32 import java.util.Hashtable;
33
34 import javax.naming.InitialContext;
35 import javax.naming.NamingException;
36
37 import junit.framework.TestCase;
38
39 public class SharedMemoryTest extends TestCase {
40
41 public SharedMemoryTest(String name) {
42 super(name);
43 }
44
45 private InitialContext createContext() throws NamingException {
46 /* Initial configuration voodoo for the default context. */
47 Hashtable contextEnv = new Hashtable();
48 contextEnv.put("java.naming.factory.initial", "org.osjava.sj.memory.MemoryContextFactory");
49
50 /* The default is 'flat', which isn't hierarchial and not what I want. */
51 contextEnv.put("jndi.syntax.direction", "left_to_right");
52
53 /* Separator is required for non-flat */
54 contextEnv.put("jndi.syntax.separator", "/");
55
56 contextEnv.put("org.osjava.sj.jndi.shared", "true");
57
58 /* The intial context. */
59 return new InitialContext(contextEnv);
60 }
61
62 // Bug report submitted by Thomas A. Oehser
63 public void testSharedMemory() {
64 try {
65 String foo = "bar";
66 InitialContext ctxA = createContext();
67 ctxA.bind("fooKey", foo);
68 Object oA = ctxA.lookup("fooKey");
69 Object oB = createContext().lookup("fooKey");
70 assertNotNull("Shared memory failing", oB);
71 assertEquals("Shared memory corrupting", oA, oB);
72 } catch (NamingException e) {
73 fail("NamingException " + e.getMessage());
74 }
75 }
76
77 public void testSharedSubContextMemory() {
78 try {
79 InitialContext context = createContext();
80 context.createSubcontext("path");
81 context.bind("path/foo", "42");
82 context = createContext();
83 assertEquals("42", context.lookup("path/foo") );
84 } catch (NamingException e) {
85 fail("NamingException " + e.getMessage());
86 }
87 }
88
89 public void testSjn73() throws Exception {
90 String propShared = "org.osjava.sj.jndi.shared";
91 System.setProperty(propShared, "true");
92 InitialContext ctx = new InitialContext();
93 assertNotNull(ctx.lookup("path.foo"));
94 InitialContext ctx1 = new InitialContext();
95 assertNotNull(ctx1.lookup("path.foo"));
96 System.getProperties().remove(propShared);
97 }
98 }