001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.test.java.editor.formatting;
043:
044: import java.awt.event.KeyEvent;
045: import java.io.File;
046: import java.io.FileWriter;
047: import java.io.IOException;
048: import junit.textui.TestRunner;
049: import lib.EditorTestCase;
050: import lib.LineDiff;
051: import org.netbeans.jellytools.Bundle;
052: import org.netbeans.jellytools.EditorOperator;
053: import org.netbeans.jellytools.actions.Action;
054: import org.netbeans.junit.NbTestSuite;
055:
056: /**
057: *
058: * @author jp159440
059: */
060: public class BasicTest extends EditorTestCase {
061:
062: private boolean generateGoldenFiles = false;
063:
064: private String curPackage;
065:
066: private String testClass;
067:
068: protected EditorOperator oper;
069:
070: /** Creates a new instance of BasicTest */
071: public BasicTest(String name) {
072: super (name);
073: curPackage = getClass().getPackage().getName();
074:
075: }
076:
077: public static NbTestSuite suite() {
078: NbTestSuite suite = new NbTestSuite(BasicTest.class);
079: return suite;
080: }
081:
082: public File getGoldenFile() {
083: String fileName = "goldenfiles/" + curPackage.replace('.', '/')
084: + "/" + testClass + ".pass";
085: File f = new java.io.File(getDataDir(), fileName);
086: if (!f.exists())
087: fail("Golden file " + f.getAbsolutePath()
088: + " does not exist");
089: return f;
090: }
091:
092: public File getNewGoldenFile() {
093: String fileName = "qa-functional/data/goldenfiles/"
094: + curPackage.replace('.', '/') + "/" + testClass
095: + ".pass";
096: File f = new File(getDataDir().getParentFile().getParentFile()
097: .getParentFile(), fileName);
098: return f;
099: }
100:
101: public void compareGoldenFile() throws IOException {
102: File fGolden = null;
103: if (!generateGoldenFiles) {
104: fGolden = getGoldenFile();
105: } else {
106: fGolden = getNewGoldenFile();
107: }
108: String refFileName = getName() + ".ref";
109: String diffFileName = getName() + ".diff";
110: File fRef = new File(getWorkDir(), refFileName);
111: FileWriter fw = new FileWriter(fRef);
112: fw.write(oper.getText());
113: fw.close();
114: LineDiff diff = new LineDiff(false);
115: if (!generateGoldenFiles) {
116: File fDiff = new File(getWorkDir(), diffFileName);
117: if (diff.diff(fGolden, fRef, fDiff))
118: fail("Golden files differ");
119: } else {
120: FileWriter fwgolden = new FileWriter(fGolden);
121: fwgolden.write(oper.getText());
122: fwgolden.close();
123: fail("Golden file generated");
124: }
125: }
126:
127: protected void setUp() throws Exception {
128: super .setUp();
129: openDefaultProject();
130: testClass = getName();
131: System.out.println(testClass);
132: System.out.println(curPackage);
133: openSourceFile(curPackage, testClass);
134: oper = new EditorOperator(testClass);
135: oper.txtEditorPane().setVerification(false);
136: }
137:
138: protected void tearDown() throws Exception {
139: compareGoldenFile();
140: super .tearDown();
141: }
142:
143: public void testBasicIndentation() {
144: oper.setCaretPosition(20, 1);
145: oper.pushEndKey();
146: oper.pushKey(KeyEvent.VK_ENTER);
147: oper.pushKey(KeyEvent.VK_ENTER);
148: oper.txtEditorPane().typeText(
149: "public void method(boolean cond1");
150: oper.pushEndKey();
151: oper.txtEditorPane().typeText("{");
152: oper.pushKey(KeyEvent.VK_ENTER);
153: oper.txtEditorPane().typeText("if(cond1");
154: oper.pushEndKey();
155: oper.txtEditorPane().typeText("sout");
156: oper.pushTabKey();
157: oper.txtEditorPane().typeText("Hello");
158: oper.pushEndKey();
159: }
160:
161: private void doReformat() {
162: String sourceMenu = Bundle.getStringTrimmed(
163: "org.netbeans.core.Bundle", "Menu/Source"); // NOI18N
164: String reformat = Bundle.getStringTrimmed(
165: "org.netbeans.modules.editor.Bundle",
166: "format_main_menu_item");
167: new Action(sourceMenu + "|" + reformat, null).perform();
168: }
169:
170: private void end() {
171: oper.pushEndKey();
172: }
173:
174: private void enter() {
175: oper.pushKey(KeyEvent.VK_ENTER);
176: }
177:
178: private void type(String text) {
179: oper.txtEditorPane().typeText(text);
180: }
181:
182: public void testAdvancedIndentation() {
183: oper.setCaretPosition(20, 1);
184: end();
185: enter();
186: enter();
187: type("public void method(");
188: end();
189: type(" {");
190: enter();
191: type("while(true");
192: end();
193: type(" {");
194: enter();
195: type("if(true");
196: end();
197: enter();
198: type("if(false");
199: end();
200: type(" {");
201: enter();
202: type("int i = ");
203: enter();
204: type("1;");
205: }
206:
207: public void testReformat() {
208: doReformat();
209: }
210:
211: /**
212: * Annotations, anonymous classes, inner classes formatting test
213: * testReformat2.java, testReformat.pass
214: */
215: public void testReformat2() {
216: doReformat();
217: }
218:
219: public void testReformatAnnotation() {
220: doReformat();
221: }
222:
223: public void testReformatIncompleteStatement() {
224: doReformat();
225: }
226:
227: public void testReformatIncompleteStatement2() {
228: doReformat();
229: }
230:
231: public static void main(String[] args) {
232: TestRunner.run(new NbTestSuite(BasicTest.class));
233: }
234:
235: }
|