KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > model > impl > SyncUpdateTest


1 package org.netbeans.modules.xml.wsdl.model.impl;
2
3 import java.beans.PropertyChangeEvent JavaDoc;
4 import java.beans.PropertyChangeListener JavaDoc;
5 import java.io.File JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.logging.Level JavaDoc;
10 import java.util.logging.Logger JavaDoc;
11 import javax.swing.text.BadLocationException JavaDoc;
12 import javax.swing.text.Document JavaDoc;
13 import javax.swing.undo.UndoManager JavaDoc;
14 import junit.framework.TestCase;
15 import org.netbeans.modules.xml.schema.model.GlobalElement;
16 import org.netbeans.modules.xml.schema.model.GlobalType;
17 import org.netbeans.modules.xml.schema.model.Schema;
18 import org.netbeans.modules.xml.wsdl.model.Binding;
19 import org.netbeans.modules.xml.wsdl.model.BindingOperation;
20 import org.netbeans.modules.xml.wsdl.model.Definitions;
21 import org.netbeans.modules.xml.wsdl.model.Documentation;
22 import org.netbeans.modules.xml.wsdl.model.Message;
23 import org.netbeans.modules.xml.wsdl.model.NamespaceLocation;
24 import org.netbeans.modules.xml.wsdl.model.NotificationOperation;
25 import org.netbeans.modules.xml.wsdl.model.OneWayOperation;
26 import org.netbeans.modules.xml.wsdl.model.Operation;
27 import org.netbeans.modules.xml.wsdl.model.Part;
28 import org.netbeans.modules.xml.wsdl.model.Port;
29 import org.netbeans.modules.xml.wsdl.model.PortType;
30 import org.netbeans.modules.xml.wsdl.model.ReferenceableWSDLComponent;
31 import org.netbeans.modules.xml.wsdl.model.RequestResponseOperation;
32 import org.netbeans.modules.xml.wsdl.model.Service;
33 import org.netbeans.modules.xml.wsdl.model.SolicitResponseOperation;
34 import org.netbeans.modules.xml.wsdl.model.TestCatalogModel;
35 import org.netbeans.modules.xml.wsdl.model.Types;
36 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
37 import org.netbeans.modules.xml.wsdl.model.Util;
38 import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
39 import org.netbeans.modules.xml.wsdl.model.extensions.xsd.WSDLSchema;
40 import org.netbeans.modules.xml.wsdl.model.spi.GenericExtensibilityElement;
41 import org.netbeans.modules.xml.wsdl.model.visitor.FindReferencedVisitor;
42 import org.netbeans.modules.xml.xam.ComponentEvent;
43 import org.netbeans.modules.xml.xam.ComponentListener;
44 import org.netbeans.modules.xml.xam.Model;
45 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
46 import org.netbeans.modules.xml.xam.dom.DocumentComponent;
47 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
48 import org.netbeans.modules.xml.xdm.visitor.FlushVisitor;
49 import org.netbeans.modules.xml.xdm.visitor.PathFromRootVisitor;
50 import org.w3c.dom.Element JavaDoc;
51
52 /**
53  *
54  * @author Nam Nguyen
55  */

56 public class SyncUpdateTest extends TestCase {
57     private TestComponentListener listener;
58     private TestPropertyListener plistener;
59     
60     public SyncUpdateTest(String JavaDoc testName) {
61         super(testName);
62     }
63     
64     static class TestPropertyListener implements PropertyChangeListener JavaDoc {
65         ArrayList JavaDoc<PropertyChangeEvent JavaDoc> events = new ArrayList JavaDoc<PropertyChangeEvent JavaDoc>();
66         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
67             events.add(evt);
68         }
69         
70         public void assertNoEvents(String JavaDoc propertyName) {
71             for (PropertyChangeEvent JavaDoc e : events) {
72                 if (propertyName.equals(e.getPropertyName())) {
73                     assertTrue("Expect no property change events "+propertyName, false);
74                 }
75             }
76             return; //matched
77
}
78         
79         public void assertEvent(String JavaDoc propertyName, Object JavaDoc source) {
80             for (PropertyChangeEvent JavaDoc e : events) {
81                 if (propertyName.equals(e.getPropertyName()) && e.getSource() == source) {
82                     return; //matched
83
}
84             }
85             assertTrue("Expect property change event "+propertyName, false);
86         }
87         
88         public void assertEvent(String JavaDoc propertyName, Object JavaDoc old, Object JavaDoc now) {
89             for (PropertyChangeEvent JavaDoc e : events) {
90                 if (propertyName.equals(e.getPropertyName())) {
91                     if (old != null && ! old.equals(e.getOldValue()) ||
92                         old == null && e.getOldValue() != null) {
93                         continue;
94                     }
95                     if (now != null && ! now.equals(e.getNewValue()) ||
96                         now == null && e.getNewValue() != null) {
97                         continue;
98                     }
99                     return; //matched
100
}
101             }
102             assertTrue("Expect property change event on "+propertyName+" with "+old+" and "+now, false);
103         }
104     }
105     
106     public static class TestComponentListener implements ComponentListener {
107         ArrayList JavaDoc<ComponentEvent> events = new ArrayList JavaDoc<ComponentEvent>();
108         
109         public void valueChanged(ComponentEvent evt) {
110             events.add(evt);
111         }
112         public void childrenDeleted(ComponentEvent evt) {
113             events.add(evt);
114         }
115         public void childrenAdded(ComponentEvent evt) {
116             events.add(evt);
117         }
118         
119         public void assertChangeEvent(Object JavaDoc source) {
120             for (ComponentEvent e : events) {
121                 if (e.getEventType() == ComponentEvent.EventType.VALUE_CHANGED &&
122                     e.getSource() == source) {
123                     return;
124                 }
125             }
126             assertFalse("Failed to receive ATTRIBUTE event on " + source, true);
127         }
128         
129         public void assertChildAddedEvent(Object JavaDoc source) {
130             for (ComponentEvent e : events) {
131                 if (e.getEventType() == ComponentEvent.EventType.CHILD_ADDED &&
132                         e.getSource() == source) {
133                     return;
134                 }
135             }
136             assertFalse("Failed to receive CHILD_ADDED event on " + source, true);
137         }
138         
139         public void assertChildRemovedEvent(Object JavaDoc source) {
140             for (ComponentEvent e : events) {
141                 if (e.getEventType() == ComponentEvent.EventType.CHILD_REMOVED &&
142                         e.getSource() == source) {
143                     return;
144                 }
145             }
146             assertFalse("Failed to receive CHILD_REMOVED event on " + source, true);
147         }
148         
149         public void assertEventCount(int count) {
150             assertEquals("Event count", count, events.size());
151         }
152         
153         public void reset() { events.clear(); events = null; }
154         
155     }
156     
157     protected void setUp() throws Exception JavaDoc {
158     }
159     
160     private WSDLModel setup(NamespaceLocation wsdl) throws Exception JavaDoc {
161         WSDLModel m = TestCatalogModel.getDefault().getWSDLModel(wsdl);
162         setup(m);
163         return m;
164     }
165     
166     private void setup(WSDLModel m) {
167         listener = new TestComponentListener();
168         plistener = new TestPropertyListener();
169         m.addComponentListener(listener);
170         m.addPropertyChangeListener(plistener);
171     }
172     
173     protected void tearDown() throws Exception JavaDoc {
174         if (listener != null) listener.reset();
175         TestCatalogModel.getDefault().clearDocumentPool();
176     }
177     
178     public <T extends ReferenceableWSDLComponent> T find(WSDLModel model, String JavaDoc name, Class JavaDoc<T> type) {
179         return new FindReferencedVisitor<T>(model.getDefinitions()).find(name, type);
180     }
181
182     public void testOperations() throws Exception JavaDoc {
183         WSDLModel model = Util.loadWSDLModel("resources/TestOperations.wsdl");
184         listener = new TestComponentListener();
185         model.addComponentListener(listener);
186      
187         Definitions d = model.getDefinitions();
188         PortType pt = d.getPortTypes().iterator().next();
189      
190         Util.setDocumentContentTo(model, "resources/TestOperations_after.wsdl");
191         model.sync();
192      
193         List JavaDoc<Operation> ops = new ArrayList JavaDoc<Operation>(pt.getOperations());
194         Operation op1 = null, op2 = null, op3 = null, op4 = null;
195         for (Operation op : ops) {
196             if (op.getName().equals("op1")) {
197                 op1 = op;
198             } else if (op.getName().equals("op2")) {
199                 op2 = op;
200             } else if (op.getName().equals("op3")) {
201                 op3 = op;
202             } else if (op.getName().equals("op4")) {
203                 op4 = op;
204             }
205         }
206      
207         assertTrue("op1 transformed to notification "+op1, op1 instanceof NotificationOperation);
208         assertTrue("op2 transformed to solicit-response "+op2, op2 instanceof SolicitResponseOperation);
209         assertTrue("op3 transformed to request-response "+op3, op3 instanceof RequestResponseOperation);
210         assertTrue("op4 transformed to one-way "+op4, op4 instanceof OneWayOperation);
211     }
212
213     public void testSyncUpdateSchema() throws Exception JavaDoc {
214         WSDLModel model = setup(NamespaceLocation.TRAVEL);
215         Schema schema = model.getDefinitions().getTypes().getSchemas().iterator().next();
216         Util.setDocumentContentTo(model, "resources/TravelReservationService_1.wsdl");
217         model.sync();
218      
219         listener.assertChangeEvent(schema);
220         assertEquals("schema.targetNamespace", "http://www.sun.com/javaone/05/TravelReservationService", schema.getTargetNamespace());
221      
222         GlobalElement fault = schema.getElements().iterator().next();
223         Util.setDocumentContentTo(model, "resources/TravelReservationService_schemaChanged.wsdl");
224         model.sync();
225      
226         assertEquals("embedded attribute changed", "xs:long", fault.getType().getRefString());
227         listener.assertChildAddedEvent(schema);
228         assertEquals("embedded element changed", 2, schema.getElements().size());
229      
230         Util.setDocumentContentTo(model, "resources/TravelReservationService_1.wsdl");
231         model.sync();
232      
233         assertEquals("added element", 1, schema.getElements().size());
234     }
235     
236      public void testFind() throws Exception JavaDoc{
237         WSDLModel model = Util.loadWSDLModel("resources/HelloService.wsdl");
238         listener = new TestComponentListener();
239         model.addComponentListener(listener);
240         Message message = find(model, "HelloServiceSEI_sayHello", Message.class);
241         this.assertNotNull("Could not find message HelloServiceSEI_sayHello", message);
242         Part part = find(model, "String_1", Part.class);
243         assertNotNull("Could not find part String_1", part);
244         NamedComponentReference<GlobalType> gtRef = part.getType();
245         assertNotNull("could not find part type reference object", gtRef);
246         GlobalType gt = gtRef.get();
247         assertNotNull("could not find part simple type", gt);
248         PortType portType = find(model, "HelloServiceSEI", PortType.class);
249         this.assertNotNull("Could not find portType HelloServiceSEI", portType);
250         RequestResponseOperation operation = find(model, "sayHello", RequestResponseOperation.class);
251         assertNotNull("Could not find operation sayHello", operation);
252         Binding binding = find(model, "HelloServiceSEIBinding", Binding.class);
253         this.assertNotNull("Could not find binding HelloServiceSEIBindin", binding);
254         Service service = find(model, "HelloService", Service.class);
255         this.assertNotNull("Could not find service HelloService", service);
256         Port port = find(model, "HelloServiceSEIPort", Port.class);
257         assertNotNull("Could not find port HelloServiceSEIPort", port);
258     }
259      
260      public void testWholeFileSync() throws Exception JavaDoc {
261         WSDLModel model = setup(NamespaceLocation.ECHOCONCAT);
262         
263         Util.setDocumentContentTo(model, "resources/ParkingLotManager.wsdl");
264         model.sync();
265      
266         listener.assertEventCount(6);
267         listener.assertChangeEvent(model.getDefinitions());
268         listener.assertChildAddedEvent(model.getDefinitions());
269         listener.assertChildRemovedEvent(model.getDefinitions());
270         Schema schema = model.getDefinitions().getTypes().getSchemas().iterator().next();
271         listener.assertChangeEvent(schema);
272         listener.assertChildAddedEvent(schema);
273         listener.assertChildRemovedEvent(schema);
274      }
275      
276      public void testRenameInput() throws Exception JavaDoc {
277         WSDLModel model = Util.loadWSDLModel("resources/synctest1.wsdl");
278         
279         Util.setDocumentContentTo(model, "resources/synctest1_renamedInput.wsdl");
280         model.sync();
281         PortType pt = model.findComponentByName("portType_0", PortType.class);
282         assertEquals(1, pt.getOperations().size());
283         assertEquals("input1", pt.getOperations().iterator().next().getInput().getName());
284      }
285      
286      public void testUpdatePeerOnInlineSchema() throws Exception JavaDoc {
287         WSDLModel model = Util.loadWSDLModel("resources/template.wsdl");
288         WSDLSchema schema = model.getDefinitions().getTypes().getExtensibilityElements(WSDLSchema.class).get(0);
289         Element saved = schema.getPeer();
290         
291         Util.setDocumentContentTo(model, "resources/simple-exit.wsdl");
292         model.sync();
293         
294         Document JavaDoc doc = model.getBaseDocument();
295         String JavaDoc wsdlText = doc.getText(0, doc.getLength());
296         schema = model.getDefinitions().getTypes().getExtensibilityElements(WSDLSchema.class).get(0);
297         assertNotSame("Embedded schema node should be updated", saved, schema.getPeer());
298         String JavaDoc schemaText = schema.getContentFragment();
299         assertTrue(schemaText, wsdlText.indexOf(schemaText) > -1);
300      }
301      
302      public void testChangedXsdImportNS() throws Exception JavaDoc {
303          WSDLModel model = Util.loadWSDLModel("resources/TravelReservationService_badImportNS.wsdl");
304          WSDLSchema schema = model.getDefinitions().getTypes().getExtensibilityElements(WSDLSchema.class).get(0);
305          Element saved = schema.getPeer();
306
307         org.netbeans.modules.xml.schema.model.Import xsdImport =
308              schema.getSchemaModel().getSchema().getImports().iterator().next();
309          assertEquals("blah", xsdImport.getNamespace());
310          
311          Util.setDocumentContentTo(model, "resources/TravelReservationService_schemaChanged.wsdl");
312          model.sync();
313          assertEquals("http://www.opentravel.org/OTA/2003/05", xsdImport.getNamespace());
314          
315          assertNotSame("Embedded schema node should be updated", saved, schema.getPeer());
316          Document JavaDoc doc = model.getBaseDocument();
317          String JavaDoc wsdlText = doc.getText(0, doc.getLength());
318          String JavaDoc schemaText = schema.getContentFragment();
319          assertTrue(schemaText, wsdlText.indexOf(schemaText) > -1);
320      }
321
322      public void testTwoTypes() throws Exception JavaDoc {
323          WSDLModel model = Util.loadWSDLModel("resources/empty.wsdl");
324          Definitions def = model.getDefinitions();
325          
326          Util.setDocumentContentTo(model, "resources/TwoTypes.wsdl");
327          model.sync();
328          assertEquals("generic getChildren returns all", 2, def.getChildren(Types.class).size());
329          Types types = def.getTypes();
330          assertEquals("foo", types.getSchemas().iterator().next().getTargetNamespace());
331          types = new ArrayList JavaDoc<Types>(def.getChildren(Types.class)).get(1);
332          assertEquals("bar", types.getSchemas().iterator().next().getTargetNamespace());
333          
334          Util.setDocumentContentTo(model, "resources/OneTypes.wsdl");
335          model.sync();
336          assertEquals(1, def.getChildren(Types.class).size());
337          types = def.getTypes();
338          assertEquals("foo", types.getSchemas().iterator().next().getTargetNamespace());
339      }
340
341      public void testEmptyOperation() throws Exception JavaDoc {
342         WSDLModel model = Util.loadWSDLModel("resources/TestOperations.wsdl");
343         Definitions d = model.getDefinitions();
344
345         try {
346             Util.setDocumentContentTo(model, "resources/TestEmptyOperation.wsdl");
347             model.sync();
348             assertFalse("Should get IOException on empty operation", true);
349         } catch(IOException JavaDoc e) {
350             // good
351
assertEquals(Model.State.NOT_WELL_FORMED, model.getState());
352         }
353      }
354      
355      // assert generic EE added/removed/changed events
356
public void testEventsOnGenericExtensibilityElement() throws Exception JavaDoc {
357         WSDLModel model = Util.loadWSDLModel("resources/TestEventsOnEE.wsdl");
358         setup(model);
359         Definitions d = model.getDefinitions();
360         Binding binding = d.getBindings().iterator().next();
361         BindingOperation bindop = binding.getBindingOperations().iterator().next();
362         Service service = d.getServices().iterator().next();
363         GenericExtensibilityElement ee0 = (GenericExtensibilityElement) binding.getExtensibilityElements().get(0);
364         GenericExtensibilityElement ee3 = (GenericExtensibilityElement) service.getExtensibilityElements().get(0);
365
366         Util.setDocumentContentTo(model, "resources/TestEventsOnEE_after.wsdl");
367         model.sync();
368
369         listener.assertChangeEvent(ee0);
370         listener.assertChildAddedEvent(bindop);
371         listener.assertChildRemovedEvent(bindop);
372         plistener.assertEvent(WSDLComponent.EXTENSIBILITY_ELEMENT_PROPERTY, bindop);
373         listener.assertChangeEvent(ee3);
374         plistener.assertEvent(DocumentComponent.TEXT_CONTENT_PROPERTY, ee3);
375      }
376      
377      public void testDocumentationChangeEvents() throws Exception JavaDoc {
378         WSDLModel model = Util.loadWSDLModel("resources/TestDocumentation.wsdl");
379         setup(model);
380         Documentation doc = Util.find(Documentation.class, model, "/definitions/portType/operation/documentation");
381
382         Util.setDocumentContentTo(model, "resources/TestDocumentation_after.wsdl");
383         model.sync();
384
385         listener.assertChangeEvent(doc);
386         plistener.assertEvent(DocumentComponent.TEXT_CONTENT_PROPERTY, doc);
387      }
388
389      
390      public void testUndoXsdImportChange() throws Exception JavaDoc {
391          WSDLModel model = Util.loadWSDLModel("resources/HotelReservationService2.wsdl");
392          UndoManager JavaDoc um = new UndoManager JavaDoc();
393          model.addUndoableEditListener(um);
394          WSDLSchema schema = model.getDefinitions().getTypes().getExtensibilityElements(WSDLSchema.class).get(0);
395          org.netbeans.modules.xml.schema.model.Import xsdImport =
396              schema.getSchemaModel().getSchema().getImports().iterator().next();
397         
398         model.startTransaction();
399         xsdImport.setSchemaLocation("OTA_TravelItinerary.xsd");
400         model.endTransaction();
401         
402         um.undo();
403         assertEquals("OTA_TravelItinerary2.xsd", xsdImport.getSchemaLocation());
404      }
405
406      public void testWsdlSchemaReformat() throws Exception JavaDoc {
407          WSDLModel model = Util.loadWSDLModel("resources/newWSDL1.wsdl");
408          setup(model);
409          List JavaDoc<WSDLSchema> schemas = model.getDefinitions().getTypes().getExtensibilityElements(WSDLSchema.class);
410
411          Util.setDocumentContentTo(model, "resources/newWSDL1_reformat.wsdl");
412          model.sync();
413          
414          for (WSDLSchema schema : schemas) {
415              checkEmbeddedSchema(schema);
416          }
417          listener.assertEventCount(0);
418      }
419      private void checkEmbeddedSchema(WSDLSchema schema) throws Exception JavaDoc {
420          WSDLModel model = schema.getModel();
421          String JavaDoc wsdlText2 = getWSDLText(model);
422          assertTrue(new PathFromRootVisitor().findPath(model.getDocument(), schema.getPeer()).size() == 4);
423          assertSame(schema.getPeer(), schema.getSchemaModel().getSchema().getPeer());
424          
425          int index2 = schema.findPosition();
426          String JavaDoc fragment2 = schema.getContentFragment();
427          assertTrue(wsdlText2.indexOf(fragment2) > 0);
428          assertTrue(wsdlText2.indexOf(fragment2, index2) > 0);
429     }
430     
431     private String JavaDoc getWSDLText(WSDLModel model) throws Exception JavaDoc {
432         javax.swing.text.Document JavaDoc d = model.getBaseDocument();
433         return d.getText(0, d.getLength());
434     }
435 }
436
Popular Tags