1 7 8 package org.netbeans.modules.xml.xdm; 9 import java.beans.PropertyChangeListener ; 10 import java.util.List ; 11 import javax.swing.undo.UndoManager ; 12 import junit.framework.*; 13 import org.netbeans.modules.xml.xam.ModelSource; 14 import org.netbeans.modules.xml.xdm.nodes.*; 15 import org.netbeans.modules.xml.xdm.visitor.PathFromRootVisitor; 16 import org.netbeans.modules.xml.xdm.visitor.FlushVisitor; 17 import org.openide.util.Lookup; 18 import org.openide.util.lookup.Lookups; 19 import org.w3c.dom.NodeList ; 20 21 25 public class XDMModelTest extends TestCase { 26 27 public XDMModelTest(String testName) { 28 super(testName); 29 } 30 31 public void testAddNegative() throws Exception { 32 Node company = (Node)model.getDocument().getChildNodes().item(0); 34 Node employee = (Node)company.getChildNodes().item(1); 35 try { 36 model.add(company, employee, 0); 37 fail("adding a node already in the tree should throw exception"); 38 } catch (IllegalArgumentException iae) { 39 40 } 41 } 42 43 public void testModifyNegative() throws Exception { 44 Node company = (Node)model.getDocument().getChildNodes().item(0); 46 Node employee = (Node)company.getChildNodes().item(1); 47 try { 48 model.modify(employee, employee); 49 fail("modifying a node already in the tree should throw exception"); 50 } catch (IllegalArgumentException iae) { 51 52 } 53 54 try { 56 Element e = (Element)model.getDocument().createElement(""); 57 model.modify(employee, e); 58 fail("attempting to modify a node with a non equal node should throw exception"); 59 } catch (IllegalArgumentException iae) { 60 61 } 62 } 63 64 67 public void testAdd() { 68 Document original = model.getDocument(); 69 um.setLimit(10); 70 model.addUndoableEditListener(um); 71 FlushVisitor fv = new FlushVisitor(); 72 String originalText = fv.flushModel(original); 73 84 Node company = (Node)model.getDocument().getChildNodes().item(0); 86 Node employee = (Node)company.getChildNodes().item(1); 87 Element customer = (Element)model.getDocument().createElement("customer"); 88 89 TestListener tl = new TestListener(); 90 model.addPropertyChangeListener(tl); 91 model.add(employee,customer,0); 92 93 String modifiedText = fv.flushModel(model.getDocument()); 94 assertNotSame("text should have been modified to add new attribute", originalText,modifiedText); 95 assertTrue("only one event should be fired " + tl.getEventsFired(), tl.getEventsFired()==1); 96 assertEquals("event should be modified", model.PROP_ADDED, tl.getLastEventName()); 97 98 PathFromRootVisitor pfrv = new PathFromRootVisitor(); 99 assertNotNull("customer should now be in the tree", 100 pfrv.findPath(model.getDocument(), customer)); 101 102 104 company = (Node)model.getDocument().getChildNodes().item(0); 106 employee = (Node)company.getChildNodes().item(1); 107 Element customer2 = (Element) employee.getChildNodes().item(0); 108 109 assertEquals("expected name was not set", customer2.getLocalName(), customer.getLocalName()); 110 111 assertTrue("undo manager should have one event", um.canUndo()); 113 assertFalse("undo manager should not allow redo", um.canRedo()); 114 115 Document newD = model.getDocument(); 116 um.undo(); 117 assertSame("model not original tree", model.getDocument(), original); 118 um.redo(); 119 assertSame("model not new tree", model.getDocument(), newD); 120 121 company = (Node)model.getDocument().getChildNodes().item(0); 123 Element emp = (Element)model.getDocument().createElement("employee"); 124 125 tl.resetFiredEvents(); 126 model.add(company,emp,0); 127 128 assertTrue("only one event should be fired " + tl.getEventsFired(), tl.getEventsFired()==1); 129 assertEquals("event should be added", model.PROP_ADDED, tl.getLastEventName()); 130 131 pfrv = new PathFromRootVisitor(); 132 assertNotNull("new employee should now be in the tree", 133 pfrv.findPath(model.getDocument(), emp)); 134 } 135 136 public void testAddToSelfClosing() throws Exception { 137 sd = Util.getResourceAsDocument("selfClosing.xml"); 138 Lookup lookup = Lookups.singleton(sd); 139 ModelSource ms = new ModelSource(lookup, true); 140 model = new XDMModel(ms); 141 model.sync(); 142 143 Document original = model.getDocument(); 144 Node company = (Node)model.getDocument().getChildNodes().item(0); 146 Element emp = (Element)model.getDocument().createElement("employee"); 147 148 model.add(company,emp,0); 149 model.flush(); 150 model.sync(); 151 company = (Node)model.getDocument().getChildNodes().item(0); 152 emp = (Element) company.getFirstChild(); 153 assertEquals("employee is not local name", emp.getLocalName(), "employee"); 154 List <Token> tokens = emp.getTokens(); 155 int endTokenCount = 0; 156 for (Token t: tokens) { 157 if (t.getType().equals(TokenType.TOKEN_ELEMENT_END_TAG)) { 158 endTokenCount++; 159 } 160 } 161 assertEquals("employee should be created using self-closing tag", 1, endTokenCount); 162 } 163 164 167 public void testAppend() { 168 Document original = model.getDocument(); 169 um.setLimit(10); 170 model.addUndoableEditListener(um); 171 182 Node company = (Node)model.getDocument().getChildNodes().item(0); 184 Node employee = (Node)company.getChildNodes().item(1); 185 Element customer = (Element)model.getDocument().createElement("customer"); 186 187 TestListener tl = new TestListener(); 188 model.addPropertyChangeListener(tl); 189 model.append(employee,customer); 190 191 assertTrue("only one event should be fired " + tl.getEventsFired(), tl.getEventsFired()==1); 192 assertEquals("event should be modified", model.PROP_ADDED, tl.getLastEventName()); 193 194 PathFromRootVisitor pfrv = new PathFromRootVisitor(); 195 assertNotNull("customer should now be in the tree", 196 pfrv.findPath(model.getDocument(), customer)); 197 198 200 company = (Node)model.getDocument().getChildNodes().item(0); 202 employee = (Node)company.getChildNodes().item(1); 203 Element customer2 = (Element) employee.getChildNodes().item(employee.getChildNodes().getLength()-1); 204 205 assertEquals("expected name was not set", customer2.getLocalName(), customer.getLocalName()); 206 207 company = (Node)model.getDocument().getChildNodes().item(0); 209 Element emp2 = (Element)model.getDocument().createElement("employee"); 210 Attribute att = (Attribute)model.getDocument().createAttribute("id2"); 211 att.setValue("987"); 212 emp2.setAttributeNode(att); 213 214 tl.resetFiredEvents(); 215 model.append(company, emp2); 216 217 assertTrue("only one event should be fired " + tl.getEventsFired(), tl.getEventsFired()==1); 218 assertEquals("event should be added", model.PROP_ADDED, tl.getLastEventName()); 219 220 pfrv = new PathFromRootVisitor(); 221 assertNotNull("new employee should now be in the tree", 222 pfrv.findPath(model.getDocument(), emp2)); 223 } 224 225 228 public void testDelete() { 229 Document original = model.getDocument(); 230 um.setLimit(10); 231 model.addUndoableEditListener(um); 232 243 Node company = (Node)model.getDocument().getChildNodes().item(0); 245 Node employee = (Node)company.getChildNodes().item(1); 246 Text txt = (Text)employee.getChildNodes().item(0); 247 248 TestListener tl = new TestListener(); 249 model.addPropertyChangeListener(tl); 250 model.delete(txt); 251 252 assertTrue("only one event should be fired " + tl.getEventsFired(), tl.getEventsFired()==1); 253 assertEquals("event should be modified", model.PROP_DELETED, tl.getLastEventName()); 254 255 PathFromRootVisitor pfrv = new PathFromRootVisitor(); 256 assertNull("txt should no longer be in the tree", 257 pfrv.findPath(model.getDocument(), txt)); 258 259 261 assertTrue("undo manager should have one event", um.canUndo()); 263 assertFalse("undo manager should not allow redo", um.canRedo()); 264 265 Document newD = model.getDocument(); 266 um.undo(); 267 assertSame("model not original tree", model.getDocument(), original); 268 um.redo(); 269 assertSame("model not new tree", model.getDocument(), newD); 270 } 271 272 275 public void testModify() throws Exception { 276 Document original = model.getDocument(); 277 um.setLimit(10); 278 model.addUndoableEditListener(um); 279 290 Node company = (Node)model.getDocument().getChildNodes().item(0); 292 Element employee = (Element)company.getChildNodes().item(1); 293 Element employee2 = (Element)employee.cloneNode(true); 294 String attrName = "sss"; 295 String attrValue0 = employee.getAttribute(attrName); 296 String attrValue = "111-11-2222"; 297 employee2.setAttribute(attrName, attrValue); 298 TestListener tl = new TestListener(); 299 300 model.addPropertyChangeListener(tl); 301 model.modify(employee,employee2); 302 303 assertTrue("only one event should be fired", tl.getEventsFired()==1); 304 assertEquals("event should be modified", model.PROP_MODIFIED, tl.getLastEventName()); 305 306 PathFromRootVisitor pfrv = new PathFromRootVisitor(); 307 assertNotSame("original company should not be in tree", 308 company, pfrv.findPath(model.getDocument(), company).get(0)); 309 assertNull("original employee should not be tree", 310 pfrv.findPath(model.getDocument(), employee)); 311 312 company = (Node)model.getDocument().getChildNodes().item(0); 314 employee = (Element)company.getChildNodes().item(1); 315 assertEquals("expected name was not set", employee.getAttribute(attrName), attrValue); 316 317 assertTrue("undo manager should have one event", um.canUndo()); 319 assertFalse("undo manager should not allow redo", um.canRedo()); 320 321 Document newD = model.getDocument(); 322 um.undo(); 323 assertSame("model not original tree", model.getDocument(), original); 324 company = (Node)model.getDocument().getChildNodes().item(0); 325 employee = (Element)company.getChildNodes().item(1); 326 assertEquals("expected name was not set", attrValue0, employee.getAttribute(attrName)); 327 328 um.redo(); 329 assertSame("model not new tree", model.getDocument(), newD); 330 company = (Node)model.getDocument().getChildNodes().item(0); 331 employee = (Element)company.getChildNodes().item(1); 332 assertEquals("expected name was not set", attrValue, employee.getAttribute(attrName)); 333 } 334 335 public void testFlush() throws Exception { 336 Document original = model.getDocument(); 337 um.setLimit(10); 338 model.addUndoableEditListener(um); 339 String origContent = sd.getText(0,sd.getLength()); 340 model.flush(); 341 String flushContent = sd.getText(0,sd.getLength()); 342 assertEquals("expected same content after flush", origContent, flushContent); 343 344 Document oldDoc = model.getDocument(); 345 assertSame("Models before and after flush are same ", original, oldDoc); 346 347 model.sync(); 349 350 Document newDoc = model.getDocument(); 351 assertSame("Models before and after flush/sync are same ", oldDoc, newDoc); 352 } 354 355 public void testSyncAndNamespace() throws Exception { 356 javax.swing.text.Document swdoc = Util.getResourceAsDocument("TestSyncNamespace.wsdl"); 357 XDMModel m = Util.loadXDMModel(swdoc); 358 Element root = (Element) m.getCurrentDocument().getDocumentElement(); 359 NodeList nl = root.getChildNodes(); 360 Element messageE = null; 361 for (int i=0; i < nl.getLength(); i++) { 362 if (nl.item(i) instanceof Element) { 363 Element e = (Element) nl.item(i); 364 if (e.getLocalName().equals("message")) { 365 messageE = e; 366 } 367 } 368 } 369 assertNotNull(messageE); 370 assertEquals("http://schemas.xmlsoap.org/wsdl/" , messageE.getNamespaceURI()); 371 372 Util.setDocumentContentTo(swdoc, "TestSyncNamespace_1.wsdl"); 373 m.sync(); 374 375 nl = messageE.getChildNodes(); 376 Element partE = null; 377 for (int i=0; i < nl.getLength(); i++) { 378 if (nl.item(i) instanceof Element) { 379 Element e = (Element) nl.item(i); 380 if (e.getLocalName().equals("part")) { 381 partE = e; 382 } 383 } 384 } 385 assertNotNull(partE); 386 assertEquals("http://schemas.xmlsoap.org/wsdl/" , messageE.getNamespaceURI()); 387 assertEquals("http://schemas.xmlsoap.org/wsdl/" , partE.getNamespaceURI()); 388 } 389 390 public void testSyncToEmptyRoot() throws Exception { 391 javax.swing.text.Document swdoc = Util.getResourceAsDocument("resources/test1.xml"); 392 XDMModel m = Util.loadXDMModel(swdoc); 393 Document old = m.getCurrentDocument(); 394 Element root = (Element) m.getCurrentDocument().getDocumentElement(); 395 assertEquals(7, root.getChildNodes().getLength()); 396 Util.setDocumentContentTo(swdoc, "resources/Empty.xml"); 397 m.prepareSync(); 398 m.sync(); 399 assertNotSame(m.getCurrentDocument(), old); 400 root = (Element) m.getCurrentDocument().getDocumentElement(); 401 assertEquals(1, root.getChildNodes().getLength()); 402 } 403 404 static class TestListener implements PropertyChangeListener { 405 private String eventName; 406 private int count = 0; 407 408 public void propertyChange(java.beans.PropertyChangeEvent evt) { 409 eventName = evt.getPropertyName(); 410 count++; 411 } 412 413 public int getEventsFired() { 414 return count; 415 } 416 417 public String getLastEventName() { 418 return eventName; 419 } 420 421 public void resetFiredEvents() { 422 count = 0; 423 } 424 } 425 426 protected void setUp() throws Exception { 427 um = new UndoManager (); 428 sd = Util.getResourceAsDocument("test.xml"); 429 Lookup lookup = Lookups.singleton(sd); 430 ModelSource ms = new ModelSource(lookup, true); 431 model = new XDMModel(ms); 432 model.sync(); 433 } 434 435 private javax.swing.text.Document sd; 436 private XDMModel model; 437 private UndoManager um; 438 } 439 | Popular Tags |