1 7 8 package org.netbeans.modules.xml.schema.model.impl; 9 10 import java.beans.PropertyChangeEvent ; 11 import java.beans.PropertyChangeListener ; 12 import java.io.File ; 13 import java.io.IOException ; 14 import java.util.ArrayList ; 15 import java.util.List ; 16 import junit.framework.*; 17 import org.netbeans.modules.xml.schema.model.Annotation; 18 import org.netbeans.modules.xml.schema.model.AppInfo; 19 import org.netbeans.modules.xml.schema.model.Documentation; 20 import org.netbeans.modules.xml.schema.model.Schema; 21 import org.netbeans.modules.xml.schema.model.SchemaModel; 22 import org.netbeans.modules.xml.schema.model.Util; 23 import org.netbeans.modules.xml.schema.model.visitor.FindSchemaComponentFromDOM; 24 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent; 25 import org.netbeans.modules.xml.xam.dom.DocumentComponent; 26 import org.netbeans.modules.xml.xam.ComponentEvent; 27 import org.netbeans.modules.xml.xam.ComponentListener; 28 import org.netbeans.modules.xml.xdm.nodes.Text; 29 import org.w3c.dom.Element ; 30 31 35 public class AnnotationImplTest extends TestCase { 36 37 public static final String TEST_XSD = "resources/loanApplication.xsd"; 38 Schema schema = null; 39 TestPropertyListener plistener; 40 TestComponentListener clistener; 41 42 public AnnotationImplTest(String testName) { 43 super(testName); 44 } 45 46 static class TestPropertyListener 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 List <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 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, false); 93 } 94 } 95 96 protected void setUp() throws Exception { 97 SchemaModel model = Util.loadSchemaModel(TEST_XSD); 98 clistener = new TestComponentListener(); 99 plistener = new TestPropertyListener(); 100 model.addComponentListener(clistener); 101 model.addPropertyChangeListener(plistener); 102 schema = model.getSchema(); 103 } 104 105 public static Test suite() { 106 TestSuite suite = new TestSuite(AnnotationImplTest.class); 107 108 return suite; 109 } 110 111 114 public void testDocumentationInternalDetail() throws Exception { 115 String xpath = "/schema/element[1]/annotation"; 116 Annotation ann = FindSchemaComponentFromDOM.find(Annotation.class, schema, xpath); 117 assertEquals("getDocumentations().size()", 2, ann.getDocumentationElements().size()); 118 Documentation doc = ann.getDocumentationElements().iterator().next(); 119 assertEquals("en", doc.getLanguage()); 120 String txt = "A loan application"; 121 assertEquals("documentation read text", txt, doc.getContent()); 122 123 String txt2 = "testDocumentation.write"; 124 schema.getModel().startTransaction(); 125 doc.setContent(txt2); 126 schema.getModel().endTransaction(); 127 assertEquals("testDocumentation.write", "testDocumentation.write", doc.getContent()); 128 129 schema.getModel().startTransaction(); 130 Element parentPeer = ((AbstractDocumentComponent)ann).getPeer(); 131 Element docElement = doc.getDocumentationElement(); 132 assertTrue("documentation element cloned", docElement != ((AbstractDocumentComponent)doc).getPeer()); 133 assertFalse("cloned element not in tree", ((org.netbeans.modules.xml.xdm.nodes.Node)docElement).isInTree()); 134 Element docMyTag = schema.getModel().getDocument().createElement("mytag"); 135 docMyTag.setAttribute("attribute1", "value1"); 136 docElement.appendChild(docMyTag); 137 doc.setDocumentationElement(docElement); 138 schema.getModel().endTransaction(); 139 assertTrue("parent component not changed", ann == doc.getParent()); 140 assertTrue("parent peer node updated", parentPeer != ((AbstractDocumentComponent)ann).getPeer()); 141 assertTrue("peer == documentation element used", docElement == ((AbstractDocumentComponent)doc).getPeer()); 142 assertTrue("documentation element child", docElement.getLastChild() == docMyTag); 143 Text txtNode = (Text)docElement.getChildNodes().item(0); 144 assertEquals("documentation element text child", txt2, txtNode.getText()); 145 assertTrue("updated element now in tree", ((org.netbeans.modules.xml.xdm.nodes.Node)docElement).isInTree()); 146 147 } 149 150 public void testDocumentationReadElement() throws IOException { 151 String xpath = "/schema/element[1]/annotation/documentation[2]"; 152 Documentation doc = FindSchemaComponentFromDOM.find(Documentation.class, schema, xpath); 153 assertEquals("documentation content when no-text", "", doc.getContent().trim()); 154 Element doce = doc.getDocumentationElement(); 155 assertEquals("documentation element count", 3, doce.getChildNodes().getLength()); 156 Element html = (Element ) doce.getChildNodes().item(1); 157 assertEquals("documentation element tag", "html", html.getNodeName()); 158 Element a = (Element ) html.getChildNodes().item(1); 159 String aText = "Testing documenation elemnent"; 160 assertEquals("doc element children", aText, ((Text)a.getFirstChild()).getText()); 161 } 162 163 public void testAppInfo() throws IOException { 164 String xpath = "/schema/element[1]/annotation"; 165 Annotation ann = FindSchemaComponentFromDOM.find(Annotation.class, schema, xpath); 166 AppInfo info = ann.getAppInfos().iterator().next(); 167 assertEquals("appinfo source", "http://www.aloan.com/loanApp", info.getURI()); 168 Element infoE = info.getAppInfoElement(); 169 Element handlingE = (Element )infoE.getChildNodes().item(1); 170 Text textnode = (Text)handlingE.getFirstChild(); 171 assertEquals("appinfo element child", "checkForPrimes", textnode.getText()); 172 173 ann.getModel().startTransaction(); 174 ann.removeAppInfo(info); 175 AppInfo info2 = ann.getModel().getFactory().createAppInfo(); 176 textnode.setText("checkIfUpdated"); 177 ann.getModel().endTransaction(); 178 } 179 180 public void testDocumentationFragment() throws Exception { 181 String xpath = "/schema/element[1]/annotation"; 182 Annotation ann = FindSchemaComponentFromDOM.find(Annotation.class, schema, xpath); 183 Documentation docu = ann.getDocumentationElements().iterator().next(); 184 String oldContent = docu.getContent(); 185 186 schema.getModel().startTransaction(); 187 try { 188 docu.setContentFragment("<bad/xml>"); 189 assertFalse("should have thrown IOException", true); 190 } catch(IOException ioe) { 191 assertTrue("gotit", true); 192 } 193 String fragment = "<good>This is a good <doc></good>"; 194 docu.setContentFragment(fragment); 195 schema.getModel().endTransaction(); 196 197 clistener.assertEvent(ComponentEvent.EventType.VALUE_CHANGED, docu); 198 plistener.assertEvent(Documentation.CONTENT_PROPERTY, oldContent, fragment); 199 assertEquals(fragment, docu.getContentFragment()); 200 Element good = (Element ) docu.getDocumentationElement().getChildNodes().item(0); 201 assertEquals("good", good.getTagName()); 202 Text text = (Text) good.getChildNodes().item(0); 203 assertEquals("This is a good <doc>", text.getText()); 204 } 205 } 206 | Popular Tags |