KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > axi > sync > MultiFileSyncTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.axi.sync;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.util.Timer JavaDoc;
25 import java.util.TimerTask JavaDoc;
26 import junit.framework.*;
27 import org.netbeans.modules.xml.axi.AXIComponent;
28 import org.netbeans.modules.xml.axi.AXIComponent.ComponentType;
29 import org.netbeans.modules.xml.axi.AXIDocument;
30 import org.netbeans.modules.xml.axi.AXIModel;
31 import org.netbeans.modules.xml.axi.Compositor;
32 import org.netbeans.modules.xml.axi.ContentModel;
33 import org.netbeans.modules.xml.axi.Element;
34 import org.netbeans.modules.xml.axi.impl.AXIModelImpl;
35 import org.netbeans.modules.xml.axi.impl.ElementProxy;
36 import org.netbeans.modules.xml.axi.visitor.DeepAXITreeVisitor;
37 import org.netbeans.modules.xml.schema.model.GlobalGroup;
38 import org.netbeans.modules.xml.schema.model.LocalElement;
39
40         
41 /**
42  * This unit test verifies sync involving multiple files.
43  *
44  * @author Samaresh (Samaresh.Panda@Sun.Com)
45  */

46 public class MultiFileSyncTest extends AbstractSyncTestCase {
47
48     public static final String JavaDoc TEST_XSD = "resources/employee.xsd";
49     public static final String JavaDoc IMPORTED_XSD = "resources/address1.xsd";
50     public static final String JavaDoc GLOBAL_ELEMENT = "employee";
51     private ContentModel usAddress;
52     private PropertyListener pcl;
53     private boolean sync1 = false;
54     private boolean sync2 = false;
55     private boolean fromAXIModel = false;
56     
57     public MultiFileSyncTest(String JavaDoc testName) {
58         super(testName, TEST_XSD, GLOBAL_ELEMENT);
59     }
60             
61     public static Test suite() {
62         TestSuite suite = new TestSuite(MultiFileSyncTest.class);
63         return suite;
64     }
65
66     public void testUpdateImportedSchema() throws Exception JavaDoc {
67         //open the imported schema
68
AXIModel iModel = getModel(IMPORTED_XSD);
69         usAddress = findContentModel(iModel, "group");
70         assert(usAddress != null);
71         
72         //deep visit the original schema
73
AXIModelImpl model = (AXIModelImpl)getAXIModel();
74         AXIDocument document = model.getRoot();
75         DeepVisitor visitor = new DeepVisitor();
76         visitor.traverse(document);
77         
78         updateAddressInAXIModel();
79         Thread.sleep(10000);
80         assert(sync1);
81         
82         //update the imported schema.
83
updateAddressInSchemaModel();
84         Thread.sleep(10000);
85         assert(sync2);
86     }
87     
88     private void updateAddressInAXIModel() throws Exception JavaDoc {
89     fromAXIModel = true;
90         usAddress.getModel().startTransaction();
91     Element name = (Element)usAddress.getChildren().get(0).getChildren().get(0);
92         assert(name.getName().equals("name"));
93         name.setName("nameUpdatedFromDV");
94     usAddress.getModel().endTransaction();
95     }
96     
97     private void updateAddressInSchemaModel() throws Exception JavaDoc {
98     fromAXIModel = false;
99         GlobalGroup type = (GlobalGroup)usAddress.getPeer();
100     type.getModel().startTransaction();
101     LocalElement name = (LocalElement)type.getChildren().get(0).getChildren().get(0);
102         assert(name.getName().equals("nameUpdatedFromDV"));
103         name.setName("nameUpdatedFromSV");
104     type.getModel().endTransaction();
105     }
106     
107     private ContentModel findContentModel(AXIModel model, String JavaDoc name) {
108         for(ContentModel cm : model.getRoot().getContentModels()) {
109             if(cm.getName().equals(name)) {
110                 return cm;
111             }
112         }
113         
114         return null;
115     }
116         
117     private class DeepVisitor extends DeepAXITreeVisitor {
118         AXIModel model;
119         public void traverse(AXIDocument document) {
120             model = document.getModel();
121             document.accept(this);
122         }
123         
124         public void visit(Compositor compositor) {
125             if(compositor.getComponentType() != ComponentType.PROXY) {
126                 visitChildren(compositor);
127                 return;
128             }
129             
130             Element e = (Element)usAddress.getChildren().get(0).getChildren().get(0);
131             ElementProxy eP = (ElementProxy)compositor.getChildren().get(0);
132             assert(eP.getModel() == model);
133             assert(eP.getOriginal().getModel() != model);
134             assert(eP.getOriginal().getModel() == usAddress.getModel());
135             assert(eP.getName().equals("name"));
136             pcl = new PropertyListener(eP);
137             eP.addPropertyChangeListener(pcl);
138             visitChildren(compositor);
139         }
140     }
141     
142     private class PropertyListener implements PropertyChangeListener JavaDoc {
143         private AXIComponent source;
144         
145         public PropertyListener (AXIComponent source) {
146             this.source = source;
147         }
148         
149     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
150             if(evt.getSource() == source) {
151                 String JavaDoc name = (String JavaDoc)evt.getNewValue();
152                 if(fromAXIModel) {
153                     assert(name.equals("nameUpdatedFromDV"));
154                     sync1 = true;
155                 } else {
156                     assert(name.equals("nameUpdatedFromSV"));
157                     sync2 = true;
158                 }
159             }
160     }
161     }
162 }
163
Popular Tags