001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.core.ivm.naming;
017:
018: import junit.framework.TestCase;
019:
020: import java.util.Map;
021:
022: import org.apache.openejb.util.Debug;
023:
024: import javax.naming.*;
025: import javax.naming.NamingException;
026:
027: /**
028: * @version $Rev: 602704 $ $Date: 2007-12-09 09:58:22 -0800 $
029: */
030: public class IvmContextTest extends TestCase {
031: public void test1() throws Exception {
032:
033: IvmContext context = new IvmContext();
034: context.bind("root/one", 1);
035: context.bind("root/two", 2);
036: context.bind("root/three", 3);
037:
038: assertContextEntry(context, "one", 1);
039: assertContextEntry(context, "two", 2);
040: assertContextEntry(context, "three", 3);
041:
042: context.unbind("root/one");
043:
044: try {
045: context.lookup("one");
046: fail("name should be unbound");
047: } catch (javax.naming.NameNotFoundException e) {
048: // pass
049: }
050:
051: // The other entries should still be there
052: // assertContextEntry(context, "one", 1);
053: assertContextEntry(context, "two", 2);
054: assertContextEntry(context, "three", 3);
055:
056: Map<String, Object> map = Debug.contextToMap(context);
057: assertFalse("name should not appear in bindings list", map
058: .containsKey("one"));
059: }
060:
061: public void test2() throws Exception {
062:
063: IvmContext context = new IvmContext();
064: context.bind("root/one", 1);
065: context.bind("root/two", 2);
066: context.bind("root/three", 3);
067:
068: assertContextEntry(context, "one", 1);
069: assertContextEntry(context, "two", 2);
070: assertContextEntry(context, "three", 3);
071:
072: context.unbind("root/two");
073:
074: try {
075: context.lookup("two");
076: fail("name should be unbound");
077: } catch (javax.naming.NameNotFoundException e) {
078: // pass
079: }
080:
081: // The other entries should still be there
082: assertContextEntry(context, "one", 1);
083: assertContextEntry(context, "three", 3);
084:
085: Map<String, Object> map = Debug.contextToMap(context);
086: assertFalse("name should not appear in bindings list", map
087: .containsKey("two"));
088: }
089:
090: public void test3() throws Exception {
091:
092: IvmContext context = new IvmContext();
093: context.bind("root/veggies/tomato/roma", 33);
094: context.bind("root/fruit/apple/grannysmith", 22);
095: context.bind("root/fruit/orange/mandarin", 44);
096:
097: assertContextEntry(context, "veggies/tomato/roma", 33);
098: assertContextEntry(context, "fruit/apple/grannysmith", 22);
099: assertContextEntry(context, "fruit/orange/mandarin", 44);
100:
101: context.unbind("root/fruit/apple/grannysmith");
102: context.prune("fruit");
103:
104: context.unbind("root/veggies/tomato/roma");
105: context.prune("veggies");
106:
107: try {
108: context.lookup("fruit/apple/grannysmith");
109: fail("name should be unbound");
110: } catch (javax.naming.NameNotFoundException pass) {
111: }
112: try {
113: context.lookup("veggies/tomato/roma");
114: fail("name should be unbound");
115: } catch (javax.naming.NameNotFoundException pass) {
116: }
117: try {
118: context.lookup("veggies/tomato");
119: fail("name should be unbound");
120: } catch (javax.naming.NameNotFoundException pass) {
121: }
122:
123: try {
124: context.lookup("veggies/fruit");
125: fail("name should be unbound");
126: } catch (javax.naming.NameNotFoundException pass) {
127: }
128:
129: Map<String, Object> map = Debug.contextToMap(context);
130: assertFalse("name should not appear in bindings list", map
131: .containsKey("veggies/tomato/roma"));
132: }
133:
134: public void testAlreadyBound() throws Exception {
135:
136: IvmContext context = new IvmContext();
137: context.bind("root/number", 2);
138: try {
139: context.bind("root/number", 3);
140: fail("A NameAlreadyBoundException should have been thrown");
141: } catch (NameAlreadyBoundException e) {
142: // pass
143: }
144:
145: }
146:
147: public void test() throws Exception {
148:
149: IvmContext context = new IvmContext();
150: context.bind("root/comp/env/rate/work/doc/lot/pop",
151: new Integer(1));
152: context.bind("root/comp/env/rate/work/doc/lot/price",
153: new Integer(2));
154: context.bind("root/comp/env/rate/work/doc/lot/break/story",
155: new Integer(3));
156:
157: Object o = context.lookup("comp/env/rate/work/doc/lot/pop");
158: assertNotNull(o);
159: assertTrue(o instanceof Integer);
160: assertEquals(o, new Integer(1));
161:
162: context.unbind("root/comp/env/rate/work/doc/lot/pop");
163:
164: try {
165: context.lookup("comp/env/rate/work/doc/lot/pop");
166: fail("name should be unbound");
167: } catch (javax.naming.NameNotFoundException e) {
168: // pass
169: }
170:
171: Map<String, Object> map = Debug.contextToMap(context);
172: assertFalse("name should not appear in bindings list", map
173: .containsKey("comp/env/rate/work/doc/lot/pop"));
174: }
175:
176: private void assertContextEntry(Context context, String s,
177: Object value) throws javax.naming.NamingException {
178: try {
179: Object two = context.lookup(s);
180: assertNotNull(two);
181: assertEquals(two, value);
182: // pass
183: } catch (NameNotFoundException e) {
184: fail("name '" + s + "' not found.");
185: }
186: }
187:
188: }
|