Skip to content

Commit

Permalink
More Java NIO usage in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
akurtakov committed Jan 12, 2025
1 parent eab27b7 commit efcd179
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011, 2017 WindRiver Corporation and others.
* Copyright (c) 2011, 2025 WindRiver Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -15,6 +15,7 @@

import java.io.*;
import java.net.*;
import java.nio.file.Files;
import java.util.List;
import org.eclipse.core.runtime.*;
import org.eclipse.equinox.internal.p2.importexport.IUDetail;
Expand Down Expand Up @@ -71,11 +72,11 @@ public void testExportFeaturesFromRemoteRepository()
IMetadataRepository repo = metaManager.loadRepository(uri, null);
assertNotNull("Fail to load remote repo", repo);
IInstallableUnit iu = AbstractProvisioningTest.createIU("A", Version.create("1.0.0"));
try (OutputStream output = new FileOutputStream(testFile)) {
try (OutputStream output = Files.newOutputStream(testFile.toPath())) {
IStatus status = importexportService.exportP2F(output, new IInstallableUnit[] { iu }, false, null);
assertTrue("Not expected return result.", status.isOK());
}
try (InputStream input = new FileInputStream(testFile)) {
try (InputStream input = Files.newInputStream(testFile.toPath())) {
List<IUDetail> ius = importexportService.importP2F(input);
assertEquals("Exported the number of features is not expected.", 1, ius.size());
assertEquals("Exported feature is not expected.", iu, ius.get(0).getIU());
Expand Down Expand Up @@ -121,11 +122,11 @@ public void testExportFeaturesFromBothRemoteRepositoryAndLocalRepository()
IMetadataRepository repo = metaManager.loadRepository(uri, null);
assertNotNull("Fail to load remote repo", repo);
IInstallableUnit iu = AbstractProvisioningTest.createIU("A", Version.create("1.0.0"));
try (OutputStream output = new FileOutputStream(testFile)) {
try (OutputStream output = Files.newOutputStream(testFile.toPath())) {
IStatus status = importexportService.exportP2F(output, new IInstallableUnit[] { iu }, false, null);
assertTrue("Not expected return result.", status.isOK());
}
try (InputStream input = new FileInputStream(testFile)) {
try (InputStream input = Files.newInputStream(testFile.toPath())) {
List<IUDetail> ius = importexportService.importP2F(input);
assertEquals("Exported the number of features is not expected.", 1, ius.size());
assertEquals("Exported feature is not expected.", iu, ius.get(0).getIU());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011, 2017 WindRiver Corporation and others.
* Copyright (c) 2011, 2025 WindRiver Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -17,6 +17,7 @@
import static org.junit.Assert.assertThrows;

import java.io.*;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.runtime.IStatus;
Expand Down Expand Up @@ -67,7 +68,7 @@ protected void tearDown() throws Exception {
public void testLoadP2f() throws IOException {
File p2fFile = getTestData("Error load test file.", "testData/importexport/test.p2f");

try (InputStream input = new FileInputStream(p2fFile)) {
try (InputStream input = Files.newInputStream(p2fFile.toPath())) {
List<IUDetail> iuDetails = importexportService.importP2F(input);
assertEquals("Should load two features from the p2f file.", 2, iuDetails.size());
int counter = 0;
Expand All @@ -88,7 +89,7 @@ public void testLoadP2f() throws IOException {
public void testLoadUnknownP2f() throws IOException {
File p2fFile = getTestData("Error load test file.", "testData/importexport/unknownformat.p2f");

try (InputStream input = new FileInputStream(p2fFile)) {
try (InputStream input = Files.newInputStream(p2fFile.toPath())) {
List<IUDetail> iuDetails = importexportService.importP2F(input);
assertEquals("Should not load any detail.", 0, iuDetails.size());
}
Expand All @@ -97,7 +98,7 @@ public void testLoadUnknownP2f() throws IOException {
public void testIncompatibleP2f() throws IOException {
File p2fFile = getTestData("Error load test file.", "testData/importexport/incompatible.p2f");

try (InputStream input = new FileInputStream(p2fFile)) {
try (InputStream input = Files.newInputStream(p2fFile.toPath())) {
assertThrows("Didn't complain the given file is not supported by current version.",
VersionIncompatibleException.class, () -> importexportService.importP2F(input));
}
Expand All @@ -112,7 +113,7 @@ public void testExportFeaturesInstalledFromLocal()
IMetadataRepository repo = metaManager.loadRepository(localRepoFile.toURI(), null);
assertNotNull("Fail to load local repo", repo);
IInstallableUnit iu = createIU("A", Version.create("1.0.0"));
try (OutputStream output = new FileOutputStream(testFile)) {
try (OutputStream output = Files.newOutputStream(testFile.toPath())) {
IStatus status = importexportService.exportP2F(output, new IInstallableUnit[] { iu }, false, null);
assertFalse("Not expected return result.", status.isOK());
assertTrue("Should be a multiple status", status.isMultiStatus());
Expand All @@ -137,7 +138,7 @@ public void testAllowExportFeaturesInstalledFromLocal()
IMetadataRepository repo = metaManager.loadRepository(localRepoFile.toURI(), null);
assertNotNull("Fail to load local repo", repo);
IInstallableUnit iu = createIU("A", Version.create("1.0.0"));
try (OutputStream output = new FileOutputStream(testFile)) {
try (OutputStream output = Files.newOutputStream(testFile.toPath())) {
IStatus status = importexportService.exportP2F(output, new IInstallableUnit[] { iu }, true, null);
assertTrue(status.isOK());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2009, 2018 IBM Corporation and others.
* Copyright (c) 2009, 2025 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -17,6 +17,7 @@

import java.io.*;
import java.net.URI;
import java.nio.file.Files;
import java.util.*;
import java.util.Map.Entry;
import org.eclipse.core.runtime.*;
Expand Down Expand Up @@ -124,7 +125,7 @@ private void processArguments(String[] args) {
*/
private Properties readProperties(File file) throws IOException {
Properties result = new Properties();
try (InputStream input = new BufferedInputStream(new FileInputStream(file))) {
try (BufferedReader input = Files.newBufferedReader(file.toPath())) {
result.load(input);
return result;
}
Expand Down

0 comments on commit efcd179

Please sign in to comment.