1   /*
2    * org.osjava.jndi.MemoryContext_createSubContextTest
3    * $Id: MemoryContextTestAbstract.java 1872 2005-08-21 02:12:54Z hen $
4    * $Rev: 1872 $ 
5    * $Date: 2005-08-20 19:12:54 -0700 (Sat, 20 Aug 2005) $ 
6    * $Author: hen $
7    * $URL: https://osjava.googlecode.com/svn/releases/simple-jndi-0.11.4.1/src/test/org/osjava/sj/memory/MemoryContextTestAbstract.java $
8    * 
9    * Created on Feb 14, 2005
10   *
11   * Copyright (c) 2004, Robert M. Zigweid All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or without
14   * modification, are permitted provided that the following conditions are met:
15   *
16   * + Redistributions of source code must retain the above copyright notice, 
17   *   this list of conditions and the following disclaimer. 
18   *
19   * + Redistributions in binary form must reproduce the above copyright notice,
20   *   this list of conditions and the following disclaimer in the documentation 
21   *   and/or other materials provided with the distribution. 
22   *
23   * + Neither the name of the Simple-JNDI nor the names of its contributors may
24   *   be used to endorse or promote products derived from this software without 
25   *   specific prior written permission.
26   *
27   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30   * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
31   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37   * POSSIBILITY OF SUCH DAMAGE.
38   */
39  
40  
41  package org.osjava.sj.memory;
42  
43  import java.util.Hashtable;
44  
45  import javax.naming.Context;
46  import javax.naming.InitialContext;
47  import javax.naming.NamingException;
48  
49  import junit.framework.TestCase;
50  
51  /***
52   * @author rzigweid
53   */
54  public abstract class MemoryContextTestAbstract extends TestCase {
55  
56      private Context context;
57      private String delimiter;
58  
59      public MemoryContextTestAbstract(String name) {
60          super(name);
61      }
62  
63      protected abstract String getDelimiter();
64  
65      public void setUp() {
66          this.delimiter = getDelimiter();
67          
68          /* Initial configuration voodoo for the default context. */
69          Hashtable contextEnv = new Hashtable();
70          contextEnv.put("java.naming.factory.initial", "org.osjava.sj.memory.MemoryContextFactory");
71  
72          /* The default is 'flat', which isn't hierarchial and not what I want. */
73          contextEnv.put("jndi.syntax.direction", "left_to_right");
74  
75          /* Separator is required for non-flat */
76          contextEnv.put("jndi.syntax.separator", this.delimiter);
77          
78          /* The intial context. */
79          try {
80              context = new InitialContext(contextEnv);
81          } catch(NamingException e) {
82              e.printStackTrace();
83          }
84      }
85      
86      public void tearDown() {
87          /* - Needs improving as you cannot close a Context with values in
88          try {
89              context.close();
90          } catch (NamingException e) {
91              e.printStackTrace();
92          }
93          */
94      }
95      
96      public void testCreateSubContext1() {
97          try {
98              /* Create the subContext object */
99              context.createSubcontext("path");
100 
101             /* Make sure that it exists. */
102             Object sub = context.lookup("path");
103             assertNotNull(sub);
104         } catch (NamingException e) {
105             fail("NamingException " + e.getMessage());
106         }
107     }
108 
109     public void testCreateNestedSubcontext() {
110         try {
111             Context sub = context.createSubcontext("path");
112             sub = sub.createSubcontext("one");
113             sub = sub.createSubcontext("two");
114 
115             assertNotNull( context.lookup("path"+this.delimiter+"one"+this.delimiter+"two") );
116         } catch (NamingException e) {
117             e.printStackTrace();
118             fail("NamingException " + e.getMessage());
119         }
120     }
121 
122     public void testLookupInContext() {
123         try {
124             context.createSubcontext("path");
125             context.bind("path"+this.delimiter+"foo", "42");
126             assertEquals("42", context.lookup("path"+this.delimiter+"foo") );
127         } catch (NamingException e) {
128             fail("NamingException " + e.getMessage());
129         }
130     }
131 }