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.superbiz.telephone;
017:
018: import junit.framework.TestCase;
019:
020: import javax.naming.InitialContext;
021: import javax.naming.Context;
022: import java.util.Properties;
023:
024: /**
025: * @version $Rev: 601953 $ $Date: 2007-12-06 17:09:47 -0800 $
026: */
027: public class TelephoneTest extends TestCase {
028:
029: //START SNIPPET: setup
030: protected void setUp() throws Exception {
031: Properties properties = new Properties();
032: properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
033: "org.apache.openejb.client.LocalInitialContextFactory");
034: properties.setProperty("openejb.embedded.remotable", "true");
035: // Uncomment these properties to change the defaults
036: //properties.setProperty("openejb.ejbd.port", "4201");
037: //properties.setProperty("openejb.ejbd.bind", "localhost");
038: //properties.setProperty("openejb.ejbd.threads", "200");
039: //properties.setProperty("openejb.ejbd.disabled", "false");
040: //properties.setProperty("openejb.ejbd.only_from", "127.0.0.1,192.168.1.1");
041:
042: new InitialContext(properties);
043: }
044:
045: //END SNIPPET: setup
046:
047: /**
048: * Lookup the Telephone bean via its remote interface but using the LocalInitialContextFactory
049: *
050: * @throws Exception
051: */
052: //START SNIPPET: localcontext
053: public void testTalkOverLocalNetwork() throws Exception {
054:
055: Properties properties = new Properties();
056: properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
057: "org.apache.openejb.client.LocalInitialContextFactory");
058: InitialContext localContext = new InitialContext(properties);
059:
060: Telephone telephone = (Telephone) localContext
061: .lookup("TelephoneBeanRemote");
062:
063: telephone
064: .speak("Did you know I am talking directly through the embedded container?");
065:
066: assertEquals("Interesting.", telephone.listen());
067:
068: telephone
069: .speak("Yep, I'm using the bean's remote interface but since the ejb container is embedded "
070: + "in the same vm I'm just using the LocalInitialContextFactory.");
071:
072: assertEquals("Really?", telephone.listen());
073:
074: telephone
075: .speak("Right, you really only have to use the RemoteInitialContextFactory if you're in a different vm.");
076:
077: assertEquals("Oh, of course.", telephone.listen());
078: }
079:
080: //END SNIPPET: localcontext
081:
082: /**
083: * Lookup the Telephone bean via its remote interface using the RemoteInitialContextFactory
084: *
085: * @throws Exception
086: */
087: //START SNIPPET: remotecontext
088: public void testTalkOverRemoteNetwork() throws Exception {
089: Properties properties = new Properties();
090: properties
091: .setProperty(Context.INITIAL_CONTEXT_FACTORY,
092: "org.apache.openejb.client.RemoteInitialContextFactory");
093: properties.setProperty(Context.PROVIDER_URL,
094: "ejbd://localhost:4201");
095: InitialContext remoteContext = new InitialContext(properties);
096:
097: Telephone telephone = (Telephone) remoteContext
098: .lookup("TelephoneBeanRemote");
099:
100: telephone.speak("Is this a local call?");
101:
102: assertEquals("No.", telephone.listen());
103:
104: telephone
105: .speak("This would be a lot cooler if I was connecting from another VM then, huh?");
106:
107: assertEquals("I wondered about that.", telephone.listen());
108:
109: telephone
110: .speak("I suppose I should hangup and call back over the LocalInitialContextFactory.");
111:
112: assertEquals("Good idea.", telephone.listen());
113:
114: telephone
115: .speak("I'll remember this though in case I ever have to call you accross a network.");
116:
117: assertEquals("Definitely.", telephone.listen());
118: }
119: //END SNIPPET: remotecontext
120:
121: }
|