This is an automated email from the ASF dual-hosted git repository.
ningjiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-toolkit.git
The following commit(s) were added to refs/heads/master by this push:
new 68a015b SCB-1428 Improve toolkit-maven-plugin test to make it running
is not dependent by maven plugin
68a015b is described below
commit 68a015bd1fd13d9b28a571fccd0fec0c3c084776
Author: MabinGo <[email protected]>
AuthorDate: Fri Aug 9 11:30:15 2019 +0800
SCB-1428 Improve toolkit-maven-plugin test to make it running is not
dependent by maven plugin
Signed-off-by: MabinGo <[email protected]>
---
scripts/test.sh | 2 +-
.../servicecomb/toolkit/plugin/GenerateUtil.java | 9 ++++----
.../toolkit/plugin/GenerateMojoTest.java | 4 ++--
.../toolkit/plugin/GenerateUtilTest.java | 1 +
.../toolkit/plugin/InvokeStaticMethodTest.java | 22 ++++++++-----------
.../toolkit/plugin/TestResourcesEx.java | 25 +++++++++++++++-------
.../servicecomb/toolkit/plugin/VerifyMojoTest.java | 2 +-
.../src/test/projects/demo-with-contract/pom.xml | 16 --------------
.../test/projects/demo-without-contract/pom.xml | 15 -------------
9 files changed, 35 insertions(+), 61 deletions(-)
diff --git a/scripts/test.sh b/scripts/test.sh
index 43a5b6f..a3b61cd 100755
--- a/scripts/test.sh
+++ b/scripts/test.sh
@@ -36,7 +36,7 @@ else
fi
echo "Running the unit tests and integration tests here!"
- mvn clean install -DskipTests && mvn clean install -Pjacoco
coveralls:report
+ mvn clean install -Pjacoco coveralls:report
if [ $? == 0 ]; then
echo "${green}Installation Success..${reset}"
diff --git
a/toolkit-maven-plugin/src/main/java/org/apache/servicecomb/toolkit/plugin/GenerateUtil.java
b/toolkit-maven-plugin/src/main/java/org/apache/servicecomb/toolkit/plugin/GenerateUtil.java
index d114987..15af20e 100755
---
a/toolkit-maven-plugin/src/main/java/org/apache/servicecomb/toolkit/plugin/GenerateUtil.java
+++
b/toolkit-maven-plugin/src/main/java/org/apache/servicecomb/toolkit/plugin/GenerateUtil.java
@@ -41,9 +41,9 @@ import
org.apache.servicecomb.toolkit.codegen.ProjectMetaConstant;
import io.swagger.codegen.config.CodegenConfigurator;
-public class GenerateUtil {
+class GenerateUtil {
- public static void generateContract(MavenProject project, String
contractOutput, String contractFileType,
+ static void generateContract(MavenProject project, String contractOutput,
String contractFileType,
String type) {
Map<String, Object> contractConfig = new HashMap<>();
@@ -62,7 +62,7 @@ public class GenerateUtil {
contractGenerator.generate();
}
- public static void generateDocument(String contractLocation, String
documentOutput, String type) throws IOException {
+ static void generateDocument(String contractLocation, String documentOutput,
String type) throws IOException {
// TODO: support users to add other getGenerator type soon
DocGenerator docGenerator =
GeneratorFactory.getGenerator(DocGenerator.class, type);
@@ -89,9 +89,8 @@ public class GenerateUtil {
});
}
- public static void generateCode(ServiceConfig service, String
contractLocation,
+ static void generateCode(ServiceConfig service, String contractLocation,
String codeOutput, Map<String, Object> externalConfig, String type)
throws IOException {
-
CodeGenerator codeGenerator =
GeneratorFactory.getGenerator(CodeGenerator.class, type);
if (codeGenerator == null) {
throw new RuntimeException("Cannot found code generator's
implementation");
diff --git
a/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/GenerateMojoTest.java
b/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/GenerateMojoTest.java
index 7fa4bc3..a1fa401 100755
---
a/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/GenerateMojoTest.java
+++
b/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/GenerateMojoTest.java
@@ -63,7 +63,7 @@ public class GenerateMojoTest {
given(project.getBasedir()).willReturn(new File("mockProject"));
// code has no contract
- testResourcesEx.createMojo(rule,
testResourcesEx.getBasedirWithoutContract(), "generate");
+ testResourcesEx.createMojo(rule, "generate");
testResourcesEx.setVariableValueToObject("project", project);
given(project.getRuntimeClasspathElements()).willReturn(testResourcesEx.getRuntimeUrlPath(testDirWithoutContract));
try {
@@ -80,7 +80,7 @@ public class GenerateMojoTest {
}
// code has contract
- testResourcesEx.createMojo(rule, testResourcesEx.getBasedirWithContract(),
"generate");
+ testResourcesEx.createMojo(rule, "generate");
testResourcesEx.setVariableValueToObject("project", project);
given(project.getRuntimeClasspathElements()).willReturn(testResourcesEx.getRuntimeUrlPath(testDirWithContract));
try {
diff --git
a/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/GenerateUtilTest.java
b/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/GenerateUtilTest.java
index 2ab9753..49e8f5b 100755
---
a/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/GenerateUtilTest.java
+++
b/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/GenerateUtilTest.java
@@ -66,6 +66,7 @@ public class GenerateUtilTest {
}
}
+ @SuppressWarnings("unchecked")
@Test
public void testGenerateCode() throws Exception {
diff --git
a/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/InvokeStaticMethodTest.java
b/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/InvokeStaticMethodTest.java
index 74f3430..e66cd70 100755
---
a/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/InvokeStaticMethodTest.java
+++
b/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/InvokeStaticMethodTest.java
@@ -22,7 +22,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
-import static org.mockito.Matchers.anyMap;
+import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
@@ -64,19 +64,13 @@ public class InvokeStaticMethodTest {
testGenerateMojoResources = new TestResourcesEx(resources);
testVerifyMojoResources = new TestResourcesEx(resources);
- String testDirWithContract =
testGenerateMojoResources.getBasedirWithContract();
- testGenerateMojoResources.createMojo(rule, testDirWithContract,
"generate");
- testVerifyMojoResources.createMojo(rule, testDirWithContract, "verify");
+ testGenerateMojoResources.createMojo(rule, "generate");
+ testVerifyMojoResources.createMojo(rule, "verify");
MavenProject project = mock(MavenProject.class);
given(project.getBasedir()).willReturn(new File("mockProject"));
testGenerateMojoResources.setVariableValueToObject("project", project);
testVerifyMojoResources.setVariableValueToObject("project", project);
-
- given(project.getRuntimeClasspathElements())
-
.willReturn(testGenerateMojoResources.getRuntimeUrlPath(testDirWithContract));
- given(project.getRuntimeClasspathElements())
-
.willReturn(testVerifyMojoResources.getRuntimeUrlPath(testDirWithContract));
}
@Test
@@ -146,7 +140,7 @@ public class InvokeStaticMethodTest {
PowerMockito.mockStatic(GenerateUtil.class);
PowerMockito.doThrow(new IOException()).when(GenerateUtil.class);
// Powermockito limit: use argument matchers to specify method which would
be mock
- GenerateUtil.generateCode(anyObject(), anyString(), anyString(), anyMap(),
anyString());
+ GenerateUtil.generateCode(anyObject(), anyString(), anyString(), any(),
anyString());
testGenerateMojoResources.setVariableValueToObject("sourceType",
"contract");
testGenerateMojoResources.setVariableValueToObject("outputDirectory",
"target/InvokeStaticMethodTest");
@@ -163,7 +157,7 @@ public class InvokeStaticMethodTest {
}
PowerMockito.doThrow(new RuntimeException()).when(GenerateUtil.class);
- GenerateUtil.generateCode(anyObject(), anyString(), anyString(), anyMap(),
anyString());
+ GenerateUtil.generateCode(anyObject(), anyString(), anyString(), any(),
anyString());
try {
testGenerateMojoResources.execute();
@@ -191,7 +185,8 @@ public class InvokeStaticMethodTest {
try {
testGenerateMojoResources.execute();
- fail("Run 'testGenerateMojoInvokeGenerateUtilGenerateDocument' failed,
expected to occur RuntimeException but not");
+ fail(
+ "Run 'testGenerateMojoInvokeGenerateUtilGenerateDocument' failed,
expected to occur RuntimeException but not");
} catch (RuntimeException e) {
assertEquals("Failed to generate document", e.getMessage());
assertThat(e.getCause().toString(), containsString("IOException"));
@@ -202,7 +197,8 @@ public class InvokeStaticMethodTest {
try {
testGenerateMojoResources.execute();
- fail("Run 'testGenerateMojoInvokeGenerateUtilGenerateDocument' failed,
expected to occur RuntimeException but not");
+ fail(
+ "Run 'testGenerateMojoInvokeGenerateUtilGenerateDocument' failed,
expected to occur RuntimeException but not");
} catch (RuntimeException e) {
assertEquals("Failed to generate document", e.getMessage());
assertThat(e.getCause().toString(), containsString("RuntimeException"));
diff --git
a/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/TestResourcesEx.java
b/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/TestResourcesEx.java
index 5796d50..98668a2 100644
---
a/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/TestResourcesEx.java
+++
b/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/TestResourcesEx.java
@@ -30,7 +30,7 @@ import org.apache.maven.plugin.testing.MojoRule;
import org.apache.maven.plugin.testing.resources.TestResources;
import org.apache.servicecomb.toolkit.common.ClassMaker;
-public class TestResourcesEx {
+class TestResourcesEx {
private String basedirWithContract;
@@ -52,19 +52,17 @@ public class TestResourcesEx {
this.contractDestination = resources.getBasedir("contract-destination") +
File.separator;
}
- void createMojo(MojoRule rule, String basedir, String pluginGoal) throws
Exception {
-
+ void createMojo(MojoRule rule, String pluginGoal) {
this.rule = rule;
-
- File pomPath = new File(basedir, "pom.xml");
- this.testMojo = (AbstractMojo) this.rule.lookupMojo(pluginGoal, pomPath);
+ this.testMojo = mockMojo(pluginGoal);
}
void setVariableValueToObject(String variable, Object value) throws
IllegalAccessException {
this.rule.setVariableValueToObject(this.testMojo, variable, value);
}
- String getVariableValueFromObject(String variable) throws
IllegalAccessException {
+ String getVariableValueFromObject(@SuppressWarnings("SameParameterValue")
String variable)
+ throws IllegalAccessException {
return (String) this.rule.getVariableValueFromObject(this.testMojo,
variable);
}
@@ -80,7 +78,7 @@ public class TestResourcesEx {
return contractLocation;
}
- public String getContractDestination() {
+ String getContractDestination() {
return contractDestination;
}
@@ -98,4 +96,15 @@ public class TestResourcesEx {
return runtimeUrlPath;
}
+
+ private AbstractMojo mockMojo(String pluginGoal) {
+ switch (pluginGoal) {
+ case "generate":
+ return new GenerateMojo();
+ case "verify":
+ return new VerifyMojo();
+ default:
+ throw new RuntimeException("undefined plugin goal");
+ }
+ }
}
diff --git
a/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/VerifyMojoTest.java
b/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/VerifyMojoTest.java
index d604d91..68d3fc0 100644
---
a/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/VerifyMojoTest.java
+++
b/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/VerifyMojoTest.java
@@ -44,7 +44,7 @@ public class VerifyMojoTest {
String testDirWithContract = testResourcesEx.getBasedirWithContract();
- testResourcesEx.createMojo(rule, testDirWithContract, "verify");
+ testResourcesEx.createMojo(rule, "verify");
MavenProject project = mock(MavenProject.class);
testResourcesEx.setVariableValueToObject("project", project);
diff --git a/toolkit-maven-plugin/src/test/projects/demo-with-contract/pom.xml
b/toolkit-maven-plugin/src/test/projects/demo-with-contract/pom.xml
index c5c13f7..a9246e0 100755
--- a/toolkit-maven-plugin/src/test/projects/demo-with-contract/pom.xml
+++ b/toolkit-maven-plugin/src/test/projects/demo-with-contract/pom.xml
@@ -41,21 +41,5 @@
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
-
</dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.servicecomb.toolkit</groupId>
- <artifactId>toolkit-maven-plugin</artifactId>
- <version>${project.version}</version>
- <extensions>true</extensions>
- <configuration>
- <!-- Set to 'code' to resolve the current project. Set to 'contract'
to resolve the contract file for the specified path.If not set, the default is
'code' -->
- <sourceType>code</sourceType>
- </configuration>
- </plugin>
- </plugins>
- </build>
</project>
\ No newline at end of file
diff --git
a/toolkit-maven-plugin/src/test/projects/demo-without-contract/pom.xml
b/toolkit-maven-plugin/src/test/projects/demo-without-contract/pom.xml
index 5058438..72e6d41 100755
--- a/toolkit-maven-plugin/src/test/projects/demo-without-contract/pom.xml
+++ b/toolkit-maven-plugin/src/test/projects/demo-without-contract/pom.xml
@@ -38,19 +38,4 @@
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.servicecomb.toolkit</groupId>
- <artifactId>toolkit-maven-plugin</artifactId>
- <version>${project.version}</version>
- <extensions>true</extensions>
- <configuration>
- <!-- Set to 'code' to resolve the current project. Set to 'contract'
to resolve the contract file for the specified path.If not set, the default is
'code' -->
- <sourceType>code</sourceType>
- </configuration>
- </plugin>
- </plugins>
- </build>
</project>
\ No newline at end of file