1 7 8 package org.netbeans.modules.xml.xdm.nodes; 9 10 import java.util.List ; 11 import junit.framework.*; 12 import org.netbeans.editor.BaseDocument; 13 import org.netbeans.modules.xml.xdm.visitor.FlushVisitor; 14 import org.netbeans.modules.xml.xdm.Util; 15 import org.w3c.dom.NodeList ; 16 17 21 public class XMLSyntaxParserTest extends TestCase { 22 23 public XMLSyntaxParserTest(String testName) { 24 super(testName); 25 } 26 27 protected void setUp() throws Exception { 28 } 29 30 public static Test suite() { 31 TestSuite suite = new TestSuite(XMLSyntaxParserTest.class); 32 33 return suite; 34 } 35 36 39 public void testParse() throws Exception { 40 BaseDocument basedoc = (BaseDocument)Util.getResourceAsDocument("nodes/test.xml"); 41 XMLSyntaxParser parser = new XMLSyntaxParser(); 42 Document doc = parser.parse(basedoc); 43 assertNotNull("Document can not be null", doc); 44 FlushVisitor fv = new FlushVisitor(); 45 String docBuf = fv.flushModel(doc); 46 assertEquals("The document should be unaltered",basedoc.getText(0,basedoc.getLength()),docBuf); 47 } 48 49 52 public void testParseInvalid() throws Exception { 53 BaseDocument basedoc = (BaseDocument)Util.getResourceAsDocument("nodes/invalid.xml"); 54 XMLSyntaxParser parser = new XMLSyntaxParser(); 55 try { 56 Document doc = parser.parse(basedoc); 57 assertTrue("Should not come here", false); 58 } catch(Exception ex) { 59 assertTrue("Invalid Token exception" , 60 ex.getMessage().contains("Invalid token") && ex.getMessage().contains("sss")); 61 } 62 } 63 64 67 public void testParseInvalidTag() throws Exception { 68 BaseDocument basedoc = (BaseDocument)Util.getResourceAsDocument("nodes/invalidtag.xml"); 69 XMLSyntaxParser parser = new XMLSyntaxParser(); 70 try { 71 Document doc = parser.parse(basedoc); 72 assertTrue("Should not come here", false); 73 } catch(Exception ex) { 74 assertTrue("Invalid Token exception" , 75 ex.getMessage().contains("Invalid token") && ex.getMessage().contains("sss")); 76 } 77 } 78 79 82 public void testParseInvalidTag2() throws Exception { 83 BaseDocument basedoc = (BaseDocument)Util.getResourceAsDocument("nodes/invalidtag2.xml"); 84 XMLSyntaxParser parser = new XMLSyntaxParser(); 85 try { 86 Document doc = parser.parse(basedoc); 87 assertTrue("Should not come here", false); 88 } catch(Exception ex) { 89 assertTrue("Invalid Token exception" , 90 ex.getMessage().contains("Invalid token '</a' does not end with '>'")); 91 } 92 } 93 94 97 public void testParseInvalidTag3() throws Exception { 98 BaseDocument basedoc = (BaseDocument)Util.getResourceAsDocument("nodes/invalidtag3.xml"); 99 XMLSyntaxParser parser = new XMLSyntaxParser(); 100 try { 101 Document doc = parser.parse(basedoc); 102 assertTrue("Should not come here", false); 103 } catch(Exception ex) { 104 assertTrue("Invalid Token exception" , 105 ex.getMessage().contains("Invalid token '<' found in document")); 106 } 107 } 108 109 112 public void testParseInvalidTag4() throws Exception { 113 BaseDocument basedoc = (BaseDocument)Util.getResourceAsDocument("nodes/invalidtag4.xml"); 114 XMLSyntaxParser parser = new XMLSyntaxParser(); 115 try { 116 Document doc = parser.parse(basedoc); 117 assertTrue("Should not come here", false); 118 } catch(Exception ex) { 119 assertTrue("Invalid Token exception" , 120 ex.getMessage().contains("Invalid token '</b' does not end with '>'")); 121 } 122 } 123 124 127 public void testParseValidTag() throws Exception { 128 BaseDocument basedoc = (BaseDocument)Util.getResourceAsDocument("nodes/validtag.xml"); 129 XMLSyntaxParser parser = new XMLSyntaxParser(); 130 try { 131 Document doc = parser.parse(basedoc); 132 } catch(Exception ex) { 133 assertTrue("Should not come here", false); 134 } 135 } 136 137 public void testParsePI() throws Exception { 138 BaseDocument basedoc = (BaseDocument)Util.getResourceAsDocument("resources/PI_after_prolog.xml"); 139 XMLSyntaxParser parser = new XMLSyntaxParser(); 140 141 Document doc = parser.parse(basedoc); 142 List <Token> tokens = doc.getTokens(); 143 assertEquals(12, tokens.size()); 144 assertEquals(TokenType.TOKEN_PI_START_TAG, tokens.get(0).getType()); 145 assertEquals(TokenType.TOKEN_PI_END_TAG, tokens.get(4).getType()); 146 assertEquals(TokenType.TOKEN_PI_START_TAG, tokens.get(6).getType()); 147 assertEquals(TokenType.TOKEN_PI_NAME, tokens.get(7).getType()); 148 assertEquals("Siebel-Property-Set", tokens.get(7).getValue()); 149 assertEquals(TokenType.TOKEN_PI_VAL, tokens.get(9).getType()); 150 NodeList nl = doc.getChildNodes(); 151 assertEquals(2, nl.getLength()); 152 } 153 154 158 public void testParseDoctype() throws Exception { 159 BaseDocument basedoc = (BaseDocument)Util.getResourceAsDocument("nodes/testDoctype.xml"); 160 XMLSyntaxParser parser = new XMLSyntaxParser(); 161 Document doc = parser.parse(basedoc); 162 assertNotNull("Document can not be null", doc); 163 FlushVisitor fv = new FlushVisitor(); 164 String docBuf = fv.flushModel(doc); 165 assertEquals("The document should be unaltered",basedoc.getText(0,basedoc.getLength()),docBuf); 166 } 167 168 } 169 | Popular Tags |