diff --git a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/ClasspathComputer.java b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/ClasspathComputer.java index 30719093764..033cc2e3718 100644 --- a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/ClasspathComputer.java +++ b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/ClasspathComputer.java @@ -259,7 +259,9 @@ private static void addLibraryEntry(IProject project, IPluginLibrary library, IP } IClasspathEntry entry = createClasspathEntry(project, jarFile, name, sourceAttachment, attrs, library.isExported()); - if (paths.add(entry.getPath())) { + if (paths.add(jarFile.getFullPath())) { + result.add(createClasspathEntry(project, jarFile, name, sourceAttachment, attrs, library.isExported())); + } result.add(entry); } } diff --git a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/classpathupdater/ClasspathUpdaterTest.java b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/classpathupdater/ClasspathUpdaterTest.java index bf2810a1b49..d5ec0d219cf 100644 --- a/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/classpathupdater/ClasspathUpdaterTest.java +++ b/ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/classpathupdater/ClasspathUpdaterTest.java @@ -1,103 +1,84 @@ package org.eclipse.pde.ui.tests.classpathupdater; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import java.util.Objects; +import java.util.Arrays; import java.util.function.Function; import org.eclipse.core.resources.IProject; -import org.eclipse.core.runtime.IStatus; import org.eclipse.jdt.core.*; import org.eclipse.pde.core.plugin.IPluginModelBase; import org.eclipse.pde.core.plugin.PluginRegistry; import org.eclipse.pde.internal.ui.wizards.tools.UpdateClasspathJob; import org.eclipse.pde.ui.tests.util.ProjectUtils; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.*; public class ClasspathUpdaterTest { private static IProject project; + private static IJavaProject jProject; - /** - * The project name and bundle symbolic name of the test project - */ - public static final String bundleName = "classpathupdater"; + private static IClasspathEntry[] originalClasspath; @BeforeClass public static void setUpBeforeClass() throws Exception { - project = ProjectUtils.importTestProject("tests/projects/" + bundleName); + project = ProjectUtils.importTestProject("tests/projects/classpathupdater"); + jProject = JavaCore.create(project); + originalClasspath = jProject.getRawClasspath(); + } + + @After + public void restoreOriginalClasspath() throws Exception { + jProject.setRawClasspath(originalClasspath, null); } @Test public void testUpdateClasspath_PreserveAttributes() throws Exception { assertTrue("Project was not created", project.exists()); - IJavaProject javaPrj = JavaCore.create(project); - final IClasspathEntry[] originalClasspath = javaPrj.getRawClasspath(); - try { - assertTrue("Update Classpath Job failed", runUpdateClasspathJob().isOK()); - checkClasspathAttribute("JavaSE-17", IClasspathAttribute.MODULE, "true", Boolean::valueOf); - checkClasspathAttribute("library.jar", IClasspathAttribute.TEST, "true", Boolean::valueOf); - } finally { - javaPrj.setRawClasspath(originalClasspath, null); - } + + runUpdateClasspathJob(); + assertClasspathAttribute("JavaSE-17", IClasspathAttribute.MODULE, true, Boolean::valueOf); + assertClasspathAttribute("library.jar", IClasspathAttribute.TEST, true, Boolean::valueOf); } @Test public void testUpdateClasspath_CreateFromScratch() throws Exception { assertTrue("Project was not created", project.exists()); - IJavaProject javaPrj = JavaCore.create(project); - final IClasspathEntry[] originalClasspath = javaPrj.getRawClasspath(); - try { - javaPrj.setRawClasspath(new IClasspathEntry[0], null); - assertTrue("Update Classpath Job failed", runUpdateClasspathJob().isOK()); - checkClasspathAttribute("JavaSE-17", IClasspathAttribute.MODULE, null, Boolean::valueOf); - checkClasspathAttribute("library.jar", IClasspathAttribute.TEST, null, Boolean::valueOf); - } finally { - javaPrj.setRawClasspath(originalClasspath, null); - } + jProject.setRawClasspath(new IClasspathEntry[0], null); + + runUpdateClasspathJob(); + assertClasspathAttribute("JavaSE-17", IClasspathAttribute.MODULE, null, Boolean::valueOf); + assertClasspathAttribute("library.jar", IClasspathAttribute.TEST, null, Boolean::valueOf); } - private IStatus runUpdateClasspathJob() throws InterruptedException { + private void runUpdateClasspathJob() throws InterruptedException { IPluginModelBase model = PluginRegistry.findModel(project.getProject()); UpdateClasspathJob job = new UpdateClasspathJob(new IPluginModelBase[] { model }); job.schedule(); job.join(); - return job.getResult(); + assertTrue("Update Classpath Job failed", job.getResult().isOK()); } - private void checkClasspathAttribute(String entryName, String attrName, String expectedValue, - Function parser) throws JavaModelException { + private void assertClasspathAttribute(String entryName, String attrName, T expectedValue, + Function parser) throws JavaModelException { IClasspathEntry entry = findClasspathEntry(entryName); - assertTrue("Classpath entry for " + entryName + " is missing", entry != null); - checkClasspathAttributeValue(entry, attrName, parser, expectedValue); - } - - private void checkClasspathAttributeValue(IClasspathEntry entry, String attrName, Function parser, - String expectedValue) { - String title = "Classpath entry for '" + entry.getPath().lastSegment() + "': attribute '" + attrName + "'"; + assertNotNull("Classpath entry for " + entryName + " is missing", entry); String attrValue = findClasspathAttributeValue(entry, attrName); - Object current = parser.apply(attrValue); // null: attribute not set - Object expected = parser.apply(expectedValue); - assertTrue(title + " is not '" + Objects.toString(expected) + "' any more", Objects.equals(current, expected)); + T current = attrValue != null ? parser.apply(attrValue) : null; // null: attribute not set + assertEquals("Classpath entry for '" + entry.getPath().lastSegment() + "': attribute '" + attrName + + "' is not '" + expectedValue + "' any more", expectedValue, current); } - private String findClasspathAttributeValue(IClasspathEntry entry, String attrName) { - for (IClasspathAttribute attr : entry.getExtraAttributes()) { - if (attrName.equals(attr.getName())) { - return attr.getValue(); - } - } - return null; + private String findClasspathAttributeValue(IClasspathEntry entry, String name) { + return Arrays.stream(entry.getExtraAttributes()) // + .filter(a -> name.equals(a.getName())).map(IClasspathAttribute::getValue) // + .findFirst().orElse(null); } private IClasspathEntry findClasspathEntry(String entryName) throws JavaModelException { - IJavaProject javaPrj = JavaCore.create(project); - IClasspathEntry[] rawClasspath = javaPrj.getRawClasspath(); - for (IClasspathEntry entry : rawClasspath) { - if (entryName.equals(entry.getPath().lastSegment())) { - return entry; - } - } - return null; + return Arrays.stream(jProject.getRawClasspath()) // + .filter(e -> entryName.equals(e.getPath().lastSegment())) // + .findFirst().orElse(null); } }