001: /**
002: * EasyBeans
003: * Copyright (C) 2007 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: TestSingleSmartFactory.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.component.smartclient.test;
025:
026: import java.io.File;
027: import java.io.FileOutputStream;
028: import java.lang.reflect.Method;
029: import java.net.URL;
030: import java.net.URLClassLoader;
031: import java.rmi.registry.LocateRegistry;
032: import java.rmi.registry.Registry;
033: import java.rmi.server.UnicastRemoteObject;
034: import java.util.Hashtable;
035:
036: import javax.naming.Context;
037: import javax.naming.InitialContext;
038:
039: import org.ow2.easybeans.component.smartclient.client.AskingClassLoader;
040: import org.ow2.easybeans.component.smartclient.server.SmartClientEndPointComponent;
041: import org.ow2.easybeans.component.smartclient.spi.SmartContextFactory;
042: import org.ow2.util.url.URLUtils;
043: import org.testng.Assert;
044: import org.testng.annotations.AfterClass;
045: import org.testng.annotations.BeforeClass;
046: import org.testng.annotations.Test;
047:
048: /**
049: * Tests for the Smart Factory.
050: * @author Florent BENOIT
051: */
052: public class TestSingleSmartFactory {
053:
054: /**
055: * RMi registry port.
056: */
057: public static final int RMI_REGISTRY_PORT = 15100;
058:
059: /**
060: * Other RMi registry port.
061: */
062: public static final int RMI2_REGISTRY_PORT = 15102;
063:
064: /**
065: * Smart endpoint port number.
066: */
067: public static final int SMART_ENDPOINT_PORT = 15101;
068:
069: /**
070: * Smart endpoint 2 port number.
071: */
072: public static final int SMART2_ENDPOINT_PORT = 15103;
073:
074: /**
075: * RMi Factory.
076: */
077: private static final String JRMP_RMI_FACTORY = "com.sun.jndi.rmi.registry.RegistryContextFactory";
078:
079: /**
080: * Tmp directory for dumping files.
081: */
082: private File tmpDir = null;
083:
084: /**
085: * Registry object.
086: */
087: private Registry registry = null;
088:
089: /**
090: * Other Registry object.
091: */
092: private Registry registry2 = null;
093:
094: /**
095: * Endpoint.
096: */
097: private SmartClientEndPointComponent endpoint = null;
098:
099: /**
100: * Other Endpoint.
101: */
102: private SmartClientEndPointComponent endpoint2 = null;
103:
104: /**
105: * Endpoint classloader.
106: */
107: private ClassLoader endpointClassLoader = null;
108:
109: /**
110: * Initial Context.
111: */
112: private Context initialContext = null;
113:
114: /**
115: * Other Initial Context.
116: */
117: private Context initialContext2 = null;
118:
119: /**
120: * Initialize a registry and smart context.
121: * @throws Exception if components are not initialized
122: */
123: @BeforeClass
124: public void init() throws Exception {
125: // Create a registry
126: this .registry = LocateRegistry
127: .createRegistry(RMI_REGISTRY_PORT);
128: // Create the associated registry component
129: DummyRegistryComponent registryComponent = new DummyRegistryComponent();
130: registryComponent.setProviderURL("rmi://localhost:"
131: + RMI_REGISTRY_PORT);
132:
133: // Create a registry
134: this .registry2 = LocateRegistry
135: .createRegistry(RMI2_REGISTRY_PORT);
136: // Create the associated registry component
137: DummyRegistryComponent registry2Component = new DummyRegistryComponent();
138: registry2Component.setProviderURL("rmi://localhost:"
139: + RMI2_REGISTRY_PORT);
140:
141: // Dir for dumping generated class
142: this .tmpDir = new File(System.getProperty("java.io.tmpdir")
143: + File.separator + System.getProperty("user.name")
144: + File.separator + "easybeans-smart-test");
145: if (!tmpDir.exists()) {
146: tmpDir.mkdirs();
147: }
148:
149: // Create a Smart Endpoint component on a special classloader in order
150: // to provide class only on this classloader and to be able to download
151: // the class
152:
153: URL tmpDirURL = URLUtils.fileToURL(tmpDir);
154:
155: endpointClassLoader = new URLClassLoader(
156: new URL[] { tmpDirURL }, Thread.currentThread()
157: .getContextClassLoader());
158: ClassLoader oldCl = Thread.currentThread()
159: .getContextClassLoader();
160: try {
161: Thread.currentThread().setContextClassLoader(
162: endpointClassLoader);
163: this .endpoint = new SmartClientEndPointComponent();
164: endpoint.setPortNumber(SMART_ENDPOINT_PORT);
165: endpoint.setRegistryComponent(registryComponent);
166: endpoint.init();
167: endpoint.start();
168:
169: this .endpoint2 = new SmartClientEndPointComponent();
170: endpoint2.setPortNumber(SMART2_ENDPOINT_PORT);
171: endpoint2.setRegistryComponent(registry2Component);
172: endpoint2.init();
173: endpoint2.start();
174: } finally {
175: Thread.currentThread().setContextClassLoader(oldCl);
176: }
177: }
178:
179: /**
180: * Try to access to the remote factory.
181: * @throws Exception if it fails
182: */
183: @Test
184: public void testAccessSmartFactory() throws Exception {
185: Hashtable<String, String> env = new Hashtable<String, String>();
186: env.put(Context.PROVIDER_URL, "smart://localhost:"
187: + SMART_ENDPOINT_PORT);
188: env.put(SmartContextFactory.EASYBEANS_SMART_JNDI_FACTORY,
189: JRMP_RMI_FACTORY);
190: env.put(Context.INITIAL_CONTEXT_FACTORY,
191: SmartContextFactory.class.getName());
192: this .initialContext = new InitialContext(env);
193: }
194:
195: /**
196: * Try to access to the other remote factory.
197: * @throws Exception if it fails
198: */
199: @Test
200: public void testAccessSmartFactory2() throws Exception {
201: Hashtable<String, String> env = new Hashtable<String, String>();
202: env.put(Context.PROVIDER_URL, "smart://localhost:"
203: + SMART2_ENDPOINT_PORT);
204: env.put(SmartContextFactory.EASYBEANS_SMART_JNDI_FACTORY,
205: JRMP_RMI_FACTORY);
206: env.put(Context.INITIAL_CONTEXT_FACTORY,
207: SmartContextFactory.class.getName());
208: this .initialContext2 = new InitialContext(env);
209: }
210:
211: /**
212: * Check if the Initial Context when using smart context is linked to the
213: * remote RMIi context.
214: * @throws Exception if environment is not retrieved.
215: */
216: @Test(dependsOnMethods="testAccessSmartFactory")
217: public void testGetProviderURL() throws Exception {
218: Assert.assertEquals(initialContext.getEnvironment().get(
219: Context.PROVIDER_URL), "rmi://localhost:"
220: + RMI_REGISTRY_PORT);
221: }
222:
223: /**
224: * Check if the Initial Context when using smart context is linked to the
225: * remote RMIi context.
226: * @throws Exception if environment is not retrieved.
227: */
228: @Test(dependsOnMethods="testAccessSmartFactory2")
229: public void testGetProviderURL2() throws Exception {
230: Assert.assertEquals(initialContext2.getEnvironment().get(
231: Context.PROVIDER_URL), "rmi://localhost:"
232: + RMI2_REGISTRY_PORT);
233: }
234:
235: /**
236: * Helper method that try to load a class that was dynamically generated on
237: * the endpoint side !
238: * @param askingClassLoader the classloader that download classes
239: * @param className the name of the class
240: * @param methodContent the content of the method to check
241: * @throws Exception if class is not obtained.
242: */
243: protected void downloadAndUseClass(
244: final AskingClassLoader askingClassLoader,
245: final String className, final String methodContent)
246: throws Exception {
247:
248: // Define our class
249: String packageName = "testSingleSmartFactory";
250: String fullClassName = packageName + "." + className;
251:
252: byte[] bytes = GenerateClass.getByteForClass(fullClassName
253: .replace('.', '/'), methodContent);
254:
255: // Dump this bytes to a classrugby chabal file (if done in memory the endpoint can't
256: // get the bytes)
257: File pkgDir = new File(tmpDir, packageName);
258: pkgDir.mkdir();
259: FileOutputStream fos = new FileOutputStream(new File(pkgDir,
260: className + ".class"));
261: fos.write(bytes);
262: fos.close();
263:
264: // Try to find my class (that was generated at runtime !)
265: Class<?> myClass = askingClassLoader.loadClass(fullClassName);
266: Assert.assertNotNull(myClass, "the class '" + className
267: + "' was not found");
268:
269: // build an instance
270: Object o = myClass.newInstance();
271: Method helloMethod = myClass.getMethod("hello");
272: Assert.assertNotNull(helloMethod,
273: "the method hello was not found on '" + helloMethod
274: + "'.");
275:
276: // Call method
277: Assert.assertEquals(methodContent, helloMethod.invoke(o));
278:
279: }
280:
281: /**
282: * Try to load a class that was dynamically generated on the endpoint side !
283: * @throws Exception if class is not obtained.
284: */
285: @Test
286: public void testLoadingClass() throws Exception {
287: AskingClassLoader askingClassLoader = new AskingClassLoader(
288: "localhost", SMART_ENDPOINT_PORT);
289: downloadAndUseClass(askingClassLoader, "buildclass",
290: "hello smart factory !");
291: }
292:
293: /**
294: * Try to load a class that was dynamically generated on the endpoint side !
295: * @throws Exception if class is not obtained.
296: */
297: @Test
298: public void testLoadingClass2() throws Exception {
299: AskingClassLoader askingClassLoader = new AskingClassLoader(
300: "localhost", SMART2_ENDPOINT_PORT);
301: downloadAndUseClass(askingClassLoader, "buildclass2",
302: "hello smart factory 2!");
303: }
304:
305: /**
306: * Stop all registries started.
307: * @throws Exception if registry is not stopped
308: */
309: @AfterClass(alwaysRun=true)
310: public void stopRegistry() throws Exception {
311: if (registry != null) {
312: UnicastRemoteObject.unexportObject(registry, true);
313: registry = null;
314: }
315:
316: if (registry2 != null) {
317: UnicastRemoteObject.unexportObject(registry2, true);
318: registry2 = null;
319: }
320: }
321:
322: /**
323: * Stop all endpoint started.
324: * @throws Exception if endpoint is not stopped
325: */
326: @AfterClass(alwaysRun=true)
327: public void stopEndPoint() throws Exception {
328: if (endpoint != null) {
329: endpoint.stop();
330: }
331: if (endpoint2 != null) {
332: endpoint2.stop();
333: }
334: }
335:
336: }
|