1 package org.netbeans.modules.xml.schema.model.impl.xdm; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 5 import javax.swing.undo.UndoManager ; 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 ; 21 import org.w3c.dom.NodeList ; 22 23 27 public class ReorderTest extends TestCase { 28 29 public ReorderTest(String testName) { 30 super(testName); 31 } 32 33 protected void setUp() throws Exception { 34 } 35 36 39 public void testReorderLocalElementUsingSchemaModel() throws Exception { 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 59 java.util.List <LocalElement> letis = new ArrayList <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 <LocalElement> clonedLetis = new ArrayList <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 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 110 public void testReorderLocalElementUsingXAM() throws Exception { 111 SchemaModel model = Util.loadSchemaModel("resources/ReorderTest.xsd"); 112 UndoManager um = new UndoManager (); 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 131 Component parent = seq; 132 int[] newPositions = {0, 3, 1, 2, 4}; java.util.List <Component> children = parent.getChildren(); 134 checkChildren(children, "CompanyLogo1, Office1, CompanyLogo2, Office2, Office3"); 135 model.startTransaction(); 137 List <Component> copies = new ArrayList <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 for (Component child : children.toArray(new Component[children.size()])) { 144 model.removeChildComponent(child); 145 } 146 assertEquals("number of childs",0, parent.getChildren().size()); 147 for (Component copy : copies) { 149 model.addChildComponent(parent, copy, -1); 150 } 151 model.endTransaction(); 152 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 <T> children, final String order) { 172 int index = 0; 173 String 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 childs = ((SchemaComponent)c).getPeer().getChildNodes(); 190 System.out.println("\nxdm nodes: "); 191 for(int i=0;i<childs.getLength();i++) { 192 Node 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 |