1 19 20 package org.netbeans.modules.editor.java; 21 22 import javax.swing.text.BadLocationException ; 23 import org.netbeans.editor.Formatter; 24 import org.netbeans.editor.Settings; 25 import org.netbeans.editor.ext.java.JavaSettingsNames; 26 import org.netbeans.modules.editor.java.JavaFormatterUnitTestCase; 27 28 29 34 public class JavaFormatterUnitTest extends JavaFormatterUnitTestCase { 35 36 public JavaFormatterUnitTest(String testMethodName) { 37 super(testMethodName); 38 } 39 40 42 public void testJavadocEnterNothingAfterCaret() { 43 setLoadDocumentText( 44 "/**\n" 45 + " * text|\n" 46 + " */\n" 47 ); 48 indentNewLine(); 49 assertDocumentTextAndCaret("Incorrect new-line indent", 50 "/**\n" 51 + " * text\n" 52 + " *|\n" 53 + " */\n" 54 ); 55 56 } 57 58 public void testJavadocEnterTextAfterCaret() { 59 setLoadDocumentText( 60 "/**\n" 61 + " * break|text\n" 62 + " */\n" 63 ); 64 indentNewLine(); 65 assertDocumentTextAndCaret("Incorrect new-line indent", 66 "/**\n" 67 + " * break\n" 68 + " * |text\n" 69 + " */\n" 70 ); 71 72 } 73 74 public void testJavadocEnterStarAfterCaret() { 75 setLoadDocumentText( 76 "/**\n" 77 + " * text|*/\n" 78 ); 79 indentNewLine(); 80 assertDocumentTextAndCaret("Incorrect new-line indent", 81 "/**\n" 82 + " * text\n" 83 + " |*/\n" 84 ); 85 86 } 87 88 public void testEnterInMultiLineSystemOutPrintln() { 89 setLoadDocumentText( 90 "void m() {\n" 91 + " System.out.println(|\n" 92 + "\n" 93 ); 94 indentNewLine(); 95 assertDocumentTextAndCaret("Incorrect new-line indent", 96 "void m() {\n" 97 + " System.out.println(\n" 98 + " |\n" 99 + "\n" 100 ); 101 102 } 103 104 public void testEnterInMultiLineSystemOutPrintlnLineThree() { 105 setLoadDocumentText( 106 "void m() {\n" 107 + " System.out.println(\n" 108 + " \"haf\"|\n" 109 + "\n" 110 ); 111 indentNewLine(); 112 assertDocumentTextAndCaret("Incorrect new-line indent", 113 "void m() {\n" 114 + " System.out.println(\n" 115 + " \"haf\"\n" 116 + " |\n" 117 + "\n" 118 ); 119 120 } 121 122 public void testEnterInMultiLineSystemOutPrintlnAfterSemiColon() { 123 setLoadDocumentText( 124 "void m() {\n" 125 + " System.out.println(\n" 126 + " \"haf\");|\n" 127 + "\n" 128 ); 129 indentNewLine(); 130 assertDocumentTextAndCaret("Incorrect new-line indent", 131 "void m() {\n" 132 + " System.out.println(\n" 133 + " \"haf\");\n" 134 + " |\n" 135 + "\n" 136 ); 137 138 } 139 140 public void testEnterInMultiLineClassDeclaration() { 141 setLoadDocumentText( 142 "public class C\n" 143 + " implements Runnable\n" 144 + " throws Exception {|\n" 145 + "}\n" 146 ); 147 indentNewLine(); 148 assertDocumentTextAndCaret("Incorrect new-line indent", 149 "public class C\n" 150 + " implements Runnable\n" 151 + " throws Exception {\n" 152 + " |\n" 153 + "}\n" 154 ); 155 156 } 157 158 public void testEnterAfterIf() { 159 setLoadDocumentText( 160 "if (true)|\n" 161 ); 162 indentNewLine(); 163 assertDocumentTextAndCaret("Incorrect new-line indent", 164 "if (true)\n" 165 + " |\n" 166 ); 167 } 168 169 public void testEnterAfterFor() { 170 setLoadDocumentText( 171 "if (int i = 0; i < 10; i++)|\n" 172 ); 173 indentNewLine(); 174 assertDocumentTextAndCaret("Incorrect new-line indent", 175 "if (int i = 0; i < 10; i++)\n" 176 + " |\n" 177 ); 178 } 179 180 public void testEnterAfterWhile() { 181 setLoadDocumentText( 182 "while (true)|\n" 183 ); 184 indentNewLine(); 185 assertDocumentTextAndCaret("Incorrect new-line indent", 186 "while (true)\n" 187 + " |\n" 188 ); 189 } 190 191 public void testEnterAfterDo() { 192 setLoadDocumentText( 193 "do|\n" 194 ); 195 indentNewLine(); 196 assertDocumentTextAndCaret("Incorrect new-line indent", 197 "do\n" 198 + " |\n" 199 ); 200 } 201 202 203 public void testEnterAfterIfStmt() { 204 setLoadDocumentText( 205 "if (true)\n" 206 + " stmt;|\n" 207 ); 208 indentNewLine(); 209 assertDocumentTextAndCaret("Incorrect new-line indent", 210 "if (true)\n" 211 + " stmt;\n" 212 + "|\n" 213 ); 214 } 215 216 public void testEnterAfterIfElse() { 217 setLoadDocumentText( 218 "if (true)\n" 219 + " stmt;\n" 220 + "else|\n" 221 ); 222 indentNewLine(); 223 assertDocumentTextAndCaret("Incorrect new-line indent", 224 "if (true)\n" 225 + " stmt;\n" 226 + "else\n" 227 + " |\n" 228 ); 229 } 230 231 public void testEnterAfterIfElseStmt() { 232 setLoadDocumentText( 233 "if (true)\n" 234 + " stmt;\n" 235 + "else\n" 236 + " stmt;|\n" 237 ); 238 indentNewLine(); 239 assertDocumentTextAndCaret("Incorrect new-line indent", 240 "if (true)\n" 241 + " stmt;\n" 242 + "else\n" 243 + " stmt;\n" 244 + "|\n" 245 ); 246 } 247 248 public void testEnterAfterIfMultiLine() { 249 setLoadDocumentText( 250 "if (1 < 5|\n" 251 ); 252 indentNewLine(); 253 assertDocumentTextAndCaret("Incorrect new-line indent", 254 "if (1 < 5\n" 255 + " |\n" 256 ); 257 } 258 259 public void testEnterAfterIfMultiLine2() { 260 setLoadDocumentText( 261 "if (1 < 5|)\n" 262 ); 263 indentNewLine(); 264 assertDocumentTextAndCaret("Incorrect new-line indent", 265 "if (1 < 5\n" 266 + " |)\n" 267 ); 268 } 269 270 272 public void testReformatMultiLineSystemOutPrintln() { 273 setLoadDocumentText( 274 "void m() {\n" 275 + " System.out.println(\n" 276 + " \"haf\");\n" 277 + "}\n" 278 ); 279 reformat(); 280 assertDocumentText("Incorrect new-line indent", 281 "void m() {\n" 282 + " System.out.println(\n" 283 + " \"haf\");\n" 284 + "}\n" 285 ); 286 287 } 288 289 public void testReformatMultiLineClassDeclaration() { 290 setLoadDocumentText( 291 "public class C\n" 292 + "implements Runnable\n" 293 + "throws Exception {|\n" 294 + "System.out.println(\"haf\");\n" 295 + "}\n" 296 ); 297 reformat(); 298 assertDocumentText("Incorrect new-line indent", 299 "public class C\n" 300 + " implements Runnable\n" 301 + " throws Exception {\n" 302 + " System.out.println(\"haf\");\n" 303 + "}\n" 304 ); 305 306 } 307 308 310 314 public void testReformatNewOnTwoLines() { 315 setLoadDocumentText( 316 "javax.swing.JPanel =\n" + 317 "new java.swing.JPanel();"); 318 reformat(); 319 assertDocumentText("Incorrect new on two lines reformating", 320 "javax.swing.JPanel =\n" + 321 " new java.swing.JPanel();"); 322 } 323 324 328 public void testReformatTernaryConditionalOperator() { 329 setLoadDocumentText( 330 "something = (someComplicatedExpression != null) ?\n" + 331 "(aComplexCalculation) :\n" + 332 "(anotherComplexCalculation);"); 333 reformat(); 334 assertDocumentText("Incorrect ternary conditional operator reformatting", 335 "something = (someComplicatedExpression != null) ?\n" + 336 " (aComplexCalculation) :\n" + 337 " (anotherComplexCalculation);"); 338 } 339 340 341 345 public void testReformatArrayInitializerWithNewline() { 346 Settings.setValue(JavaKit.class, JavaSettingsNames.JAVA_FORMAT_NEWLINE_BEFORE_BRACE, Boolean.TRUE); 347 setLoadDocumentText( 348 "int[] foo = new int[] {1, 2, 3};"); 349 reformat(); 350 assertDocumentText("Incorrect array initializer with newline reformatting", 351 "int[] foo = new int[] {1, 2, 3};"); 352 Settings.setValue(JavaKit.class, JavaSettingsNames.JAVA_FORMAT_NEWLINE_BEFORE_BRACE, Boolean.FALSE); 353 } 354 355 359 public void testReformatNewlineBracesToNormalOnes() { 360 setLoadDocumentText( 361 "try\n" + 362 "{\n" + 363 "System.out.println(\"test\");\n" + 364 "}\n" + 365 "catch (ClassCastException e)\n" + 366 "{\n" + 367 "System.err.println(\"exception\");\n" + 368 "}"); 369 reformat(); 370 assertDocumentText("Incorrect array initializer with newline reformatting", 371 "try {\n" + 372 " System.out.println(\"test\");\n" + 373 "} catch (ClassCastException e) {\n" + 374 " System.err.println(\"exception\");\n" + 375 "}"); 376 } 377 378 382 public void testReformatMultilineConstructor() { 383 setLoadDocumentText( 384 "public class Test {\n" + 385 "public Test(int one,\n" + 386 "int two,\n" + 387 "int three,\n" + 388 "int four) {\n" + 389 "this.one = one;\n" + 390 "}\n" + 391 "}"); 392 reformat(); 393 assertDocumentText("Incorrect multiline constructor reformatting", 394 "public class Test {\n" + 395 " public Test(int one,\n" + 396 " int two,\n" + 397 " int three,\n" + 398 " int four) {\n" + 399 " this.one = one;\n" + 400 " }\n" + 401 "}"); 402 } 403 404 408 public void testReformatIfElseWithoutBrackets() { 409 setLoadDocumentText( 410 "if (count == 0)\n" + 411 "return 0.0f;\n" + 412 "else\n" + 413 "return performanceSum / getCount()"); 414 reformat(); 415 assertDocumentText("Incorrect reformatting of if-else without brackets", 416 "if (count == 0)\n" + 417 " return 0.0f;\n" + 418 "else\n" + 419 " return performanceSum / getCount()"); 420 } 421 422 } 423 | Popular Tags |