KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > web > core > syntax > JSPFormatterTest


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.test.web.core.syntax;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import junit.framework.Test;
27 import junit.framework.TestSuite;
28 import org.netbeans.api.jsp.lexer.JspTokenId;
29 import org.netbeans.api.lexer.Language;
30 import org.netbeans.editor.BaseDocument;
31 import org.netbeans.editor.BaseKit;
32 import org.netbeans.junit.NbTestCase;
33 import org.netbeans.modules.editor.NbEditorDocument;
34 import org.netbeans.modules.web.core.syntax.JSPKit;
35 import org.netbeans.modules.web.core.syntax.formatting.JspFormatter;
36
37 /**
38  *
39  * @author Tomasz Slota
40  */

41 public class JSPFormatterTest extends NbTestCase {
42     private static final String JavaDoc PROP_MIME_TYPE = "mimeType"; //NOI18N
43

44 // static {
45
// MockServices.setServices(new Class[] {RepositoryImpl.class});
46
// }
47

48     /** Creates a new instance of JSPFormatterTest */
49     public JSPFormatterTest(String JavaDoc testName) {
50         super(testName);
51     }
52     
53     public static Test suite() {
54         TestSuite suite = new TestSuite(JSPFormatterTest.class);
55         
56         return suite;
57     }
58     
59     // public tests
60

61     public void testReformatSample1() throws Exception JavaDoc{
62         testReformat("TestPage1.jsp");
63     }
64     
65     public void testReformatSample2() throws Exception JavaDoc{
66         testReformat("TestPage2.jsp");
67     }
68     
69     public void testIssue82272() throws Exception JavaDoc{
70         testReformat("issue82272.jsp");
71     }
72     
73     public void testIssue83616() throws Exception JavaDoc{
74         testReformat("issue83616.jsp");
75     }
76     
77     // end of public tests
78

79     private void testReformat(String JavaDoc testFileName) throws Exception JavaDoc {
80         System.out.println("testReformat(" + testFileName + ")");
81         JspFormatter formatter = new JspFormatter(JSPKit.class);
82         BaseDocument doc = createDocument();
83         
84         
85         String JavaDoc txtRawJSP = readFileContentToString(new File JavaDoc(new File JavaDoc(
86                 getTestFilesDir(), "testReformat"), testFileName));
87         
88         doc.insertString(0, txtRawJSP, null);
89         formatter.reformat(doc, 0, doc.getLength(), false);
90         getRef().print(doc.getText(0, doc.getLength()));
91         
92         compareReferenceFiles();
93     }
94     
95     private String JavaDoc readFileContentToString(File JavaDoc file) throws IOException JavaDoc {
96         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
97         BufferedReader JavaDoc rdr = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
98         
99         String JavaDoc line;
100         
101         try{
102             while ((line = rdr.readLine()) != null){
103                 buff.append(line + "\n");
104             }
105         } finally{
106             rdr.close();
107         }
108         
109         return buff.toString();
110     }
111     
112     private File JavaDoc getTestFilesDir(){
113         return new File JavaDoc(new File JavaDoc(getDataDir(), "input"), "JSPFormatterTest");
114     }
115     
116     protected BaseDocument createDocument() {
117         NbEditorDocument doc = new NbEditorDocument(JSPKit.class);
118         doc.putProperty(PROP_MIME_TYPE, BaseKit.getKit(JSPKit.class).getContentType());
119         doc.putProperty(Language.class, JspTokenId.language()); //hack for LanguageManager - shoudl be removed
120

121         return doc;
122     }
123 }
124
Popular Tags