Quellcode durchsuchen

add maven invoker util

Miaomz vor 6 Jahren
Ursprung
Commit
10812bf2eb

+ 5 - 0
pom.xml

@@ -113,6 +113,11 @@
             <artifactId>pmd-java</artifactId>
             <version>6.24.0</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.maven.shared</groupId>
+            <artifactId>maven-invoker</artifactId>
+            <version>2.0.11</version>
+        </dependency>
         <!--<dependency>-->
             <!--<groupId>pmd</groupId>-->
             <!--<artifactId>pmd</artifactId>-->

+ 37 - 0
src/main/java/com/moekr/moocoder/util/MavenUtil.java

@@ -0,0 +1,37 @@
+package com.moekr.moocoder.util;
+
+
+import lombok.extern.apachecommons.CommonsLog;
+import org.apache.maven.shared.invoker.*;
+
+import java.io.File;
+import java.util.Collections;
+
+/**
+ * @author miaomuzhi
+ * @since 2020/5/30
+ */
+@CommonsLog
+public class MavenUtil {
+    //TODO automatically find maven home
+    private static final String MAVEN_HOME = "/usr/local/Cellar/maven/3.5.3/libexec";
+
+    private MavenUtil() {}
+
+    public static boolean buildMavenProject(String srcDir){
+        try {
+            InvocationRequest request = new DefaultInvocationRequest();
+            request.setPomFile( new File( srcDir + File.separator + "pom.xml" ) );
+            request.setGoals( Collections.singletonList("compile"));
+
+            Invoker invoker = new DefaultInvoker();
+            invoker.setMavenHome(new File(MAVEN_HOME));
+            invoker.setWorkingDirectory(new File(srcDir));
+            InvocationResult result = invoker.execute(request);
+            return result.getExitCode() == 0;  //non-zero exit code indicates compile or test failure
+        } catch (MavenInvocationException e) {
+            log.info(e);
+            return false;
+        }
+    }
+}

+ 7 - 1
src/test/java/com/moekr/moocoder/logic/AsyncWrapperTest.java

@@ -1,6 +1,8 @@
 package com.moekr.moocoder.logic;
 
 import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
 
 import static org.junit.jupiter.api.Assertions.*;
 
@@ -8,12 +10,16 @@ import static org.junit.jupiter.api.Assertions.*;
  * @author miaomuzhi
  * @since 2020/5/30
  */
+@SpringBootTest
 class AsyncWrapperTest {
+    @Autowired
+    private AsyncWrapper wrapper;
 
     @Test
     void asyncInvoke() {
-        AsyncWrapper wrapper = new AsyncWrapper();
         System.out.println(System.nanoTime());
         wrapper.asyncInvoke(() -> {System.out.println(System.nanoTime());}, 10000);
+        System.out.println("Hello");
+        wrapper.asyncInvoke(() -> {System.out.println("Hi");}, 1000);
     }
 }

+ 18 - 0
src/test/java/com/moekr/moocoder/util/MavenUtilTest.java

@@ -0,0 +1,18 @@
+package com.moekr.moocoder.util;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+/**
+ * @author miaomuzhi
+ * @since 2020/5/30
+ */
+class MavenUtilTest {
+
+    @Test
+    void buildMavenProject() {
+        String srcDir = getClass().getResource("/maven/ComputeCoverage").getFile();
+        assertTrue(MavenUtil.buildMavenProject(srcDir));
+    }
+}

+ 14 - 0
src/test/resources/maven/ComputeCoverage/README.md

@@ -0,0 +1,14 @@
+java
+CurrencyExchange
+
+##  题目要求
+编写一个货币兑换程序,将欧元兑换成美元。提示输入手中的欧元数,以及欧元的当前汇率。打印可以兑换的美元数。
+
+注意:使用英文标点符号,输出中的数字保留两位小数。
+
+##  示例
+输出:	How many euros are you exchanging?  
+输入:	81  
+输出:	What is the exchange rate?  
+输入:	137.51  
+输出:	81.00 euros at an exchange rate of 137.51 is 111.38 U.S. dollars.

+ 49 - 0
src/test/resources/maven/ComputeCoverage/pom.xml

@@ -0,0 +1,49 @@
+<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">
+	<modelVersion>4.0.0</modelVersion>
+	<groupId>edu.nju.software</groupId>
+	<artifactId>CurrencyCalculation</artifactId>
+	<version>0.0.1-SNAPSHOT</version>
+	<name>Compute</name>
+
+	<dependencies>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.12</version>
+        </dependency>
+    </dependencies>
+
+	<build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>1.8</source>
+                    <target>1.8</target>
+                </configuration>
+            </plugin>
+            <plugin>
+                <groupId>org.codehaus.mojo</groupId>
+                <artifactId>cobertura-maven-plugin</artifactId>
+                <version>2.5.1</version>
+                <configuration>
+                    <formats>
+                        <format>xml</format>
+                    </formats>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+	<reporting>
+	    <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-surefire-report-plugin</artifactId>
+                <version>2.21.0</version>
+            </plugin>
+        </plugins>
+    </reporting>
+</project>

+ 22 - 0
src/test/resources/maven/ComputeCoverage/src/main/java/Compute.java

@@ -0,0 +1,22 @@
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+public class Compute {
+
+	public int add(int num1,int num2){
+		return num1+num2;
+	}
+
+	public int minus(int num1,int num2){
+		return num1-num2;
+	}
+	public int multipy(int num1,int num2){
+		return num1*num2;
+	}
+	public double divide(int num1,int num2){
+		if (num2==0)
+			return 0;
+		return num1/(num2*1.0);
+	}
+}

+ 29 - 0
src/test/resources/maven/ComputeCoverage/src/test/java/ComputeTest.java

@@ -0,0 +1,29 @@
+import static org.junit.Assert.assertEquals;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintStream;
+import java.util.Formatter;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ComputeTest {
+
+
+	@Test
+	public void test1() {
+		Compute compute=new Compute();
+		assertEquals(3,compute.add(1,2));
+	}
+
+	@Test
+	public void test2() {
+		Compute compute=new Compute();
+		assertEquals(-1,compute.minus(1,2));
+	}
+
+
+}