-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#274 the post analysis script can be loaded from the classpath
- Loading branch information
Showing
8 changed files
with
229 additions
and
48 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
69 changes: 69 additions & 0 deletions
69
japicmp-maven-plugin/src/main/java/japicmp/maven/PostAnalysisScriptExecutor.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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package japicmp.maven; | ||
|
||
import japicmp.model.JApiClass; | ||
import org.apache.maven.plugin.MojoFailureException; | ||
import org.apache.maven.plugin.logging.Log; | ||
|
||
import javax.script.Bindings; | ||
import javax.script.ScriptEngine; | ||
import javax.script.ScriptEngineManager; | ||
import javax.script.ScriptException; | ||
import java.io.*; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class PostAnalysisScriptExecutor { | ||
|
||
public List<JApiClass> apply(Parameter parameter, List<JApiClass> jApiClasses, Log log) throws MojoFailureException { | ||
List<JApiClass> filteredList = jApiClasses; | ||
if (parameter != null) { | ||
String postAnalysisFilterScript = parameter.getPostAnalysisScript(); | ||
if (postAnalysisFilterScript != null) { | ||
try { | ||
InputStream inputStream = getInputStream(postAnalysisFilterScript); | ||
if (inputStream != null) { | ||
ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("groovy"); | ||
Bindings bindings = scriptEngine.createBindings(); | ||
bindings.put("jApiClasses", jApiClasses); | ||
try (InputStreamReader fileReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) { | ||
Object returnValue = scriptEngine.eval(fileReader, bindings); | ||
if (returnValue instanceof List) { | ||
List returnedList = (List) returnValue; | ||
filteredList = new ArrayList<>(returnedList.size()); | ||
for (Object obj : returnedList) { | ||
if (obj instanceof JApiClass) { | ||
JApiClass jApiClass = (JApiClass) obj; | ||
filteredList.add(jApiClass); | ||
} | ||
} | ||
} else { | ||
throw new MojoFailureException("Post-analysis script does not return a list."); | ||
} | ||
} catch (ScriptException e) { | ||
throw new MojoFailureException("Execution of post-analysis script failed: " + e.getMessage(), e); | ||
} catch (IOException e) { | ||
throw new MojoFailureException("Failed to load post-analysis script '" + postAnalysisFilterScript + ": " + e.getMessage(), e); | ||
} | ||
} else { | ||
throw new MojoFailureException("Post-analysis script '" + postAnalysisFilterScript + " does not exist."); | ||
} | ||
} catch (FileNotFoundException e) { | ||
throw new MojoFailureException("Post-analysis script '" + postAnalysisFilterScript + " does not exist.", e); | ||
} | ||
} else { | ||
log.debug("No post-analysis script provided."); | ||
} | ||
} | ||
return filteredList; | ||
} | ||
|
||
private InputStream getInputStream(String postAnalysisFilterScript) throws FileNotFoundException { | ||
if (Files.exists(Paths.get(postAnalysisFilterScript))) { | ||
return new FileInputStream(postAnalysisFilterScript); | ||
} | ||
return PostAnalysisScriptExecutor.class.getClassLoader().getResourceAsStream(postAnalysisFilterScript); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
japicmp-testbase/japicmp-test-maven-plugin-post-analysis-script-artifact-test/pom.xml
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<groupId>com.github.siom79.japicmp</groupId> | ||
<artifactId>japicmp-testbase</artifactId> | ||
<version>0.15.3-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>japicmp-test-maven-plugin-post-analysis-script-artifact-test</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.github.siom79.japicmp</groupId> | ||
<artifactId>japicmp-test-maven-plugin-post-analysis-script-artifact</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>com.github.siom79.japicmp</groupId> | ||
<artifactId>japicmp-maven-plugin</artifactId> | ||
<version>${project.version}</version> | ||
<configuration> | ||
<oldVersion> | ||
<dependency> | ||
<groupId>com.github.siom79.japicmp</groupId> | ||
<artifactId>japicmp-test-v1</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
</oldVersion> | ||
<newVersion> | ||
<dependency> | ||
<groupId>com.github.siom79.japicmp</groupId> | ||
<artifactId>japicmp-test-v1</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
</newVersion> | ||
<parameter> | ||
<!-- The post-analysis script is contained in "japicmp-test-maven-plugin-post-analysis-script-artifact" --> | ||
<postAnalysisScript>post-analysis-script.groovy</postAnalysisScript> | ||
</parameter> | ||
</configuration> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.github.siom79.japicmp</groupId> | ||
<artifactId>japicmp-test-maven-plugin-post-analysis-script-artifact</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
</dependencies> | ||
<executions> | ||
<execution> | ||
<phase>pre-integration-test</phase> | ||
<goals> | ||
<goal>cmp</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-failsafe-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<phase>integration-test</phase> | ||
<goals> | ||
<goal>integration-test</goal> | ||
<goal>verify</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
26 changes: 26 additions & 0 deletions
26
...st-analysis-script-artifact-test/src/test/java/japicmp/test/ITTestPostAnalysisScript.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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package japicmp.test; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
|
||
public class ITTestPostAnalysisScript { | ||
|
||
@Test | ||
public void testSkipViaUserProperty() throws IOException { | ||
Path path = Paths.get(System.getProperty("user.dir"), "target", "japicmp", "japicmp.diff"); | ||
Assert.assertTrue(Files.exists(path)); | ||
List<String> lines = Files.readAllLines(path); | ||
Assert.assertTrue(lines.size() > 0); | ||
for (String line : lines) { | ||
if (line.contains("japicmp.test.AbstractModifier")) { | ||
Assert.fail("Diff file contains class that should have been filtered by groovy script from classpath"); | ||
} | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
japicmp-testbase/japicmp-test-maven-plugin-post-analysis-script-artifact/pom.xml
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<groupId>com.github.siom79.japicmp</groupId> | ||
<artifactId>japicmp-testbase</artifactId> | ||
<version>0.15.3-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>japicmp-test-maven-plugin-post-analysis-script-artifact</artifactId> | ||
|
||
</project> |
9 changes: 9 additions & 0 deletions
9
...maven-plugin-post-analysis-script-artifact/src/main/resources/post-analysis-script.groovy
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
def it = jApiClasses.iterator() | ||
while (it.hasNext()) { | ||
def jApiClass = it.next() | ||
def fqn = jApiClass.getFullyQualifiedName() | ||
if (fqn == "japicmp.test.AbstractModifier") { | ||
it.remove() | ||
} | ||
} | ||
return jApiClasses |
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
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