Skip to main content
aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorCarsten Hammer2019-05-25 13:38:32 +0000
committerKarsten Thoms2019-09-25 18:52:27 +0000
commit46ff79e0c747ec2308cff542015e4b82d6882472 (patch)
tree8402cbf7e54a5fa2c1adb7f9bdd3fbd604ddfd25 /tests
parentc869c9dfb29c62db5a39b440b425eea6bb85caac (diff)
downloadeclipse.platform.team-46ff79e0c747ec2308cff542015e4b82d6882472.tar.gz
eclipse.platform.team-46ff79e0c747ec2308cff542015e4b82d6882472.tar.xz
eclipse.platform.team-46ff79e0c747ec2308cff542015e4b82d6882472.zip
Use try-with-resourcesI20190926-0625
Convert try finally block to try-with-resources Change-Id: Iff35bb041daf77cd29f9c17ee931c8b2de186e4c Signed-off-by: Carsten Hammer <carsten.hammer@t-online.de>
Diffstat (limited to 'tests')
-rw-r--r--tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/CVSTestSetup.java45
-rw-r--r--tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/EclipseTest.java5
-rw-r--r--tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/JUnitTestCase.java5
-rw-r--r--tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/compatible/SameResultEnv.java14
-rw-r--r--tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/provider/ModuleTest.java5
-rw-r--r--tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/provider/ResourceDeltaTest.java5
-rw-r--r--tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/ui/ProjectSetImporterTests.java20
-rw-r--r--tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/ui/benchmark/BenchmarkUtils.java33
-rw-r--r--tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/ui/benchmark/Bug152581Test.java5
9 files changed, 55 insertions, 82 deletions
diff --git a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/CVSTestSetup.java b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/CVSTestSetup.java
index bc3180255..1f76a79d0 100644
--- a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/CVSTestSetup.java
+++ b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/CVSTestSetup.java
@@ -13,17 +13,30 @@
*******************************************************************************/
package org.eclipse.team.tests.ccvs.core;
-import java.io.*;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.PrintStream;
-import junit.extensions.TestSetup;
-import junit.framework.Test;
-
-import org.eclipse.core.runtime.*;
-import org.eclipse.team.internal.ccvs.core.*;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.OperationCanceledException;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.team.internal.ccvs.core.CVSException;
+import org.eclipse.team.internal.ccvs.core.CVSProviderPlugin;
+import org.eclipse.team.internal.ccvs.core.CVSStatus;
+import org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation;
import org.eclipse.team.internal.ccvs.core.connection.CVSCommunicationException;
import org.eclipse.team.internal.ccvs.core.connection.CVSRepositoryLocation;
import org.eclipse.team.internal.ccvs.core.util.KnownRepositories;
+import junit.extensions.TestSetup;
+import junit.framework.Test;
+
public class CVSTestSetup extends TestSetup {
public static final String REPOSITORY_LOCATION;
public static final boolean INITIALIZE_REPO;
@@ -62,18 +75,14 @@ public class CVSTestSetup extends TestSetup {
if (propertiesFile == null) return;
File file = new File(propertiesFile);
if (file.isDirectory()) file = new File(file, "repository.properties");
- try {
- BufferedReader reader = new BufferedReader(new FileReader(file));
- try {
- for (String line; (line = reader.readLine()) != null; ) {
- if (line.startsWith("#")) continue;
- int sep = line.indexOf("=");
- String property = line.substring(0, sep).trim();
- String value = line.substring(sep + 1).trim();
- System.setProperty("eclipse.cvs." + property, value);
- }
- } finally {
- reader.close();
+ try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
+ for (String line; (line = reader.readLine()) != null;) {
+ if (line.startsWith("#"))
+ continue;
+ int sep = line.indexOf("=");
+ String property = line.substring(0, sep).trim();
+ String value = line.substring(sep + 1).trim();
+ System.setProperty("eclipse.cvs." + property, value);
}
} catch (Exception e) {
System.err.println("Could not read repository properties file: " + file.getAbsolutePath());
diff --git a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/EclipseTest.java b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/EclipseTest.java
index a6cdf88e1..6c22b010d 100644
--- a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/EclipseTest.java
+++ b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/EclipseTest.java
@@ -271,12 +271,9 @@ public class EclipseTest extends ResourceTest {
public static String getFileContents(IFile file) throws IOException, CoreException {
StringBuilder buf = new StringBuilder();
- Reader reader = new InputStreamReader(new BufferedInputStream(file.getContents()));
- try {
+ try (Reader reader = new InputStreamReader(new BufferedInputStream(file.getContents()))) {
int c;
while ((c = reader.read()) != -1) buf.append((char)c);
- } finally {
- reader.close();
}
return buf.toString();
}
diff --git a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/JUnitTestCase.java b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/JUnitTestCase.java
index 5e018d788..2cc2d7bbf 100644
--- a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/JUnitTestCase.java
+++ b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/JUnitTestCase.java
@@ -144,8 +144,7 @@ public abstract class JUnitTestCase extends TestCase {
protected static void writeToFile(IFile file, String[] contents)
throws IOException, CoreException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
- PrintStream os = new PrintStream(bos);
- try {
+ try (PrintStream os = new PrintStream(bos)) {
for (String content : contents) {
os.println(content);
}
@@ -156,8 +155,6 @@ public abstract class JUnitTestCase extends TestCase {
mkdirs(file.getParent());
file.create(bis, false /*force*/, null);
}
- } finally {
- os.close();
}
}
diff --git a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/compatible/SameResultEnv.java b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/compatible/SameResultEnv.java
index 1d72833ac..fe7aed3c4 100644
--- a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/compatible/SameResultEnv.java
+++ b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/compatible/SameResultEnv.java
@@ -369,22 +369,18 @@ public final class SameResultEnv extends JUnitTestCase {
assertEquals(mFile1.isReadOnly(), mFile2.isReadOnly());
// Compare the content of the files
- try {
- InputStream in1 = mFile1.getContents();
- InputStream in2 = mFile2.getContents();
- byte[] buffer1 = new byte[(int)mFile1.getSize()];
- byte[] buffer2 = new byte[(int)mFile2.getSize()];
+ byte[] buffer1 = new byte[(int) mFile1.getSize()];
+ byte[] buffer2 = new byte[(int) mFile2.getSize()];
+ try (InputStream in1 = mFile1.getContents(); InputStream in2 = mFile2.getContents()) {
// This is not the right way to do it, because the Stream
// may read less than the whole file
in1.read(buffer1);
in2.read(buffer2);
- in1.close();
- in2.close();
- assertEquals("Length differs for file " + mFile1.getName(), buffer1.length, buffer2.length);
- assertEquals("Contents differs for file " + mFile1.getName(), new String(buffer1),new String(buffer2));
} catch (IOException e) {
throw new CVSException("Error in TestCase");
}
+ assertEquals("Length differs for file " + mFile1.getName(), buffer1.length, buffer2.length);
+ assertEquals("Contents differs for file " + mFile1.getName(), new String(buffer1),new String(buffer2));
// We can not do the check, because the reference client does
// check out dirty files ?!?
diff --git a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/provider/ModuleTest.java b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/provider/ModuleTest.java
index 80cfcf3c1..b3a0b3bd3 100644
--- a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/provider/ModuleTest.java
+++ b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/provider/ModuleTest.java
@@ -82,11 +82,8 @@ public class ModuleTest extends EclipseTest {
waitMsec(1000);
IProject cvsroot = checkoutProject(null, "CVSROOT", null);
- InputStream in = url.openStream();
- try {
+ try (InputStream in = url.openStream()) {
cvsroot.getFile("modules").setContents(in, false, false, DEFAULT_MONITOR);
- } finally {
- in.close();
}
commitProject(cvsroot);
diff --git a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/provider/ResourceDeltaTest.java b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/provider/ResourceDeltaTest.java
index c4a03dcff..cc44611b9 100644
--- a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/provider/ResourceDeltaTest.java
+++ b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/core/provider/ResourceDeltaTest.java
@@ -156,15 +156,12 @@ public class ResourceDeltaTest extends EclipseTest {
// wait to ensure the timestamp differs from the one Core has
waitMsec(1500);
InputStream in = new BufferedInputStream(getRandomContents());
- OutputStream out = new BufferedOutputStream(new FileOutputStream(ioFile));
- try {
+ try (OutputStream out = new BufferedOutputStream(new FileOutputStream(ioFile))) {
int next = in.read();
while (next != -1) {
out.write(next);
next = in.read();
}
- } finally {
- out.close();
}
}
diff --git a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/ui/ProjectSetImporterTests.java b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/ui/ProjectSetImporterTests.java
index 1012510c3..8e91640bc 100644
--- a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/ui/ProjectSetImporterTests.java
+++ b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/ui/ProjectSetImporterTests.java
@@ -94,10 +94,8 @@ public class ProjectSetImporterTests extends EclipseTest {
IProject project2 = createProject("testImportProject",
new String[] { "file.txt", "folder1/", "folder1/a.txt" });
- PrintWriter out = null;
- try {
- out = new PrintWriter(new BufferedWriter(new FileWriter(PSF_FILE)),
- true);
+ try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(PSF_FILE)),
+ true)) {
out.println(psf_header_0);
out.println(psf_header_1);
@@ -123,9 +121,6 @@ public class ProjectSetImporterTests extends EclipseTest {
fail("1.", e.getCause());
} catch (IOException e) {
fail("2.", e);
- } finally {
- if (out != null)
- out.close();
}
}
@@ -136,11 +131,9 @@ public class ProjectSetImporterTests extends EclipseTest {
IProject project2 = createProject("testBug234149_aFewProviders",
new String[0]);
- // create psf with two providers
- PrintWriter out = null;
- try {
- out = new PrintWriter(new BufferedWriter(new FileWriter(PSF_FILE)),
- true);
+ try ( // create psf with two providers
+ PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(PSF_FILE)),
+ true)) {
// add first provider to psf
out.println(psf_header_0);
@@ -172,9 +165,6 @@ public class ProjectSetImporterTests extends EclipseTest {
fail("1.", e.getCause());
} catch (IOException e) {
fail("2.", e);
- } finally {
- if (out != null)
- out.close();
}
}
diff --git a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/ui/benchmark/BenchmarkUtils.java b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/ui/benchmark/BenchmarkUtils.java
index e86cb12a6..0da269355 100644
--- a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/ui/benchmark/BenchmarkUtils.java
+++ b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/ui/benchmark/BenchmarkUtils.java
@@ -156,18 +156,19 @@ public class BenchmarkUtils {
fileSize = (int) Math.abs(gen.nextGaussian() * variance + meanSize);
} while (fileSize > meanSize + variance * 4); // avoid huge files
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- String fileName;
- if (gen.nextInt(100) < probBinary) {
- fileName = makeUniqueName(gen, "file", "class"); // binary
- writeRandomBytes(gen, os, fileSize);
- } else {
- fileName = makeUniqueName(gen, "file", "txt"); // text
- writeRandomText(gen, os, fileSize);
+ IFile file;
+ try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
+ String fileName;
+ if (gen.nextInt(100) < probBinary) {
+ fileName = makeUniqueName(gen, "file", "class"); // binary
+ writeRandomBytes(gen, os, fileSize);
+ } else {
+ fileName = makeUniqueName(gen, "file", "txt"); // text
+ writeRandomText(gen, os, fileSize);
+ }
+ file = parent.getFile(new Path(fileName));
+ file.create(new ByteArrayInputStream(os.toByteArray()), true, new NullProgressMonitor());
}
- IFile file = parent.getFile(new Path(fileName));
- file.create(new ByteArrayInputStream(os.toByteArray()), true, new NullProgressMonitor());
- os.close();
return file;
}
@@ -221,10 +222,7 @@ public class BenchmarkUtils {
*/
public static void modifyFile(SequenceGenerator gen, IFile file)
throws IOException, CoreException {
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- try {
- InputStream is = file.getContents(true);
- try {
+ try (ByteArrayOutputStream os = new ByteArrayOutputStream(); InputStream is = file.getContents(true)) {
byte[] buffer = new byte[8192];
int rsize;
boolean changed = false;
@@ -240,11 +238,6 @@ public class BenchmarkUtils {
}
if (! changed) os.write('!'); // make sure we actually did change the file
file.setContents(new ByteArrayInputStream(os.toByteArray()), false /*force*/, true /*keepHistory*/, null);
- } finally {
- is.close();
- }
- } finally {
- os.close();
}
}
diff --git a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/ui/benchmark/Bug152581Test.java b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/ui/benchmark/Bug152581Test.java
index 239b09c32..79f82a94c 100644
--- a/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/ui/benchmark/Bug152581Test.java
+++ b/tests/org.eclipse.team.tests.cvs.core/src/org/eclipse/team/tests/ccvs/ui/benchmark/Bug152581Test.java
@@ -93,8 +93,7 @@ public class Bug152581Test extends BenchmarkTest {
private IProject createProject(String filename) throws IOException, CoreException {
File file = BenchmarkTestSetup.getTestFile(filename + ".txt");
- InputStream content = getContents(file, "Could not read seed file " + filename + ".txt");
- try {
+ try (InputStream content = getContents(file, "Could not read seed file " + filename + ".txt")) {
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
IProject project = getUniqueTestProject(filename);
populateProject(reader, project);
@@ -102,8 +101,6 @@ public class Bug152581Test extends BenchmarkTest {
// Perform an update to prune any empty directories
updateProject(project, null, false);
return project;
- } finally {
- content.close();
}
}

Back to the top