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;
017:
018: import junit.framework.TestCase;
019: import org.apache.openejb.server.ejbd.EjbServer;
020: import org.apache.openejb.server.ServiceDaemon;
021: import org.apache.openejb.core.ServerFederation;
022: import org.apache.openejb.loader.SystemInstance;
023: import org.apache.openejb.assembler.classic.Assembler;
024: import org.apache.openejb.config.ConfigurationFactory;
025: import org.apache.openejb.jee.EjbJar;
026: import org.apache.openejb.jee.StatelessBean;
027:
028: import javax.naming.Context;
029: import javax.naming.InitialContext;
030: import javax.naming.NamingException;
031: import javax.naming.NameClassPair;
032: import javax.naming.NamingEnumeration;
033: import javax.naming.Binding;
034: import javax.ejb.Remote;
035: import java.util.Properties;
036: import java.util.Collections;
037: import java.util.ArrayList;
038: import java.util.HashMap;
039: import java.util.Map;
040:
041: /**
042: * @version $Rev: 607077 $ $Date: 2007-12-27 06:55:23 -0800 $
043: */
044: public class JndiTest extends TestCase {
045: public void test() throws Exception {
046: EjbServer ejbServer = new EjbServer();
047:
048: Properties initProps = new Properties();
049: initProps.setProperty("openejb.deployments.classpath.include",
050: "");
051: initProps.setProperty(
052: "openejb.deployments.classpath.filter.descriptors",
053: "true");
054: OpenEJB.init(initProps, new ServerFederation());
055: ejbServer.init(new Properties());
056:
057: ServiceDaemon serviceDaemon = new ServiceDaemon(ejbServer, 0,
058: "localhost");
059: serviceDaemon.start();
060:
061: int port = serviceDaemon.getPort();
062:
063: Assembler assembler = SystemInstance.get().getComponent(
064: Assembler.class);
065: ConfigurationFactory config = new ConfigurationFactory();
066:
067: EjbJar ejbJar = new EjbJar();
068: ejbJar.addEnterpriseBean(new StatelessBean("Orange",
069: Fruit.class));
070: ejbJar
071: .addEnterpriseBean(new StatelessBean("Apple",
072: Fruit.class));
073: ejbJar
074: .addEnterpriseBean(new StatelessBean("Peach",
075: Fruit.class));
076: ejbJar
077: .addEnterpriseBean(new StatelessBean("Pear",
078: Fruit.class));
079: ejbJar
080: .addEnterpriseBean(new StatelessBean("Plum",
081: Fruit.class));
082: ejbJar.addEnterpriseBean(new StatelessBean("ejb/Orange",
083: Fruit.class));
084: ejbJar.addEnterpriseBean(new StatelessBean("ejb/Apple",
085: Fruit.class));
086: ejbJar.addEnterpriseBean(new StatelessBean("ejb/Peach",
087: Fruit.class));
088: ejbJar.addEnterpriseBean(new StatelessBean("ejb/Pear",
089: Fruit.class));
090: ejbJar.addEnterpriseBean(new StatelessBean("ejb/Plum",
091: Fruit.class));
092: assembler
093: .createApplication(config.configureApplication(ejbJar));
094:
095: try {
096:
097: // good creds
098: Properties props = new Properties();
099: props
100: .put("java.naming.factory.initial",
101: "org.apache.openejb.client.RemoteInitialContextFactory");
102: props.put("java.naming.provider.url", "ejbd://127.0.0.1:"
103: + port);
104: Context context = new InitialContext(props);
105:
106: assertNameClassPair(context.list(""));
107: assertNameClassPair(context.list("ejb"));
108:
109: assertBindings(context.listBindings(""));
110: assertBindings(context.listBindings("ejb"));
111:
112: } finally {
113: serviceDaemon.stop();
114: OpenEJB.destroy();
115: }
116:
117: }
118:
119: private void assertNameClassPair(
120: NamingEnumeration<NameClassPair> namingEnumeration) {
121: assertNotNull("namingEnumeration", namingEnumeration);
122:
123: Map<String, String> map = new HashMap<String, String>();
124: while (namingEnumeration.hasMoreElements()) {
125: NameClassPair pair = namingEnumeration.nextElement();
126: map.put(pair.getName(), pair.getClassName());
127: }
128:
129: assertTrue("OrangeRemote", map.containsKey("OrangeRemote"));
130: assertTrue("AppleRemote", map.containsKey("AppleRemote"));
131: assertTrue("PeachRemote", map.containsKey("PeachRemote"));
132: assertTrue("PearRemote", map.containsKey("PearRemote"));
133: assertTrue("PlumRemote", map.containsKey("PlumRemote"));
134: }
135:
136: private void assertBindings(
137: NamingEnumeration<Binding> namingEnumeration) {
138: assertNotNull("namingEnumeration", namingEnumeration);
139:
140: Map<String, Object> map = new HashMap<String, Object>();
141: while (namingEnumeration.hasMoreElements()) {
142: Binding pair = namingEnumeration.nextElement();
143: map.put(pair.getName(), pair.getObject());
144: }
145:
146: assertTrue("OrangeRemote", map.containsKey("OrangeRemote"));
147: assertTrue("OrangeRemote is FruitRemote", map
148: .get("OrangeRemote") instanceof FruitRemote);
149:
150: assertTrue("AppleRemote", map.containsKey("AppleRemote"));
151: assertTrue("AppleRemote is FruitRemote",
152: map.get("AppleRemote") instanceof FruitRemote);
153:
154: assertTrue("PeachRemote", map.containsKey("PeachRemote"));
155: assertTrue("PeachRemote is FruitRemote",
156: map.get("PeachRemote") instanceof FruitRemote);
157:
158: assertTrue("PearRemote", map.containsKey("PearRemote"));
159: assertTrue("PearRemote is FruitRemote",
160: map.get("PearRemote") instanceof FruitRemote);
161:
162: assertTrue("PlumRemote", map.containsKey("PlumRemote"));
163: assertTrue("PlumRemote is FruitRemote",
164: map.get("PlumRemote") instanceof FruitRemote);
165: }
166:
167: @Remote
168: public static interface FruitRemote {
169: }
170:
171: public static class Fruit implements FruitRemote {
172: }
173: }
|