1 7 8 package org.netbeans.modules.xml.wsdl.model; 9 10 import java.beans.PropertyChangeEvent ; 11 import java.beans.PropertyChangeListener ; 12 import java.util.ArrayList ; 13 import java.util.List ; 14 import javax.swing.undo.UndoManager ; 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 27 public class DefinitionsTest extends TestCase { 28 29 public DefinitionsTest(String testName) { 30 super(testName); 31 } 32 33 protected void setUp() throws Exception { 34 } 35 36 protected void tearDown() throws Exception { 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 { 47 List <PropertyChangeEvent > events = new ArrayList <PropertyChangeEvent >(); 48 public void propertyChange(PropertyChangeEvent evt) { 49 events.add(evt); 50 } 51 52 public void assertEvent(String propertyName, Object old, Object now) { 53 for (PropertyChangeEvent 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; } 65 } 66 assertTrue("Expect property change event on "+propertyName+" with "+old+" and "+now, false); 67 } 68 } 69 70 class TestComponentListener implements ComponentListener { 71 ArrayList <ComponentEvent> accu = new ArrayList <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 <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 { 98 UndoManager um = new UndoManager (); 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 { 125 WSDLModelImpl model = (WSDLModelImpl)Util.loadWSDLModel("resources/schemaUsingNamespaceFromWsdlRoot.wsdl"); 126 Schema schema = model.getDefinitions().getTypes().getSchemas().iterator().next(); 127 List <GlobalElement> elements = new ArrayList <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 |