33
33
import org .eclipse .core .internal .resources .Project ;
34
34
import org .eclipse .core .internal .resources .ProjectDescription ;
35
35
import org .eclipse .core .internal .resources .ProjectDescriptionReader ;
36
- import org .eclipse .core .internal .resources .Workspace ;
37
36
import org .eclipse .core .resources .ICommand ;
38
37
import org .eclipse .core .resources .IProject ;
39
38
import org .eclipse .core .resources .IResource ;
@@ -283,14 +282,10 @@ private String getLongDescriptionURI() {
283
282
/**
284
283
* Reads and returns the project description stored in the given file store.
285
284
*/
286
- private ProjectDescription readDescription (IFileStore store ) throws CoreException {
287
- InputStream input = null ;
288
- try {
289
- input = store .openInputStream (EFS .NONE , getMonitor ());
285
+ private ProjectDescription readDescription (IFileStore store ) throws CoreException , IOException {
286
+ try (InputStream input = store .openInputStream (EFS .NONE , getMonitor ())) {
290
287
InputSource in = new InputSource (input );
291
288
return new ProjectDescriptionReader (getWorkspace ()).read (in );
292
- } finally {
293
- assertClose (input );
294
289
}
295
290
}
296
291
@@ -342,17 +337,15 @@ public void testInvalidProjectDescription1() throws Throwable {
342
337
IWorkspace workspace = getWorkspace ();
343
338
IPath root = workspace .getRoot ().getLocation ();
344
339
IPath location = root .append ("ModelObjectReaderWriterTest.txt" );
340
+ deleteOnTearDown (location );
341
+
345
342
ProjectDescriptionReader reader = new ProjectDescriptionReader (workspace );
346
343
// Write out the project description file
347
344
ensureDoesNotExistInFileSystem (location .toFile ());
348
345
InputStream stream = new ByteArrayInputStream (invalidProjectDescription .getBytes ());
349
346
createFileInFileSystem (location , stream );
350
- try {
351
- ProjectDescription projDesc = reader .read (location );
352
- assertNull ("1.0" , projDesc );
353
- } finally {
354
- Workspace .clear (location .toFile ());
355
- }
347
+ ProjectDescription projDesc = reader .read (location );
348
+ assertNull (projDesc );
356
349
}
357
350
358
351
public void testInvalidProjectDescription2 () throws Throwable {
@@ -419,19 +412,17 @@ public void testLongProjectDescription() throws Throwable {
419
412
String longProjectDescription = getLongDescription ();
420
413
421
414
IPath location = getRandomLocation ();
422
- try {
423
- ProjectDescriptionReader reader = new ProjectDescriptionReader (getWorkspace ());
424
- // Write out the project description file
425
- ensureDoesNotExistInFileSystem (location .toFile ());
426
- InputStream stream = new ByteArrayInputStream (longProjectDescription .getBytes ());
427
- createFileInFileSystem (location , stream );
428
- ProjectDescription projDesc = reader .read (location );
429
- ensureDoesNotExistInFileSystem (location .toFile ());
430
- for (LinkDescription link : projDesc .getLinks ().values ()) {
431
- assertEquals ("1.0." + link .getProjectRelativePath (), LONG_LOCATION_URI , link .getLocationURI ());
432
- }
433
- } finally {
434
- Workspace .clear (location .toFile ());
415
+ deleteOnTearDown (location );
416
+
417
+ ProjectDescriptionReader reader = new ProjectDescriptionReader (getWorkspace ());
418
+ // Write out the project description file
419
+ ensureDoesNotExistInFileSystem (location .toFile ());
420
+ InputStream stream = new ByteArrayInputStream (longProjectDescription .getBytes ());
421
+ createFileInFileSystem (location , stream );
422
+ ProjectDescription projDesc = reader .read (location );
423
+ ensureDoesNotExistInFileSystem (location .toFile ());
424
+ for (LinkDescription link : projDesc .getLinks ().values ()) {
425
+ assertEquals ("1.0." + link .getProjectRelativePath (), LONG_LOCATION_URI , link .getLocationURI ());
435
426
}
436
427
}
437
428
@@ -441,19 +432,17 @@ public void testLongProjectDescription() throws Throwable {
441
432
public void testLongProjectDescriptionURI () throws Throwable {
442
433
String longProjectDescription = getLongDescriptionURI ();
443
434
IPath location = getRandomLocation ();
444
- try {
445
- ProjectDescriptionReader reader = new ProjectDescriptionReader (ResourcesPlugin .getWorkspace ());
446
- // Write out the project description file
447
- ensureDoesNotExistInFileSystem (location .toFile ());
448
- InputStream stream = new ByteArrayInputStream (longProjectDescription .getBytes ());
449
- createFileInFileSystem (location , stream );
450
- ProjectDescription projDesc = reader .read (location );
451
- ensureDoesNotExistInFileSystem (location .toFile ());
452
- for (LinkDescription link : projDesc .getLinks ().values ()) {
453
- assertEquals ("1.0." + link .getProjectRelativePath (), LONG_LOCATION_URI , link .getLocationURI ());
454
- }
455
- } finally {
456
- Workspace .clear (location .toFile ());
435
+ deleteOnTearDown (location );
436
+
437
+ ProjectDescriptionReader reader = new ProjectDescriptionReader (ResourcesPlugin .getWorkspace ());
438
+ // Write out the project description file
439
+ ensureDoesNotExistInFileSystem (location .toFile ());
440
+ InputStream stream = new ByteArrayInputStream (longProjectDescription .getBytes ());
441
+ createFileInFileSystem (location , stream );
442
+ ProjectDescription projDesc = reader .read (location );
443
+ ensureDoesNotExistInFileSystem (location .toFile ());
444
+ for (LinkDescription link : projDesc .getLinks ().values ()) {
445
+ assertEquals ("1.0." + link .getProjectRelativePath (), LONG_LOCATION_URI , link .getLocationURI ());
457
446
}
458
447
}
459
448
@@ -467,23 +456,22 @@ public void testMultiLineCharFields() throws Throwable {
467
456
IWorkspace workspace = getWorkspace ();
468
457
IPath root = workspace .getRoot ().getLocation ();
469
458
IPath multiLocation = root .append ("multiLineTest.txt" );
459
+ deleteOnTearDown (multiLocation );
470
460
IPath singleLocation = root .append ("singleLineTest.txt" );
461
+ deleteOnTearDown (singleLocation );
462
+
471
463
ProjectDescriptionReader reader = new ProjectDescriptionReader (workspace );
472
464
// Write out the project description file
473
465
ensureDoesNotExistInFileSystem (multiLocation .toFile ());
474
466
ensureDoesNotExistInFileSystem (singleLocation .toFile ());
475
467
InputStream multiStream = new ByteArrayInputStream (multiLineProjectDescription .getBytes ());
476
468
InputStream singleStream = new ByteArrayInputStream (singleLineProjectDescription .getBytes ());
477
- try {
478
- createFileInFileSystem (multiLocation , multiStream );
479
- createFileInFileSystem (singleLocation , singleStream );
480
- ProjectDescription multiDesc = reader .read (multiLocation );
481
- ProjectDescription singleDesc = reader .read (singleLocation );
482
- compareProjectDescriptions (1 , multiDesc , singleDesc );
483
- } finally {
484
- Workspace .clear (multiLocation .toFile ());
485
- Workspace .clear (singleLocation .toFile ());
486
- }
469
+
470
+ createFileInFileSystem (multiLocation , multiStream );
471
+ createFileInFileSystem (singleLocation , singleStream );
472
+ ProjectDescription multiDesc = reader .read (multiLocation );
473
+ ProjectDescription singleDesc = reader .read (singleLocation );
474
+ compareProjectDescriptions (1 , multiDesc , singleDesc );
487
475
}
488
476
489
477
public void testMultipleProjectDescriptions () throws Throwable {
@@ -495,16 +483,11 @@ public void testMultipleProjectDescriptions() throws Throwable {
495
483
for (int i = 0 ; i < members .length ; i ++) {
496
484
URL currentURL = null ;
497
485
currentURL = new URL (whereToLook , members [i ]);
498
- InputStream is = null ;
499
- try {
500
- is = currentURL .openStream ();
501
- } catch (IOException e ) {
502
- fail ("0.5" );
486
+ try (InputStream is = currentURL .openStream ()) {
487
+ InputSource in = new InputSource (is );
488
+ ProjectDescription description = reader .read (in );
489
+ compareProjectDescriptions (i + 1 , description , baselines .get (members [i ]));
503
490
}
504
- InputSource in = new InputSource (is );
505
- ProjectDescription description = reader .read (in );
506
-
507
- compareProjectDescriptions (i + 1 , description , baselines .get (members [i ]));
508
491
}
509
492
}
510
493
@@ -583,14 +566,12 @@ public void testProjectDescription2() throws Throwable {
583
566
}
584
567
585
568
/* test read */
586
- InputStream input = tempStore .openInputStream (EFS .NONE , getMonitor ());
587
569
ProjectDescription description2 ;
588
- try {
570
+ try ( InputStream input = tempStore . openInputStream ( EFS . NONE , getMonitor ())) {
589
571
InputSource in = new InputSource (input );
590
572
description2 = reader .read (in );
591
- } finally {
592
- input .close ();
593
573
}
574
+
594
575
assertTrue ("1.1" , description .getName ().equals (description2 .getName ()));
595
576
assertTrue ("1.2" , location .equals (description .getLocationURI ()));
596
577
@@ -665,18 +646,13 @@ protected URI uriFromPortableString(String pathString) {
665
646
* Writes a project description to a file store
666
647
*/
667
648
private void writeDescription (IFileStore store , ProjectDescription description ) throws IOException , CoreException {
668
- OutputStream output = null ;
669
- try {
670
- output = store .openOutputStream (EFS .NONE , getMonitor ());
649
+ try (OutputStream output = store .openOutputStream (EFS .NONE , getMonitor ())) {
671
650
new ModelObjectWriter ().write (description , output , System .lineSeparator ());
672
- } finally {
673
- assertClose (output );
674
651
}
675
-
676
652
}
677
653
678
654
// Regression for Bug 300669
679
- public void testProjectDescriptionWithFiltersAndNullProject () {
655
+ public void testProjectDescriptionWithFiltersAndNullProject () throws Exception {
680
656
String projectDescription = "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\n " + //
681
657
"<projectDescription>\n " + //
682
658
" <name>rome_dfw</name>\n " + //
@@ -705,18 +681,14 @@ public void testProjectDescriptionWithFiltersAndNullProject() {
705
681
706
682
IPath root = getWorkspace ().getRoot ().getLocation ();
707
683
IPath location = root .append ("ModelObjectReaderWriterTest.txt" );
684
+ deleteOnTearDown (location );
685
+
708
686
ProjectDescriptionReader reader = new ProjectDescriptionReader (getWorkspace ());
709
687
// Write out the project description file
710
688
ensureDoesNotExistInFileSystem (location .toFile ());
711
689
InputStream stream = new ByteArrayInputStream (projectDescription .getBytes ());
712
690
createFileInFileSystem (location , stream );
713
- try {
714
- ProjectDescription projDesc = reader .read (location );
715
- assertNotNull ("1.0" , projDesc );
716
- } catch (IOException e ) {
717
- fail ("1.1" , e );
718
- } finally {
719
- Workspace .clear (location .toFile ());
720
- }
691
+ ProjectDescription projDesc = reader .read (location );
692
+ assertNotNull (projDesc );
721
693
}
722
694
}
0 commit comments