Lines 22-28
Link Here
|
22 |
public class TryWithResourcesStatementTest extends AbstractRegressionTest { |
22 |
public class TryWithResourcesStatementTest extends AbstractRegressionTest { |
23 |
|
23 |
|
24 |
static { |
24 |
static { |
25 |
// TESTS_NAMES = new String[] { "test053" }; |
25 |
TESTS_NAMES = new String[] { "test055" }; |
26 |
// TESTS_NUMBERS = new int[] { 50 }; |
26 |
// TESTS_NUMBERS = new int[] { 50 }; |
27 |
// TESTS_RANGE = new int[] { 11, -1 }; |
27 |
// TESTS_RANGE = new int[] { 11, -1 }; |
28 |
} |
28 |
} |
Lines 3341-3346
Link Here
|
3341 |
"Object.Integer cannot be resolved to a type\n" + |
3341 |
"Object.Integer cannot be resolved to a type\n" + |
3342 |
"----------\n"); |
3342 |
"----------\n"); |
3343 |
} |
3343 |
} |
|
|
3344 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3345 |
// a method uses an AutoCloseable without ever closing it. |
3346 |
public void test055() { |
3347 |
Map options = getCompilerOptions(); |
3348 |
// FIXME(SH): make configurable |
3349 |
// options.put(CompilerOptions.OPTION_ReportUnclosedAutocloseable, CompilerOptions.ERROR); |
3350 |
this.runNegativeTest( |
3351 |
new String[] { |
3352 |
"X.java", |
3353 |
"import java.io.File;\n" + |
3354 |
"import java.io.FileReader;\n" + |
3355 |
"import java.io.IOException;\n" + |
3356 |
"public class X {\n" + |
3357 |
" void foo() throws IOException {\n" + |
3358 |
" File file = new File(\"somefile\");\n" + |
3359 |
" FileReader fileReader = new FileReader(file);\n" + |
3360 |
" char[] in = new char[50];\n" + |
3361 |
" fileReader.read(in);\n" + |
3362 |
" }\n" + |
3363 |
" public static void main(String[] args) throws IOException {\n" + |
3364 |
" new X().foo();\n" + |
3365 |
" }\n" + |
3366 |
"}\n" |
3367 |
}, |
3368 |
"----------\n" + |
3369 |
"1. ERROR in X.java (at line 7)\n" + |
3370 |
" FileReader fileReader = new FileReader(file);\n" + |
3371 |
" ^^^^^^^^^^\n" + |
3372 |
"Value of type AutoCloseable is not closed neither explicitly nor using a try-with-resources.\n" + |
3373 |
"----------\n", |
3374 |
null, |
3375 |
true, |
3376 |
options); |
3377 |
} |
3378 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3379 |
// a method uses an AutoCloseable and closes it properly. |
3380 |
public void test055a() { |
3381 |
Map options = getCompilerOptions(); |
3382 |
// options.put(CompilerOptions.OPTION_ReportUnclosedAutocloseable, CompilerOptions.ERROR); |
3383 |
this.runConformTest( |
3384 |
new String[] { |
3385 |
"X.java", |
3386 |
"import java.io.File;\n" + |
3387 |
"import java.io.FileReader;\n" + |
3388 |
"import java.io.IOException;\n" + |
3389 |
"public class X {\n" + |
3390 |
" void foo() throws IOException {\n" + |
3391 |
" File file = new File(\"somefile\");\n" + |
3392 |
" FileReader fileReader = new FileReader(file);\n" + |
3393 |
" char[] in = new char[50];\n" + |
3394 |
" fileReader.read(in);\n" + |
3395 |
" fileReader.close();\n" + |
3396 |
" }\n" + |
3397 |
" public static void main(String[] args) {\n" + |
3398 |
" try {\n" + |
3399 |
" new X().foo();\n" + |
3400 |
" } catch (IOException ioex) {\n" + |
3401 |
" System.out.println(\"caught\");\n" + |
3402 |
" }\n" + |
3403 |
" }\n" + |
3404 |
"}\n" |
3405 |
}, |
3406 |
"caught", /*output*/ |
3407 |
null/*classLibs*/, |
3408 |
true/*shouldFlush*/, |
3409 |
null/*vmargs*/, |
3410 |
options, |
3411 |
null/*requestor*/); |
3412 |
} |
3413 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3414 |
// a method uses an AutoCloseable properly within try-with-resources. |
3415 |
public void test055b() { |
3416 |
Map options = getCompilerOptions(); |
3417 |
// options.put(CompilerOptions.OPTION_ReportUnclosedAutocloseable, CompilerOptions.ERROR); |
3418 |
this.runConformTest( |
3419 |
new String[] { |
3420 |
"X.java", |
3421 |
"import java.io.File;\n" + |
3422 |
"import java.io.FileReader;\n" + |
3423 |
"import java.io.IOException;\n" + |
3424 |
"public class X {\n" + |
3425 |
" void foo() throws IOException {\n" + |
3426 |
" File file = new File(\"somefile\");\n" + |
3427 |
" try (FileReader fileReader = new FileReader(file)) {\n" + |
3428 |
" char[] in = new char[50];\n" + |
3429 |
" fileReader.read(in);\n" + |
3430 |
" }\n" + |
3431 |
" }\n" + |
3432 |
" public static void main(String[] args) {\n" + |
3433 |
" try {\n" + |
3434 |
" new X().foo();\n" + |
3435 |
" } catch (IOException ioex) {\n" + |
3436 |
" System.out.println(\"caught\");\n" + |
3437 |
" }\n" + |
3438 |
" }\n" + |
3439 |
"}\n" |
3440 |
}, |
3441 |
"caught", /*output*/ |
3442 |
null/*classLibs*/, |
3443 |
true/*shouldFlush*/, |
3444 |
null/*vmargs*/, |
3445 |
options, |
3446 |
null/*requestor*/); |
3447 |
} |
3448 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3449 |
// a method uses an AutoCloseable but may return without closing it. |
3450 |
public void test055c() { |
3451 |
Map options = getCompilerOptions(); |
3452 |
// options.put(CompilerOptions.OPTION_ReportUnclosedAutocloseable, CompilerOptions.ERROR); |
3453 |
this.runNegativeTest( |
3454 |
new String[] { |
3455 |
"X.java", |
3456 |
"import java.io.File;\n" + |
3457 |
"import java.io.FileReader;\n" + |
3458 |
"import java.io.IOException;\n" + |
3459 |
"public class X {\n" + |
3460 |
" void foo(boolean flag) throws IOException {\n" + |
3461 |
" File file = new File(\"somefile\");\n" + |
3462 |
" FileReader fileReader = new FileReader(file);\n" + |
3463 |
" char[] in = new char[50];\n" + |
3464 |
" fileReader.read(in);\n" + |
3465 |
" if (flag)\n" + |
3466 |
" return;\n" + |
3467 |
" fileReader.close();\n" + |
3468 |
" }\n" + |
3469 |
" public static void main(String[] args) throws IOException {\n" + |
3470 |
" new X().foo(false);\n" + |
3471 |
" }\n" + |
3472 |
"}\n" |
3473 |
}, |
3474 |
"----------\n" + |
3475 |
"1. ERROR in X.java (at line 11)\n" + |
3476 |
" return;\n" + |
3477 |
" ^^^^^^^\n" + |
3478 |
"Instance \'fileReader\' of type AutoCloseable is not closed at this point.\n" + |
3479 |
"----------\n", |
3480 |
null, |
3481 |
true, |
3482 |
options); |
3483 |
} |
3484 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3485 |
// one method returns an AutoCleasble, a second method uses this object without ever closing it. |
3486 |
public void test055d() { |
3487 |
Map options = getCompilerOptions(); |
3488 |
//FIXME(SH): make configurable |
3489 |
// options.put(CompilerOptions.OPTION_ReportUnclosedAutocloseable, CompilerOptions.ERROR); |
3490 |
this.runNegativeTest( |
3491 |
new String[] { |
3492 |
"X.java", |
3493 |
"import java.io.File;\n" + |
3494 |
"import java.io.FileReader;\n" + |
3495 |
"import java.io.IOException;\n" + |
3496 |
"public class X {\n" + |
3497 |
" FileReader getReader(String filename) throws IOException {\n" + |
3498 |
" File file = new File(\"somefile\");\n" + |
3499 |
" FileReader fileReader = new FileReader(file);\n" + |
3500 |
" return fileReader;\n" + // don't complain here, pass responsibility to caller |
3501 |
" }\n" + |
3502 |
" void foo() throws IOException {\n" + |
3503 |
" FileReader reader = getReader(\"somefile\");\n" + |
3504 |
" char[] in = new char[50];\n" + |
3505 |
" reader.read(in);\n" + |
3506 |
" }\n" + |
3507 |
" public static void main(String[] args) throws IOException {\n" + |
3508 |
" new X().foo();\n" + |
3509 |
" }\n" + |
3510 |
"}\n" |
3511 |
}, |
3512 |
"----------\n" + |
3513 |
"1. ERROR in X.java (at line 11)\n" + |
3514 |
" FileReader reader = getReader(\"somefile\");\n" + |
3515 |
" ^^^^^^\n" + |
3516 |
"Value of type AutoCloseable is not closed neither explicitly nor using a try-with-resources.\n" + |
3517 |
"----------\n", |
3518 |
null, |
3519 |
true, |
3520 |
options); |
3521 |
} |
3344 |
public static Class testClass() { |
3522 |
public static Class testClass() { |
3345 |
return TryWithResourcesStatementTest.class; |
3523 |
return TryWithResourcesStatementTest.class; |
3346 |
} |
3524 |
} |