Lines 16-28
Link Here
|
16 |
|
16 |
|
17 |
import java.util.Map; |
17 |
import java.util.Map; |
18 |
|
18 |
|
|
|
19 |
import org.eclipse.jdt.core.JavaCore; |
19 |
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; |
20 |
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; |
20 |
|
21 |
|
21 |
import junit.framework.Test; |
22 |
import junit.framework.Test; |
22 |
public class TryWithResourcesStatementTest extends AbstractRegressionTest { |
23 |
public class TryWithResourcesStatementTest extends AbstractRegressionTest { |
23 |
|
24 |
|
24 |
static { |
25 |
static { |
25 |
// TESTS_NAMES = new String[] { "test053" }; |
26 |
TESTS_NAMES = new String[] { "test055" }; |
26 |
// TESTS_NUMBERS = new int[] { 50 }; |
27 |
// TESTS_NUMBERS = new int[] { 50 }; |
27 |
// TESTS_RANGE = new int[] { 11, -1 }; |
28 |
// TESTS_RANGE = new int[] { 11, -1 }; |
28 |
} |
29 |
} |
Lines 3341-3346
Link Here
|
3341 |
"Object.Integer cannot be resolved to a type\n" + |
3342 |
"Object.Integer cannot be resolved to a type\n" + |
3342 |
"----------\n"); |
3343 |
"----------\n"); |
3343 |
} |
3344 |
} |
|
|
3345 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3346 |
// a method uses an AutoCloseable without ever closing it. |
3347 |
public void test055() { |
3348 |
Map options = getCompilerOptions(); |
3349 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3350 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3351 |
this.runNegativeTest( |
3352 |
new String[] { |
3353 |
"X.java", |
3354 |
"import java.io.File;\n" + |
3355 |
"import java.io.FileReader;\n" + |
3356 |
"import java.io.IOException;\n" + |
3357 |
"public class X {\n" + |
3358 |
" void foo() throws IOException {\n" + |
3359 |
" File file = new File(\"somefile\");\n" + |
3360 |
" FileReader fileReader = new FileReader(file);\n" + |
3361 |
// not invoking any methods on FileReader, try to avoid necessary call to superclass() |
3362 |
// " char[] in = new char[50];\n" + |
3363 |
// " fileReader.read(in);\n" + |
3364 |
" }\n" + |
3365 |
" public static void main(String[] args) throws IOException {\n" + |
3366 |
" new X().foo();\n" + |
3367 |
" }\n" + |
3368 |
"}\n" |
3369 |
}, |
3370 |
"----------\n" + |
3371 |
"1. ERROR in X.java (at line 7)\n" + |
3372 |
" FileReader fileReader = new FileReader(file);\n" + |
3373 |
" ^^^^^^^^^^\n" + |
3374 |
"Value of type AutoCloseable is not closed neither explicitly nor using a try-with-resources.\n" + |
3375 |
"----------\n", |
3376 |
null, |
3377 |
true, |
3378 |
options); |
3379 |
} |
3380 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3381 |
// a method uses an AutoCloseable and closes it properly. |
3382 |
public void test055a() { |
3383 |
Map options = getCompilerOptions(); |
3384 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3385 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3386 |
this.runConformTest( |
3387 |
new String[] { |
3388 |
"X.java", |
3389 |
"import java.io.File;\n" + |
3390 |
"import java.io.FileReader;\n" + |
3391 |
"import java.io.IOException;\n" + |
3392 |
"public class X {\n" + |
3393 |
" void foo() throws IOException {\n" + |
3394 |
" File file = new File(\"somefile\");\n" + |
3395 |
" FileReader fileReader = new FileReader(file);\n" + |
3396 |
" char[] in = new char[50];\n" + |
3397 |
" fileReader.read(in);\n" + |
3398 |
" fileReader.close();\n" + |
3399 |
" }\n" + |
3400 |
" public static void main(String[] args) {\n" + |
3401 |
" try {\n" + |
3402 |
" new X().foo();\n" + |
3403 |
" } catch (IOException ioex) {\n" + |
3404 |
" System.out.println(\"caught\");\n" + |
3405 |
" }\n" + |
3406 |
" }\n" + |
3407 |
"}\n" |
3408 |
}, |
3409 |
"caught", /*output*/ |
3410 |
null/*classLibs*/, |
3411 |
true/*shouldFlush*/, |
3412 |
null/*vmargs*/, |
3413 |
options, |
3414 |
null/*requestor*/); |
3415 |
} |
3416 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3417 |
// a method uses an AutoCloseable properly within try-with-resources. |
3418 |
public void test055b() { |
3419 |
Map options = getCompilerOptions(); |
3420 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3421 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3422 |
this.runConformTest( |
3423 |
new String[] { |
3424 |
"X.java", |
3425 |
"import java.io.File;\n" + |
3426 |
"import java.io.FileReader;\n" + |
3427 |
"import java.io.IOException;\n" + |
3428 |
"public class X {\n" + |
3429 |
" void foo() throws IOException {\n" + |
3430 |
" File file = new File(\"somefile\");\n" + |
3431 |
" try (FileReader fileReader = new FileReader(file)) {\n" + |
3432 |
" char[] in = new char[50];\n" + |
3433 |
" fileReader.read(in);\n" + |
3434 |
" }\n" + |
3435 |
" }\n" + |
3436 |
" public static void main(String[] args) {\n" + |
3437 |
" try {\n" + |
3438 |
" new X().foo();\n" + |
3439 |
" } catch (IOException ioex) {\n" + |
3440 |
" System.out.println(\"caught\");\n" + |
3441 |
" }\n" + |
3442 |
" }\n" + |
3443 |
"}\n" |
3444 |
}, |
3445 |
"caught", /*output*/ |
3446 |
null/*classLibs*/, |
3447 |
true/*shouldFlush*/, |
3448 |
null/*vmargs*/, |
3449 |
options, |
3450 |
null/*requestor*/); |
3451 |
} |
3452 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3453 |
// a method uses two AutoCloseables (testing independent analysis) |
3454 |
// - one closeable may be unclosed at a conditional return |
3455 |
// - the other is only conditionally closed |
3456 |
public void test055c() { |
3457 |
Map options = getCompilerOptions(); |
3458 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3459 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3460 |
this.runNegativeTest( |
3461 |
new String[] { |
3462 |
"X.java", |
3463 |
"import java.io.File;\n" + |
3464 |
"import java.io.FileReader;\n" + |
3465 |
"import java.io.IOException;\n" + |
3466 |
"public class X {\n" + |
3467 |
" void foo(boolean flag1, boolean flag2) throws IOException {\n" + |
3468 |
" File file = new File(\"somefile\");\n" + |
3469 |
" char[] in = new char[50];\n" + |
3470 |
" FileReader fileReader1 = new FileReader(file);\n" + |
3471 |
" fileReader1.read(in);\n" + |
3472 |
" FileReader fileReader2 = new FileReader(file);\n" + |
3473 |
" fileReader2.read(in);\n" + |
3474 |
" if (flag1) {\n" + |
3475 |
" fileReader2.close();\n" + |
3476 |
" return;\n" + |
3477 |
" } else if (flag2) {\n" + |
3478 |
" fileReader2.close();\n" + |
3479 |
" }\n" + |
3480 |
" fileReader1.close();\n" + |
3481 |
" }\n" + |
3482 |
" public static void main(String[] args) throws IOException {\n" + |
3483 |
" new X().foo(false, true);\n" + |
3484 |
" }\n" + |
3485 |
"}\n" |
3486 |
}, |
3487 |
"----------\n" + |
3488 |
"1. WARNING in X.java (at line 10)\n" + |
3489 |
" FileReader fileReader2 = new FileReader(file);\n" + |
3490 |
" ^^^^^^^^^^^\n" + |
3491 |
"Value of type AutoCloseable is not closed on all paths.\n" + |
3492 |
"----------\n" + |
3493 |
"2. ERROR in X.java (at line 14)\n" + |
3494 |
" return;\n" + |
3495 |
" ^^^^^^^\n" + |
3496 |
"Instance \'fileReader1\' of type AutoCloseable is not closed at this point.\n" + |
3497 |
"----------\n", |
3498 |
null, |
3499 |
true, |
3500 |
options); |
3501 |
} |
3502 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3503 |
// one method returns an AutoCleasble, a second method uses this object without ever closing it. |
3504 |
public void test055d() { |
3505 |
Map options = getCompilerOptions(); |
3506 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3507 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3508 |
this.runNegativeTest( |
3509 |
new String[] { |
3510 |
"X.java", |
3511 |
"import java.io.File;\n" + |
3512 |
"import java.io.FileReader;\n" + |
3513 |
"import java.io.IOException;\n" + |
3514 |
"public class X {\n" + |
3515 |
" FileReader getReader(String filename) throws IOException {\n" + |
3516 |
" File file = new File(\"somefile\");\n" + |
3517 |
" FileReader fileReader = new FileReader(file);\n" + |
3518 |
" return fileReader;\n" + // don't complain here, pass responsibility to caller |
3519 |
" }\n" + |
3520 |
" void foo() throws IOException {\n" + |
3521 |
" FileReader reader = getReader(\"somefile\");\n" + |
3522 |
" char[] in = new char[50];\n" + |
3523 |
" reader.read(in);\n" + |
3524 |
" }\n" + |
3525 |
" public static void main(String[] args) throws IOException {\n" + |
3526 |
" new X().foo();\n" + |
3527 |
" }\n" + |
3528 |
"}\n" |
3529 |
}, |
3530 |
"----------\n" + |
3531 |
"1. ERROR in X.java (at line 11)\n" + |
3532 |
" FileReader reader = getReader(\"somefile\");\n" + |
3533 |
" ^^^^^^\n" + |
3534 |
"Value of type AutoCloseable is not closed neither explicitly nor using a try-with-resources.\n" + |
3535 |
"----------\n", |
3536 |
null, |
3537 |
true, |
3538 |
options); |
3539 |
} |
3540 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3541 |
// a method explicitly closes its AutoCloseable rather than using t-w-r |
3542 |
public void test055e() { |
3543 |
Map options = getCompilerOptions(); |
3544 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3545 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
3546 |
options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.ERROR); |
3547 |
this.runNegativeTest( |
3548 |
new String[] { |
3549 |
"X.java", |
3550 |
"import java.io.File;\n" + |
3551 |
"import java.io.FileReader;\n" + |
3552 |
"import java.io.IOException;\n" + |
3553 |
"public class X {\n" + |
3554 |
" void foo() throws IOException {\n" + |
3555 |
" File file = new File(\"somefile\");\n" + |
3556 |
" FileReader fileReader;\n" + |
3557 |
" fileReader = new FileReader(file);\n" + |
3558 |
" char[] in = new char[50];\n" + |
3559 |
" fileReader.read(in);\n" + |
3560 |
" fileReader.close();\n" + |
3561 |
" }\n" + |
3562 |
" public static void main(String[] args) throws IOException {\n" + |
3563 |
" new X().foo();\n" + |
3564 |
" }\n" + |
3565 |
"}\n" |
3566 |
}, |
3567 |
"----------\n" + |
3568 |
"1. ERROR in X.java (at line 8)\n" + |
3569 |
" fileReader = new FileReader(file);\n" + |
3570 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
3571 |
"Instance \'fileReader\' should be managed by try-with-resource.\n" + |
3572 |
"----------\n", |
3573 |
null, |
3574 |
true, |
3575 |
options); |
3576 |
} |
3577 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
3578 |
// an AutoCloseable local is re-assigned |
3579 |
public void test055f() { |
3580 |
Map options = getCompilerOptions(); |
3581 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
3582 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
3583 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
3584 |
this.runNegativeTest( |
3585 |
new String[] { |
3586 |
"X.java", |
3587 |
"import java.io.File;\n" + |
3588 |
"import java.io.FileReader;\n" + |
3589 |
"import java.io.IOException;\n" + |
3590 |
"public class X {\n" + |
3591 |
" void foo() throws IOException {\n" + |
3592 |
" File file = new File(\"somefile\");\n" + |
3593 |
" FileReader fileReader = new FileReader(file);\n" + |
3594 |
" char[] in = new char[50];\n" + |
3595 |
" fileReader.read(in);\n" + |
3596 |
" fileReader = new FileReader(file);\n" + |
3597 |
" fileReader.read(in);\n" + |
3598 |
" fileReader.close();\n" + |
3599 |
" }\n" + |
3600 |
" public static void main(String[] args) throws IOException {\n" + |
3601 |
" new X().foo();\n" + |
3602 |
" }\n" + |
3603 |
"}\n" |
3604 |
}, |
3605 |
"----------\n" + |
3606 |
"1. ERROR in X.java (at line 10)\n" + |
3607 |
" fileReader = new FileReader(file);\n" + |
3608 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
3609 |
"Instance \'fileReader\' of type AutoCloseable is not closed at this point.\n" + |
3610 |
"----------\n", |
3611 |
null, |
3612 |
true, |
3613 |
options); |
3614 |
} |
3344 |
public static Class testClass() { |
3615 |
public static Class testClass() { |
3345 |
return TryWithResourcesStatementTest.class; |
3616 |
return TryWithResourcesStatementTest.class; |
3346 |
} |
3617 |
} |