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.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 org.netbeans.jellytools.Bundle;
051: import org.netbeans.jellytools.EditorOperator;
052: import org.netbeans.jellytools.actions.Action;
053: import org.netbeans.junit.NbTestSuite;
054:
055: //import org.netbeans.test.editor.LineDiff;
056:
057: /**
058: *
059: * @author jp159440
060: */
061: public class BasicTest extends EditorTestCase {
062:
063: private boolean generateGoldenFiles = false;
064:
065: private String curPackage;
066:
067: private String testClass;
068:
069: protected EditorOperator oper;
070:
071: /** Creates a new instance of BasicTest */
072: public BasicTest(String name) {
073: super (name);
074: curPackage = getClass().getPackage().getName();
075:
076: }
077:
078: public static NbTestSuite suite() {
079: NbTestSuite suite = new NbTestSuite(BasicTest.class);
080: return suite;
081: }
082:
083: public File getGoldenFile() {
084: String fileName = "goldenfiles/" + curPackage.replace('.', '/')
085: + "/" + testClass + ".pass";
086: File f = new java.io.File(getDataDir(), fileName);
087: if (!f.exists())
088: fail("Golden file " + f.getAbsolutePath()
089: + " does not exist");
090: return f;
091: }
092:
093: public File getNewGoldenFile() {
094: String fileName = "qa-functional/data/goldenfiles/"
095: + curPackage.replace('.', '/') + "/" + testClass
096: + ".pass";
097: File f = new File(getDataDir().getParentFile().getParentFile()
098: .getParentFile(), fileName);
099: System.out.println(f);
100: return f;
101: }
102:
103: public void compareGoldenFile() throws IOException {
104: File fGolden = null;
105: if (!generateGoldenFiles) {
106: fGolden = getGoldenFile();
107: } else {
108: fGolden = getNewGoldenFile();
109: }
110: String refFileName = getName() + ".ref";
111: String diffFileName = getName() + ".diff";
112: File fRef = new File(getWorkDir(), refFileName);
113: FileWriter fw = new FileWriter(fRef);
114: fw.write(oper.getText());
115: fw.close();
116: // LineDiff diff = new LineDiff(false);
117: if (!generateGoldenFiles) {
118: File fDiff = new File(getWorkDir(), diffFileName);
119: System.out.println("fRef file is: " + fRef);
120: System.out.println("fGolden file is: " + fGolden);
121: System.out.println("fDiff file is: " + fDiff);
122: assertFile(fRef, fGolden, fDiff);
123: // if(diff.diff(fGolden, fRef, fDiff)) fail("Golden files differ");
124: } else {
125: FileWriter fwgolden = new FileWriter(fGolden);
126: fwgolden.write(oper.getText());
127: fwgolden.close();
128: fail("Golden file generated");
129: }
130: }
131:
132: protected void setUp() throws Exception {
133: super .setUp();
134: openDefaultProject();
135: testClass = getName();
136: System.out.println(testClass);
137: System.out.println(curPackage);
138: openSourceFile(curPackage, testClass);
139: oper = new EditorOperator(testClass);
140: oper.txtEditorPane().setVerification(false);
141: }
142:
143: protected void tearDown() throws Exception {
144: compareGoldenFile();
145: super .tearDown();
146: }
147:
148: public void testBasicIndentation() {
149: oper.setCaretPosition(279, 1);
150: oper.pushEndKey();
151: oper.pushKey(KeyEvent.VK_ENTER);
152: oper.pushKey(KeyEvent.VK_ENTER);
153: oper.txtEditorPane().typeText(
154: "public void method(boolean cond1");
155: oper.pushEndKey();
156: oper.txtEditorPane().typeText("{");
157: oper.pushKey(KeyEvent.VK_ENTER);
158: oper.txtEditorPane().typeText("if(cond1");
159: oper.pushEndKey();
160: oper.txtEditorPane().typeText("sout");
161: oper.pushTabKey();
162: oper.txtEditorPane().typeText("Hello");
163: oper.pushEndKey();
164: }
165:
166: private void end() {
167: oper.pushEndKey();
168: }
169:
170: private void enter() {
171: oper.pushKey(KeyEvent.VK_ENTER);
172: }
173:
174: private void type(String text) {
175: oper.txtEditorPane().typeText(text);
176: }
177:
178: public void testAdvancedIndentation() {
179: oper.setCaretPosition(120, 1);
180: end();
181: enter();
182: enter();
183: type("public void method(");
184: end();
185: type(" {");
186: enter();
187: type("while(true");
188: end();
189: type(" {");
190: enter();
191: type("if(true");
192: end();
193: enter();
194: type("if(false");
195: end();
196: type(" {");
197: enter();
198: type("int i = ");
199: enter();
200: type("1;");
201: }
202:
203: public void testReformat() {
204: String sourceMenu = Bundle.getStringTrimmed(
205: "org.netbeans.core.Bundle", "Menu/Source"); // NOI18N
206: String reformat = Bundle.getStringTrimmed(
207: "org.netbeans.modules.editor.Bundle",
208: "format_main_menu_item");
209: new Action(sourceMenu + "|" + reformat, null).perform();
210: }
211:
212: /**
213: * Annotations, anonymous classes, inner classes formatting test
214: * testReformat2.java, testReformat.pass
215: */
216: public void testReformat2() {
217: String sourceMenu = Bundle.getStringTrimmed(
218: "org.netbeans.core.Bundle", "Menu/Source"); // NOI18N
219: String reformat = Bundle.getStringTrimmed(
220: "org.netbeans.modules.editor.Bundle",
221: "format_main_menu_item");
222: new Action(sourceMenu + "|" + reformat, null).perform();
223: }
224:
225: public static void main(String[] args) {
226: TestRunner.run(new NbTestSuite(BasicTest.class));
227: }
228:
229: }
|