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

fix(MavenLauncher): allowing rebuilding of classpath #3270

Merged
merged 2 commits into from
Feb 24, 2020
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
10 changes: 9 additions & 1 deletion src/main/java/spoon/MavenLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private void init(String mavenProject, String[] classpath) {
}

if (classpath == null) {
classpath = model.buildClassPath(mvnHome, sourceType, LOGGER, false);
classpath = model.buildClassPath(mvnHome, sourceType, LOGGER, forceRefresh);
}

// dependencies
Expand All @@ -134,4 +134,12 @@ private void init(String mavenProject, String[] classpath) {
// compliance level
this.getEnvironment().setComplianceLevel(model.getSourceVersion());
}

/**
* Triggers regeneration of the classpath that is used for building the model, based on pom.xml
*/
public void rebuildClasspath() {
String[] classpath = model.buildClassPath(mvnHome, sourceType, LOGGER, true);
this.getModelBuilder().setSourceClasspath(classpath);
}
}
32 changes: 32 additions & 0 deletions src/test/java/spoon/MavenLauncherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import spoon.support.compiler.SpoonPom;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Paths;
Expand Down Expand Up @@ -142,6 +144,36 @@ public void testSystemDependency() {
assertTrue(launcher.getEnvironment().getSourceClasspath()[0].endsWith("lib/bridge-method-annotation-1.13.jar"));
}

@Test
public void testForceRefresh() throws FileNotFoundException {
// ensure classpath file exists so first constructor invocation won't build classpath
File file = new File("./src/test/resources/maven-launcher/system-dependency/spoon.classpath.tmp");
new PrintWriter(file).close();

// contract: classpath is not built
MavenLauncher launcher = new MavenLauncher("./src/test/resources/maven-launcher/system-dependency", MavenLauncher.SOURCE_TYPE.ALL_SOURCE);
assertEquals(0, launcher.getEnvironment().getSourceClasspath().length);

// contract: calling constructor with forceRefresh=true should result in classpath being rebuilt
MavenLauncher newLauncher = new MavenLauncher("./src/test/resources/maven-launcher/system-dependency", MavenLauncher.SOURCE_TYPE.ALL_SOURCE, true);
assertEquals(1, newLauncher.getEnvironment().getSourceClasspath().length);
}

@Test
public void testRebuildClasspath() throws FileNotFoundException {
// ensure classpath file exists so first constructor invocation won't build classpath
File file = new File("./src/test/resources/maven-launcher/system-dependency/spoon.classpath.tmp");
new PrintWriter(file).close();

// contract: classpath is not built
MavenLauncher launcher = new MavenLauncher("./src/test/resources/maven-launcher/system-dependency", MavenLauncher.SOURCE_TYPE.ALL_SOURCE);
assertEquals(0, launcher.getEnvironment().getSourceClasspath().length);

// contract: classpath should be rebuilt
launcher.rebuildClasspath();
assertEquals(1, launcher.getEnvironment().getSourceClasspath().length);
}

@Test
public void mavenLauncherTestWithVerySimpleProject() {
MavenLauncher launcher = new MavenLauncher("./src/test/resources/maven-launcher/very-simple", MavenLauncher.SOURCE_TYPE.ALL_SOURCE);
Expand Down