summaryrefslogtreecommitdiffstats
path: root/sdo-java/trunk-cts/sdo2.1/src/main/java/test/sdo21/tests/api/ContainmentCycleSerializationTest.java
blob: 47900402bfcac15054c58cffdbbbdcb19eb858fb (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
/*
 *  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 test.sdo21.tests.api;

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

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import test.sdo21.tests.TestData.StandardDynamicFactory;
import test.sdo21.tests.TestData.StandardFactory;
import test.sdo21.tests.TestData.StandardXSDFactory;
import test.sdo21.tests.TestData.TestDataFactory;
import test.sdo21.tests.util.CTSUtil;

import commonj.sdo.DataObject;
import commonj.sdo.Sequence;
import commonj.sdo.helper.HelperContext;

/**
 * Tests serialization of containment cycles.  Each test passes if the serialization does
 * not generate output representing the containment cycle.  There is a test case for 
 * each of the various mechanisms through which a containment cycle may be created and serialized.  
 * 
 * Note also that Java serialization of a containment cycle should result in an IllegalStateException, 
 * while XMLHelper and XMLStreamHelper serialization should successfully serialize the DataObject
 * absent the containment cycle.
 */
public abstract class ContainmentCycleSerializationTest extends CTSConsistencyBase {    

  
    public ContainmentCycleSerializationTest() {
    } 
    
    public static class DynamicMetadata extends ContainmentCycleSerializationTest {
      public DynamicMetadata() {
        
      }
      public TestDataFactory createTestDataFactory() {
  
        return new StandardDynamicFactory();
      }
    }
  
    public static class XSDMetadata extends ContainmentCycleSerializationTest {
      
      public XSDMetadata() {
      }
      
      public TestDataFactory createTestDataFactory() {
  
        return new StandardXSDFactory();
      }
    }
    
    
    @Before
    public void setUp () throws Exception {
      super.setUp();
    }
    
    @After
    public void tearDown() throws Exception {
      super.tearDown();
    }
    
    
    /**
     * Attempt to create a 1-Member Containment Cycle using set____ on a single valued property
     * and verify the behavior of serialization.  
     * @throws Exception 
     */
    @Test
    public void oneMemberSingleValueSetDataObject() throws Exception {        
        DataObject dataObj1 = factory.createTestData(getScope(), StandardFactory.API_TYPE);           
        setSingleValueOneMember(dataObj1);
        attemptSerialization(dataObj1, getScope());
    }
    
    /**
     * Attempt to create an n-Member Containment Cycle using set____ on a single valued property
     * and verify the behavior of serialization.  
     * @throws Exception 
     */
    @Test
    public void nMemberSingleValueSetDataObject() throws Exception {
        DataObject dataObj1 = factory.createTestData(getScope(), StandardFactory.API_TYPE);
        DataObject dataObj2 = factory.createTestData(getScope(), StandardFactory.API_TYPE);
        DataObject dataObj3 = factory.createTestData(getScope(), StandardFactory.API_TYPE);
        
        setSingleValueNMember(dataObj1, dataObj2, dataObj3);
        attemptSerialization(dataObj1, getScope());
    }
    
    /**
     * Attempt to create a 1-Member Containment Cycle using set____ on a multi valued property
     * and verify the behavior of serialization.  
     * @throws Exception 
     */
    @Test
    public void oneMemberMultiValueSetDataObject() throws Exception {
        DataObject dataObj1 = factory.createTestData(getScope(), StandardFactory.API_TYPE);
        DataObject dataObj2 = factory.createTestData(getScope(), StandardFactory.API_TYPE);
        DataObject dataObj3 = factory.createTestData(getScope(), StandardFactory.API_TYPE);
        
        setMultiValueOneMember(dataObj1, dataObj2, dataObj3);
        attemptSerialization(dataObj1, getScope());
    }
    
    /**
     * Attempt to create an n-Member Containment Cycle using set____ on a multi valued property
     * and verify the behavior of serialization.  
     * @throws Exception 
     */
    @Test
    public void nMemberMultiValueSetDataObject() throws Exception {
        DataObject dataObj1 = factory.createTestData(getScope(), StandardFactory.API_TYPE);
        DataObject dataObj2 = factory.createTestData(getScope(), StandardFactory.API_TYPE);
        DataObject dataObj3 = factory.createTestData(getScope(), StandardFactory.API_TYPE);
        
        setManyValueNMember(dataObj1, dataObj2, dataObj3);
        attemptSerialization(dataObj1, getScope());
    }
    
    /**
     * Attempt to create a 1-Member Containment Cycle by making updates to a List returned
     * from the DataObject and verify the behavior of serialization.  This is the static version 
     * of this test case.
     * @throws Exception 
     */
    @Test
    public void oneMemberReturnedList() throws Exception {
        DataObject dataObj1 = factory.createTestData(getScope(), StandardFactory.API_TYPE);
        
        returnedListOneMember(dataObj1);
        attemptSerialization(dataObj1, getScope());
    }
    
    /**
     * Attempt to create an n-Member Containment Cycle by making updates to a List returned
     * from the DataObject and verify the behavior of serialization.  This is the static version 
     * of this test case.
     * @throws Exception 
     */
    @Test
    public void nMemberReturnedList() throws Exception {
        DataObject dataObj1 = factory.createTestData(getScope(), StandardFactory.API_TYPE);
        DataObject dataObj2 = factory.createTestData(getScope(), StandardFactory.API_TYPE);
        DataObject dataObj3 = factory.createTestData(getScope(), StandardFactory.API_TYPE);
        
        returnedListNMember(dataObj1, dataObj2, dataObj3);
        attemptSerialization(dataObj1, getScope());
    }
    
    /**
     * Attempt to create a 1-Member Containment Cycle using by using a Sequence to make updates
     * to the DataObject, affecting a single valued property, and verify the behavior of 
     * serialization.  
     * @throws Exception 
     */
    @Test
    public void oneMemberSingleValueSequence() throws Exception {
        DataObject dataObj1 = factory.createTestData(getScope(), StandardFactory.SEQ_TYPE);
        
        sequenceSingleValueOneMember(dataObj1);
        attemptSerialization(dataObj1, getScope());
    }
    
    /**
     * Attempt to create an n-Member Containment Cycle using by using a Sequence to make updates
     * to the DataObject, affecting a single valued property, and verify the behavior of 
     * serialization.  
     * @throws Exception 
     */
     @Test
    public void nMemberSingleValueSequence() throws Exception {
        DataObject dataObj1 = factory.createTestData(getScope(), StandardFactory.SEQ_TYPE);
        DataObject dataObj2 = factory.createTestData(getScope(), StandardFactory.SEQ_TYPE);
        DataObject dataObj3 = factory.createTestData(getScope(), StandardFactory.SEQ_TYPE);
        
        sequenceSingleValueNMember(dataObj1, dataObj2, dataObj3);
        attemptSerialization(dataObj1, getScope());
    }
    
    /**
     * Attempt to create a 1-Member Containment Cycle using by using a Sequence to make updates
     * to the DataObject, affecting a multi valued property, and verify the behavior of 
     * serialization.  
     * @throws Exception 
     */
    @Test
    public void oneMemberMultiValueSequence() throws Exception {
        DataObject dataObj1 = factory.createTestData(getScope(), StandardFactory.SEQ_TYPE);
        DataObject dataObj2 = factory.createTestData(getScope(), StandardFactory.SEQ_TYPE);
        DataObject dataObj3 = factory.createTestData(getScope(), StandardFactory.SEQ_TYPE);
        
        sequenceMultiValueOneMember(dataObj1, dataObj2, dataObj3);
        attemptSerialization(dataObj1, getScope());
    }
    
    /**
     * Attempt to create an n-Member Containment Cycle using by using a Sequence to make updates
     * to the DataObject, affecting a multi valued property, and verify the behavior of 
     * serialization.  
     * @throws Exception 
     */
     @Test
    public void nMemberMultiValueSequence() throws Exception {
        DataObject dataObj1 = factory.createTestData(getScope(), StandardFactory.SEQ_TYPE);
        DataObject dataObj2 = factory.createTestData(getScope(), StandardFactory.SEQ_TYPE);
        DataObject dataObj3 = factory.createTestData(getScope(), StandardFactory.SEQ_TYPE);
        
        sequenceManyValueNMember(dataObj1, dataObj2, dataObj3);
        attemptSerialization(dataObj1, getScope());
    }

    /** 
     * Attempt to serialize the DataObject in via XML, XMLStreamHelper,
     * and Java serialization.
     * @param dataObj
     */
    private void attemptSerialization(DataObject dataObj, HelperContext scope) {    
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        
        assertTrue("Containment cycle was not established.", 
                containmentCycleExists(dataObj));   
        try {
            CTSUtil.serializeDataObjectXML(dataObj, baos, scope);
        } catch (Exception e) {
            fail("XML serialization of a containment cycle resulted in a(n) " + e.getClass().getName() + ".");
            e.printStackTrace();
        }
         
        assertTrue("Containment cycle was affected by XMLHelper serialization.", 
                containmentCycleExists(dataObj));
  
        try {
             CTSUtil.serializeDataObjectJava(getTestHelper(), dataObj, baos, scope); 
             fail("Java serialization of a containment cycle should result in an IllegalStateException."); 
        } catch (Exception e) {
            // Do nothing.  An Exception is expected in this case.
        }
 
        assertTrue("Containment cycle was affected by Java serialization.", 
                containmentCycleExists(dataObj));

        // try {
        //     ParamatizedTestUtil.serializeDataObjectStream(dataObj, baos, scope);    
       //  } catch (Exception e) {
       //        fail("XMLStreamHelper serialization of a containment cycle resulted in a(n) " + e.getClass().getName() + ".");
       //     e.printStackTrace();
       // }

       // assertTrue("Containment cycle was affected by XMLStreamHelper serialization.", 
       //         containmentCycleExists(dataObj));  
    }

    /**
     * Returns true if input DataObject root is part of a containment cycle, 
     * false otherwise.
     * @param root
     * @return
     */
    private static boolean containmentCycleExists (DataObject root)
    {
        DataObject curr = root.getContainer();
        
        while (curr != null)
        {
            if (curr == root)
                return true;
            
            curr = curr.getContainer();
        }
        return false;
    }

    /**
     * Use setDataObject to create a one member containment cycle.  
     * @param dataObj1
     */
    private void setSingleValueOneMember(DataObject dataObj1) {
        try {
            dataObj1.setDataObject("contain", dataObj1);
        } catch (Exception e) {
            // do nothing - success
        }
    }
    
    /**
     * Use setDataObject to create an N-member containment cycle.
     * @param dataObj1
     * @param dataObj2
     * @param dataobj3
     */
    private void setSingleValueNMember(DataObject dataObj1, DataObject dataObj2, DataObject dataObj3) {
        dataObj1.setDataObject("contain", dataObj2);
        dataObj2.setDataObject("contain", dataObj3);
        
        try {
            dataObj3.setDataObject("contain", dataObj1);
        } catch (Exception e) {
            // do nothing - success
        }
    }

    /**
     * Use setList to create a one member containment cycle. 
     * @param dataObj1
     * @param dataObj2
     * @param dataObj3
     */
    private void setMultiValueOneMember(DataObject dataObj1, DataObject dataObj2, DataObject dataObj3) {
        List addList = new ArrayList();
        addList.add(dataObj2);
        addList.add(dataObj1);
        addList.add(dataObj3);
        
        try {
            dataObj1.setList("containMany", addList);
        } catch (Exception e) {
            // do nothing - success
        }
    }

    /**
     * Use setList to create an N-member containment cycle. 
     * @param dataObj1
     * @param dataObj2
     * @param dataObj3
     */
    private void setManyValueNMember(DataObject dataObj1, DataObject dataObj2, DataObject dataObj3) {
        List addList = new ArrayList();
        
        dataObj1.setDataObject("contain", dataObj2);
        dataObj2.setDataObject("contain", dataObj3);
        addList.add(dataObj1);
        
        try {
            dataObj3.setList("containMany", addList);
        } catch (Exception e) {
            // do nothing - success
        }
    }

    /**
     * Create a 1-member containment cycle by making updates to a returned List.  
     * @param dataObj1
     */
    private void returnedListOneMember(DataObject dataObj1) {
        List returnedList = dataObj1.getList("containMany");

        try {
            returnedList.add(dataObj1);
        } catch (Exception e) {
            // do nothing - success
        }
    }
    
    /**
     * Create an n-member containment cycle by making updates to a returned List.  
     * @param dataObj1
     * @param dataObj2
     * @param dataobj3
     */
    private void returnedListNMember(DataObject dataObj1, DataObject dataObj2, DataObject dataObj3) {
        dataObj1.setDataObject("contain", dataObj2);
        dataObj2.setDataObject("contain", dataObj3);
        
        List returnedList = dataObj3.getList("containMany");
     
        try {
               returnedList.add(dataObj1);
         } catch (Exception e) {
            // do nothing - success
        }
    }

    /**
     * Create a 1-member containment cycle by making updates to a Sequence.  
     * The data member in this case is single valued.
     * @param dataObj1
     * @param dataObj2
     * @param dataObj3
     */
    private void sequenceSingleValueOneMember(DataObject dataObj1) {
        assertTrue("DataObject is not Sequenced.  Test case may not proceed.", dataObj1.getType().isSequenced());
        
        Sequence sequence = dataObj1.getSequence();

        try {
            sequence.add("contain", dataObj1);
         } catch (Exception e) {
             // do nothing - success
         }
        
    }
    
    /**
     * Create an n-member containment cycle by making updates to a Sequence.  
     * The data member in this case is single valued.
     * @param dataObj1
     * @param dataObj2
     * @param dataObj3
     */
    private void sequenceSingleValueNMember(DataObject dataObj1, DataObject dataObj2, DataObject dataObj3) {
        assertTrue("DataObject is not Sequenced.  Test case may not proceed.", dataObj1.getType().isSequenced());

        Sequence sequence1 = dataObj1.getSequence();
        Sequence sequence2 = dataObj2.getSequence();
        Sequence sequence3 = dataObj3.getSequence();

        sequence1.add("contain", (Object) dataObj2);
        sequence2.add("contain", (Object) dataObj3);

       
        try {
           sequence3.add("contain", (Object) dataObj1);
         } catch (Exception e) {
             // do nothing - success
         }
    }

    /**
     * Create a 1-member containment cycle by making updates to a Sequence.  
     * The data member in this case is many valued.
     * @param dataObj1
     * @param dataObj2
     * @param dataObj3
     */
    private void sequenceMultiValueOneMember(DataObject dataObj1, DataObject dataObj2, DataObject dataObj3) {
        assertTrue("DataObject is not Sequenced.  Test case may not proceed.", dataObj1.getType().isSequenced());

        Sequence sequence = dataObj1.getSequence();
        
        sequence.add("containMany", dataObj2);
        sequence.add("containMany", dataObj3);
        
        try {
            sequence.add("containMany", dataObj1);
          } catch (Exception e) {
              // do nothing - success
          }
    }
    
    /**
     * Create an n-member containment cycle by making updates to a Sequence.  
     * The data member in this case is many valued.
     * @param dataObj1
     * @param dataObj2
     * @param dataObj3
     */
    private void sequenceManyValueNMember(DataObject dataObj1, DataObject dataObj2, DataObject dataObj3) {
        assertTrue("DataObject is not Sequenced.  Test case may not proceed.", dataObj1.getType().isSequenced());

        List addList = new ArrayList();
        
        addList.add(dataObj2);
        dataObj1.setList("containMany", addList);
        
        addList.clear();
        addList.add(dataObj3);
        dataObj2.setList("containMany", addList);

        Sequence sequence = dataObj3.getSequence();

        try {
            sequence.add("containMany", dataObj1);
          } catch (Exception e) {
              // do nothing - success
          }
    }
}