summaryrefslogtreecommitdiffstats
path: root/sandbox/axis2-1.4/modules/binding-corba/src/test/java/org/apache/tuscany/sca/binding/corba/testing/CorbaTypesTestCase.java
blob: 4558fd3d5d7b163ea755c5c0bc23180fe1545d5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */

package org.apache.tuscany.sca.binding.corba.testing;

import java.lang.reflect.Array;

import junit.framework.TestCase;

import org.apache.tuscany.sca.binding.corba.exceptions.CorbaException;
import org.apache.tuscany.sca.binding.corba.exceptions.RequestConfigurationException;
import org.apache.tuscany.sca.binding.corba.impl.reference.DynaCorbaRequest;
import org.apache.tuscany.sca.binding.corba.impl.reference.DynaCorbaResponse;
import org.apache.tuscany.sca.binding.corba.testing.hierarchy.DivByZeroException;
import org.apache.tuscany.sca.binding.corba.testing.hierarchy.DummyObject;
import org.apache.tuscany.sca.binding.corba.testing.hierarchy.NotSupportedException;
import org.apache.tuscany.sca.binding.corba.testing.hierarchy.SimpleStruct;
import org.apache.tuscany.sca.binding.corba.testing.hierarchy.SomeStruct;
import org.apache.tuscany.sca.binding.corba.testing.servants.ArraysSetterServant;
import org.apache.tuscany.sca.binding.corba.testing.servants.CalcServant;
import org.apache.tuscany.sca.binding.corba.testing.servants.ObjectManagerServant;
import org.apache.tuscany.sca.binding.corba.testing.servants.PrimitivesSetterServant;
import org.apache.tuscany.sca.binding.corba.testing.servants.TestObjectServant;
import org.omg.CORBA.ORB;
import org.omg.CORBA.Object;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContext;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.CosNaming.NamingContextHelper;

/**
 * @version $Rev$ $Date$
 */
public class CorbaTypesTestCase extends TestCase {

	private Process tnameservProcess;
	private Object refPrimitivesSetter;
	private Object refArraysSetter;
	private Object refTestObject;
	private Object refCalcObject;
	private Object refObjectManager;

	/**
	 * Spawns tnameserv process (must be in PATH). Initializes test servants and
	 * stores it's references so tests can use it.
	 */
	public void setUp() {
		try {
			String[] args = { "-ORBInitialPort", "11100" };

			tnameservProcess = Runtime.getRuntime().exec(
					"tnameserv " + args[0] + " " + args[1]);

			try {
				// let the tnameserv have time to start
				Thread.sleep(TestConstants.TNAMESERV_SPAWN_WAIT);
			} catch (Exception e) {
				e.printStackTrace();
			}

			ORB orb = ORB.init(args, null);
			Object nameService = orb.resolve_initial_references("NameService");
			NamingContext namingContext = NamingContextHelper
					.narrow(nameService);

			PrimitivesSetterServant singleSetter = new PrimitivesSetterServant();
			ArraysSetterServant arraysSetter = new ArraysSetterServant();
			TestObjectServant complexObject = new TestObjectServant();
			CalcServant calcObject = new CalcServant();
			ObjectManagerServant objectManager = new ObjectManagerServant();

			orb.connect(singleSetter);
			orb.connect(arraysSetter);

			NameComponent nc;
			NameComponent[] path;

			nc = new NameComponent("PrimitivesSetter", "");
			path = new NameComponent[] { nc };
			namingContext.rebind(path, singleSetter);

			nc = new NameComponent("ArraysSetter", "");
			path = new NameComponent[] { nc };
			namingContext.rebind(path, arraysSetter);

			nc = new NameComponent("TestObject", "");
			path = new NameComponent[] { nc };
			namingContext.rebind(path, complexObject);

			nc = new NameComponent("CalcObject", "");
			path = new NameComponent[] { nc };
			namingContext.rebind(path, calcObject);
			
			nc = new NameComponent("ObjectManager", "");
			path = new NameComponent[] { nc };
			namingContext.rebind(path, objectManager);

			NamingContextExt nce = NamingContextExtHelper.narrow(orb
					.resolve_initial_references("NameService"));

			refArraysSetter = nce.resolve(nce.to_name("ArraysSetter"));
			refPrimitivesSetter = nce.resolve(nce.to_name("PrimitivesSetter"));
			refTestObject = nce.resolve(nce.to_name("TestObject"));
			refCalcObject = nce.resolve(nce.to_name("CalcObject"));
			refObjectManager = nce.resolve(nce.to_name("ObjectManager"));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Kills previously spawned tnameserv process.
	 */
	public void tearDown() {
		tnameservProcess.destroy();
		try {
			// let the tnameserv have time to die
			Thread.sleep(TestConstants.TNAMESERV_SPAWN_WAIT);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Tests remote operation, basing on given reference, operation name,
	 * arguments, expected return type and content
	 * 
	 * @param ref
	 *            remote object
	 * @param operationName
	 *            operation to invoke
	 * @param clazz
	 *            expected return type
	 * @param arguments
	 *            array of operation arguments
	 * @param equalTo
	 *            expected return content
	 */
	private void dynaTestInvoker(Object ref, String operationName,
			Class<?> clazz, java.lang.Object[] arguments,
			java.lang.Object equalTo) {

		try {
			DynaCorbaRequest request = new DynaCorbaRequest(ref, operationName);
			request.setOutputType(clazz);
			for (int i = 0; arguments != null && i < arguments.length; i++) {
				request.addArgument(arguments[i]);
			}

			DynaCorbaResponse response = request.invoke();
			java.lang.Object content = (java.lang.Object) response.getContent();
			assertTrue(content.getClass().equals(clazz));
			if (equalTo != null && equalTo.getClass().isArray()) {
				for (int i = 0; i < Array.getLength(equalTo); i++) {
					assertTrue(Array.get(content, i).equals(
							Array.get(equalTo, i)));
				}
			} else {
				assertTrue(content.equals(equalTo));
			}
		} catch (Exception e) {
			e.printStackTrace();
			fail("Error while invoking " + operationName);
		}
	}

	/**
	 * Tests passing (and getting as result) varied primitives
	 */
	public void test_setPrimitives() {

		dynaTestInvoker(refPrimitivesSetter, "setBoolean", Boolean.class,
				new Boolean[] { true }, true);
		dynaTestInvoker(refPrimitivesSetter, "setOctet", Byte.class,
				new Byte[] { 1 }, (byte) 1);
		dynaTestInvoker(refPrimitivesSetter, "setChar", Character.class,
				new Character[] { 'A' }, 'A');
		dynaTestInvoker(refPrimitivesSetter, "setShort", Short.class,
				new Short[] { 1 }, (short) 1);
		dynaTestInvoker(refPrimitivesSetter, "setLong", Integer.class,
				new Integer[] { 1 }, (int) 1);
		dynaTestInvoker(refPrimitivesSetter, "setLongLong", Long.class,
				new Long[] { (long) 1 }, (long) 1);
		dynaTestInvoker(refPrimitivesSetter, "setFloat", Float.class,
				new Float[] { (float) 1 }, (float) 1);
		dynaTestInvoker(refPrimitivesSetter, "setDouble", Double.class,
				new Double[] { (double) 1 }, (double) 1);
		dynaTestInvoker(refPrimitivesSetter, "setString", String.class,
				new String[] { "1" }, "1");

	}

	/**
	 * Tests passing (and getting as result) varied types sequences
	 */
	public void test_setArrays() {

		dynaTestInvoker(refArraysSetter, "setBoolean", Boolean[].class,
				new Boolean[][] { new Boolean[] { false, true } },
				new Boolean[] { false, true });

		dynaTestInvoker(refArraysSetter, "setChar", Character[].class,
				new Character[][] { new Character[] { 'A', 'B' } },
				new Character[] { 'A', 'B' });

		dynaTestInvoker(refArraysSetter, "setOctet", Byte[].class,
				new Byte[][] { new Byte[] { 1, 2 } }, new Byte[] { 1, 2 });

		dynaTestInvoker(refArraysSetter, "setShort", Short[].class,
				new Short[][] { new Short[] { 1, 2 } }, new Short[] { 1, 2 });

		dynaTestInvoker(refArraysSetter, "setLong", Integer[].class,
				new Integer[][] { new Integer[] { 1, 2 } }, new Integer[] { 1,
						2 });

		dynaTestInvoker(refArraysSetter, "setLongLong", Long[].class,
				new Long[][] { new Long[] { new Long(1), new Long(2) } },
				new Long[] { new Long(1), new Long(2) });

		dynaTestInvoker(
				refArraysSetter,
				"setFloat",
				Float[].class,
				new Float[][] { new Float[] { new Float(1.0), new Float(2.0) } },
				new Float[] { new Float(1.0), new Float(2.0) });

		dynaTestInvoker(refArraysSetter, "setDouble", Double[].class,
				new Double[][] { new Double[] { new Double(1.0),
						new Double(2.0) } }, new Double[] { new Double(1.0),
						new Double(2.0) });

		dynaTestInvoker(refArraysSetter, "setString", String[].class,
				new String[][] { new String[] { "A", "B" } }, new String[] {
						"A", "B" });

	}

	/**
	 * Tests passing (and getting as result) complex structure
	 */
	public void test_TestObject_setStruct() {
		DynaCorbaRequest request = new DynaCorbaRequest(refTestObject,
				"setStruct");

		SomeStruct struct = new SomeStruct();
		SimpleStruct inner = new SimpleStruct();
		inner.field1 = TestConstants.STR_1;
		inner.field2 = TestConstants.INT_1;
		struct.innerStruct = inner;
		struct.str_list = TestConstants.STR_ARR_2;
		struct.twoDimSeq = TestConstants.INT_ARRAY_2_DIM;
		struct.threeDimSeq = TestConstants.INT_ARRAY_3_DIM;
		struct.str = TestConstants.STR_1;

		request.addArgument(struct);
		request.setOutputType(SomeStruct.class);

		try {
			DynaCorbaResponse response = request.invoke();
			SomeStruct result = (SomeStruct) response.getContent();
			int sum = 0;
			for (int i = 0; i < result.twoDimSeq.length; i++) {
				for (int j = 0; j < result.twoDimSeq[i].length; j++) {
					sum++;
					assertEquals(TestConstants.INT_ARRAY_2_DIM[i][j],
							result.twoDimSeq[i][j]);
				}
			}
			sum = 0;
			for (int i = 0; i < result.threeDimSeq.length; i++) {
				for (int j = 0; j < result.threeDimSeq[i].length; j++) {
					for (int k = 0; k < result.threeDimSeq[i][j].length; k++) {
						sum++;
						assertEquals(TestConstants.INT_ARRAY_3_DIM[i][j][k],
								result.threeDimSeq[i][j][k]);
					}
				}
			}
			assertEquals(TestConstants.STR_1, result.str);
			assertEquals(TestConstants.STR_ARR_2[0], result.str_list[0]);
			assertEquals(TestConstants.STR_ARR_2[1], result.str_list[1]);
			assertEquals(TestConstants.STR_1, result.innerStruct.field1);
			assertEquals(TestConstants.INT_1, result.innerStruct.field2);
		} catch (Exception e) {
			e.printStackTrace();
			fail("Exception occured during tests: " + e);
		}
	}

	/**
	 * Test passing (and getting as result) simple two-field structure
	 */
	public void test_TestObject_setSimpleStruct() {
		SimpleStruct struct = new SimpleStruct();
		struct.field1 = TestConstants.STR_1;
		struct.field2 = TestConstants.INT_1;
		DynaCorbaRequest request = new DynaCorbaRequest(refTestObject,
				"setSimpleStruct");
		request.setOutputType(SimpleStruct.class);
		request.addArgument(struct);
		try {
			DynaCorbaResponse response = request.invoke();
			SimpleStruct retStruct = (SimpleStruct) response.getContent();
			assertTrue(retStruct.field1.equals(struct.field1)
					&& retStruct.field2 == struct.field2);
		} catch (Exception e) {
			e.printStackTrace();
			fail("Exception occured during tests: " + e);
		}
	}

	/**
	 * Tests passing (and getting as result) two dim. sequence of long.
	 */
	public void test_TestObject_setLongSeq2() {
		int[][] arr1 = new int[2][2];
		for (int i = 0; i < 2; i++) {
			for (int j = 0; j < 2; j++) {
				arr1[i][j] = (int) (Math.random() * 1000);
			}
		}
		DynaCorbaRequest request = new DynaCorbaRequest(refTestObject,
				"setLongSeq2");
		request.setOutputType(arr1.getClass());
		request.addArgument(arr1);
		try {
			DynaCorbaResponse response = request.invoke();
			int[][] arr2 = (int[][]) response.getContent();
			for (int i = 0; i < 2; i++) {
				for (int j = 0; j < 2; j++) {
					assertEquals(arr1[i][j], arr2[i][j]);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			fail("Exception occured during tests: " + e);
		}
	}

	/**
	 * Tests passing multiple complex attributes.
	 */
	public void test_TestObject_pickStructFromArgs() {
		SomeStruct arg1 = new SomeStruct();
		SomeStruct arg2 = new SomeStruct();
		SomeStruct arg3 = new SomeStruct();

		SimpleStruct inner = new SimpleStruct();
		inner.field1 = TestConstants.STR_1;
		inner.field2 = TestConstants.INT_1;

		arg1.innerStruct = inner;
		arg2.innerStruct = inner;
		arg3.innerStruct = inner;

		arg1.str = TestConstants.STR_1;
		arg2.str = TestConstants.STR_2;
		arg3.str = TestConstants.STR_3;

		arg1.str_list = TestConstants.STR_ARR_1;
		arg2.str_list = TestConstants.STR_ARR_2;
		arg3.str_list = TestConstants.STR_ARR_2;

		arg1.threeDimSeq = TestConstants.INT_ARRAY_3_DIM;
		arg2.threeDimSeq = TestConstants.INT_ARRAY_3_DIM;
		arg3.threeDimSeq = TestConstants.INT_ARRAY_3_DIM;

		arg1.twoDimSeq = TestConstants.INT_ARRAY_2_DIM;
		arg2.twoDimSeq = TestConstants.INT_ARRAY_2_DIM;
		arg3.twoDimSeq = TestConstants.INT_ARRAY_2_DIM;

		DynaCorbaRequest request = new DynaCorbaRequest(refTestObject,
				"pickStructFromArgs");
		request.setOutputType(SomeStruct.class);
		request.addArgument(arg1);
		request.addArgument(arg2);
		request.addArgument(arg3);
		request.addArgument(1);
		try {

			DynaCorbaResponse response = request.invoke();
			SomeStruct result = (SomeStruct) response.getContent();

			// just make sure that servant returned right structure
			assertTrue(result.str.equals(TestConstants.STR_1));
		} catch (Exception e) {
			fail("Exception occured during tests " + e);
			e.printStackTrace();
		}

	}

	/**
	 * Tests handling user defined remote exception (single declared)
	 */
	public void test_singleException() {
		DynaCorbaRequest request1 = new DynaCorbaRequest(refCalcObject, "div");
		request1.addArgument(2d);
		request1.addArgument(2d);
		request1.setOutputType(Double.class);
		request1.addExceptionType(DivByZeroException.class);
		try {
			request1.invoke();
		} catch (Exception e) {
			fail();
		}

		DynaCorbaRequest request2 = new DynaCorbaRequest(refCalcObject, "div");
		request2.addArgument(2d);
		request2.addArgument(0d);
		request2.setOutputType(Double.class);
		request2.addExceptionType(DivByZeroException.class);
		try {
			request2.invoke();
		} catch (DivByZeroException e) {
			assertTrue(e.info != null && e.arguments != null
					&& e.arguments.arg1 == 2 && e.arguments.arg2 == 0);
		} catch (Exception exc) {
			fail();
		}
	}

	/**
	 * Tests handling user defined multiple exceptions
	 */
	public void test_multipleExceptions() {
		DynaCorbaRequest request = new DynaCorbaRequest(refCalcObject,
				"divForSmallArgs");
		request.addArgument(101d);
		request.addArgument(101d);
		request.setOutputType(Double.class);
		request.addExceptionType(DivByZeroException.class);
		request.addExceptionType(NotSupportedException.class);
		try {
			request.invoke();
		} catch (Exception e) {
			assertTrue(e instanceof RequestConfigurationException);
		}
	}

	/**
	 * Tests handling exceptions while user defined no exceptions
	 */
	public void test_noDeclaredException() {
		DynaCorbaRequest request = new DynaCorbaRequest(refCalcObject, "div");
		request.addArgument(1d);
		request.addArgument(0d);
		request.setOutputType(Double.class);
		try {
			request.invoke();
			fail();
		} catch (Exception e) {
			assertTrue(e instanceof RequestConfigurationException);
		}
	}

	/**
	 * Tests handling non existing operation situation
	 */
	public void test_noOperationException() {
		DynaCorbaRequest request = new DynaCorbaRequest(refCalcObject,
				"thisOperationSurelyDoesNotExist");
		try {
			request.invoke();
			fail();
		} catch (Exception e) {
			assertTrue(e instanceof CorbaException);
		}
	}
	
	/**
	 * Tests obtaining references to other objects and using them with specified user interface
	 */
	public void test_enchancedReferences() {
		try {
			DynaCorbaRequest request = new DynaCorbaRequest(refObjectManager, "getDummyObject");
			request.setOutputType(DummyObject.class);
			DynaCorbaResponse response = request.invoke();
			DummyObject dummy = (DummyObject) response.getContent();
			DummyObject dummy2 = dummy.cloneObject();
			assertNotSame(dummy.getLong(), dummy2.getLong());
		} catch (Exception e) {
			fail();
		}
	}

}