Skip to content

Commit

Permalink
Fix MavenInvokerTest
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarsotovalero committed Apr 1, 2022
1 parent 6578719 commit ee91512
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package se.kth.depclean.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand All @@ -36,23 +37,25 @@ private MavenInvoker() {
/**
* Creates a native process to execute a custom command. This method is used to invoke maven plugins directly.
*
* @param cmd The command to be executed.
* @param cmd The command to be executed.
* @param directory The working directory of the subprocess,
* or null if the subprocess should inherit the working directory of the current process.
* @return The console output.
* @throws IOException In case of IO issues.
* @throws InterruptedException In case of IO issues.
*/
public static String[] runCommand(String cmd) throws IOException, InterruptedException {
public static String[] runCommand(String cmd, File directory) throws IOException, InterruptedException {
Process process;
ArrayList<String> list;
if (OsUtils.isUnix()) {
list = new ArrayList<>();
process = Runtime.getRuntime().exec(cmd);
process = Runtime.getRuntime().exec(cmd, null, directory);
InputStream inputStream = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
return outputToConsole(process, list, br);
} else if (OsUtils.isWindows()) {
list = new ArrayList<>();
process = Runtime.getRuntime().exec("cmd /C " + cmd);
process = Runtime.getRuntime().exec("cmd /C " + cmd, null, directory);
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
return outputToConsole(process, list, br);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public void copyAndExtractDependencies() {
/* Copy direct dependencies locally */
try {
MavenInvoker.runCommand("mvn dependency:copy-dependencies -DoutputDirectory="
+ project.getBuild().getDirectory() + File.separator + DIRECTORY_TO_COPY_DEPENDENCIES);
+ project.getBuild().getDirectory() + File.separator + DIRECTORY_TO_COPY_DEPENDENCIES,
null);
} catch (IOException | InterruptedException e) {
getLog().error("Unable to resolve all the dependencies.");
Thread.currentThread().interrupt();
Expand Down Expand Up @@ -200,7 +201,7 @@ public String getBuildDirectory() {

@Override
public void generateDependencyTree(File treeFile) throws IOException, InterruptedException {
MavenInvoker.runCommand("mvn dependency:tree -DoutputFile=" + treeFile + " -Dverbose=true");
MavenInvoker.runCommand("mvn dependency:tree -DoutputFile=" + treeFile + " -Dverbose=true", null);
}

@SneakyThrows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.io.File;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -27,15 +26,16 @@ class MavenInvokerTest {
@Test
@DisplayName("Test that the Maven dependency tree, then the dependency tree is obtained")
void testRunCommandToGetDependencyTree() throws IOException, InterruptedException {
MavenInvoker.runCommand("mvn dependency:tree -DoutputFile=" + producedTree);
File directory = new File("src/test/resources/MavenInvokerResources/basic_spring_maven_project");
MavenInvoker.runCommand("mvn dependency:tree -DoutputFile=tree_produced.txt", directory);
assertTrue(producedTree.exists());
assertThat(producedTree).hasSameTextualContentAs(expectedTree);
}

//@AfterAll
//public static void tearDown() throws IOException {
// if (producedTree.exists()) {
// FileUtils.forceDelete(producedTree);
// }
//}
@AfterAll
public static void tearDown() throws IOException {
if (producedTree.exists()) {
FileUtils.forceDelete(producedTree);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,79 +1,67 @@
se.kth.castor:depclean-maven-plugin:maven-plugin:2.0.2-SNAPSHOT
+- se.kth.castor:depclean-core:jar:2.0.2-SNAPSHOT:compile
| +- org.ow2.asm:asm:jar:9.1:compile
| +- org.codehaus.plexus:plexus-component-annotations:jar:2.0.0:compile
| +- org.codehaus.plexus:plexus-utils:jar:3.3.0:compile
| +- org.jgrapht:jgrapht-core:jar:1.3.0:compile
| | \- org.jheaps:jheaps:jar:0.9:compile
| +- org.jetbrains:annotations:jar:17.0.0:compile
| \- com.google.guava:guava:jar:30.1-jre:compile
| +- com.google.guava:failureaccess:jar:1.0.1:compile
| +- com.google.guava:listenablefuture:jar:9999.0-empty-to-avoid-conflict-with-guava:compile
| +- com.google.code.findbugs:jsr305:jar:3.0.2:compile
| +- org.checkerframework:checker-qual:jar:3.5.0:compile
| +- com.google.errorprone:error_prone_annotations:jar:2.3.4:compile
| \- com.google.j2objc:j2objc-annotations:jar:1.3:compile
+- org.apache.maven:maven-core:jar:3.6.0:compile
| +- org.apache.maven:maven-model:jar:3.6.0:compile
| +- org.apache.maven:maven-settings:jar:3.6.0:compile
| +- org.apache.maven:maven-settings-builder:jar:3.6.0:compile
| | \- org.sonatype.plexus:plexus-sec-dispatcher:jar:1.4:compile
| | \- org.sonatype.plexus:plexus-cipher:jar:1.4:compile
| +- org.apache.maven:maven-builder-support:jar:3.6.0:compile
| +- org.apache.maven:maven-repository-metadata:jar:3.6.0:compile
| +- org.apache.maven:maven-artifact:jar:3.6.0:compile
| +- org.apache.maven:maven-model-builder:jar:3.6.0:compile
| +- org.apache.maven:maven-resolver-provider:jar:3.6.0:compile
| +- org.apache.maven.resolver:maven-resolver-impl:jar:1.3.1:compile
| +- org.apache.maven.resolver:maven-resolver-api:jar:1.3.1:compile
| +- org.apache.maven.resolver:maven-resolver-spi:jar:1.3.1:compile
| +- org.apache.maven.resolver:maven-resolver-util:jar:1.3.1:compile
| +- org.apache.maven.shared:maven-shared-utils:jar:3.2.1:compile
| +- org.eclipse.sisu:org.eclipse.sisu.plexus:jar:0.3.3:compile
| | \- javax.enterprise:cdi-api:jar:1.0:compile
| | \- javax.annotation:jsr250-api:jar:1.0:compile
| +- org.eclipse.sisu:org.eclipse.sisu.inject:jar:0.3.3:compile
| +- com.google.inject:guice:jar:no_aop:4.2.1:compile
| | \- aopalliance:aopalliance:jar:1.0:compile
| +- javax.inject:javax.inject:jar:1:compile
| +- org.codehaus.plexus:plexus-classworlds:jar:2.5.2:compile
| \- org.apache.commons:commons-lang3:jar:3.8.1:compile
+- org.apache.maven:maven-plugin-api:jar:3.6.0:compile
+- org.apache.maven:maven-project:jar:3.0-alpha-2:compile
| +- org.codehaus.plexus:plexus-interpolation:jar:1.1:compile
| +- org.apache.maven:maven-compat:jar:3.0-alpha-2:compile
| | \- org.apache.maven.wagon:wagon-provider-api:jar:1.0-beta-4:compile
| +- org.codehaus.plexus:plexus-container-default:jar:1.0-beta-3.0.5:compile
| | +- org.apache.xbean:xbean-reflect:jar:3.4:compile
| | | \- commons-logging:commons-logging-api:jar:1.1:compile
| | \- com.google.code.google-collections:google-collect:jar:snapshot-20080530:compile
| +- org.codehaus.woodstox:wstx-asl:jar:3.2.6:compile
| | \- stax:stax-api:jar:1.0.1:compile
| +- org.sonatype.spice:model-builder:jar:1.3:compile
| \- org.apache.maven:maven-project-builder:jar:3.0-alpha-2:compile
+- org.apache.maven.plugin-tools:maven-plugin-annotations:jar:3.6.0:compile
+- org.apache.maven.shared:maven-dependency-tree:jar:3.0.1:compile
| \- org.eclipse.aether:aether-util:jar:0.9.0.M2:compile
+- commons-io:commons-io:jar:2.5:compile
+- com.google.code.gson:gson:jar:2.8.6:compile
+- org.whitesource:maven-dependency-tree-parser:jar:1.0.6:compile
| \- commons-lang:commons-lang:jar:2.4:compile
+- org.apache.maven.plugin-testing:maven-plugin-testing-tools:jar:3.3.0:compile
| \- org.apache.maven.shared:maven-invoker:jar:2.0.11:compile
+- com.soebes.itf.jupiter.extension:itf-extension-maven:jar:0.9.0:test
| +- org.apiguardian:apiguardian-api:jar:1.1.0:test
| \- org.junit.platform:junit-platform-commons:jar:1.6.2:test
+- com.soebes.itf.jupiter.extension:itf-assertj:jar:0.9.0:test
+- com.soebes.itf.jupiter.extension:itf-jupiter-extension:jar:0.9.0:test
+- org.assertj:assertj-core:jar:3.19.0:test
+- org.projectlombok:lombok:jar:1.18.22:provided
+- org.slf4j:slf4j-api:jar:2.0.0-alpha1:compile
+- org.slf4j:slf4j-log4j12:jar:1.8.0-beta4:compile
| \- log4j:log4j:jar:1.2.17:compile
+- org.junit.jupiter:junit-jupiter-api:jar:5.6.2:test
| \- org.opentest4j:opentest4j:jar:1.2.0:test
+- org.junit.jupiter:junit-jupiter-engine:jar:5.6.2:test
| \- org.junit.platform:junit-platform-engine:jar:1.6.2:test
\- org.junit.vintage:junit-vintage-engine:jar:5.6.2:test
\- junit:junit:jar:4.13:compile
\- org.hamcrest:hamcrest-core:jar:1.3:compile
com.example:demo:jar:0.0.1-SNAPSHOT
+- org.springframework.boot:spring-boot-starter-thymeleaf:jar:2.4.4:compile
| +- org.springframework.boot:spring-boot-starter:jar:2.4.4:compile
| | +- org.springframework.boot:spring-boot:jar:2.4.4:compile
| | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.4.4:compile
| | +- org.springframework.boot:spring-boot-starter-logging:jar:2.4.4:compile
| | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile
| | | | \- ch.qos.logback:logback-core:jar:1.2.3:compile
| | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.13.3:compile
| | | | \- org.apache.logging.log4j:log4j-api:jar:2.13.3:compile
| | | \- org.slf4j:jul-to-slf4j:jar:1.7.30:compile
| | +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile
| | \- org.yaml:snakeyaml:jar:1.27:compile
| +- org.thymeleaf:thymeleaf-spring5:jar:3.0.12.RELEASE:compile
| | +- org.thymeleaf:thymeleaf:jar:3.0.12.RELEASE:compile
| | | +- org.attoparser:attoparser:jar:2.0.5.RELEASE:compile
| | | \- org.unbescape:unbescape:jar:1.1.6.RELEASE:compile
| | \- org.slf4j:slf4j-api:jar:1.7.30:compile
| \- org.thymeleaf.extras:thymeleaf-extras-java8time:jar:3.0.4.RELEASE:compile
+- org.springframework.boot:spring-boot-starter-web:jar:2.4.4:compile
| +- org.springframework.boot:spring-boot-starter-json:jar:2.4.4:compile
| | +- com.fasterxml.jackson.core:jackson-databind:jar:2.11.4:compile
| | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.11.4:compile
| | | \- com.fasterxml.jackson.core:jackson-core:jar:2.11.4:compile
| | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.11.4:compile
| | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.11.4:compile
| | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.11.4:compile
| +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.4.4:compile
| | +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.44:compile
| | +- org.glassfish:jakarta.el:jar:3.0.3:compile
| | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.44:compile
| +- org.springframework:spring-web:jar:5.3.5:compile
| | \- org.springframework:spring-beans:jar:5.3.5:compile
| \- org.springframework:spring-webmvc:jar:5.3.5:compile
| +- org.springframework:spring-aop:jar:5.3.5:compile
| +- org.springframework:spring-context:jar:5.3.5:compile
| \- org.springframework:spring-expression:jar:5.3.5:compile
\- org.springframework.boot:spring-boot-starter-test:jar:2.4.4:test
+- org.springframework.boot:spring-boot-test:jar:2.4.4:test
+- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.4.4:test
+- com.jayway.jsonpath:json-path:jar:2.4.0:test
| \- net.minidev:json-smart:jar:2.3:test
| \- net.minidev:accessors-smart:jar:1.2:test
| \- org.ow2.asm:asm:jar:5.0.4:test
+- jakarta.xml.bind:jakarta.xml.bind-api:jar:2.3.3:test
| \- jakarta.activation:jakarta.activation-api:jar:1.2.2:test
+- org.assertj:assertj-core:jar:3.18.1:test
+- org.hamcrest:hamcrest:jar:2.2:test
+- org.junit.jupiter:junit-jupiter:jar:5.7.1:test
| +- org.junit.jupiter:junit-jupiter-api:jar:5.7.1:test
| | +- org.apiguardian:apiguardian-api:jar:1.1.0:test
| | +- org.opentest4j:opentest4j:jar:1.2.0:test
| | \- org.junit.platform:junit-platform-commons:jar:1.7.1:test
| +- org.junit.jupiter:junit-jupiter-params:jar:5.7.1:test
| \- org.junit.jupiter:junit-jupiter-engine:jar:5.7.1:test
| \- org.junit.platform:junit-platform-engine:jar:1.7.1:test
+- org.mockito:mockito-core:jar:3.6.28:test
| +- net.bytebuddy:byte-buddy:jar:1.10.22:test
| +- net.bytebuddy:byte-buddy-agent:jar:1.10.22:test
| \- org.objenesis:objenesis:jar:3.1:test
+- org.mockito:mockito-junit-jupiter:jar:3.6.28:test
+- org.skyscreamer:jsonassert:jar:1.5.0:test
| \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
+- org.springframework:spring-core:jar:5.3.5:compile
| \- org.springframework:spring-jcl:jar:5.3.5:compile
+- org.springframework:spring-test:jar:5.3.5:test
\- org.xmlunit:xmlunit-core:jar:2.7.0:test

This file was deleted.

0 comments on commit ee91512

Please sign in to comment.