-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1a3b757
commit 2bb141c
Showing
2 changed files
with
41 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 38 additions & 57 deletions
95
...ipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/classpathupdater/ClasspathUpdaterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, Object> parser) throws JavaModelException { | ||
private <T> void assertClasspathAttribute(String entryName, String attrName, T expectedValue, | ||
Function<String, T> 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<String, Object> 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); | ||
} | ||
} |