From efcd179909b082dc342e5153c817bead627448af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=8A?= =?UTF-8?q?=D1=80=20=D0=9A=D1=83=D1=80=D1=82=D0=B0=D0=BA=D0=BE=D0=B2?= Date: Sun, 12 Jan 2025 16:22:36 +0200 Subject: [PATCH] More Java NIO usage in tests --- .../tests/importexport/ImportExportRemoteTests.java | 11 ++++++----- .../p2/tests/importexport/ImportExportTests.java | 13 +++++++------ .../p2/tests/verifier/VerifierApplication.java | 5 +++-- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/bundles/org.eclipse.equinox.p2.tests.ui/src/org/eclipse/equinox/p2/tests/importexport/ImportExportRemoteTests.java b/bundles/org.eclipse.equinox.p2.tests.ui/src/org/eclipse/equinox/p2/tests/importexport/ImportExportRemoteTests.java index 32fe026571..f85db8b524 100644 --- a/bundles/org.eclipse.equinox.p2.tests.ui/src/org/eclipse/equinox/p2/tests/importexport/ImportExportRemoteTests.java +++ b/bundles/org.eclipse.equinox.p2.tests.ui/src/org/eclipse/equinox/p2/tests/importexport/ImportExportRemoteTests.java @@ -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 @@ -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; @@ -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 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()); @@ -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 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()); diff --git a/bundles/org.eclipse.equinox.p2.tests.ui/src/org/eclipse/equinox/p2/tests/importexport/ImportExportTests.java b/bundles/org.eclipse.equinox.p2.tests.ui/src/org/eclipse/equinox/p2/tests/importexport/ImportExportTests.java index cbde872269..ccbc4135d6 100644 --- a/bundles/org.eclipse.equinox.p2.tests.ui/src/org/eclipse/equinox/p2/tests/importexport/ImportExportTests.java +++ b/bundles/org.eclipse.equinox.p2.tests.ui/src/org/eclipse/equinox/p2/tests/importexport/ImportExportTests.java @@ -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 @@ -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; @@ -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 iuDetails = importexportService.importP2F(input); assertEquals("Should load two features from the p2f file.", 2, iuDetails.size()); int counter = 0; @@ -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 iuDetails = importexportService.importP2F(input); assertEquals("Should not load any detail.", 0, iuDetails.size()); } @@ -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)); } @@ -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()); @@ -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()); } diff --git a/bundles/org.eclipse.equinox.p2.tests.verifier/src/org/eclipse/equinox/internal/p2/tests/verifier/VerifierApplication.java b/bundles/org.eclipse.equinox.p2.tests.verifier/src/org/eclipse/equinox/internal/p2/tests/verifier/VerifierApplication.java index dd63131450..d4d4dad4f2 100644 --- a/bundles/org.eclipse.equinox.p2.tests.verifier/src/org/eclipse/equinox/internal/p2/tests/verifier/VerifierApplication.java +++ b/bundles/org.eclipse.equinox.p2.tests.verifier/src/org/eclipse/equinox/internal/p2/tests/verifier/VerifierApplication.java @@ -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 @@ -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.*; @@ -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; }