1 19 20 package org.netbeans.test.editor.formatting; 21 22 23 import java.awt.event.KeyEvent ; 24 import java.io.File ; 25 import java.io.FileWriter ; 26 import java.io.IOException ; 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 39 public class BasicTest extends EditorTestCase{ 40 41 private boolean generateGoldenFiles = false; 42 43 private String curPackage; 44 45 private String testClass; 46 47 protected EditorOperator oper; 48 49 50 public BasicTest(String 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 getGoldenFile() { 62 String fileName = "goldenfiles/"+curPackage.replace('.', '/')+ "/" + testClass + ".pass"; 63 File f = new java.io.File (getDataDir(),fileName); 64 if(!f.exists()) fail("Golden file "+f.getAbsolutePath()+ " does not exist"); 65 return f; 66 } 67 68 public File getNewGoldenFile() { 69 String fileName = "qa-functional/data/goldenfiles/"+curPackage.replace('.', '/')+ "/" + testClass + ".pass"; 70 File f = new File (getDataDir().getParentFile().getParentFile().getParentFile(),fileName); 71 return f; 72 } 73 74 public void compareGoldenFile() throws IOException { 75 File fGolden = null; 76 if(!generateGoldenFiles) { 77 fGolden = getGoldenFile(); 78 } else { 79 fGolden = getNewGoldenFile(); 80 } 81 String refFileName = getName()+".ref"; 82 String diffFileName = getName()+".diff"; 83 File fRef = new File (getWorkDir(),refFileName); 84 FileWriter fw = new FileWriter (fRef); 85 fw.write(oper.getText()); 86 fw.close(); 87 LineDiff diff = new LineDiff(false); 88 if(!generateGoldenFiles) { 89 File fDiff = new File (getWorkDir(),diffFileName); 90 if(diff.diff(fGolden, fRef, fDiff)) fail("Golden files differ"); 91 } else { 92 FileWriter fwgolden = new FileWriter (fGolden); 93 fwgolden.write(oper.getText()); 94 fwgolden.close(); 95 fail("Golden file generated"); 96 } 97 } 98 99 protected void setUp() throws Exception { 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 { 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 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 sourceMenu = Bundle.getStringTrimmed("org.netbeans.core.Bundle", "Menu/Source"); String reformat = Bundle.getStringTrimmed("org.netbeans.modules.editor.Bundle", "format_main_menu_item"); 173 new Action(sourceMenu+"|"+reformat, null).perform(); 174 } 175 176 180 public void testReformat2() { 181 String sourceMenu = Bundle.getStringTrimmed("org.netbeans.core.Bundle", "Menu/Source"); String 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 [] args) { 187 TestRunner.run(new NbTestSuite(BasicTest.class)); 188 } 189 190 191 } 192 193 | Popular Tags |