KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xdm > nodes > XMLSyntaxParserTest


1 /*
2  * XMLSyntaxParserTest.java
3  * JUnit based test
4  *
5  * Created on September 26, 2005, 12:38 PM
6  */

7
8 package org.netbeans.modules.xml.xdm.nodes;
9
10 import java.util.List JavaDoc;
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 JavaDoc;
16
17 /**
18  *
19  * @author Administrator
20  */

21 public class XMLSyntaxParserTest extends TestCase {
22     
23     public XMLSyntaxParserTest(String JavaDoc testName) {
24         super(testName);
25     }
26     
27     protected void setUp() throws Exception JavaDoc {
28     }
29     
30     public static Test suite() {
31         TestSuite suite = new TestSuite(XMLSyntaxParserTest.class);
32         
33         return suite;
34     }
35     
36     /**
37      * Test of parse method, of class org.netbeans.modules.xmltools.xmlmodel.nodes.XMLSyntaxParser.
38      */

39     public void testParse() throws Exception JavaDoc {
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 JavaDoc docBuf = fv.flushModel(doc);
46         assertEquals("The document should be unaltered",basedoc.getText(0,basedoc.getLength()),docBuf);
47     }
48     
49     /**
50      * Test of parse method, of class org.netbeans.modules.xmltools.xmlmodel.nodes.XMLSyntaxParser.
51      */

52     public void testParseInvalid() throws Exception JavaDoc {
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 JavaDoc ex) {
59             assertTrue("Invalid Token exception" ,
60                     ex.getMessage().contains("Invalid token") && ex.getMessage().contains("sss"));
61         }
62     }
63     
64     /**
65      * Test of parse method, of class org.netbeans.modules.xmltools.xmlmodel.nodes.XMLSyntaxParser.
66      */

67     public void testParseInvalidTag() throws Exception JavaDoc {
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 JavaDoc ex) {
74             assertTrue("Invalid Token exception" ,
75                     ex.getMessage().contains("Invalid token") && ex.getMessage().contains("sss"));
76         }
77     }
78     
79     /**
80      * Test of parse method, of class org.netbeans.modules.xmltools.xmlmodel.nodes.XMLSyntaxParser.
81      */

82     public void testParseInvalidTag2() throws Exception JavaDoc {
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 JavaDoc ex) {
89             assertTrue("Invalid Token exception" ,
90                     ex.getMessage().contains("Invalid token '</a' does not end with '>'"));
91         }
92     }
93     
94     /**
95      * Test of parse method, of class org.netbeans.modules.xmltools.xmlmodel.nodes.XMLSyntaxParser.
96      */

97     public void testParseInvalidTag3() throws Exception JavaDoc {
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 JavaDoc ex) {
104             assertTrue("Invalid Token exception" ,
105                     ex.getMessage().contains("Invalid token '<' found in document"));
106         }
107     }
108     
109     /**
110      * Test of parse method, of class org.netbeans.modules.xmltools.xmlmodel.nodes.XMLSyntaxParser.
111      */

112     public void testParseInvalidTag4() throws Exception JavaDoc {
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 JavaDoc ex) {
119             assertTrue("Invalid Token exception" ,
120                     ex.getMessage().contains("Invalid token '</b' does not end with '>'"));
121         }
122     }
123     
124     /**
125      * Test of parse method, of class org.netbeans.modules.xmltools.xmlmodel.nodes.XMLSyntaxParser.
126      */

127     public void testParseValidTag() throws Exception JavaDoc {
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 JavaDoc ex) {
133             assertTrue("Should not come here", false);
134         }
135     }
136
137     public void testParsePI() throws Exception JavaDoc {
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 JavaDoc<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 JavaDoc nl = doc.getChildNodes();
151         assertEquals(2, nl.getLength());
152     }
153
154     /**
155      * Test of parse method, of class org.netbeans.modules.xmltools.xmlmodel.nodes.XMLSyntaxParser.
156      * Test the parsing of doctype
157      */

158     public void testParseDoctype() throws Exception JavaDoc {
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 JavaDoc docBuf = fv.flushModel(doc);
165         assertEquals("The document should be unaltered",basedoc.getText(0,basedoc.getLength()),docBuf);
166     }
167     
168 }
169
Popular Tags