Lines 14-21
Link Here
|
14 |
*******************************************************************************/ |
14 |
*******************************************************************************/ |
15 |
package org.eclipse.jdt.core.tests.compiler.regression; |
15 |
package org.eclipse.jdt.core.tests.compiler.regression; |
16 |
|
16 |
|
|
|
17 |
import java.io.IOException; |
18 |
import java.net.URL; |
17 |
import java.util.Map; |
19 |
import java.util.Map; |
18 |
|
20 |
|
|
|
21 |
import org.eclipse.core.runtime.FileLocator; |
22 |
import org.eclipse.core.runtime.Path; |
23 |
import org.eclipse.core.runtime.Platform; |
19 |
import org.eclipse.jdt.core.JavaCore; |
24 |
import org.eclipse.jdt.core.JavaCore; |
20 |
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; |
25 |
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; |
21 |
|
26 |
|
Lines 23-29
Link Here
|
23 |
public class TryWithResourcesStatementTest extends AbstractRegressionTest { |
28 |
public class TryWithResourcesStatementTest extends AbstractRegressionTest { |
24 |
|
29 |
|
25 |
static { |
30 |
static { |
26 |
// TESTS_NAMES = new String[] { "test056throw"}; |
31 |
// TESTS_NAMES = new String[] { "test061"}; |
27 |
// TESTS_NUMBERS = new int[] { 50 }; |
32 |
// TESTS_NUMBERS = new int[] { 50 }; |
28 |
// TESTS_RANGE = new int[] { 11, -1 }; |
33 |
// TESTS_RANGE = new int[] { 11, -1 }; |
29 |
} |
34 |
} |
Lines 5354-5360
Link Here
|
5354 |
"X::~X\n" + |
5359 |
"X::~X\n" + |
5355 |
"true"); |
5360 |
"true"); |
5356 |
} |
5361 |
} |
5357 |
|
5362 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
|
|
5363 |
// Bug 360908 - Avoid resource leak warning when the underlying/chained resource is closed explicitly |
5364 |
// a resource wrapper is not closed but the underlying resource is |
5365 |
public void test061a() { |
5366 |
Map options = getCompilerOptions(); |
5367 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
5368 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
5369 |
this.runConformTest( |
5370 |
new String[] { |
5371 |
"X.java", |
5372 |
"import java.io.File;\n" + |
5373 |
"import java.io.BufferedInputStream;\n" + |
5374 |
"import java.io.FileInputStream;\n" + |
5375 |
"import java.io.IOException;\n" + |
5376 |
"public class X {\n" + |
5377 |
" void foo() throws IOException {\n" + |
5378 |
" File file = new File(\"somefile\");\n" + |
5379 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
5380 |
" BufferedInputStream bis = new BufferedInputStream(fileStream);\n" + |
5381 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
5382 |
" System.out.println(bis.available());\n" + |
5383 |
" fileStream.close();\n" + |
5384 |
" }\n" + |
5385 |
" void inline() throws IOException {\n" + |
5386 |
" File file = new File(\"somefile\");\n" + |
5387 |
" FileInputStream fileStream;\n" + |
5388 |
" BufferedInputStream bis = new BufferedInputStream(fileStream = new FileInputStream(file));\n" + |
5389 |
" System.out.println(bis.available());\n" + |
5390 |
" fileStream.close();\n" + |
5391 |
" }\n" + |
5392 |
" public static void main(String[] args) throws IOException {\n" + |
5393 |
" try {\n" + |
5394 |
" new X().foo();\n" + |
5395 |
" } catch (IOException ex) {" + |
5396 |
" System.out.println(\"Got IO Exception\");\n" + |
5397 |
" }\n" + |
5398 |
" }\n" + |
5399 |
"}\n" |
5400 |
}, |
5401 |
"Got IO Exception", |
5402 |
null, |
5403 |
true, |
5404 |
null, |
5405 |
options, |
5406 |
null); |
5407 |
} |
5408 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
5409 |
// a closeable without OS resource is not closed |
5410 |
public void test061b() { |
5411 |
Map options = getCompilerOptions(); |
5412 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
5413 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
5414 |
this.runConformTest( |
5415 |
new String[] { |
5416 |
"X.java", |
5417 |
"import java.io.StringReader;\n" + |
5418 |
"import java.io.IOException;\n" + |
5419 |
"public class X {\n" + |
5420 |
" void foo() throws IOException {\n" + |
5421 |
" StringReader string = new StringReader(\"content\");\n" + |
5422 |
" System.out.println(string.read());\n" + |
5423 |
" }\n" + |
5424 |
" public static void main(String[] args) throws IOException {\n" + |
5425 |
" new X().foo();\n" + |
5426 |
" }\n" + |
5427 |
"}\n" |
5428 |
}, |
5429 |
"99", // character 'c' |
5430 |
null, |
5431 |
true, |
5432 |
null, |
5433 |
options, |
5434 |
null); |
5435 |
} |
5436 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
5437 |
// a resource wrapper is not closed but the underlying closeable is resource-free |
5438 |
public void test061c() { |
5439 |
Map options = getCompilerOptions(); |
5440 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
5441 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
5442 |
this.runConformTest( |
5443 |
new String[] { |
5444 |
"X.java", |
5445 |
"import java.io.BufferedReader;\n" + |
5446 |
"import java.io.StringReader;\n" + |
5447 |
"import java.io.IOException;\n" + |
5448 |
"public class X {\n" + |
5449 |
" void foo() throws IOException {\n" + |
5450 |
" StringReader input = new StringReader(\"content\");\n" + |
5451 |
" BufferedReader br = new BufferedReader(input);\n" + |
5452 |
" BufferedReader doubleWrap = new BufferedReader(br);\n" + |
5453 |
" System.out.println(br.read());\n" + |
5454 |
" }\n" + |
5455 |
" void inline() throws IOException {\n" + |
5456 |
" BufferedReader br = new BufferedReader(new StringReader(\"content\"));\n" + |
5457 |
" System.out.println(br.read());\n" + |
5458 |
" }\n" + |
5459 |
" public static void main(String[] args) throws IOException {\n" + |
5460 |
" new X().foo();\n" + |
5461 |
" }\n" + |
5462 |
"}\n" |
5463 |
}, |
5464 |
"99", |
5465 |
null, |
5466 |
true, |
5467 |
null, |
5468 |
options, |
5469 |
null); |
5470 |
} |
5471 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
5472 |
// a resource wrapper is not closed neither is the underlying resource |
5473 |
public void test061d() { |
5474 |
Map options = getCompilerOptions(); |
5475 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
5476 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
5477 |
this.runNegativeTest( |
5478 |
new String[] { |
5479 |
"X.java", |
5480 |
"import java.io.File;\n" + |
5481 |
"import java.io.BufferedInputStream;\n" + |
5482 |
"import java.io.FileInputStream;\n" + |
5483 |
"import java.io.IOException;\n" + |
5484 |
"public class X {\n" + |
5485 |
" void foo() throws IOException {\n" + |
5486 |
" File file = new File(\"somefile\");\n" + |
5487 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
5488 |
" BufferedInputStream bis = new BufferedInputStream(fileStream);\n" + |
5489 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
5490 |
" System.out.println(bis.available());\n" + |
5491 |
" }\n" + |
5492 |
" void inline() throws IOException {\n" + |
5493 |
" File file = new File(\"somefile\");\n" + |
5494 |
" BufferedInputStream bis2 = new BufferedInputStream(new FileInputStream(file));\n" + |
5495 |
" System.out.println(bis2.available());\n" + |
5496 |
" }\n" + |
5497 |
" public static void main(String[] args) throws IOException {\n" + |
5498 |
" try {\n" + |
5499 |
" new X().foo();\n" + |
5500 |
" } catch (IOException ex) {" + |
5501 |
" System.out.println(\"Got IO Exception\");\n" + |
5502 |
" }\n" + |
5503 |
" }\n" + |
5504 |
"}\n" |
5505 |
}, |
5506 |
"----------\n" + |
5507 |
"1. ERROR in X.java (at line 10)\n" + |
5508 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
5509 |
" ^^^^^^^^^^\n" + |
5510 |
"Resource leak: \'doubleWrap\' is never closed\n" + |
5511 |
"----------\n" + |
5512 |
"2. ERROR in X.java (at line 15)\n" + |
5513 |
" BufferedInputStream bis2 = new BufferedInputStream(new FileInputStream(file));\n" + |
5514 |
" ^^^^\n" + |
5515 |
"Resource leak: \'bis2\' is never closed\n" + |
5516 |
"----------\n", |
5517 |
null, |
5518 |
true, |
5519 |
options); |
5520 |
} |
5521 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
5522 |
// Bug 361073 - Avoid resource leak warning when the top level resource is closed explicitly |
5523 |
//a resource wrapper is closed closing also the underlying resource |
5524 |
public void test061e() { |
5525 |
Map options = getCompilerOptions(); |
5526 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
5527 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
5528 |
this.runConformTest( |
5529 |
new String[] { |
5530 |
"X.java", |
5531 |
"import java.io.File;\n" + |
5532 |
"import java.io.BufferedInputStream;\n" + |
5533 |
"import java.io.FileInputStream;\n" + |
5534 |
"import java.io.IOException;\n" + |
5535 |
"public class X {\n" + |
5536 |
" FileInputStream fis;" + |
5537 |
" void foo() throws IOException {\n" + |
5538 |
" File file = new File(\"somefile\");\n" + |
5539 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
5540 |
" BufferedInputStream bis = new BufferedInputStream(fileStream);\n" + |
5541 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
5542 |
" System.out.println(bis.available());\n" + |
5543 |
" bis.close();\n" + |
5544 |
" }\n" + |
5545 |
" void inline() throws IOException {\n" + |
5546 |
" File file = new File(\"somefile\");\n" + |
5547 |
" BufferedInputStream bis2 = new BufferedInputStream(fis = new FileInputStream(file));\n" + // field assignment |
5548 |
" System.out.println(bis2.available());\n" + |
5549 |
" bis2.close();\n" + |
5550 |
" FileInputStream fileStream = null;\n" + |
5551 |
" BufferedInputStream bis3 = new BufferedInputStream(fileStream = new FileInputStream(file));\n" + |
5552 |
" System.out.println(bis3.available());\n" + |
5553 |
" bis3.close();\n" + |
5554 |
" }\n" + |
5555 |
" public static void main(String[] args) throws IOException {\n" + |
5556 |
" try {\n" + |
5557 |
" new X().foo();\n" + |
5558 |
" } catch (IOException ex) {" + |
5559 |
" System.out.println(\"Got IO Exception\");\n" + |
5560 |
" }\n" + |
5561 |
" }\n" + |
5562 |
"}\n" |
5563 |
}, |
5564 |
"Got IO Exception", |
5565 |
null, |
5566 |
true, |
5567 |
null, |
5568 |
options, |
5569 |
null); |
5570 |
} |
5571 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
5572 |
// Bug 361073 - Avoid resource leak warning when the top level resource is closed explicitly |
5573 |
// a resource wrapper is closed closing also the underlying resource - original test case |
5574 |
public void test061f() throws IOException { |
5575 |
Map options = getCompilerOptions(); |
5576 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
5577 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
5578 |
URL url = FileLocator.toFileURL(FileLocator.find(Platform.getBundle("org.eclipse.jdt.core.tests.compiler"), new Path("META-INF/MANIFEST.MF"), null)); |
5579 |
this.runConformTest( |
5580 |
new String[] { |
5581 |
"X.java", |
5582 |
"import java.io.InputStream;\n" + |
5583 |
"import java.io.InputStreamReader;\n" + |
5584 |
"import java.io.BufferedReader;\n" + |
5585 |
"import java.io.IOException;\n" + |
5586 |
"import java.net.URL;\n" + |
5587 |
"public class X {\n" + |
5588 |
" boolean loadURL(final URL url) throws IOException {\n" + |
5589 |
" InputStream stream = null;\n" + |
5590 |
" BufferedReader reader = null;\n" + |
5591 |
" try {\n" + |
5592 |
" stream = url.openStream();\n" + |
5593 |
" reader = new BufferedReader(new InputStreamReader(stream));\n" + |
5594 |
" System.out.println(reader.readLine());\n" + |
5595 |
" } finally {\n" + |
5596 |
" try {\n" + |
5597 |
" if (reader != null)\n" + |
5598 |
" reader.close();\n" + |
5599 |
" } catch (IOException x) {\n" + |
5600 |
" }\n" + |
5601 |
" }\n" + |
5602 |
" return false; // 'stream' may not be closed at this location\n" + |
5603 |
" }\n" + |
5604 |
" public static void main(String[] args) throws IOException {\n" + |
5605 |
" try {\n" + |
5606 |
" new X().loadURL(new URL(\""+url.toString()+"\"));\n" + |
5607 |
" } catch (IOException ex) {\n" + |
5608 |
" System.out.println(\"Got IO Exception\"+ex);\n" + |
5609 |
" }\n" + |
5610 |
" }\n" + |
5611 |
"}\n" |
5612 |
}, |
5613 |
"Manifest-Version: 1.0", |
5614 |
null, |
5615 |
true, |
5616 |
null, |
5617 |
options, |
5618 |
null); |
5619 |
} |
5620 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
5621 |
// Bug 360908 - Avoid resource leak warning when the underlying/chained resource is closed explicitly |
5622 |
// Different points in a resource chain are closed |
5623 |
public void test061g() { |
5624 |
Map options = getCompilerOptions(); |
5625 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
5626 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
5627 |
this.runNegativeTest( |
5628 |
new String[] { |
5629 |
"X.java", |
5630 |
"import java.io.File;\n" + |
5631 |
"import java.io.BufferedInputStream;\n" + |
5632 |
"import java.io.FileInputStream;\n" + |
5633 |
"import java.io.IOException;\n" + |
5634 |
"public class X {\n" + |
5635 |
" void closeMiddle() throws IOException {\n" + |
5636 |
" File file = new File(\"somefile\");\n" + |
5637 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
5638 |
" BufferedInputStream bis = new BufferedInputStream(fileStream);\n" + |
5639 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
5640 |
" System.out.println(bis.available());\n" + |
5641 |
" bis.close();\n" + |
5642 |
" }\n" + |
5643 |
" void closeOuter() throws IOException {\n" + |
5644 |
" File file2 = new File(\"somefile\");\n" + |
5645 |
" FileInputStream fileStream2 = new FileInputStream(file2);\n" + |
5646 |
" BufferedInputStream bis2 = new BufferedInputStream(fileStream2);\n" + |
5647 |
" BufferedInputStream doubleWrap2 = new BufferedInputStream(bis2);\n" + |
5648 |
" System.out.println(bis2.available());\n" + |
5649 |
" doubleWrap2.close();\n" + |
5650 |
" }\n" + |
5651 |
" void neverClosed() throws IOException {\n" + |
5652 |
" File file3 = new File(\"somefile\");\n" + |
5653 |
" FileInputStream fileStream3 = new FileInputStream(file3);\n" + |
5654 |
" BufferedInputStream bis3 = new BufferedInputStream(fileStream3);\n" + |
5655 |
" BufferedInputStream doubleWrap3 = new BufferedInputStream(bis3);\n" + |
5656 |
" System.out.println(doubleWrap3.available());\n" + |
5657 |
" }\n" + |
5658 |
"}\n" |
5659 |
}, |
5660 |
"----------\n" + |
5661 |
"1. ERROR in X.java (at line 26)\n" + |
5662 |
" BufferedInputStream doubleWrap3 = new BufferedInputStream(bis3);\n" + |
5663 |
" ^^^^^^^^^^^\n" + |
5664 |
"Resource leak: \'doubleWrap3\' is never closed\n" + |
5665 |
"----------\n", |
5666 |
null, |
5667 |
true, |
5668 |
options); |
5669 |
} |
5670 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
5671 |
// Bug 360908 - Avoid resource leak warning when the underlying/chained resource is closed explicitly |
5672 |
// Different points in a resource chain are potentially closed |
5673 |
public void test061h() { |
5674 |
Map options = getCompilerOptions(); |
5675 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
5676 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
5677 |
this.runNegativeTest( |
5678 |
new String[] { |
5679 |
"X.java", |
5680 |
"import java.io.File;\n" + |
5681 |
"import java.io.BufferedInputStream;\n" + |
5682 |
"import java.io.FileInputStream;\n" + |
5683 |
"import java.io.IOException;\n" + |
5684 |
"public class X {\n" + |
5685 |
" void closeMiddle(boolean b) throws IOException {\n" + |
5686 |
" File file = new File(\"somefile\");\n" + |
5687 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
5688 |
" BufferedInputStream bis = new BufferedInputStream(fileStream);\n" + |
5689 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
5690 |
" System.out.println(bis.available());\n" + |
5691 |
" if (b)\n" + |
5692 |
" bis.close();\n" + |
5693 |
" }\n" + |
5694 |
" void closeOuter(boolean b) throws IOException {\n" + |
5695 |
" File file2 = new File(\"somefile\");\n" + |
5696 |
" FileInputStream fileStream2 = new FileInputStream(file2);\n" + |
5697 |
" BufferedInputStream bis2 = new BufferedInputStream(fileStream2);\n" + |
5698 |
" BufferedInputStream doubleWrap2 = new BufferedInputStream(bis2);\n" + |
5699 |
" System.out.println(bis2.available());\n" + |
5700 |
" if (b)\n" + |
5701 |
" doubleWrap2.close();\n" + |
5702 |
" }\n" + |
5703 |
" void potAndDef(boolean b) throws IOException {\n" + |
5704 |
" File file3 = new File(\"somefile\");\n" + |
5705 |
" FileInputStream fileStream3 = new FileInputStream(file3);\n" + |
5706 |
" BufferedInputStream bis3 = new BufferedInputStream(fileStream3);\n" + |
5707 |
" BufferedInputStream doubleWrap3 = new BufferedInputStream(bis3);\n" + |
5708 |
" System.out.println(doubleWrap3.available());\n" + |
5709 |
" if (b) bis3.close();\n" + |
5710 |
" fileStream3.close();\n" + |
5711 |
" }\n" + |
5712 |
"}\n" |
5713 |
}, |
5714 |
"----------\n" + |
5715 |
"1. ERROR in X.java (at line 10)\n" + |
5716 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
5717 |
" ^^^^^^^^^^\n" + |
5718 |
"Potential resource leak: \'doubleWrap\' may not be closed\n" + |
5719 |
"----------\n" + |
5720 |
"2. ERROR in X.java (at line 19)\n" + |
5721 |
" BufferedInputStream doubleWrap2 = new BufferedInputStream(bis2);\n" + |
5722 |
" ^^^^^^^^^^^\n" + |
5723 |
"Potential resource leak: \'doubleWrap2\' may not be closed\n" + |
5724 |
"----------\n", |
5725 |
null, |
5726 |
true, |
5727 |
options); |
5728 |
} |
5358 |
public static Class testClass() { |
5729 |
public static Class testClass() { |
5359 |
return TryWithResourcesStatementTest.class; |
5730 |
return TryWithResourcesStatementTest.class; |
5360 |
} |
5731 |
} |