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: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Vasily Zakharov
021: * @version $Revision: 1.1.2.5 $
022: */package org.apache.harmony.rmi;
023:
024: import java.io.IOException;
025:
026: import java.net.InetSocketAddress;
027: import java.net.Socket;
028:
029: import java.rmi.Remote;
030:
031: import java.rmi.server.UnicastRemoteObject;
032:
033: import java.util.HashSet;
034: import java.util.Iterator;
035:
036: import org.apache.harmony.rmi.common.SubProcess;
037:
038: import junit.framework.TestCase;
039:
040: /**
041: * Base class for RMI unit tests.
042: *
043: * @author Vasily Zakharov
044: * @version $Revision: 1.1.2.5 $
045: */
046: public abstract class RMITestBase extends TestCase {
047:
048: /**
049: * Direct socket connection configuration.
050: */
051: protected static final int CONFIG_DIRECT_SOCKET = 0;
052:
053: /**
054: * Direct HTTP connection configuration.
055: */
056: protected static final int CONFIG_DIRECT_HTTP = 1;
057:
058: /**
059: * Proxy HTTP connection configuration.
060: */
061: protected static final int CONFIG_PROXY_HTTP = 2;
062:
063: /**
064: * Proxy CGI connection configuration.
065: */
066: protected static final int CONFIG_PROXY_CGI = 3;
067:
068: /**
069: * RMI Registry host.
070: */
071: protected static final String REGISTRY_HOST = "localhost";
072:
073: /**
074: * Default RMI registry port.
075: */
076: protected static final int REGISTRY_PORT = 1099;
077:
078: /**
079: * Custom port to start RMI registry on.
080: */
081: protected static final int CUSTOM_PORT_1 = 5555;
082:
083: /**
084: * Custom port to start RMI registry on.
085: */
086: protected static final int CUSTOM_PORT_2 = 7777;
087:
088: /**
089: * Custom port to start RMI registry on.
090: */
091: protected static final int CUSTOM_PORT_3 = 8888;
092:
093: /**
094: * Custom port to start RMI registry on.
095: */
096: protected static final int CUSTOM_PORT_4 = 9999;
097:
098: /**
099: * Test string.
100: */
101: protected static final String TEST_STRING_1 = "TEST_1_STRING";
102:
103: /**
104: * Test string.
105: */
106: protected static final String TEST_STRING_2 = "TEST_2_STRING";
107:
108: /**
109: * Test string.
110: */
111: protected static final String TEST_STRING_3 = "TEST_3_STRING";
112:
113: /**
114: * Test string.
115: */
116: protected static final String TEST_STRING_4 = "TEST_4_STRING";
117:
118: /**
119: * HTTP Proxy host.
120: */
121: protected static final String PROXY_HOST = "your.proxy.host";
122:
123: /**
124: * HTTP Proxy port.
125: */
126: protected static final int PROXY_PORT = 3128;
127:
128: /**
129: * HTTP Proxy access timeout (in milliseconds)
130: */
131: protected static final int PROXY_TIMEOUT = 3000;
132:
133: /**
134: * Timeout tick (in milliseconds).
135: */
136: protected static final int TIMEOUT_TICK = 1000;
137:
138: /**
139: * Default connection timeout.
140: */
141: protected static final long CONNECTION_TIMEOUT = 0;
142:
143: /**
144: * Garbage collector timeout (in milliseconds).
145: */
146: protected static final int GC_TIMEOUT = 20000;
147:
148: /**
149: * If verbose logging need to be turned on.
150: */
151: protected static final boolean VERBOSE = false;
152:
153: /**
154: * List of exported objects.
155: */
156: protected HashSet exportedObjects = new HashSet();
157:
158: /**
159: * No-arg constructor to enable serialization.
160: */
161: protected RMITestBase() {
162: super ();
163: }
164:
165: /**
166: * Constructs this test case with the given name.
167: *
168: * @param name
169: * Name for this test case.
170: */
171: protected RMITestBase(String name) {
172: super (name);
173: }
174:
175: /**
176: * Sets environment to system properties.
177: *
178: * @param disableHttp
179: * @param eagerHttpFallback
180: * @param disableDirectSocket
181: * @param enableDirectHTTP
182: * @param disablePlainHTTP
183: * @param useProxy
184: * @param connectionTimeout
185: * @param logging
186: */
187: protected static void setEnvironment(boolean disableHttp,
188: boolean eagerHttpFallback, boolean disableDirectSocket,
189: boolean enableDirectHTTP, boolean disablePlainHTTP,
190: boolean useProxy, long connectionTimeout, boolean logging) {
191: System.setProperty("java.rmi.dgc.leaseValue", Integer
192: .toString(GC_TIMEOUT));
193: System.setProperty("java.rmi.server.disableHttp", Boolean
194: .toString(disableHttp));
195: System.setProperty(
196: "harmony.rmi.transport.proxy.eagerHttpFallback",
197: Boolean.toString(eagerHttpFallback));
198: System.setProperty("harmony.rmi.transport.disableDirectSocket",
199: Boolean.toString(disableDirectSocket));
200: System.setProperty(
201: "harmony.rmi.transport.proxy.enableDirectHTTP", Boolean
202: .toString(enableDirectHTTP));
203: System.setProperty(
204: "harmony.rmi.transport.proxy.disablePlainHTTP", Boolean
205: .toString(disablePlainHTTP));
206: System.setProperty("http.proxyHost", (useProxy ? PROXY_HOST
207: : ""));
208: System.setProperty("http.proxyPort", (useProxy ? Integer
209: .toString(PROXY_PORT) : ""));
210: System.setProperty("harmony.rmi.transport.connectionTimeout",
211: ((connectionTimeout > 0) ? Long
212: .toString(connectionTimeout) : ""));
213: System.setProperty("harmony.rmi.dgc.logLevel", "VERBOSE");
214: System.setProperty("harmony.rmi.transport.logLevel",
215: (logging ? "VERBOSE" : ""));
216: System.setProperty("harmony.rmi.transport.tcp.logLevel",
217: (logging ? "VERBOSE" : ""));
218: System.setProperty("harmony.rmi.transport.proxy.logLevel",
219: (logging ? "VERBOSE" : ""));
220: }
221:
222: /**
223: * Sets environment for direct socket connections.
224: *
225: * @param config
226: * Configuration to set environment for.
227: */
228: protected static void setEnvironmentForConfig(int config) {
229: switch (config) {
230: case CONFIG_DIRECT_SOCKET:
231: setEnvironment(false, // disableHttp
232: false, // eagerHttpFallback
233: false, // disableDirectSocket
234: false, // enableDirectHTTP
235: false, // disablePlainHTTP
236: false, // useProxy
237: CONNECTION_TIMEOUT, // connectionTimeout
238: VERBOSE // Logging
239: );
240: break;
241: case CONFIG_DIRECT_HTTP:
242: setEnvironment(false, // disableHttp
243: false, // eagerHttpFallback
244: true, // disableDirectSocket
245: true, // enableDirectHTTP
246: false, // disablePlainHTTP
247: false, // useProxy
248: CONNECTION_TIMEOUT, // connectionTimeout
249: VERBOSE // Logging
250: );
251: break;
252: case CONFIG_PROXY_HTTP:
253: setEnvironment(false, // disableHttp
254: false, // eagerHttpFallback
255: true, // disableDirectSocket
256: false, // enableDirectHTTP
257: false, // disablePlainHTTP
258: true, // useProxy
259: CONNECTION_TIMEOUT, // connectionTimeout
260: VERBOSE // Logging
261: );
262: break;
263: case CONFIG_PROXY_CGI:
264: setEnvironment(false, // disableHttp
265: false, // eagerHttpFallback
266: true, // disableDirectSocket
267: false, // enableDirectHTTP
268: true, // disablePlainHTTP
269: true, // useProxy
270: CONNECTION_TIMEOUT, // connectionTimeout
271: VERBOSE // Logging
272: );
273: break;
274: default:
275: assert false : ("Bad config number: " + config);
276: }
277: }
278:
279: /**
280: * Starts process in a separate JVM.
281: *
282: * @param className
283: * Name of the class to run in process created.
284: *
285: * @param id
286: * Identifier of function to run (class specific).
287: *
288: * @param config
289: * Number of the configuration to run.
290: *
291: * @param endorsed
292: * If endorsedDirs and bootClassPath should be propagated.
293: *
294: * @return Subprocess created.
295: *
296: * @throws Exception
297: * If some error occurs.
298: */
299: protected static SubProcess startProcess(String className,
300: String id, int config, boolean endorsed) throws Exception {
301: return JavaInvoker.invokeSimilar(null, className, new String[] {
302: id, Integer.toString(config) }, endorsed, endorsed);
303: }
304:
305: /**
306: * Checks if the specified socket/port is available.
307: *
308: * @param host
309: * Host to check.
310: *
311: * @param port
312: * Port to check.
313: *
314: * @param timeout
315: * Time (in milliseconds) to wait for response.
316: *
317: * @return <code>true</code> if the specified host/port is available,
318: * <code>false</code> otherwise.
319: */
320: protected static boolean checkSocket(String host, int port,
321: int timeout) {
322: try {
323: Socket socket = new Socket();
324: socket.connect(new InetSocketAddress(host, port), timeout);
325: socket.close();
326: return true;
327: } catch (IOException e) {
328: return false;
329: }
330: }
331:
332: /**
333: * Checks if proxy is available.
334: *
335: * @return <code>true</code> if proxy is available,
336: * <code>false</code> otherwise.
337: */
338: protected static boolean checkProxy() {
339: return checkSocket(PROXY_HOST, PROXY_PORT, PROXY_TIMEOUT);
340: }
341:
342: /**
343: * Checks if proxy is available.
344: *
345: * @param testName
346: * Test name (for diagnostics purposes).
347: *
348: * @return <code>true</code> if proxy is available,
349: * <code>false</code> otherwise.
350: */
351: protected static boolean checkProxy(String testName) {
352: boolean ret = checkProxy();
353:
354: if (!ret) {
355: System.out.println("WARNING: " + testName + " is skipped "
356: + "because proxy (" + PROXY_HOST + ':' + PROXY_PORT
357: + ") is not accessible.");
358: }
359: return ret;
360: }
361:
362: /**
363: * Prints the specified array to standard output.
364: *
365: * @param array
366: * Array to print.
367: */
368: protected static void printArray(Object[] array) {
369: if (array == null) {
370: System.out.println(" Array is NULL.");
371: }
372:
373: for (int i = 0; i < array.length; ++i) {
374: System.out.println(" " + array[i]);
375: }
376: }
377:
378: /**
379: * Aborts current process with the specified code.
380: * Really calls {@link System#exit(int) System.exit(exitCode)}.
381: *
382: * @param exitCode
383: */
384: protected static void abort(int exitCode) {
385: System.exit(exitCode);
386: }
387:
388: /**
389: * Aborts current process.
390: * Really calls {@link System#exit(int) System.exit(-1)}.
391: */
392: protected static void abort() {
393: abort(-1);
394: }
395:
396: /**
397: * Unexports exported objects.
398: *
399: * @throws Exception
400: * If some error occurs.
401: */
402: protected void unexportObjects() throws Exception {
403: for (Iterator i = exportedObjects.iterator(); i.hasNext();) {
404: Remote obj = (Remote) i.next();
405: System.err.println("Unexporting " + obj + " ...");
406: UnicastRemoteObject.unexportObject(obj, true);
407: System.err.println("Done.\n");
408: }
409: }
410: }
|