summaryrefslogtreecommitdiffstats
path: root/sdo-java/branches/sdo-1.1.1-incubating/impl/src/test/java/org/apache/tuscany/sdo/test/ChangeSummaryOnDataObjectTestCase.java
blob: b9b9c0f2ea54a12cf6faa0fca2f19cfcd0e758aa (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
/**
 *
 *  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.InputStream;
import java.math.BigDecimal;
import java.net.URL;
import java.util.List;

import junit.framework.TestCase;

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

import commonj.sdo.ChangeSummary;
import commonj.sdo.DataGraph;
import commonj.sdo.DataObject;
import commonj.sdo.Property;
import commonj.sdo.Type;
import commonj.sdo.helper.HelperContext;
import commonj.sdo.helper.TypeHelper;
import commonj.sdo.helper.XSDHelper;


public class ChangeSummaryOnDataObjectTestCase extends TestCase {

  
  private final String TEST_DATA = "/simplechangesummary.xml";
  HelperContext hc;
  XSDHelper xh;
  TypeHelper th;

  public void testBasicsDO() {
    Type cst = th.getType("commonj.sdo","ChangeSummaryType");
    Type strt = th.getType("commonj.sdo", "String");
    
    Type newt = SDOUtil.createType(hc, "testcases.changesummary", "simpleCS", false);
    Property strProp = SDOUtil.createProperty(newt, "strElem", strt);
    SDOUtil.createProperty(newt, "changeSummary", cst);
    
    DataObject iNewt = hc.getDataFactory().create(newt);
        
    testBasicsBody(strProp, iNewt);
}

  public void testBasicsDG() {

    Type strt = th.getType("commonj.sdo", "String");
    

    Type newt = SDOUtil.createType(hc, "testcases.changesummary", "simpleNOCS", false);
    Property strProp = SDOUtil.createProperty(newt, "strElem", strt);

    DataGraph graph = SDOUtil.createDataGraph();
    DataObject iNewt = graph.createRootObject(newt);
    
    testBasicsBody(strProp, iNewt);
}
  
  
  /**
   * @param strProp
   * @param iNewt
   */
  private void testBasicsBody(Property strProp, DataObject iNewt) {
    ChangeSummary cs = iNewt.getChangeSummary();
    cs.beginLogging();

    List co = cs.getChangedDataObjects();
    assertEquals(0, co.size());
    iNewt.set(strProp, "some text");
    assertEquals(0, co.size());
    co = cs.getChangedDataObjects();
    assertEquals(1, co.size());
    
    List oldValues = cs.getOldValues((DataObject)co.get(0));
    
    ChangeSummary.Setting ov1 = (ChangeSummary.Setting)oldValues.get(0);
    Property p = ov1.getProperty();
    assertEquals("strElem", p.getName());
    Object v = ov1.getValue();
    assertEquals(null, v);

    assertTrue(cs.isLogging());
    cs.endLogging();
    assertFalse(cs.isLogging());
    
  }



  public void testDynamicNestedDataObjectsDG() throws Exception {
    DataGraph dataGraph = SDOUtil.createDataGraph();
    DataObject quote = dataGraph.createRootObject(th.getType("http://www.example.com/simple", "Quote"));

    testDynamicNestedDOBody(quote);
    
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    SDOUtil.saveDataGraph(dataGraph, baos, null);
    // SDOUtil.saveDataGraph(dataGraph, System.out, null);
    
   assertTrue(TestUtil.equalXmlFiles(new ByteArrayInputStream(baos.toByteArray()), getClass().getResource(TEST_DATA)));

   
    assertEquals(1, quote.getList("quotes").size());
    assertEquals("fbnt", quote.getString("symbol"));
    dataGraph.getChangeSummary().undoChanges();
    // SDOUtil.saveDataGraph(dataGraph, System.out, null);
    assertEquals(0, quote.getList("quotes").size());
    assertNull(quote.getString("symbol"));
    

  }
  
  public void testDynamicNestedDataObjectsDO() throws Exception {
    Type quoteType = th.getType("http://www.example.com/simpleCS", "RootQuote");
    DataObject quote = hc.getDataFactory().create(quoteType);

    testDynamicNestedDOBody(quote);   
    //hc.getXMLHelper().save(quote, "http://www.example.com/simpleCS", "stockQuote", System.out);
    assertEquals(1, quote.getList("quotes").size());
    assertEquals("fbnt", quote.getString("symbol"));

    quote.getChangeSummary().undoChanges();
    
    assertEquals(0, quote.getList("quotes").size());
    assertNull(quote.getString("symbol"));

    

  }
  
  /**
   * @param quote
   */
  private void testDynamicNestedDOBody(DataObject quote) {
    // Begin logging changes
    //
    ChangeSummary changeSummary = quote.getChangeSummary();
    assertNotNull(changeSummary);
    assertFalse(changeSummary.isLogging());
    changeSummary.beginLogging();
    

    // Modify the data graph in various fun and interesting ways
    //
    quote.setString("symbol", "fbnt");
    quote.setString("companyName", "FlyByNightTechnology");
    quote.setBigDecimal("price", new BigDecimal("1000.0"));
    quote.setBigDecimal("open1", new BigDecimal("1000.0"));
    quote.setBigDecimal("high", new BigDecimal("1000.0"));
    quote.setBigDecimal("low", new BigDecimal("1000.0"));
    quote.setDouble("volume", 1000);
    quote.setDouble("change1", 1000);

    DataObject child = quote.createDataObject("quotes");
    child.setBigDecimal("price", new BigDecimal("2000.0"));

    changeSummary.endLogging();
    assertEquals(2, changeSummary.getChangedDataObjects().size());  // 2 DataObjects
    assertTrue(changeSummary.getChangedDataObjects().contains(quote));
    assertTrue(changeSummary.getChangedDataObjects().contains(child));
    assertFalse(changeSummary.isCreated(quote));
    assertTrue(changeSummary.isCreated(child));
    
    ChangeSummary.Setting ov = changeSummary.getOldValue(quote, quote.getType().getProperty("symbol"));
    assertNull(ov.getValue());
    

  }

  protected void setUp() throws Exception {
    super.setUp();

//    uncomment these lines for sending aspect trace to a file
//    tracing.lib.TraceMyClasses tmc = (TraceMyClasses)Aspects.aspectOf(TraceMyClasses.class);
//    tmc.initStream(new PrintStream("c:\\temp\\trace.log"));

    // Populate the meta data for the test (Stock Quote) model
    URL url = getClass().getResource("/simple.xsd");
    InputStream inputStream = url.openStream();
    hc = SDOUtil.createHelperContext();
    th = hc.getTypeHelper();
    xh = hc.getXSDHelper();
    xh.define(inputStream, url.toString());
    inputStream.close();

    URL url2 = getClass().getResource("/simpleWithChangeSummary.xsd");
    InputStream inputStream2 = url2.openStream();
    xh.define(inputStream2, url2.toString());
    inputStream.close();
    
    
  
}

}