001: // ========================================================================
002: // $Id: TestNamingEntries.java 887 2006-09-05 13:46:42Z janb $
003: // Copyright 2006 Mort Bay Consulting Pty. Ltd.
004: // ------------------------------------------------------------------------
005: // Licensed under the Apache License, Version 2.0 (the "License");
006: // you may not use this file except in compliance with the License.
007: // You may obtain a copy of the License at
008: // http://www.apache.org/licenses/LICENSE-2.0
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014: // ========================================================================
015:
016: package org.mortbay.jetty.plus.naming;
017:
018: import java.util.Enumeration;
019: import java.util.Hashtable;
020:
021: import javax.naming.Context;
022: import javax.naming.InitialContext;
023: import javax.naming.Name;
024: import javax.naming.NamingException;
025: import javax.naming.RefAddr;
026: import javax.naming.Reference;
027: import javax.naming.Referenceable;
028: import javax.naming.StringRefAddr;
029: import javax.naming.spi.ObjectFactory;
030:
031: import junit.framework.TestCase;
032:
033: /**
034: * TestEnvEntry
035: *
036: *
037: */
038: public class TestNamingEntries extends TestCase {
039: public static class SomeObject {
040: private int value;
041:
042: public SomeObject(int value) {
043: this .value = value;
044: }
045:
046: public int getValue() {
047: return this .value;
048: }
049: }
050:
051: public static class SomeObjectFactory implements ObjectFactory {
052:
053: public SomeObjectFactory() {
054:
055: }
056:
057: /**
058: * @see javax.naming.spi.ObjectFactory#getObjectInstance(java.lang.Object, javax.naming.Name, javax.naming.Context, java.util.Hashtable)
059: * @param arg0
060: * @param arg1
061: * @param arg2
062: * @param arg3
063: * @return
064: * @throws Exception
065: */
066: public Object getObjectInstance(Object arg0, Name arg1,
067: Context arg2, Hashtable arg3) throws Exception {
068: Reference ref = (Reference) arg0;
069:
070: RefAddr refAddr = ref.get(0);
071: String valueName = refAddr.getType();
072: if (!valueName.equalsIgnoreCase("val"))
073: throw new RuntimeException(
074: "Unrecognized refaddr type = " + valueName);
075:
076: String value = (String) refAddr.getContent();
077:
078: return new SomeObject(Integer.parseInt(value.trim()));
079:
080: }
081:
082: }
083:
084: public static class SomeOtherObject extends SomeObject implements
085: Referenceable {
086:
087: private String svalue;
088:
089: public SomeOtherObject(String value) {
090: super (Integer.parseInt(value.trim()));
091:
092: }
093:
094: /**
095: * @see javax.naming.Referenceable#getReference()
096: * @return
097: * @throws NamingException
098: */
099: public Reference getReference() throws NamingException {
100: RefAddr refAddr = new StringRefAddr("val", String
101: .valueOf(getValue()));
102: Reference ref = new Reference(SomeOtherObject.class
103: .getName(), refAddr, SomeOtherObjectFactory.class
104: .getName(), null);
105: return ref;
106: }
107: }
108:
109: public static class SomeOtherObjectFactory implements ObjectFactory {
110:
111: public SomeOtherObjectFactory() {
112:
113: }
114:
115: /**
116: * @see javax.naming.spi.ObjectFactory#getObjectInstance(java.lang.Object, javax.naming.Name, javax.naming.Context, java.util.Hashtable)
117: * @param arg0
118: * @param arg1
119: * @param arg2
120: * @param arg3
121: * @return
122: * @throws Exception
123: */
124: public Object getObjectInstance(Object arg0, Name arg1,
125: Context arg2, Hashtable arg3) throws Exception {
126: Reference ref = (Reference) arg0;
127:
128: RefAddr refAddr = ref.get(0);
129: String valueName = refAddr.getType();
130: if (!valueName.equalsIgnoreCase("val"))
131: throw new RuntimeException(
132: "Unrecognized refaddr type = " + valueName);
133:
134: String value = (String) refAddr.getContent();
135:
136: return new SomeOtherObject(value.trim());
137: }
138:
139: }
140:
141: public SomeObject someObject;
142:
143: public void setUp() {
144: this .someObject = new SomeObject(4);
145: }
146:
147: public void testEnvEntry() throws Exception {
148: InitialContext icontext = new InitialContext();
149:
150: //override webxml
151: EnvEntry ee = new EnvEntry("nameA", someObject, true);
152: assertNotNull(EnvEntry.getEnvEntry(NamingEntry.SCOPE_GLOBAL,
153: "nameA"));
154: assertTrue(EnvEntry.getEnvEntry(NamingEntry.SCOPE_GLOBAL,
155: "nameA") instanceof EnvEntry);
156: Object x = icontext.lookup("nameA");
157: assertNotNull(x);
158: assertEquals(x, someObject);
159: }
160:
161: public void testResource() throws Exception {
162: InitialContext icontext = new InitialContext();
163:
164: Resource resource = new Resource("resourceA", someObject);
165: assertNotNull(Resource.getResource(NamingEntry.SCOPE_GLOBAL,
166: "resourceA"));
167: assertTrue(Resource.getResource(NamingEntry.SCOPE_GLOBAL,
168: "resourceA") instanceof Resource);
169: assertEquals(icontext.lookup("resourceA"), someObject);
170: }
171:
172: public void testResourceReferenceable() throws Exception {
173: SomeOtherObject someOtherObj = new SomeOtherObject("100");
174: InitialContext icontext = new InitialContext();
175: Resource res = new Resource("resourceByReferenceable",
176: someOtherObj);
177: Object o = icontext.lookup("resourceByReferenceable");
178: assertNotNull(o);
179: System.err.println(o);
180: assertTrue(o instanceof SomeOtherObject);
181: assertEquals(((SomeOtherObject) o).getValue(), 100);
182:
183: }
184:
185: public void testResourceReference() throws Exception {
186: RefAddr refAddr = new StringRefAddr("val", "10");
187: Reference ref = new Reference(SomeObject.class.getName(),
188: refAddr, SomeObjectFactory.class.getName(), null);
189:
190: InitialContext icontext = new InitialContext();
191: Resource resource = new Resource("resourceByRef", ref);
192: assertNotNull(Resource.getResource(NamingEntry.SCOPE_GLOBAL,
193: "resourceByRef"));
194: assertTrue(Resource.getResource(NamingEntry.SCOPE_GLOBAL,
195: "resourceByRef") instanceof Resource);
196: Object o = icontext.lookup("resourceByRef");
197: assertNotNull(o);
198: assertTrue(o instanceof SomeObject);
199:
200: assertEquals(((SomeObject) o).getValue(), 10);
201: }
202:
203: }
|