summaryrefslogtreecommitdiffstats
path: root/sdo-java/branches/sdo-java-M2/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/StaticSequenceNoEmfTestCase.java
blob: 966e52cdba295e0b7d88293221bf1375ee6828d5 (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
/**
 *
 *  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.sdo.test;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import junit.framework.TestCase;

import org.apache.tuscany.sdo.util.SDOUtil;

import com.example.sequences.MixedQuote;
import com.example.sequences.MixedRepeatingChoice;
import com.example.sequences.RepeatingChoice;
import com.example.sequences.SequencesFactory;
import com.example.sequences.TwoRCs;
import com.example.sequences.TwoRCsMixed;
import commonj.sdo.DataObject;
import commonj.sdo.Sequence;
import commonj.sdo.Type;
import commonj.sdo.helper.XMLHelper;


public class StaticSequenceNoEmfTestCase extends TestCase {
    private final String TEST_NAMESPACE = "http://www.example.com/sequences";
    private final String MQ_TEST_DATA = "/mixedStaticTestResult.xml";
    private final String RC_TEST_DATA = "/repeatingChoiceTestResult.xml";
    private final String MRC_TEST_DATA = "/mixedRepeatingChoiceTestResult.xml";
    private final String RC2_TEST_DATA = "/twoRepeatingChoicesTestResult.xml";
    private final String RC2M_TEST_DATA = "/twoRepeatingChoicesMixedTestResult.xml";

    /**
     * Sequenced type SDO 2 test.
     */
    public void testMixedQuoteType() throws IOException {
        MixedQuote quote = SequencesFactory.INSTANCE.createMixedQuote();
        
        Type t = ((DataObject)quote).getType();
        List ps = t.getProperties();


        Sequence sequence = quote.getMixed();

        sequence.add("\n  ");

        quote.setSymbol("fbnt");

        sequence.add("\n  ");

        quote.setCompanyName("FlyByNightTechnology");

        sequence.add("\n  some text\n  ");

        List quotes = quote.getQuotes();
        MixedQuote child = SequencesFactory.INSTANCE.createMixedQuote();
        quotes.add(child);
        
        child.setPrice(new BigDecimal("2000.0"));

        sequence.add("\n  more text\n  ");

        sequence.add("price", new BigDecimal("1000.0"));

        sequence.add("\n");
        
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        XMLHelper.INSTANCE.save((DataObject)quote, TEST_NAMESPACE, "mixedStockQuote", baos);
        assertTrue(baos.toString(), TestUtil.equalXmlFiles(new ByteArrayInputStream(baos.toByteArray()), getClass().getResource(MQ_TEST_DATA)));
    }
    
    public void testRepeatingChoice() throws IOException
    {
      RepeatingChoice rc = SequencesFactory.INSTANCE.createRepeatingChoice();
      
      List as = rc.getA();
      List bs = rc.getB();
      
      bs.add(new Integer(1));      
      as.add("foo");
      as.add("bar");
      bs.add(new Integer(2));
     

      
     
      Sequence group = rc.getGroup();
      assertEquals(group.size(), 4);
      assertEquals(rc.getA().size(), 2);
      assertEquals(rc.getB().size(),2);
      
      assertEquals(group.getValue(0), new Integer(1));
      assertEquals(group.getValue(1), "foo");
      assertEquals(group.getValue(2), "bar");
      assertEquals(group.getValue(3), new Integer(2));
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      
      XMLHelper.INSTANCE.save((DataObject)rc, TEST_NAMESPACE, "rc", baos);
      assertTrue(baos.toString(), TestUtil.equalXmlFiles(new ByteArrayInputStream(baos.toByteArray()), getClass().getResource(RC_TEST_DATA)));
    }
    
    public void testMixedRepeatingChoice() throws Exception {
      MixedRepeatingChoice mrc = SequencesFactory.INSTANCE.createMixedRepeatingChoice();
      
      List as = mrc.getA();
      List bs = mrc.getB();
      

      bs.add(new Integer(1));
      as.add("foo");
      Sequence mixed = mrc.getMixed();
      mixed.add("some mixed text");
      as.add("bar");
      bs.add(new Integer(2));
      
      // FIXME reintroduce check
      // assertEquals(4, mrc.getGroup().size());
      assertEquals(5, mrc.getMixed().size());
      assertEquals(2, mrc.getA().size());
      assertEquals(2, mrc.getB().size(),2);
      
      
       
      assertEquals(mixed.getValue(0), new Integer(1));
      assertEquals(mixed.getValue(1), "foo");
      assertEquals(mixed.getValue(3), "bar");
      assertEquals(mixed.getValue(4), new Integer(2));
      
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      
      
      XMLHelper.INSTANCE.save((DataObject)mrc, TEST_NAMESPACE, "mrc", baos);
      assertTrue(baos.toString(), TestUtil.equalXmlFiles(new ByteArrayInputStream(baos.toByteArray()), getClass().getResource(MRC_TEST_DATA)));
    }
    
    public void test2RepeatingChoices()  throws Exception {
      TwoRCs rc2 = SequencesFactory.INSTANCE.createTwoRCs();
      
      List as = rc2.getA();
      List bs = rc2.getB();
      List ys = rc2.getY();
      List zs = rc2.getZ();
      
      zs.add(new Integer(99));
      bs.add(new Integer(1));      
      ys.add("fred");
      as.add("foo");
      as.add("bar");
      bs.add(new Integer(2));
      rc2.setSplit("banana");
     
     
      Sequence group = rc2.getGroup();
      assertEquals(group.size(), 4);
      assertEquals(rc2.getA().size(), 2);
      assertEquals(rc2.getB().size(),2);
      
      Sequence group1 = rc2.getGroup1();
      assertEquals(group1.size(), 2);
      assertEquals(rc2.getY().size(), 1);
      assertEquals(rc2.getZ().size(),1);
      
      assertEquals(group.getValue(0), new Integer(1));
      assertEquals(group.getValue(1), "foo");
      assertEquals(group.getValue(2), "bar");
      assertEquals(group.getValue(3), new Integer(2));
      assertEquals(group1.getValue(0), new Integer(99));
      assertEquals(group1.getValue(1), "fred");
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      

      XMLHelper.INSTANCE.save((DataObject)rc2, TEST_NAMESPACE, "rc2", baos);
      assertTrue(baos.toString(), TestUtil.equalXmlFiles(new ByteArrayInputStream(baos.toByteArray()), getClass().getResource(RC2_TEST_DATA)));
    }

    protected void setUp() throws Exception {
        super.setUp();
        SDOUtil.registerStaticTypes(SequencesFactory.class);
    }

    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public void test2RepeatingChoicesMixed()  throws Exception {
      TwoRCsMixed rc2m = SequencesFactory.INSTANCE.createTwoRCsMixed();
      
      List as = rc2m.getA();
      List bs = rc2m.getB();
      List ys = rc2m.getY();
      List zs = rc2m.getZ();
      Sequence mixed = rc2m.getMixed();
      
      bs.add(new Integer(1));
      mixed.add("where will this appear?");
      as.add("foo");
      as.add("bar");
      bs.add(new Integer(2));
      rc2m.setSplit("pea");
      zs.add(new Integer(99));
      ys.add("fred");

    
      assertEquals(8, mixed.size());
      
      Sequence group = rc2m.getGroup();
      // FIXME reintroduce test assertEquals(4, group.size());
      assertEquals(rc2m.getA().size(), 2);
      assertEquals(rc2m.getB().size(),2);
      
      Sequence group1 = rc2m.getGroup1();
      // FIXME ditto assertEquals(group1.size(), 2);
      assertEquals(rc2m.getY().size(), 1);
      assertEquals(rc2m.getZ().size(),1);
      
      int i = 0;
      assertEquals(mixed.getValue(i++), new Integer(1));
      assertEquals(mixed.getValue(i++), "where will this appear?");
      assertEquals(mixed.getValue(i++), "foo");
      assertEquals(mixed.getValue(i++), "bar");
      assertEquals(mixed.getValue(i++), new Integer(2));
      assertEquals(mixed.getValue(i++), "pea");
      assertEquals(mixed.getValue(i++), new Integer(99));
      assertEquals(mixed.getValue(i++), "fred");
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      
      XMLHelper.INSTANCE.save((DataObject)rc2m, TEST_NAMESPACE, "rc2m", baos);
      assertTrue(baos.toString(), TestUtil.equalXmlFiles(new ByteArrayInputStream(baos.toByteArray()), getClass().getResource(RC2M_TEST_DATA)));
    }
    
    
}