KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.swing.text.Document JavaDoc;
25 import junit.framework.*;
26 import org.netbeans.modules.xml.axi.AXIDocument;
27 import org.netbeans.modules.xml.axi.AXIModel;
28 import org.netbeans.modules.xml.schema.model.SchemaModel;
29 import org.netbeans.modules.xml.xam.Model.State;
30 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
31 import org.openide.util.WeakListeners;
32
33         
34 /**
35  * This unit test verifies various state change of the AXIModel.
36  * It starts with a broken schema. Fixes it to change the state
37  * from NOT_WELL_FORMED to VALID. Then it again makes the schema
38  * invalid and the state changes from VALID to NOT_WELL_FORMED.
39  *
40  * @author Samaresh (Samaresh.Panda@Sun.Com)
41  */

42 public class StateChangeTest extends AbstractSyncTestCase {
43                 
44     public static final String JavaDoc TEST_XSD = "resources/brokenSchema.xsd";
45     private Document JavaDoc document;
46     private AXIModel.State expectedState = null;
47     private boolean invalidToValid = false;
48     private boolean validToInvalid = false;
49     
50     public StateChangeTest(String JavaDoc testName) {
51         super(testName, TEST_XSD, null);
52     }
53     
54     /**
55      * Overwrites setUp.
56      */

57     protected void setUp() throws Exception JavaDoc {
58         super.setUp();
59         SchemaModel sm = getSchemaModel();
60         document = ((AbstractDocumentModel)sm).getBaseDocument();
61         AXIModel axiModel = getAXIModel();
62         if (axiModel != null) {
63             ModelStateChangeListener listener = new ModelStateChangeListener(axiModel);
64             axiModel.addPropertyChangeListener(listener);
65         }
66     }
67         
68     public static Test suite() {
69         TestSuite suite = new TestSuite(StateChangeTest.class);
70         return suite;
71     }
72
73     public void testStateChange() throws InterruptedException JavaDoc {
74         AXIModel model = getAXIModel();
75         //make the schema valid from invalid
76
assert(model.getState() == AXIModel.State.NOT_WELL_FORMED);
77         String JavaDoc replaceFrom = "<xsd:element name=\"address\" type=\"addr:USAddr";
78         String JavaDoc replaceTo = "<xsd:element name=\"address\" type=\"addr:USAddress\"/>";
79         replaceInDocument(replaceFrom, replaceTo);
80         expectedState = AXIModel.State.VALID;
81         Thread.sleep(10000);
82         
83         //make the schema invalid from valid
84
replaceInDocument(replaceTo, replaceFrom);
85         expectedState = AXIModel.State.NOT_WELL_FORMED;
86         Thread.sleep(10000);
87         
88         //finally make sure both the tests have been carried out
89
assert(invalidToValid);
90         assert(validToInvalid);
91     }
92     
93     private void replaceInDocument(String JavaDoc replaceFrom, String JavaDoc replaceTo) {
94         javax.swing.text.AbstractDocument JavaDoc doc = (javax.swing.text.AbstractDocument JavaDoc)document;
95         int len = replaceFrom.length();
96         try {
97             String JavaDoc content = doc.getText(0,doc.getLength());
98             int index = content.lastIndexOf(replaceFrom);
99             while (index>=0) {
100                 doc.replace(index,len,replaceTo,null);
101                 content=content.substring(0,index);
102                 index = content.lastIndexOf(replaceFrom);
103             }
104         } catch (javax.swing.text.BadLocationException JavaDoc ex) {
105             ex.printStackTrace();
106         }
107     }
108     
109     /**
110      * Model state change listener.
111      */

112     private class ModelStateChangeListener implements PropertyChangeListener JavaDoc {
113         private AXIModel model;
114         ModelStateChangeListener(AXIModel model) {
115             this.model = model;
116         }
117         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
118             String JavaDoc property = evt.getPropertyName();
119             if(!AXIModel.STATE_PROPERTY.equals(property)) {
120                 return;
121             }
122             
123             State newState = (State)evt.getNewValue();
124             State oldState = (State)evt.getOldValue();
125             Object JavaDoc source = evt.getSource();
126             assert(source == model);
127             if(newState == AXIModel.State.VALID) {
128                 assert(oldState == AXIModel.State.NOT_WELL_FORMED);
129                 assert(expectedState == newState);
130                 AXIDocument doc = model.getRoot();
131                 assert(doc.getChildren().size() == 7);
132                 assert(doc.getContentModels().size() == 3);
133                 assert(doc.getChildElements().size() == 3);
134                 invalidToValid = true;
135                 return;
136             }
137             
138             if(newState != AXIModel.State.VALID) {
139                 assert(oldState == AXIModel.State.VALID);
140                 assert(expectedState == newState);
141                 validToInvalid = true;
142                 return;
143             }
144         }
145     }
146 }
147
Popular Tags