KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > text > indent > XMLFormatterTest


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.modules.xml.text.indent;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.BufferedWriter JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileReader JavaDoc;
26 import javax.swing.JTextArea JavaDoc;
27 import junit.framework.*;
28 import java.io.IOException JavaDoc;
29 import java.io.Writer JavaDoc;
30 import java.util.regex.Pattern JavaDoc;
31 import javax.swing.event.DocumentEvent JavaDoc;
32 import javax.swing.text.AbstractDocument.DefaultDocumentEvent;
33 import javax.swing.text.AttributeSet JavaDoc;
34 import javax.swing.text.BadLocationException JavaDoc;
35 import javax.swing.text.Document JavaDoc;
36 import javax.swing.text.JTextComponent JavaDoc;
37 import org.netbeans.editor.BaseDocument;
38 import org.netbeans.editor.BaseKit;
39 import org.netbeans.editor.EditorUI;
40 import org.netbeans.editor.Syntax;
41 import org.netbeans.editor.SyntaxSupport;
42 import org.netbeans.editor.TokenID;
43 import org.netbeans.editor.TokenItem;
44 import org.netbeans.editor.ext.ExtFormatter;
45 import org.netbeans.editor.Utilities;
46 import org.netbeans.editor.ext.AbstractFormatLayer;
47 import org.netbeans.editor.ext.FormatSupport;
48 import org.netbeans.editor.ext.FormatWriter;
49 import org.netbeans.junit.NbTestCase;
50 import org.netbeans.modules.xml.text.syntax.XMLKit;
51 import org.netbeans.modules.xml.text.syntax.XMLSyntaxSupport;
52
53 import org.openide.ErrorManager;
54 import org.openide.util.NbBundle;
55 import org.openide.util.RequestProcessor;
56
57 /**
58  *
59  * @author Tomasz Slota
60  */

61 public class XMLFormatterTest extends NbTestCase {
62     /**
63      * Used for testing dynamic indentation ("smart enter")
64      */

65     public final static String JavaDoc LINE_BREAK_METATAG = "|";
66     
67     public XMLFormatterTest(String JavaDoc testName) {
68         super(testName);
69     }
70
71     protected void setUp() throws Exception JavaDoc {
72     }
73
74     protected void tearDown() throws Exception JavaDoc {
75     }
76
77     public static Test suite() {
78         TestSuite suite = new TestSuite(XMLFormatterTest.class);
79         
80         return suite;
81     }
82
83     public void testReformatSample1(){
84         testReformat("web.xml");
85     }
86     
87     public void testReformatSample2(){
88         testReformat("netbeans_build.xml");
89     }
90     
91     private String JavaDoc extractCRMetatag(String JavaDoc text){
92         int indexOfMetatag = text.indexOf(LINE_BREAK_METATAG);
93         
94         if (indexOfMetatag == -1){
95             return text;
96         }
97         
98         String JavaDoc firstPart = text.substring(0, indexOfMetatag);
99         String JavaDoc secondPart = text.substring(indexOfMetatag + LINE_BREAK_METATAG.length());
100         
101         if (secondPart.indexOf(LINE_BREAK_METATAG) > -1){
102             throw new IllegalArgumentException JavaDoc("text contains more than one line break tag");
103         }
104         
105         return firstPart + secondPart;
106     }
107     
108     private void testReformat(String JavaDoc testFileName) {
109         System.out.println("testReformat(" + testFileName + ")");
110         XMLFormatter formatter = new XMLFormatter(XMLKit.class);
111         BaseDocument doc = createEmptyBaseDocument();
112          
113         try{
114             String JavaDoc txtRawXML = readStringFromFile(new File JavaDoc(new File JavaDoc(
115                 getTestFilesDir(), "testReformat"), testFileName));
116                     
117             doc.insertString(0, txtRawXML, null);
118             formatter.reformat(doc, 0, doc.getLength(), false);
119             getRef().print(doc.getText(0, doc.getLength()));
120         }
121         catch (Exception JavaDoc e){
122             fail(e.getMessage()); // should never happen
123
}
124         
125         compareReferenceFiles();
126     }
127
128     private String JavaDoc readStringFromFile(File JavaDoc file) throws IOException JavaDoc {
129         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
130         
131         BufferedReader JavaDoc rdr = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
132         
133         String JavaDoc line;
134         
135         try{
136             while ((line = rdr.readLine()) != null){
137                 buff.append(line + "\n");
138             }
139         } finally{
140             rdr.close();
141         }
142         
143         return buff.toString();
144     }
145     
146     private File JavaDoc getTestFilesDir(){
147         return new File JavaDoc(new File JavaDoc(getDataDir(), "input"), "XMLFormatterTest");
148     }
149     
150     private BaseDocument createEmptyBaseDocument(){
151         return new BaseDocument(XMLKit.class, false);
152     }
153 }
154
Popular Tags