KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > model > impl > xdm > ReorderTest


1 package org.netbeans.modules.xml.schema.model.impl.xdm;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5 import javax.swing.undo.UndoManager JavaDoc;
6 import junit.framework.*;
7 import org.netbeans.modules.xml.schema.model.LocalElement;
8 import org.netbeans.modules.xml.schema.model.SchemaComponent;
9 import org.netbeans.modules.xml.schema.model.SchemaModel;
10 import org.netbeans.modules.xml.schema.model.Util;
11 import org.netbeans.modules.xml.schema.model.impl.GlobalElementImpl;
12 import org.netbeans.modules.xml.schema.model.impl.LocalComplexTypeImpl;
13 import org.netbeans.modules.xml.schema.model.impl.LocalElementImpl;
14 import org.netbeans.modules.xml.schema.model.impl.SchemaImpl;
15 import org.netbeans.modules.xml.schema.model.impl.SequenceImpl;
16 import org.netbeans.modules.xml.xam.Component;
17 import org.netbeans.modules.xml.xam.Named;
18 import org.netbeans.modules.xml.xdm.nodes.Element;
19 import org.netbeans.modules.xml.xdm.nodes.Text;
20 import org.w3c.dom.Node JavaDoc;
21 import org.w3c.dom.NodeList JavaDoc;
22
23 /**
24  *
25  * @author Ayub Khan
26  */

27 public class ReorderTest extends TestCase {
28     
29     public ReorderTest(String JavaDoc testName) {
30         super(testName);
31     }
32     
33     protected void setUp() throws Exception JavaDoc {
34     }
35
36     /**
37      * Test of cut/paste operation
38      */

39     public void testReorderLocalElementUsingSchemaModel() throws Exception JavaDoc {
40         SchemaModel model = Util.loadSchemaModel("resources/ReorderTest.xsd");
41         SchemaImpl schema = (SchemaImpl) model.getSchema();
42         GlobalElementImpl gei = (GlobalElementImpl) schema.getElements().iterator().next();
43         LocalComplexTypeImpl lcti = (LocalComplexTypeImpl) gei.getInlineType();
44         SequenceImpl seq = (SequenceImpl) lcti.getDefinition();
45         
46         /* before
47   <xsd:element name="OrgChart">
48     <xsd:complexType>
49       <xsd:sequence>
50         <xsd:element name="CompanyLogo1"/>
51         <xsd:element name="Office1" type="OfficeType" maxOccurs="unbounded"/>
52         <xsd:element name="CompanyLogo2"/>
53         <xsd:element name="Office2" type="OfficeType" maxOccurs="unbounded"/>
54         <xsd:element name="Office3" type="OfficeType" maxOccurs="unbounded"/>
55       </xsd:sequence>
56     </xsd:complexType>
57   </xsd:element>
58          **/

59         java.util.List JavaDoc<LocalElement> letis = new ArrayList JavaDoc<LocalElement>();
60         for(SchemaComponent sc:seq.getContent())
61             letis.add((LocalElement)sc);
62         
63         assertEquals("testReorderLocalElement.schema.node", 5, seq.getChildren().size());
64         assertEquals("testReorderLocalElement.schema.node", 11, seq.getPeer().getChildNodes().getLength());
65         checkChildren(seq.getChildren(), "CompanyLogo1, Office1, CompanyLogo2, Office2, Office3");
66         
67         model.startTransaction();
68         java.util.List JavaDoc<LocalElement> clonedLetis = new ArrayList JavaDoc<LocalElement>();
69         for(LocalElement leti:letis) {
70             clonedLetis.add((LocalElementImpl) leti.copy(seq));
71             seq.removeContent(leti);
72         }
73         model.endTransaction();
74
75         assertEquals("testReorderLocalElement.schema.node", 0, seq.getChildren().size());
76         assertEquals("testReorderLocalElement.schema.node", 0, seq.getPeer().getChildNodes().getLength());
77         checkChildren(seq.getChildren(), "");
78         
79         model.startTransaction();
80         int index = 0;
81         seq.addContent(clonedLetis.get(0), index++);
82         seq.addContent(clonedLetis.get(2), index++);
83         seq.addContent(clonedLetis.get(3), index++);
84         seq.addContent(clonedLetis.get(1), index++);
85         seq.addContent(clonedLetis.get(4), index++);
86         model.endTransaction();
87         
88         /* after
89   <xsd:element name="OrgChart">
90     <xsd:complexType>
91       <xsd:sequence>
92         <xsd:element name="CompanyLogo1"/>
93         <xsd:element name="CompanyLogo2"/>
94         <xsd:element name="Office2" type="OfficeType" maxOccurs="unbounded"/>
95         <xsd:element name="Office1" type="OfficeType" maxOccurs="unbounded"/>
96         <xsd:element name="Office3" type="OfficeType" maxOccurs="unbounded"/>
97       </xsd:sequence>
98     </xsd:complexType>
99   </xsd:element>
100          **/

101         
102         assertEquals("testReorderLocalElement.schema.node", 5, seq.getChildren().size());
103         assertEquals("testReorderLocalElement.schema.node", 11, seq.getPeer().getChildNodes().getLength());
104         checkChildren(seq.getChildren(), "CompanyLogo1, CompanyLogo2, Office2, Office1, Office3");
105     }
106     
107     /**
108      * Test of cut/paste operation
109      */

110     public void testReorderLocalElementUsingXAM() throws Exception JavaDoc {
111         SchemaModel model = Util.loadSchemaModel("resources/ReorderTest.xsd");
112         UndoManager JavaDoc um = new UndoManager JavaDoc();
113         model.addUndoableEditListener(um);
114         SchemaImpl schema = (SchemaImpl) model.getSchema();
115         GlobalElementImpl gei = (GlobalElementImpl) schema.getElements().iterator().next();
116         LocalComplexTypeImpl lcti = (LocalComplexTypeImpl) gei.getInlineType();
117         SequenceImpl seq = (SequenceImpl) lcti.getDefinition();
118         
119         /* before
120   <xsd:element name="OrgChart">
121     <xsd:complexType>
122       <xsd:sequence>
123         <xsd:element name="CompanyLogo1"/>
124         <xsd:element name="Office1" type="OfficeType" maxOccurs="unbounded"/>
125         <xsd:element name="CompanyLogo2"/>
126         <xsd:element name="Office2" type="OfficeType" maxOccurs="unbounded"/>
127         <xsd:element name="Office3" type="OfficeType" maxOccurs="unbounded"/>
128       </xsd:sequence>
129   </xsd:element>
130          **/

131         Component parent = seq;
132         int[] newPositions = {0, 3, 1, 2, 4}; //CompanyLogo1, CompanyLogo2, Office2, Office1, Office3
133
java.util.List JavaDoc<Component> children = parent.getChildren();
134                 checkChildren(children, "CompanyLogo1, Office1, CompanyLogo2, Office2, Office3");
135         // parent is the Component whose children are being reordered
136
model.startTransaction();
137                 List JavaDoc<Component> copies = new ArrayList JavaDoc<Component>(children);
138                 for (int currentPos = 0; currentPos < newPositions.length; currentPos++) {
139                     copies.set(newPositions[currentPos], children.get(currentPos).copy(parent));
140                 }
141         checkChildren(copies, "CompanyLogo1, CompanyLogo2, Office2, Office1, Office3");
142             // Cannot remove children until after they are copied.
143
for (Component child : children.toArray(new Component[children.size()])) {
144                 model.removeChildComponent(child);
145             }
146             assertEquals("number of childs",0, parent.getChildren().size());
147             // Now add the copies back to the parent.
148
for (Component copy : copies) {
149                 model.addChildComponent(parent, copy, -1);
150             }
151                         model.endTransaction();
152         /* after
153   <xsd:element name="OrgChart">
154     <xsd:complexType>
155       <xsd:sequence>
156         <xsd:element name="CompanyLogo1"/>
157         <xsd:element name="CompanyLogo2"/>
158         <xsd:element name="Office2" type="OfficeType" maxOccurs="unbounded"/>
159         <xsd:element name="Office1" type="OfficeType" maxOccurs="unbounded"/>
160         <xsd:element name="Office3" type="OfficeType" maxOccurs="unbounded"/>
161       </xsd:sequence>
162     </xsd:complexType>
163   </xsd:element>
164          **/

165         checkChildren(parent.getChildren(), "CompanyLogo1, CompanyLogo2, Office2, Office1, Office3");
166         
167         um.undo();
168         checkChildren(parent.getChildren(), "CompanyLogo1, Office1, CompanyLogo2, Office2, Office3");
169     }
170     
171     private <T extends Component> void checkChildren(final List JavaDoc<T> children, final String JavaDoc order) {
172         int index = 0;
173                 String JavaDoc result = "";
174         for (int i=0; i<children.size(); i++) {
175                     T sc = children.get(i);
176                     result += ((Named)sc).getName();
177                     if (i != children.size()-1) {
178                         result += ", ";
179                     }
180         }
181                 if (order != null) {
182                     assertEquals("check sequence", order, result);
183                 } else {
184                     System.out.println(result);
185                 }
186     }
187
188         private void checkChildrenNodes(final Component c) {
189         NodeList JavaDoc childs = ((SchemaComponent)c).getPeer().getChildNodes();
190         System.out.println("\nxdm nodes: ");
191         for(int i=0;i<childs.getLength();i++) {
192             Node JavaDoc child = childs.item(i);
193             if(child instanceof Element)
194                 System.out.print(((Element)child).getAttribute("name")+", ");
195             else if(child instanceof Text)
196                 System.out.print("["+((Text)child).getTokens().get(0).getValue().replaceAll("\n", "~")+"]");
197         }
198         System.out.println();
199         }
200 }
201
Popular Tags