KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > model > DefinitionsTest


1 /*
2  * DefinitionsTest.java
3  * JUnit based test
4  *
5  * Created on May 2, 2006, 6:01 PM
6  */

7
8 package org.netbeans.modules.xml.wsdl.model;
9
10 import java.beans.PropertyChangeEvent JavaDoc;
11 import java.beans.PropertyChangeListener JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.List JavaDoc;
14 import javax.swing.undo.UndoManager JavaDoc;
15 import junit.framework.*;
16 import org.netbeans.modules.xml.schema.model.GlobalElement;
17 import org.netbeans.modules.xml.schema.model.Schema;
18 import org.netbeans.modules.xml.wsdl.model.impl.WSDLModelImpl;
19 import org.netbeans.modules.xml.xam.ComponentEvent;
20 import org.netbeans.modules.xml.xam.ComponentListener;
21 import org.netbeans.modules.xml.xam.dom.DocumentComponent;
22
23 /**
24  *
25  * @author nn136682
26  */

27 public class DefinitionsTest extends TestCase {
28     
29     public DefinitionsTest(String JavaDoc testName) {
30         super(testName);
31     }
32
33     protected void setUp() throws Exception JavaDoc {
34     }
35
36     protected void tearDown() throws Exception JavaDoc {
37         TestCatalogModel.getDefault().clearDocumentPool();
38     }
39
40     public static Test suite() {
41         TestSuite suite = new TestSuite(DefinitionsTest.class);
42         
43         return suite;
44     }
45
46     static class PropertyListener implements PropertyChangeListener JavaDoc {
47         List JavaDoc<PropertyChangeEvent JavaDoc> events = new ArrayList JavaDoc<PropertyChangeEvent JavaDoc>();
48         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
49             events.add(evt);
50         }
51         
52         public void assertEvent(String JavaDoc propertyName, Object JavaDoc old, Object JavaDoc now) {
53             for (PropertyChangeEvent JavaDoc e : events) {
54                 if (propertyName.equals(e.getPropertyName())) {
55                     if (old != null && ! old.equals(e.getOldValue()) ||
56                         old == null && e.getOldValue() != null) {
57                         continue;
58                     }
59                     if (now != null && ! now.equals(e.getNewValue()) ||
60                         now == null && e.getNewValue() != null) {
61                         continue;
62                     }
63                     return; //matched
64
}
65             }
66             assertTrue("Expect property change event on "+propertyName+" with "+old+" and "+now, false);
67         }
68     }
69     
70     class TestComponentListener implements ComponentListener {
71         ArrayList JavaDoc<ComponentEvent> accu = new ArrayList JavaDoc<ComponentEvent>();
72         public void valueChanged(ComponentEvent evt) {
73             accu.add(evt);
74         }
75         public void childrenAdded(ComponentEvent evt) {
76             accu.add(evt);
77         }
78         public void childrenDeleted(ComponentEvent evt) {
79             accu.add(evt);
80         }
81         public void reset() { accu.clear(); }
82         public int getEventCount() { return accu.size(); }
83         public java.util.List JavaDoc<ComponentEvent> getEvents() { return accu; }
84     
85         private void assertEvent(ComponentEvent.EventType type, DocumentComponent source) {
86             for (ComponentEvent e : accu) {
87                 if (e.getEventType().equals(type) &&
88                     e.getSource() == source) {
89                     return;
90                 }
91             }
92             assertTrue("Expect component change event " + type +" on source " + source +
93                     ". Instead received: " + accu, false);
94         }
95     }
96     
97     public void testSetTypes() throws Exception JavaDoc {
98         UndoManager JavaDoc um = new UndoManager JavaDoc();
99         WSDLModel model = TestCatalogModel.getDefault().getWSDLModel(NamespaceLocation.ECHOCONCAT);
100         model.addUndoableEditListener(um);
101         TestComponentListener cl = new TestComponentListener();
102         PropertyListener pl = new PropertyListener();
103         model.addComponentListener(cl);
104         model.addPropertyChangeListener(pl);
105         
106         Definitions d = model.getDefinitions();
107         int childCount = d.getChildren().size();
108         Types types = d.getTypes();
109         assertNotNull(types);
110         model.startTransaction();
111         d.setTypes(null);
112         model.endTransaction();
113         
114         cl.assertEvent(ComponentEvent.EventType.CHILD_REMOVED, d);
115         pl.assertEvent(Definitions.TYPES_PROPERTY, types, null);
116
117         
118         um.undo();
119         assertEquals(childCount, d.getChildren().size());
120         um.redo();
121         assertEquals(childCount-1, d.getChildren().size());
122     }
123
124     public void testEmbeddedSchemaUsingNamspacesFromDefinitions() throws Exception JavaDoc {
125         WSDLModelImpl model = (WSDLModelImpl)Util.loadWSDLModel("resources/schemaUsingNamespaceFromWsdlRoot.wsdl");
126         Schema schema = model.getDefinitions().getTypes().getSchemas().iterator().next();
127         List JavaDoc<GlobalElement> elements = new ArrayList JavaDoc<GlobalElement>(schema.getElements());
128         assertEquals(2, elements.size());
129         assertEquals("someType", elements.get(0).getType().get().getName());
130         assertEquals("PurchaseOrderType", elements.get(1).getType().get().getName());
131     }
132 }
133
Popular Tags