KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > ext > html > HTMLSyntaxSupportTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.editor.ext.html;
21
22 import org.netbeans.editor.ext.html.parser.SyntaxElement;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import javax.swing.text.BadLocationException JavaDoc;
26 import org.netbeans.editor.BaseDocument;
27 import org.netbeans.editor.ext.html.dtd.DTD;
28 import org.netbeans.editor.ext.html.test.TestBase;
29 import org.netbeans.modules.editor.html.HTMLKit;
30 import org.netbeans.modules.editor.html.NbReaderProvider;
31
32
33 /**Html syntax support tests
34  * This class extends TestBase class which provides access to the html editor module layer
35  *
36  * @author Marek Fukala
37  */

38 public class HTMLSyntaxSupportTest extends TestBase {
39     
40     static HTMLSyntaxSupport sup;
41     static BaseDocument doc;
42         
43     public HTMLSyntaxSupportTest() throws IOException JavaDoc, BadLocationException JavaDoc {
44         super("htmlsyntaxsupporttest");
45         setUpSuite();
46     }
47     
48     private void setUpSuite() throws IOException JavaDoc, BadLocationException JavaDoc {
49         File JavaDoc inputFile = new File JavaDoc(getDataDir(), "input/HTMLSyntaxSupportTest/index.html");
50         String JavaDoc content = Utils.readFileContentToString(inputFile);
51         doc = createDocument();
52         doc.insertString(0,content,null);
53         sup = new HTMLSyntaxSupport(doc);
54         NbReaderProvider.setupReaders(); //initialize DTD providers
55
}
56     
57     public void tearDown() {
58     }
59     
60     //test methods -----------
61

62     public void testBasis() {
63         assertNotNull(sup.getDocument());
64         assertEquals(sup.getDocument(), doc);
65         
66         assertNotNull(sup.getDocType());
67         assertEquals("-//W3C//DTD HTML 4.01 Transitional//EN", sup.getDocType());
68         
69         DTD dtd = sup.getDTD();
70         assertNotNull(dtd);
71     }
72     
73     public void testSyntaxElementsBasis() throws BadLocationException JavaDoc {
74         SyntaxElement se = sup.getElementChain(0);
75         
76         assertNotNull(se);
77         assertNull(se.getPrevious()); //there shouldn't be any element before
78

79         SyntaxElement next = se.getNext();
80         assertNotNull(next);
81         
82         SyntaxElement prev = next.getPrevious();
83         assertNotNull(next);
84         assertEquals(prev, se);
85     }
86     
87     
88     public void testSyntaxElementsForEveryPositionInDocument() throws BadLocationException JavaDoc {
89         for(int i = 0; i < doc.getLength(); i++) {
90             SyntaxElement se = sup.getElementChain(i);
91             assertNotNull(se);
92             getRef().println(i+":"+se);
93         }
94         compareReferenceFiles();
95     }
96     
97     public void testWalkThroughSyntaxElements() throws BadLocationException JavaDoc {
98         SyntaxElement se = sup.getElementChain(0);
99         do {
100             getRef().println(se);
101             
102             SyntaxElement next = se.getNext();
103             
104             assertNotSame(next, se);
105             se = next;
106         } while(se != null);
107         
108         compareReferenceFiles();
109     }
110     
111     public void testWalkBackwartThroughtSyntaxElements() throws BadLocationException JavaDoc {
112         SyntaxElement se = sup.getElementChain(doc.getLength() - 1);
113         do {
114             getRef().println(se);
115             
116             SyntaxElement prev = se.getPrevious();
117             
118             assertNotSame(prev, se);
119             se = prev;
120         } while(se != null);
121         
122         compareReferenceFiles();
123     }
124     
125 // public void testIsSingletonTag() throws BadLocationException {
126
// HTMLSyntaxSupport sup = createSupportForText(TEXT1);
127
//
128
// //test the <html> tag
129
// int htmlTagOffset = 2;
130
// TokenItem ti = sup.getTokenChain(htmlTagOffset, htmlTagOffset+1);
131
// assertNotNull(ti);
132
// assertFalse(sup.isSingletonTag(ti));
133
//
134
// //test the <br/> tag
135
// htmlTagOffset = 35;
136
// ti = sup.getTokenChain(htmlTagOffset, htmlTagOffset+1);
137
// assertNotNull(ti);
138
// assertTrue(sup.isSingletonTag(ti));
139
// }
140

141     public void testFindMatchingBlock() throws BadLocationException JavaDoc {
142         for(int i = 0; i < doc.getLength(); i++) {
143             int[] match = sup.findMatchingBlock(i, false);
144             if(match == null) {
145                 getRef().println(i + " => NO MATCH");
146             } else {
147                 getRef().println(i + " => " + "[" + match[0] + ";" + match[1] + "]");
148             }
149         }
150         
151         compareReferenceFiles();
152     }
153     
154     private static HTMLSyntaxSupport createSupportForText(String JavaDoc text) throws BadLocationException JavaDoc {
155         BaseDocument bd = new BaseDocument(HTMLKit.class, false);
156         bd.insertString(0,text,null);
157         return new HTMLSyntaxSupport(bd);
158     }
159     
160     // 0 1 2 3 4
161
// 01234567890123456789012345678901234567890123456789
162
private static final String JavaDoc TEXT1 = "<html><body bgcolor=\"green\">hello<br/></body></html>";
163     
164 }
165
Popular Tags