summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/testing/itest/corba/src/test/java/org/apache/tuscany/sca/test/corba/ScenarioOneTestCase.java
blob: 4b58fab1f0537a8264636a5e1cc233608a4a22ca (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
/*
 * 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.test.corba;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import junit.framework.Assert;

import org.apache.tuscany.sca.host.corba.jse.DefaultCorbaHost;
import org.apache.tuscany.sca.host.corba.naming.TransientNameServer;
import org.apache.tuscany.sca.host.corba.naming.TransientNameService;
import org.apache.tuscany.sca.node.Node;
import org.apache.tuscany.sca.node.NodeFactory;
import org.apache.tuscany.sca.test.corba.generated.Color;
import org.apache.tuscany.sca.test.corba.generated.InnerStruct;
import org.apache.tuscany.sca.test.corba.generated.RichStruct;
import org.apache.tuscany.sca.test.corba.generated.ScenarioOne;
import org.apache.tuscany.sca.test.corba.generated.ScenarioOneHelper;
import org.apache.tuscany.sca.test.corba.generated.ScenarioOneOperations;
import org.apache.tuscany.sca.test.corba.generated.UnexpectedException;
import org.apache.tuscany.sca.test.corba.generated.WrongColor;
import org.apache.tuscany.sca.test.corba.types.ScenarioOneServant;
import org.apache.tuscany.sca.test.corba.types.TColor;
import org.apache.tuscany.sca.test.corba.types.TInnerStruct;
import org.apache.tuscany.sca.test.corba.types.TRichStruct;
import org.apache.tuscany.sca.test.corba.types.TScenarioOne;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.omg.CORBA.ORB;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContext;
import org.omg.CosNaming.NamingContextHelper;

/**
 * @version $Rev$ $Date$
 * This test class contains three main tests:<br>
 * 1. Tuscany is being used as a consumer of some non-Tuscany CORBA service<br>
 * 2. Tuscany is being used as a service provider, which will be consumed by
 * non-Tuscany CORBA client<br>
 * 3. Tuscany is being used as a service provider, which will be consumed by
 * Tuscany client<br>
 * But that's not all, there are some other variations. Tuscany CORBA binding
 * supports using Java interface generated by IDLJ, also it supports interfaces
 * provided by user - I combined those cases in each test.<br>
 */
public class ScenarioOneTestCase {

    // note that those values are also used in resources/*.composite file
    private static int ORB_INITIAL_PORT = 5060;
    private static String SERVICE_NAME = "ScenarioOne";

    private static Node node;

    private static TransientNameServer server;
    private static ORB orb;

    /**
     * Sets up name service, creates and registers traditional CORBA service,
     * obtains SCADomain
     */
    @BeforeClass
    public static void setUp() {
        TestCorbaHost.setCorbaHost(new DefaultCorbaHost());
        try {
            try {
                server =
                    new TransientNameServer("localhost", ORB_INITIAL_PORT, TransientNameService.DEFAULT_SERVICE_NAME);
                Thread t = server.start();
                if (t == null) {
                    Assert.fail("The naming server cannot be started");
                }
                orb = server.getORB();
            } catch (Throwable e) {
                e.printStackTrace();
                Assert.fail(e.getMessage());
            }
            org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
            NamingContext ncRef = NamingContextHelper.narrow(objRef);
            NameComponent nc = new NameComponent(SERVICE_NAME, "");
            NameComponent path[] = {nc};
            ScenarioOne scenarioOne = new ScenarioOneServant();
            ncRef.rebind(path, scenarioOne);
            // obtain node
            node = NodeFactory.getInstance().createNode("ScenarioOne.composite", TestCorbaHost.class.getClassLoader()).start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Kills previously spawned name service.
     */
    @AfterClass
    public static void tearDown() {
        server.stop();
        node.stop();
    }

    /**
     * Creates nicely filled structure for user provided interface.
     * 
     * @return
     */
    private static TRichStruct getTRichStruct() {
        int[][] intArr = new int[][] { {1, 2}, {3, 4}};
        TInnerStruct innerStruct = new TInnerStruct(intArr, "Test", TColor.green);
        String[] strSeq = {"i", "Test"};
        return new TRichStruct(innerStruct, strSeq, 1);

    }

    /**
     * Creates nicely filled structure for generated interface.
     * 
     * @return
     */
    private static RichStruct getRichStruct() {
        int[][] intArr = new int[][] { {1, 2}, {3, 4}};
        InnerStruct innerStruct2 = new InnerStruct(intArr, "Test", Color.green);
        String[] strSeq = {"i", "Test"};
        return new RichStruct(innerStruct2, strSeq, 1);
    }

    /**
     * Compares String arrays
     * 
     * @param arg1
     * @param arg2
     * @return
     */
    private boolean areSrringArraysEqual(String[] arg1, String[] arg2) {
        try {
            for (int i = 0; i < arg1.length; i++) {
                if (!arg1[i].equals(arg2[i])) {
                    return false;
                }
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * Compares two dimensional int arrays
     * 
     * @param arg1
     * @param arg2
     * @return
     */
    private boolean areTwoDimIntArraysEqual(int[][] arg1, int[][] arg2) {
        try {
            for (int i = 0; i < arg1.length; i++) {
                for (int j = 0; j < arg1[i].length; j++) {
                    if (arg1[i][j] != arg2[i][j]) {
                        return false;
                    }
                }
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    private boolean equalTo(TInnerStruct arg1, TInnerStruct arg2) {
        return (arg1.color.value() == arg2.color.value() && arg1.stringField.equals(arg2.stringField) && areTwoDimIntArraysEqual(arg1.twoDimLongSequence,
                                                                                                                                 arg2.twoDimLongSequence));
    }

    private boolean equalTo(InnerStruct arg1, InnerStruct arg2) {
        return (arg1.color.value() == arg2.color.value() && arg1.stringField.equals(arg2.stringField) && areTwoDimIntArraysEqual(arg1.twoDimLongSequence,
                                                                                                                                 arg2.twoDimLongSequence));
    }

    private boolean equalTo(TRichStruct arg1, TRichStruct arg2) {
        return (equalTo(arg1.innerStruct, arg2.innerStruct) && arg2.longField == arg1.longField && areSrringArraysEqual(arg1.stringSequence,
                                                                                                                        arg2.stringSequence));
    }

    private boolean equalTo(RichStruct arg1, RichStruct arg2) {
        return (equalTo(arg1.innerStruct, arg2.innerStruct) && arg2.longField == arg1.longField && areSrringArraysEqual(arg1.stringSequence,
                                                                                                                        arg2.stringSequence));
    }

    /**
     * Helper method used several times for various components. Executes several
     * tests using Tuscany reference binding. This helper uses generated Java
     * interface.
     * 
     * @param componentName
     */
    private void testClientUsingGeneratedInterface(String componentName) {
        ScenarioOneOperations component = node.getService(ScenarioOneOperations.class, componentName);
        RichStruct richStruct = getRichStruct();

        try {
            RichStruct result = component.setRichStruct(richStruct);
            assertTrue(equalTo(result, richStruct));
        } catch (Exception e) {
            e.printStackTrace();
            fail();
        }

        try {
            richStruct.longField = 0;
            component.setRichStruct(richStruct);
            fail();
        } catch (Exception e) {
            assertTrue(e instanceof UnexpectedException);
        }

        try {
            richStruct.longField = 1;
            richStruct.innerStruct.color = Color.red;
            component.setRichStruct(richStruct);
        } catch (Exception e) {
            assertTrue(e instanceof WrongColor);
        }
    }

    /**
     * Helper method used several times for various components. Executes several
     * tests using Tuscany reference binding. This helper uses user provided
     * Java interface.
     * 
     * @param componentName
     */
    private void testClientUsingUserProvidedInterface(String componentName) {
        TScenarioOne component = node.getService(TScenarioOne.class, componentName);
        TRichStruct tRichStruct = getTRichStruct();

        try {
            TRichStruct result = component.setRichStruct(tRichStruct);
            assertTrue(equalTo(result, tRichStruct));
        } catch (Exception e) {
            fail();
        }

        try {
            tRichStruct.longField = 0;
            component.setRichStruct(tRichStruct);
            fail();
        } catch (Exception e) {
            assertTrue(e instanceof UnexpectedException);
        }

        try {
            tRichStruct.longField = 1;
            tRichStruct.innerStruct.color = TColor.red;
            component.setRichStruct(tRichStruct);
        } catch (Exception e) {
            assertTrue(e instanceof WrongColor);
        }
    }

    public void testServiceUsingGeneratedClient(String serviceName) {
        try {
            org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
            NamingContext ncRef = NamingContextHelper.narrow(objRef);
            NameComponent nc = new NameComponent(serviceName, "");
            NameComponent path[] = {nc};
            ScenarioOne so = ScenarioOneHelper.narrow(ncRef.resolve(path));

            RichStruct richStruct = getRichStruct();
            RichStruct result = so.setRichStruct(richStruct);
            assertTrue(equalTo(result, richStruct));

            try {
                richStruct.innerStruct.color = Color.red;
                result = so.setRichStruct(richStruct);
                fail();
            } catch (Exception e) {
                assertTrue(e instanceof WrongColor);
            }

            try {
                richStruct.innerStruct.color = Color.green;
                richStruct.longField = 0;
                result = so.setRichStruct(richStruct);
                fail();
            } catch (Exception e) {
                assertTrue(e instanceof UnexpectedException);
            }

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

    /**
     * Service is exposed in traditional way (using CORBA API from JDK).
     * Reference is obtained from Tuscany.
     */
    @Test
    public void test_TraditionalService_TuscanyClient() {

        // Client is using user provided interface
        testClientUsingUserProvidedInterface("ScenarioOne");

        // Client is using generated interface
        testClientUsingGeneratedInterface("ScenarioOneGenerated");
    }

    /**
     * Service is exposed by Tuscany. Reference is obtained in traditional way.
     * (using CORBA API from JDK)
     * 
     * @throws Exception
     */
    @Test
    public void test_TuscanyService_TraditionalClient() throws Exception {

        // tests service which uses user provided interface
        testServiceUsingGeneratedClient("ScenarioOneTuscany");

        // tests service which uses generated interface
        testServiceUsingGeneratedClient("ScenarioOneTuscanyGenerated");
    }

    /**
     * Service is exposed by Tuscany. Reference is obtained from Tuscany. There
     * are 4 combinations (basing on if we are using generated or user provided
     * interfaces, both on service and reference side).
     */
    @Test
    public void test_TuscanyService_TuscanyClient() {

        // Client is using user provided interface, service is using user
        // provided interface.
        testClientUsingUserProvidedInterface("TU2TS1");

        // Client is using user provided interface, service is using generated
        // interface.
        testClientUsingUserProvidedInterface("TU2TS2");

        // Client is using generated interface, service is using user provided
        // interface.
        testClientUsingGeneratedInterface("TG2TS1");

        // Client is using generated interface, service is using generated
        // interface.
        testClientUsingGeneratedInterface("TG2TS2");
    }
    
    /**
     * Tests using reference obtained by corbaname URI
     */
    @Test
    public void test_serviceAndReferenceByURI() {
        testClientUsingUserProvidedInterface("UriBinding");
    }

}