Skip to content

Commit 0a723c5

Browse files
committed
Use Java NIO in tests
1 parent 3f7070e commit 0a723c5

File tree

28 files changed

+151
-224
lines changed

28 files changed

+151
-224
lines changed

ant/org.eclipse.ant.tests.ui/Ant Editor Tests/org/eclipse/ant/tests/ui/editor/CodeCompletionTest.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
import static org.junit.Assert.fail;
2929

3030
import java.io.File;
31-
import java.io.FileNotFoundException;
31+
import java.io.IOException;
32+
import java.nio.file.Files;
3233

3334
import javax.xml.parsers.DocumentBuilder;
3435
import javax.xml.parsers.ParserConfigurationException;
@@ -458,12 +459,12 @@ private void assertDoesNotContain(String displayString, ICompletionProposal[] pr
458459
* Tests the property proposals for the case that they are defined in a dependent targets.
459460
*/
460461
@Test
461-
public void testPropertyProposalDefinedInDependantTargets() throws FileNotFoundException {
462+
public void testPropertyProposalDefinedInDependantTargets() throws IOException {
462463
TestTextCompletionProcessor processor = new TestTextCompletionProcessor(getAntModel("dependencytest.xml")); //$NON-NLS-1$
463464

464465
File file = getBuildFile("dependencytest.xml"); //$NON-NLS-1$
465466
processor.setEditedFile(file);
466-
String documentText = getFileContentAsString(file);
467+
String documentText = Files.readString(file.toPath());
467468

468469
processor.setLineNumber(35);
469470
processor.setColumnNumber(41);

ant/org.eclipse.ant.tests.ui/Ant Editor Tests/org/eclipse/ant/tests/ui/editor/formatter/XmlDocumentFormatterTest.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
* https://www.eclipse.org/legal/epl-2.0/
88
*
99
* SPDX-License-Identifier: EPL-2.0
10-
*
10+
*
1111
* Contributors:
1212
* John-Mason P. Shackelford - initial API and implementation
1313
*******************************************************************************/
1414
package org.eclipse.ant.tests.ui.editor.formatter;
1515

1616
import static org.junit.Assert.assertEquals;
1717

18+
import java.nio.file.Files;
19+
1820
import org.eclipse.ant.internal.ui.editor.formatter.FormattingPreferences;
1921
import org.eclipse.ant.internal.ui.editor.formatter.XmlDocumentFormatter;
2022
import org.eclipse.ant.tests.ui.testplugin.AbstractAntUITest;
@@ -92,8 +94,8 @@ private void simpleTest(String sourceFileName, String targetFileName, Formatting
9294

9395
XmlDocumentFormatter xmlFormatter = new XmlDocumentFormatter();
9496
xmlFormatter.setDefaultLineDelimiter(System.getProperty("line.separator")); //$NON-NLS-1$
95-
String result = xmlFormatter.format(getFileContentAsString(getBuildFile(sourceFileName)), prefs);
96-
String expectedResult = getFileContentAsString(getBuildFile(targetFileName));
97+
String result = xmlFormatter.format(Files.readString(getBuildFile(sourceFileName).toPath()), prefs);
98+
String expectedResult = Files.readString(getBuildFile(targetFileName).toPath());
9799

98100
assertEquals(expectedResult, result);
99101
}

ant/org.eclipse.ant.tests.ui/Ant Tests/org/eclipse/ant/tests/ui/BuildTests.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
import static org.junit.Assert.assertTrue;
2121

2222
import java.io.File;
23-
import java.io.FileNotFoundException;
23+
import java.io.IOException;
24+
import java.nio.file.Files;
2425
import java.util.HashMap;
2526
import java.util.Map;
2627

@@ -164,7 +165,7 @@ public void testPropertySubstitution() throws CoreException {
164165
* Tests specifying the XmlLogger as a listener (bug 80435)
165166
*/
166167
@Test
167-
public void testXmlLoggerListener() throws CoreException, FileNotFoundException {
168+
public void testXmlLoggerListener() throws CoreException, IOException {
168169
launch("echoing", "-listener org.apache.tools.ant.XmlLogger"); //$NON-NLS-1$ //$NON-NLS-2$
169170
assertEquals("Incorrect number of messages logged for build. Should be 8. Was " //$NON-NLS-1$
170171
+ ConsoleLineTracker.getNumberOfMessages(), 8, ConsoleLineTracker.getNumberOfMessages());
@@ -174,7 +175,7 @@ public void testXmlLoggerListener() throws CoreException, FileNotFoundException
174175
// find the log file generated by the xml logger
175176
getProject().getFolder("buildfiles").refreshLocal(IResource.DEPTH_INFINITE, null); //$NON-NLS-1$
176177
File file = getBuildFile("log.xml"); //$NON-NLS-1$
177-
String content = getFileContentAsString(file);
178+
String content = Files.readString(file.toPath());
178179
assertTrue("XML logging file is empty", content.length() > 0); //$NON-NLS-1$
179180
}
180181

ant/org.eclipse.ant.tests.ui/Ant Tests/org/eclipse/ant/tests/ui/separateVM/SeparateVMTests.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
import static org.junit.Assert.assertTrue;
2121

2222
import java.io.File;
23-
import java.io.FileNotFoundException;
23+
import java.io.IOException;
24+
import java.nio.file.Files;
2425
import java.util.HashMap;
2526
import java.util.List;
2627
import java.util.Map;
@@ -251,7 +252,7 @@ public void testProjectHelp() throws CoreException {
251252
* Tests specifying the XmlLogger as a listener (bug 80435)
252253
*/
253254
@Test
254-
public void testXmlLoggerListener() throws CoreException, FileNotFoundException {
255+
public void testXmlLoggerListener() throws CoreException, IOException {
255256
launch("echoingSepVM", "-listener org.apache.tools.ant.XmlLogger"); //$NON-NLS-1$ //$NON-NLS-2$
256257
assertLines(6);
257258
assertTrue("Incorrect last message. Should start with Total time:. Message: " //$NON-NLS-1$
@@ -262,7 +263,7 @@ public void testXmlLoggerListener() throws CoreException, FileNotFoundException
262263
IFile iFile = getProject().getFolder("buildfiles").getFile("log.xml"); //$NON-NLS-1$ //$NON-NLS-2$
263264
assertTrue("Could not find log file named: log.xml", iFile.exists()); //$NON-NLS-1$
264265
File file = iFile.getLocation().toFile();
265-
String content = getFileContentAsString(file);
266+
String content = Files.readString(file.toPath());
266267
assertTrue("XML logging file is empty", content.length() > 0); //$NON-NLS-1$
267268
}
268269

ant/org.eclipse.ant.tests.ui/test plugin/org/eclipse/ant/tests/ui/testplugin/AbstractAntUITest.java

+4-19
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@
1919

2020
import java.io.BufferedReader;
2121
import java.io.File;
22-
import java.io.FileInputStream;
23-
import java.io.FileNotFoundException;
2422
import java.io.IOException;
2523
import java.io.InputStream;
2624
import java.io.InputStreamReader;
2725
import java.io.UnsupportedEncodingException;
2826
import java.lang.reflect.InvocationTargetException;
2927
import java.lang.reflect.Method;
28+
import java.nio.file.Files;
3029

3130
import javax.xml.parsers.SAXParser;
3231

@@ -208,15 +207,12 @@ protected static IProject getProject() {
208207
*/
209208
protected IDocument getDocument(String fileName) {
210209
File file = getBuildFile(fileName);
211-
InputStream in;
212210
try {
213-
in = new FileInputStream(file);
214-
}
215-
catch (FileNotFoundException e) {
211+
String initialContent = Files.readString(file.toPath());
212+
return new Document(initialContent);
213+
} catch (IOException e) {
216214
return null;
217215
}
218-
String initialContent = getStreamContentAsString(in);
219-
return new Document(initialContent);
220216
}
221217

222218
/**
@@ -369,17 +365,6 @@ protected ILaunchConfiguration getLaunchConfiguration(String buildFileName) {
369365
return config;
370366
}
371367

372-
/**
373-
* Returns the content of the specified file as <code>String</code>.
374-
*/
375-
protected String getFileContentAsString(File aFile) throws FileNotFoundException {
376-
InputStream stream = new FileInputStream(aFile);
377-
InputStreamReader reader = new InputStreamReader(stream);
378-
BufferedReader bufferedReader = new BufferedReader(reader);
379-
380-
return getReaderContentAsString(bufferedReader);
381-
}
382-
383368
/**
384369
* Parses the given input stream with the given parser using the given handler
385370
*/

resources/tests/org.eclipse.core.tests.resources.saveparticipant1/src/org/eclipse/core/tests/resources/saveparticipant1/SaveParticipant1Plugin.java

+19-4
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,24 @@
1313
*******************************************************************************/
1414
package org.eclipse.core.tests.resources.saveparticipant1;
1515

16-
import java.io.*;
17-
import org.eclipse.core.resources.*;
18-
import org.eclipse.core.runtime.*;
16+
import java.io.DataInputStream;
17+
import java.io.DataOutputStream;
18+
import java.io.FileOutputStream;
19+
import java.io.IOException;
20+
import java.nio.file.Files;
21+
22+
import org.eclipse.core.resources.IResource;
23+
import org.eclipse.core.resources.ISaveContext;
24+
import org.eclipse.core.resources.ISaveParticipant;
25+
import org.eclipse.core.resources.ISavedState;
26+
import org.eclipse.core.resources.IWorkspace;
27+
import org.eclipse.core.resources.ResourcesPlugin;
28+
import org.eclipse.core.runtime.CoreException;
29+
import org.eclipse.core.runtime.IPath;
30+
import org.eclipse.core.runtime.IStatus;
31+
import org.eclipse.core.runtime.MultiStatus;
32+
import org.eclipse.core.runtime.Plugin;
33+
import org.eclipse.core.runtime.Status;
1934
import org.eclipse.core.tests.resources.ResourceDeltaVerifier;
2035

2136
/**
@@ -104,7 +119,7 @@ public void prepareToSave(ISaveContext context) throws CoreException {
104119
*/
105120
private void readExpectedSavedNumber() {
106121
IPath location = getStateLocation().append(SAVE_NUMBER_LOCATION);
107-
try (DataInputStream input = new DataInputStream(new FileInputStream(location.toOSString()))) {
122+
try (DataInputStream input = new DataInputStream(Files.newInputStream(location.toPath()))) {
108123
expectedPreviousSaveNumber = input.readInt();
109124
} catch (IOException e) {
110125
expectedPreviousSaveNumber = 0;

resources/tests/org.eclipse.core.tests.resources.saveparticipant2/src/org/eclipse/core/tests/resources/saveparticipant2/SaveParticipant2Plugin.java

+19-4
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,24 @@
1313
*******************************************************************************/
1414
package org.eclipse.core.tests.resources.saveparticipant2;
1515

16-
import java.io.*;
17-
import org.eclipse.core.resources.*;
18-
import org.eclipse.core.runtime.*;
16+
import java.io.DataInputStream;
17+
import java.io.DataOutputStream;
18+
import java.io.FileOutputStream;
19+
import java.io.IOException;
20+
import java.nio.file.Files;
21+
22+
import org.eclipse.core.resources.IResource;
23+
import org.eclipse.core.resources.ISaveContext;
24+
import org.eclipse.core.resources.ISaveParticipant;
25+
import org.eclipse.core.resources.ISavedState;
26+
import org.eclipse.core.resources.IWorkspace;
27+
import org.eclipse.core.resources.ResourcesPlugin;
28+
import org.eclipse.core.runtime.CoreException;
29+
import org.eclipse.core.runtime.IPath;
30+
import org.eclipse.core.runtime.IStatus;
31+
import org.eclipse.core.runtime.MultiStatus;
32+
import org.eclipse.core.runtime.Plugin;
33+
import org.eclipse.core.runtime.Status;
1934
import org.eclipse.core.tests.resources.ResourceDeltaVerifier;
2035

2136
/**
@@ -115,7 +130,7 @@ public void prepareToSave(ISaveContext context) throws CoreException {
115130
*/
116131
private void readExpectedSavedNumber() {
117132
IPath location = getStateLocation().append(SAVE_NUMBER_LOCATION);
118-
try (DataInputStream input = new DataInputStream(new FileInputStream(location.toOSString()))) {
133+
try (DataInputStream input = new DataInputStream(Files.newInputStream(location.toPath()))) {
119134
expectedPreviousSaveNumber = input.readInt();
120135
} catch (IOException e) {
121136
expectedPreviousSaveNumber = 0;

resources/tests/org.eclipse.core.tests.resources.saveparticipant3/src/org/eclipse/core/tests/resources/saveparticipant3/SaveParticipantPlugin.java

+18-4
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,23 @@
1313
*******************************************************************************/
1414
package org.eclipse.core.tests.resources.saveparticipant3;
1515

16-
import java.io.*;
17-
import org.eclipse.core.resources.*;
18-
import org.eclipse.core.runtime.*;
16+
import java.io.DataInputStream;
17+
import java.io.DataOutputStream;
18+
import java.io.FileOutputStream;
19+
import java.io.IOException;
20+
import java.nio.file.Files;
21+
22+
import org.eclipse.core.resources.ISaveContext;
23+
import org.eclipse.core.resources.ISaveParticipant;
24+
import org.eclipse.core.resources.ISavedState;
25+
import org.eclipse.core.resources.IWorkspace;
26+
import org.eclipse.core.resources.ResourcesPlugin;
27+
import org.eclipse.core.runtime.CoreException;
28+
import org.eclipse.core.runtime.IPath;
29+
import org.eclipse.core.runtime.IStatus;
30+
import org.eclipse.core.runtime.MultiStatus;
31+
import org.eclipse.core.runtime.Plugin;
32+
import org.eclipse.core.runtime.Status;
1933
import org.eclipse.core.tests.resources.ResourceDeltaVerifier;
2034

2135
/**
@@ -99,7 +113,7 @@ public void prepareToSave(ISaveContext context) throws CoreException {
99113
*/
100114
private void readExpectedSavedNumber() {
101115
IPath location = getStateLocation().append(SAVE_NUMBER_LOCATION);
102-
try (DataInputStream input = new DataInputStream(new FileInputStream(location.toOSString()))) {
116+
try (DataInputStream input = new DataInputStream(Files.newInputStream(location.toPath()))) {
103117
expectedPreviousSaveNumber = input.readInt();
104118
} catch (IOException e) {
105119
expectedPreviousSaveNumber = 0;

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/FileCacheTest.java

+4-15
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@
1919
import static org.junit.Assert.assertTrue;
2020

2121
import java.io.File;
22-
import java.io.FileInputStream;
23-
import java.io.FileNotFoundException;
24-
import java.io.IOException;
2522
import java.io.OutputStream;
23+
import java.nio.file.Files;
2624
import org.eclipse.core.filesystem.EFS;
2725
import org.eclipse.core.filesystem.IFileStore;
2826
import org.eclipse.core.runtime.IPath;
@@ -37,15 +35,6 @@
3735
*/
3836
public class FileCacheTest {
3937

40-
/**
41-
* Returns the byte[] contents of the given file.
42-
*/
43-
private byte[] getBytes(File cachedFile) throws FileNotFoundException, IOException {
44-
try (FileInputStream in = new FileInputStream(cachedFile)) {
45-
return in.readAllBytes();
46-
}
47-
}
48-
4938
@Before
5039
public void setUp() throws Exception {
5140
MemoryTree.TREE.deleteAll();
@@ -66,7 +55,7 @@ public void testCacheFile() throws Exception {
6655
File cachedFile = store.toLocalFile(EFS.CACHE, getMonitor());
6756
assertTrue("1.0", cachedFile.exists());
6857
assertTrue("1.1", !cachedFile.isDirectory());
69-
assertThat(getBytes(cachedFile)).containsExactly(contents);
58+
assertThat(Files.readAllBytes(cachedFile.toPath())).containsExactly(contents);
7059

7160
// write out new file contents
7261
byte[] newContents = "newContents".getBytes();
@@ -75,13 +64,13 @@ public void testCacheFile() throws Exception {
7564
}
7665

7766
// old cache will be out of date
78-
assertThat(newContents).isNotEqualTo(getBytes(cachedFile));
67+
assertThat(newContents).isNotEqualTo(Files.readAllBytes(cachedFile.toPath()));
7968

8069
// fetching the cache again should return up to date file
8170
cachedFile = store.toLocalFile(EFS.CACHE, getMonitor());
8271
assertTrue("3.0", cachedFile.exists());
8372
assertTrue("3.1", !cachedFile.isDirectory());
84-
assertThat(getBytes(cachedFile)).containsExactly(newContents);
73+
assertThat(Files.readAllBytes(cachedFile.toPath())).containsExactly(newContents);
8574
}
8675

8776
@Test

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/resources/ProjectPreferencesTest.java

+6-17
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@
3737
import java.io.ByteArrayInputStream;
3838
import java.io.ByteArrayOutputStream;
3939
import java.io.File;
40-
import java.io.FileInputStream;
4140
import java.io.FileOutputStream;
4241
import java.io.FileReader;
4342
import java.io.FileWriter;
4443
import java.io.IOException;
4544
import java.io.InputStream;
4645
import java.io.InputStreamReader;
4746
import java.io.OutputStream;
47+
import java.nio.file.Files;
4848
import java.util.ArrayList;
4949
import java.util.Arrays;
5050
import java.util.List;
@@ -244,10 +244,8 @@ public void testListener() throws Exception {
244244
// get settings filename
245245
File file = getFileInFilesystem(project, qualifier);
246246
Properties props = new Properties();
247-
try (FileInputStream fileInput = new FileInputStream(file)) {
248-
try (InputStream input = new BufferedInputStream(fileInput)) {
249-
props.load(input);
250-
}
247+
try (BufferedReader reader = Files.newBufferedReader(file.toPath())) {
248+
props.load(reader);
251249
}
252250

253251
// change settings in the file
@@ -987,10 +985,8 @@ public void testListenerOnChangeFile() throws Exception {
987985
// get settings filename
988986
File file = getFileInFilesystem(project, qualifier);
989987
Properties props = new Properties();
990-
try (InputStream fileInput = new FileInputStream(file)) {
991-
try (InputStream input = new BufferedInputStream(fileInput)) {
992-
props.load(input);
993-
}
988+
try (BufferedReader input = Files.newBufferedReader(file.toPath())) {
989+
props.load(input);
994990
}
995991

996992
// reset the listener
@@ -1052,16 +1048,9 @@ public void testLoadIsImport() throws Exception {
10521048
node.flush();
10531049
assertEquals("1.00", oldValue, node.get(key, null));
10541050

1055-
byte[] buffer = null;
10561051
// copy the data into a buffer for later use
10571052
File fileInFS = getFileInFilesystem(project, qualifier);
1058-
try (InputStream fileInput = new FileInputStream(fileInFS)) {
1059-
try (InputStream input = new BufferedInputStream(fileInput)) {
1060-
ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
1061-
buffer = output.toByteArray();
1062-
input.transferTo(output);
1063-
}
1064-
}
1053+
byte[] buffer = Files.readAllBytes(fileInFS.toPath());
10651054

10661055
// remove the file from the project
10671056
IFile fileInWS = getFileInWorkspace(project, qualifier);

0 commit comments

Comments
 (0)