KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xdm > XDMModelTest


1 /*
2  * XMLModelTest.java
3  * JUnit based test
4  *
5  * Created on August 5, 2005, 12:13 PM
6  */

7
8 package org.netbeans.modules.xml.xdm;
9 import java.beans.PropertyChangeListener JavaDoc;
10 import java.util.List JavaDoc;
11 import javax.swing.undo.UndoManager JavaDoc;
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 JavaDoc;
20
21 /**
22  *
23  * @author Administrator
24  */

25 public class XDMModelTest extends TestCase {
26     
27     public XDMModelTest(String JavaDoc testName) {
28         super(testName);
29     }
30     
31     public void testAddNegative() throws Exception JavaDoc {
32         // verify that a node which is already in the tree cannot be added
33
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 JavaDoc iae) {
39             
40         }
41     }
42     
43     public void testModifyNegative() throws Exception JavaDoc {
44         // verify that a node which is already in the tree cannot be added
45
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 JavaDoc iae) {
51             
52         }
53         
54         // now try to substitute a different node
55
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 JavaDoc iae) {
60             
61         }
62     }
63     
64     /**
65      * Test of add method, of class xml.nodes.XMLModel.
66      */

67     public void testAdd() {
68         Document original = model.getDocument();
69         um.setLimit(10);
70         model.addUndoableEditListener(um);
71         FlushVisitor fv = new FlushVisitor();
72         String JavaDoc originalText = fv.flushModel(original);
73 // Expected model
74
// Document
75
// Element -- company
76
// Element -- employee
77
// Attribute -- ssn xx-xx-xxxx
78
// Attribute -- id
79
// Attribute -- phone
80
// Text -- Vidhya
81
// End Element
82
// End Element
83

84         // first add another child element
85
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 JavaDoc 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         // TODO verify some of the nodes which should not have been cloned
103

104         // now verify that the new child element is added in right location
105
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         // verify undo / redo
112
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         //Adding a brand new element for testing
122
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 JavaDoc {
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         //Adding a brand new element for testing
145
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 JavaDoc<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     /**
165      * Test of append method, of class xml.nodes.XMLModel.
166      */

167     public void testAppend() {
168         Document original = model.getDocument();
169         um.setLimit(10);
170         model.addUndoableEditListener(um);
171 // Expected model
172
// Document
173
// Element -- company
174
// Element -- employee
175
// Attribute -- ssn xx-xx-xxxx
176
// Attribute -- id
177
// Attribute -- phone
178
// Text -- Vidhya
179
// End Element
180
// End Element
181

182         // first append another child element
183
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         // TODO verify some of the nodes which should not have been cloned
199

200         // now verify that the new child element is added in right location (at the end)
201
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         //Appending a brand new element with attributes
208
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     /**
226      * Test of delete method, of class xml.nodes.XMLTreeGenerator.
227      */

228     public void testDelete() {
229         Document original = model.getDocument();
230         um.setLimit(10);
231         model.addUndoableEditListener(um);
232         // Expected model
233
// Document
234
// Element -- company
235
// Element -- employee
236
// Attribute -- ssn xx-xx-xxxx
237
// Attribute -- id
238
// Attribute -- phone
239
// Text -- Vidhya
240
// End Element
241
// End Element
242

243         // first get text element
244
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         // TODO verify some of the nodes which should not have been cloned
260

261         // verify undo / redo
262
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     /**
273      * Test of modify method, of class xml.nodes.XMLTreeGenerator.
274      */

275     public void testModify() throws Exception JavaDoc {
276         Document original = model.getDocument();
277         um.setLimit(10);
278         model.addUndoableEditListener(um);
279         // Expected model
280
// Document
281
// Element -- company
282
// Element -- employee
283
// Attribute -- ssn xx-xx-xxxx
284
// Attribute -- id
285
// Attribute -- phone
286
// Text -- Vidhya
287
// End Element
288
// End Element
289

290         // first get employee element
291
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 JavaDoc attrName = "sss";
295         String JavaDoc attrValue0 = employee.getAttribute(attrName);
296         String JavaDoc 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         // now verify that the new employee is what we set
313
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         // verify undo / redo
318
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 JavaDoc {
336         Document original = model.getDocument();
337         um.setLimit(10);
338         model.addUndoableEditListener(um);
339         String JavaDoc origContent = sd.getText(0,sd.getLength());
340         model.flush();
341         String JavaDoc 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         //Force sync to make sure the new model is the same as the current one
348
model.sync();
349         
350         Document newDoc = model.getDocument();
351         assertSame("Models before and after flush/sync are same ", oldDoc, newDoc);
352         //TODO should have a good way of testing old and new models.
353
}
354     
355     public void testSyncAndNamespace() throws Exception JavaDoc {
356         javax.swing.text.Document JavaDoc swdoc = Util.getResourceAsDocument("TestSyncNamespace.wsdl");
357         XDMModel m = Util.loadXDMModel(swdoc);
358         Element root = (Element) m.getCurrentDocument().getDocumentElement();
359         NodeList JavaDoc 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 JavaDoc {
391         javax.swing.text.Document JavaDoc 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 JavaDoc {
405         private String JavaDoc eventName;
406         private int count = 0;
407         
408         public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
409             eventName = evt.getPropertyName();
410             count++;
411         }
412         
413         public int getEventsFired() {
414             return count;
415         }
416         
417         public String JavaDoc getLastEventName() {
418             return eventName;
419         }
420         
421         public void resetFiredEvents() {
422             count = 0;
423         }
424     }
425     
426     protected void setUp() throws Exception JavaDoc {
427         um = new UndoManager JavaDoc();
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 JavaDoc sd;
436     private XDMModel model;
437     private UndoManager JavaDoc um;
438 }
439
Popular Tags