summaryrefslogtreecommitdiffstats
path: root/sdo-java/branches/sdo-1.1.1-incubating/impl/src/test/java/org/apache/tuscany/sdo/test/JavaSerializeDeserializeTestCase.java
blob: df69555d7617dd86d9d5b67c6d51bd8d3651117e (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
/**
 *
 *  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.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

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

import commonj.sdo.DataGraph;
import commonj.sdo.DataObject;
import commonj.sdo.Type;
import commonj.sdo.helper.DataFactory;
import commonj.sdo.helper.HelperContext;
import commonj.sdo.helper.TypeHelper;

public class JavaSerializeDeserializeTestCase extends TestCase
{
	
    public void testScopeDefinedSerializeDeserializeOfDataObject()
    {
        HelperContext hc = SDOUtil.createHelperContext();
        Object originalDataObject = createDynamically(hc,true);
        
        runSerializeDeserialize((DataObject)originalDataObject, hc);
    }
        
    public void testScopeDefinedSerializeDeserializeOfDataGraph()
    {
        HelperContext hc = SDOUtil.createHelperContext();
        DataGraph testDO = (DataGraph)createDynamically(hc,false);
        
        runSerializeDeserializeWithDataGraph(testDO, hc);
    }
    
    private String xsdString = "<xsd:schema targetNamespace=\"http://www.example.com/simple\" " +
        "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " + 
        "xmlns:simple=\"http://www.example.com/simple\">" +
        "<xsd:element name=\"company\" type=\"simple:Company\"/>" +
        "<xsd:complexType name=\"Company\">" +
        "<xsd:sequence>" +
        "<xsd:element name=\"symbol\" type=\"xsd:string\"/>" +
        "<xsd:element name=\"companyName\" type=\"xsd:string\"/>" +
        "<xsd:element name=\"employees\" type=\"simple:Employee\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>" +
        "</xsd:sequence>" +
        "</xsd:complexType>" +
        
        "<xsd:complexType name=\"Employee\">" +
        "<xsd:sequence>" +
        "<xsd:element name=\"employeeID\" type=\"xsd:string\"/>" +
        "<xsd:element name=\"employeeName\" type=\"xsd:string\"/>" +
        "</xsd:sequence>" +
        "</xsd:complexType>" +       
        "</xsd:schema>";
    
    public void testLargePayload()
    {
        HelperContext hc = SDOUtil.createHelperContext();
        hc.getXSDHelper().define(xsdString);
        DataObject company = hc.getDataFactory().create("http://www.example.com/simple", "Company");
        company.setString("symbol", "EXAMPLE");
        company.setString("companyName", "Example Inc.");
        List employees = company.getList("employees");
        DataObject employee;
        for (int i=0; i<1000; i++) {
            employee = hc.getDataFactory().create("http://www.example.com/simple", "Employee");
            employee.setString("employeeID", "ID #" + i);
            employee.setString("employeeName", "Empoyee #" + i);
            employees.add(employee);
        }
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = SDOUtil.createObjectOutputStream(bos, hc);
            oos.writeObject(company);
            oos.flush();
            byte[] bytes = bos.toByteArray();
            oos.close();
            bos.close();
            
            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = SDOUtil.createObjectInputStream(bis, hc);
            ois.readObject();
            ois.close();
            bis.close();
        }
        catch (Exception e) {
            e.printStackTrace();
            fail("An Exception occurred while deserializing the output of the serialization: "  + e.toString());
        }
    }
	
        
    /**
     * Serialize the DataObject then Deserialize the output. 
     * to testDO.
     * @param testDO
     * @param scope 
     */
    
    public void runSerializeDeserialize(DataObject originalDataObject, HelperContext hc) 
    {    	
            
        populateFields(originalDataObject);
        DataObject tempDO = null;
        ByteArrayOutputStream baos = null;
        
        try
        {
            baos = serialize(originalDataObject, hc);
        
        }
        catch (Exception e)
        {
            e.printStackTrace();
            fail("An Exception occurred while serializing the DataObject: " + e.toString());    		
        }
        
        try
        {
            tempDO = deserialize(baos, hc);
        
        }
        catch (Exception e) 
        {
            e.printStackTrace();
            fail("An Exception occurred while deserializing the output of the serialization: "  + e.toString());
        }      
        
        assertNotNull("Deserialization returned a null value.", tempDO);
        
        assertSame(tempDO.getType(), originalDataObject.getType());
            

    } 
    
    /**
     * Serialize the DataGraph
     * @param dataGraph
     * @param scope
     */
    public void runSerializeDeserializeWithDataGraph(DataGraph dataGraph, HelperContext hc) 
    {           
        DataObject originalDataObject = dataGraph.getRootObject();
        populateFields(originalDataObject);
        DataObject tempDO = null;
        ByteArrayOutputStream baos = null;
            
        try
        {
            baos = serialize(dataGraph, hc);
        
        }
        catch (Exception e)
        {
            e.printStackTrace();
            fail("An Exception occurred while serializing the DataObject: " + e.toString());                
        }
        
        try
        {
            tempDO = deserialize(baos, hc);
        
        }
        catch (Exception e) 
        {
            e.printStackTrace();
            fail("An Exception occurred while deserializing the output of the serialization: "  + e.toString());
        }      
        
        assertNotNull("Deserialization returned a null value.", tempDO);
        
        assertSame(tempDO.getType(), originalDataObject.getType());
        
        
    
    }  

    /**
     * serializeDataObject is a private method to be called by the other methods
     * in the ScrenarioLibrary
     * 
     * @param dataObject
     * @param fileName
     * @throws IOException
     */
    public ByteArrayOutputStream serialize(Object object, HelperContext hc) throws IOException 
    {
        //FileOutputStream fos = new FileOutputStream("temp");
        ByteArrayOutputStream byteArrayOutput = new ByteArrayOutputStream();
        ObjectOutputStream out = SDOUtil.createObjectOutputStream(byteArrayOutput, hc);
        out.writeObject(object);
        out.close();
        return byteArrayOutput;
    }

    /**
     * deserializeDataObject is a private method to be called by the other
     * methods in the ScrenarioLibrary
     * 
     * @param fileName
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public DataObject deserialize(ByteArrayOutputStream baos, HelperContext hc) throws IOException, ClassNotFoundException 
    {
        //FileInputStream fis = new FileInputStream("temp");
        ObjectInputStream input = null;
        ByteArrayInputStream byteArrayInput = new ByteArrayInputStream(baos.toByteArray());
        
        input = SDOUtil.createObjectInputStream(byteArrayInput, hc);
        
        Object object = input.readObject();
        input.close();
        if(object instanceof DataGraph)
            return ((DataGraph)object).getRootObject();
        else
            return (DataObject)object;
    }
    
    /**
     * populateFields uses set<Type> to set each of the fields in the
     * DataObject. It is used to ensure a known set of expected values that are
     * not other than the default values for the various fields.
     * 
     * @param testDO
     * @throws ExpectedConditionError
     */
    public static void populateFields(DataObject testDO) 
    {

        testDO.setString("stringVal", "Testing");

    }
    /**
     * createDynamically() creates the SDO Types using the TypeHelper.  This method should be kept in
     * synch with the XSD used for createDynamicallyWithStaticResources.  The same XSD is used for
     * the static generation of SDO Types using XSD2JavaGenerator.
     */
    public Object createDynamically(HelperContext hc, boolean createDataObject)
    {
        
        TypeHelper types = hc.getTypeHelper();
        DataFactory dataFactory = hc.getDataFactory();
    
    	Type stringType = types.getType("commonj.sdo", "String");
        
    	DataObject testType = dataFactory.create("commonj.sdo", "Type");
    	testType.set("uri", "http://www.example.com/api_test");
    	testType.set("name", "APITest");
    	
        DataObject stringProperty = testType.createDataObject("property");
        stringProperty.set("name", "stringVal");
        stringProperty.set("type", stringType);
        
           
        List types2Define = new ArrayList();
        types2Define.add(testType);
        List apiXSD = types.define(types2Define);
        Type apiXSDType = (Type) apiXSD.get(0);
        
        if(createDataObject)
            return dataFactory.create(apiXSDType);;
        
        // Create an empty DataGraph and attach the document root to it. Otherwise, where is the documentRoot ?
        DataGraph dataGraph = SDOUtil.createDataGraph();
        /*DataObject testDO =*/ dataGraph.createRootObject(apiXSDType);
        
        
        return dataGraph;
        
    }
}