KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > editor > formatting > BasicTest


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.test.editor.formatting;
21
22
23 import java.awt.event.KeyEvent JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileWriter JavaDoc;
26 import java.io.IOException JavaDoc;
27 import junit.textui.TestRunner;
28 import lib.EditorTestCase;
29 import org.netbeans.jellytools.Bundle;
30 import org.netbeans.jellytools.EditorOperator;
31 import org.netbeans.jellytools.actions.Action;
32 import org.netbeans.junit.NbTestSuite;
33 import org.netbeans.test.editor.LineDiff;
34
35 /**
36  *
37  * @author jp159440
38  */

39 public class BasicTest extends EditorTestCase{
40     
41     private boolean generateGoldenFiles = false;
42     
43     private String JavaDoc curPackage;
44     
45     private String JavaDoc testClass;
46     
47     protected EditorOperator oper;
48     
49     /** Creates a new instance of BasicTest */
50     public BasicTest(String JavaDoc name) {
51         super(name);
52         curPackage = getClass().getPackage().getName();
53         
54     }
55     
56     public static NbTestSuite suite() {
57         NbTestSuite suite = new NbTestSuite(BasicTest.class);
58         return suite;
59     }
60     
61     public File JavaDoc getGoldenFile() {
62         String JavaDoc fileName = "goldenfiles/"+curPackage.replace('.', '/')+ "/" + testClass + ".pass";
63         File JavaDoc f = new java.io.File JavaDoc(getDataDir(),fileName);
64         if(!f.exists()) fail("Golden file "+f.getAbsolutePath()+ " does not exist");
65         return f;
66     }
67     
68     public File JavaDoc getNewGoldenFile() {
69         String JavaDoc fileName = "qa-functional/data/goldenfiles/"+curPackage.replace('.', '/')+ "/" + testClass + ".pass";
70         File JavaDoc f = new File JavaDoc(getDataDir().getParentFile().getParentFile().getParentFile(),fileName);
71         return f;
72     }
73     
74     public void compareGoldenFile() throws IOException JavaDoc {
75         File JavaDoc fGolden = null;
76         if(!generateGoldenFiles) {
77             fGolden = getGoldenFile();
78         } else {
79             fGolden = getNewGoldenFile();
80         }
81         String JavaDoc refFileName = getName()+".ref";
82         String JavaDoc diffFileName = getName()+".diff";
83         File JavaDoc fRef = new File JavaDoc(getWorkDir(),refFileName);
84         FileWriter JavaDoc fw = new FileWriter JavaDoc(fRef);
85         fw.write(oper.getText());
86         fw.close();
87         LineDiff diff = new LineDiff(false);
88         if(!generateGoldenFiles) {
89             File JavaDoc fDiff = new File JavaDoc(getWorkDir(),diffFileName);
90             if(diff.diff(fGolden, fRef, fDiff)) fail("Golden files differ");
91         } else {
92             FileWriter JavaDoc fwgolden = new FileWriter JavaDoc(fGolden);
93             fwgolden.write(oper.getText());
94             fwgolden.close();
95             fail("Golden file generated");
96         }
97     }
98     
99     protected void setUp() throws Exception JavaDoc {
100         super.setUp();
101         openDefaultProject();
102         testClass = getName();
103         System.out.println(testClass );
104         System.out.println(curPackage);
105         openSourceFile(curPackage, testClass);
106         oper = new EditorOperator(testClass);
107         oper.txtEditorPane().setVerification(false);
108     }
109     
110     protected void tearDown() throws Exception JavaDoc {
111         compareGoldenFile();
112         super.tearDown();
113     }
114     
115     public void testBasicIndentation() {
116         oper.setCaretPosition(20,1);
117         oper.pushEndKey();
118         oper.pushKey(KeyEvent.VK_ENTER);
119         oper.pushKey(KeyEvent.VK_ENTER);
120         oper.txtEditorPane().typeText("public void method(boolean cond1");
121         oper.pushEndKey();
122         oper.txtEditorPane().typeText("{");
123         oper.pushKey(KeyEvent.VK_ENTER);
124         oper.txtEditorPane().typeText("if(cond1");
125         oper.pushEndKey();
126         oper.txtEditorPane().typeText("sout");
127         oper.pushTabKey();
128         oper.txtEditorPane().typeText("Hello");
129         oper.pushEndKey();
130     }
131     
132     private void end() {
133         oper.pushEndKey();
134     }
135     
136     private void enter() {
137         oper.pushKey(KeyEvent.VK_ENTER);
138     }
139     
140     private void type(String JavaDoc text) {
141         oper.txtEditorPane().typeText(text);
142     }
143     
144     
145     public void testAdvancedIndentation() {
146         oper.setCaretPosition(20,1);
147         end();
148         enter();
149         enter();
150         type("public void method(");
151         end();
152         type(" {");
153         enter();
154         type("while(true");
155         end();
156         type(" {");
157         enter();
158         type("if(true");
159         end();
160         enter();
161         type("if(false");
162         end();
163         type(" {");
164         enter();
165         type("int i = ");
166         enter();
167         type("1;");
168     }
169     
170     public void testReformat() {
171         String JavaDoc sourceMenu = Bundle.getStringTrimmed("org.netbeans.core.Bundle", "Menu/Source"); // NOI18N
172
String JavaDoc reformat = Bundle.getStringTrimmed("org.netbeans.modules.editor.Bundle", "format_main_menu_item");
173         new Action(sourceMenu+"|"+reformat, null).perform();
174     }
175     
176     /**
177      * Annotations, anonymous classes, inner classes formatting test
178      * testReformat2.java, testReformat.pass
179      */

180     public void testReformat2() {
181         String JavaDoc sourceMenu = Bundle.getStringTrimmed("org.netbeans.core.Bundle", "Menu/Source"); // NOI18N
182
String JavaDoc reformat = Bundle.getStringTrimmed("org.netbeans.modules.editor.Bundle", "format_main_menu_item");
183         new Action(sourceMenu+"|"+reformat, null).perform();
184     }
185     
186     public static void main(String JavaDoc[] args) {
187         TestRunner.run(new NbTestSuite(BasicTest.class));
188     }
189     
190     
191 }
192
193
Popular Tags