Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Java NIO in tests #1679

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
import static org.junit.Assert.fail;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;

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

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

processor.setLineNumber(35);
processor.setColumnNumber(41);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
*
* Contributors:
* John-Mason P. Shackelford - initial API and implementation
*******************************************************************************/
package org.eclipse.ant.tests.ui.editor.formatter;

import static org.junit.Assert.assertEquals;

import java.nio.file.Files;

import org.eclipse.ant.internal.ui.editor.formatter.FormattingPreferences;
import org.eclipse.ant.internal.ui.editor.formatter.XmlDocumentFormatter;
import org.eclipse.ant.tests.ui.testplugin.AbstractAntUITest;
Expand Down Expand Up @@ -92,8 +94,8 @@ private void simpleTest(String sourceFileName, String targetFileName, Formatting

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

assertEquals(expectedResult, result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -251,7 +252,7 @@ public void testProjectHelp() throws CoreException {
* Tests specifying the XmlLogger as a listener (bug 80435)
*/
@Test
public void testXmlLoggerListener() throws CoreException, FileNotFoundException {
public void testXmlLoggerListener() throws CoreException, IOException {
launch("echoingSepVM", "-listener org.apache.tools.ant.XmlLogger"); //$NON-NLS-1$ //$NON-NLS-2$
assertLines(6);
assertTrue("Incorrect last message. Should start with Total time:. Message: " //$NON-NLS-1$
Expand All @@ -262,7 +263,7 @@ public void testXmlLoggerListener() throws CoreException, FileNotFoundException
IFile iFile = getProject().getFolder("buildfiles").getFile("log.xml"); //$NON-NLS-1$ //$NON-NLS-2$
assertTrue("Could not find log file named: log.xml", iFile.exists()); //$NON-NLS-1$
File file = iFile.getLocation().toFile();
String content = getFileContentAsString(file);
String content = Files.readString(file.toPath());
assertTrue("XML logging file is empty", content.length() > 0); //$NON-NLS-1$
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;

import javax.xml.parsers.SAXParser;

Expand Down Expand Up @@ -208,15 +207,12 @@ protected static IProject getProject() {
*/
protected IDocument getDocument(String fileName) {
File file = getBuildFile(fileName);
InputStream in;
try {
in = new FileInputStream(file);
}
catch (FileNotFoundException e) {
String initialContent = Files.readString(file.toPath());
return new Document(initialContent);
} catch (IOException e) {
return null;
}
String initialContent = getStreamContentAsString(in);
return new Document(initialContent);
}

/**
Expand Down Expand Up @@ -369,17 +365,6 @@ protected ILaunchConfiguration getLaunchConfiguration(String buildFileName) {
return config;
}

/**
* Returns the content of the specified file as <code>String</code>.
*/
protected String getFileContentAsString(File aFile) throws FileNotFoundException {
InputStream stream = new FileInputStream(aFile);
InputStreamReader reader = new InputStreamReader(stream);
BufferedReader bufferedReader = new BufferedReader(reader);

return getReaderContentAsString(bufferedReader);
}

/**
* Parses the given input stream with the given parser using the given handler
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Automatic-Module-Name: org.eclipse.core.tests.resources.saveparticipant1
Bundle-Name: Save Participant 1
Bundle-Activator: org.eclipse.core.tests.resources.saveparticipant1.SaveParticipant1Plugin
Bundle-SymbolicName: org.eclipse.core.tests.resources.saveparticipant1
Bundle-Version: 3.6.100.qualifier
Bundle-Version: 3.6.200.qualifier
Bundle-Vendor: Eclipse.org
Export-Package: org.eclipse.core.tests.resources.saveparticipant1
Require-Bundle: org.eclipse.core.resources,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,24 @@
*******************************************************************************/
package org.eclipse.core.tests.resources.saveparticipant1;

import java.io.*;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ISaveContext;
import org.eclipse.core.resources.ISaveParticipant;
import org.eclipse.core.resources.ISavedState;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.tests.resources.ResourceDeltaVerifier;

/**
Expand Down Expand Up @@ -104,7 +119,7 @@ public void prepareToSave(ISaveContext context) throws CoreException {
*/
private void readExpectedSavedNumber() {
IPath location = getStateLocation().append(SAVE_NUMBER_LOCATION);
try (DataInputStream input = new DataInputStream(new FileInputStream(location.toOSString()))) {
try (DataInputStream input = new DataInputStream(Files.newInputStream(location.toPath()))) {
expectedPreviousSaveNumber = input.readInt();
} catch (IOException e) {
expectedPreviousSaveNumber = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Bundle-ManifestVersion: 2
Automatic-Module-Name: org.eclipse.core.tests.resources.saveparticipant2
Bundle-Name: Save Participant 2
Bundle-SymbolicName: org.eclipse.core.tests.resources.saveparticipant2
Bundle-Version: 3.6.100.qualifier
Bundle-Version: 3.6.200.qualifier
Bundle-Vendor: Eclipse.org
Export-Package: org.eclipse.core.tests.resources.saveparticipant2
Require-Bundle: org.eclipse.core.resources,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,24 @@
*******************************************************************************/
package org.eclipse.core.tests.resources.saveparticipant2;

import java.io.*;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ISaveContext;
import org.eclipse.core.resources.ISaveParticipant;
import org.eclipse.core.resources.ISavedState;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.tests.resources.ResourceDeltaVerifier;

/**
Expand Down Expand Up @@ -115,7 +130,7 @@ public void prepareToSave(ISaveContext context) throws CoreException {
*/
private void readExpectedSavedNumber() {
IPath location = getStateLocation().append(SAVE_NUMBER_LOCATION);
try (DataInputStream input = new DataInputStream(new FileInputStream(location.toOSString()))) {
try (DataInputStream input = new DataInputStream(Files.newInputStream(location.toPath()))) {
expectedPreviousSaveNumber = input.readInt();
} catch (IOException e) {
expectedPreviousSaveNumber = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Bundle-ManifestVersion: 2
Bundle-Name: Save Participant 3
Bundle-SymbolicName: org.eclipse.core.tests.resources.saveparticipant3
Automatic-Module-Name: org.eclipse.core.tests.resources.saveparticipant3
Bundle-Version: 3.6.100.qualifier
Bundle-Version: 3.6.200.qualifier
Bundle-Vendor: Eclipse.org
Export-Package: org.eclipse.core.tests.resources.saveparticipant3
Require-Bundle: org.eclipse.core.resources,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,23 @@
*******************************************************************************/
package org.eclipse.core.tests.resources.saveparticipant3;

import java.io.*;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;

import org.eclipse.core.resources.ISaveContext;
import org.eclipse.core.resources.ISaveParticipant;
import org.eclipse.core.resources.ISavedState;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.tests.resources.ResourceDeltaVerifier;

/**
Expand Down Expand Up @@ -99,7 +113,7 @@ public void prepareToSave(ISaveContext context) throws CoreException {
*/
private void readExpectedSavedNumber() {
IPath location = getStateLocation().append(SAVE_NUMBER_LOCATION);
try (DataInputStream input = new DataInputStream(new FileInputStream(location.toOSString()))) {
try (DataInputStream input = new DataInputStream(Files.newInputStream(location.toPath()))) {
expectedPreviousSaveNumber = input.readInt();
} catch (IOException e) {
expectedPreviousSaveNumber = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Eclipse Core Tests Resources
Bundle-SymbolicName: org.eclipse.core.tests.resources; singleton:=true
Bundle-Version: 3.11.700.qualifier
Bundle-Version: 3.11.800.qualifier
Bundle-Vendor: Eclipse.org
Export-Package: org.eclipse.core.tests.filesystem,
org.eclipse.core.tests.internal.alias,
Expand Down
2 changes: 1 addition & 1 deletion resources/tests/org.eclipse.core.tests.resources/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<version>4.35.0-SNAPSHOT</version>
</parent>
<artifactId>org.eclipse.core.tests.resources</artifactId>
<version>3.11.700-SNAPSHOT</version>
<version>3.11.800-SNAPSHOT</version>
<packaging>eclipse-test-plugin</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.runtime.IPath;
Expand All @@ -37,15 +35,6 @@
*/
public class FileCacheTest {

/**
* Returns the byte[] contents of the given file.
*/
private byte[] getBytes(File cachedFile) throws FileNotFoundException, IOException {
try (FileInputStream in = new FileInputStream(cachedFile)) {
return in.readAllBytes();
}
}

@Before
public void setUp() throws Exception {
MemoryTree.TREE.deleteAll();
Expand All @@ -66,7 +55,7 @@ public void testCacheFile() throws Exception {
File cachedFile = store.toLocalFile(EFS.CACHE, getMonitor());
assertTrue("1.0", cachedFile.exists());
assertTrue("1.1", !cachedFile.isDirectory());
assertThat(getBytes(cachedFile)).containsExactly(contents);
assertThat(Files.readAllBytes(cachedFile.toPath())).containsExactly(contents);

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

// old cache will be out of date
assertThat(newContents).isNotEqualTo(getBytes(cachedFile));
assertThat(newContents).isNotEqualTo(Files.readAllBytes(cachedFile.toPath()));

// fetching the cache again should return up to date file
cachedFile = store.toLocalFile(EFS.CACHE, getMonitor());
assertTrue("3.0", cachedFile.exists());
assertTrue("3.1", !cachedFile.isDirectory());
assertThat(getBytes(cachedFile)).containsExactly(newContents);
assertThat(Files.readAllBytes(cachedFile.toPath())).containsExactly(newContents);
}

@Test
Expand Down
Loading
Loading