summaryrefslogtreecommitdiffstats
path: root/sdo-java/branches/emf-2.5/impl/src/test/java/org/apache/tuscany/sdo/test/ChangeSummaryPropertyTestCase.java
blob: ace509d1b4fe345fb671c7d08598435a782e44ab (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
/**
 *
 *  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.net.URL;
import java.util.List;

import junit.framework.TestCase;

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

import commonj.sdo.ChangeSummary;
import commonj.sdo.DataObject;
import commonj.sdo.Property;
import commonj.sdo.Sequence;
import commonj.sdo.Type;
import commonj.sdo.helper.DataFactory;
import commonj.sdo.helper.HelperContext;
import commonj.sdo.helper.XMLHelper;
import commonj.sdo.helper.XSDHelper;

public class ChangeSummaryPropertyTestCase extends TestCase {
    private final String TEST_MODEL = "/simpleWithChangeSummary.xsd";
    private final String TEST_NAMESPACE = "http://www.example.com/simpleCS";

    private final String TEST_DATA_BEFORE_UNDO = "/simpleWithChangeSummary.xml";
    private final String TEST_DATA_AFTER_UNDO = "/simpleWithChangeSummaryUndone.xml";
    
    HelperContext hc;
    DataFactory dataFactory;
    XMLHelper xmlHelper;
    
    void verify(ChangeSummary cs, DataObject quote, String nameSpace, String element, String beforeUndo, String afterUndo) throws IOException {
        // Stop logging changes and serialize the resulting data graph
        //
        cs.endLogging();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        xmlHelper.save(quote, nameSpace, element, baos);
        //xmlHelper.save(quote, nameSpace, element, System.out);

        byte[] bytes = baos.toByteArray();
        URL url = getClass().getResource(beforeUndo);
        assertTrue(TestUtil.equalXmlFiles(new ByteArrayInputStream(bytes), url));
        
        DataObject loaded = xmlHelper.load(new ByteArrayInputStream(bytes)).getRootObject();
        baos.reset();
        xmlHelper.save(loaded, nameSpace, element, baos);
        //xmlHelper.save(loaded, nameSpace, element, System.out);
        assertTrue(TestUtil.equalXmlFiles(new ByteArrayInputStream(baos.toByteArray()), url));
        
        // Undo all changes and then serialize the resulting data graph again
        //
        cs.undoChanges();
        
        baos.reset();
        xmlHelper.save(quote, nameSpace, element, baos);
        //xmlHelper.save(quote, nameSpace, element, System.out);
        
        assertTrue(TestUtil.equalXmlFiles(new ByteArrayInputStream(baos.toByteArray()), getClass().getResource(afterUndo)));
    }
    
    /**
     * ChangeSummary property test.
     */
    protected final void change(String nameSpace, String beforeUndo, String afterUndo) throws Exception {
        Type quoteType = hc.getTypeHelper().getType(nameSpace, "RootQuote");
        DataObject quote = dataFactory.create(quoteType);
        
        ChangeSummary cs = quote.getChangeSummary();
        ChangeSummary csp = (ChangeSummary)quote.get("changes");

        assertSame(cs, csp);

        quote.setString("symbol", "fbnt");
        quote.setString("companyName", "FlyByNightTechnology");
        quote.setBigDecimal("price", new BigDecimal("1000.0"));
        DataObject child = quote.createDataObject("quotes");
        child.setBigDecimal("price", new BigDecimal("1500.0"));
        child = quote.createDataObject("quotes");
        child.setBigDecimal("price", new BigDecimal("2000.0"));
        child = child.createDataObject("quotes");
        child.setBigDecimal("price", new BigDecimal("2000.99"));
        child = quote.createDataObject("quotes");
        child.setBigDecimal("price", new BigDecimal("2500.0"));

        // Begin logging changes
        //
        cs.beginLogging();

        // Modify the data graph in various ways
        //
        quote.setString("symbol", "FBNT");
        quote.setBigDecimal("price", new BigDecimal("999.0"));
        quote.setDouble("volume", 1000);

        child = quote.createDataObject("quotes");
        child.setBigDecimal("price", new BigDecimal("3000.0"));
        child = quote.createDataObject("quotes");
        child.setBigDecimal("price", new BigDecimal("4000.0"));

        quote.getDataObject("quotes[2]").delete();

        verify(cs, quote, nameSpace, "stockQuote", beforeUndo, afterUndo);
    }
    
    /**
     * Simple ChangeSummary property test.
     */
    public void testChangeSummary() throws Exception {
        change(TEST_NAMESPACE, TEST_DATA_BEFORE_UNDO, TEST_DATA_AFTER_UNDO);
    }
    
    static final String SequenceTest_NameSpace = "http://www.example.com/sequenceCS";

    /**
     * Mixed ChangeSummary property test.
     */
    public void testMixedChangeSummary() throws Exception {
        change(SequenceTest_NameSpace, "/mixedChangeSummary.xml", "/mixedChangeSummaryUndone.xml");
    }
    
    protected final DataObject createDataObject(Type quoteType, Object value, Sequence sequence, Property property) {
        DataObject child = dataFactory.create(quoteType);
        child.set("symbol", value);
        sequence.add(property, child);
        return child;
    }
    
    static final String SequenceTest_ELEMENT = "openQuote";

    private XSDHelper xsdHelper;

    /**
     * Open/any ChangeSummary property test.
     */
    public void testOpenChangeSummary() throws Exception {
        DataObject quote = dataFactory.create(xsdHelper.getGlobalProperty(SequenceTest_NameSpace, SequenceTest_ELEMENT, true).getType());
        
        ChangeSummary cs = quote.getChangeSummary();
        ChangeSummary csp = (ChangeSummary)quote.get("changes");

        assertSame(cs, csp);

        quote.set("symbol", "fbnt");
        Property property = xsdHelper.getGlobalProperty("http://www.example.com/open", "openStockQuote", true);
        Type quoteType = property.getType();
        List openStockQuotes = ((DataObject)quote).getList(property);
        
        addQuote(openStockQuotes, quoteType, "1500.0");
        DataObject osq2 = addQuote(openStockQuotes, quoteType, "2000.0");
        addQuote(osq2.getList(property), quoteType, "2000.99");
        addQuote(openStockQuotes, quoteType, "2500.0");

        // Begin logging changes
        //
        cs.beginLogging();

        // Modify the data graph in various ways
        //
        quote.set("symbol", "FBNT");

        addQuote(openStockQuotes, quoteType, "3000.0");
        addQuote(openStockQuotes, quoteType, "4000.0");

        openStockQuotes.remove(osq2);


        verify(cs, quote, SequenceTest_NameSpace, SequenceTest_ELEMENT, "/openChangeSummary.xml", "/openChangeSummaryUndone.xml");
    }

    private DataObject addQuote(List openStockQuotes, Type quoteType,
            Object value) {
        DataObject osq = dataFactory.create(quoteType);
        osq.set("symbol", value);
	    openStockQuotes.add(osq);
        return osq;
    }

    void define(String model) throws Exception {
        // Populate the meta data for the test model
        URL url = getClass().getResource(model);
        xsdHelper.define(url.openStream(), url.toString());
    }
    
    protected void setUp() throws Exception {
        super.setUp();
        hc = SDOUtil.createHelperContext();
        xsdHelper = hc.getXSDHelper();
        dataFactory = hc.getDataFactory();
        xmlHelper = hc.getXMLHelper();

        // Populate the meta data for the test (Stock Quote) model
        define(TEST_MODEL);

        define("/SequenceChangeSummary.xsd");
    }
}