You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ni...@apache.org on 2019/07/10 03:49:39 UTC
[servicecomb-toolkit] 06/08: SCB-1351 Some issues about using
toolkit plugin
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
commit 54fd93f13b6fdb7efa8e06b2b85c19e2a0284a1c
Author: MabinGo <bi...@huawei.com>
AuthorDate: Mon Jul 8 10:29:42 2019 +0800
SCB-1351 Some issues about using toolkit plugin
Optimize the code according to user feedback and supplement UT
Signed-off-by: MabinGo <bi...@huawei.com>
---
.../servicecomb/toolkit/cli/DocGenerate.java | 2 +-
.../servicecomb/toolkit/common/DocumentType.java | 22 ++++++++
.../servicecomb/toolkit/common/SourceType.java | 23 ++++++++
.../docgen/ContractsSwaggerUIGenerator.java | 2 +-
docgen/src/test/java/docgen/DocGeneratorTest.java | 2 +-
.../toolkit/plugin/GenerateContractsDocMojo.java | 58 +++++++++++++++-----
.../java/plugin/GenerateContractsDocMojoTest.java | 59 +++++++++++++++++++--
.../classes-no-contract/demo/Application.class | Bin 0 -> 745 bytes
.../classes/demo/Application.class | Bin 0 -> 745 bytes
.../classes/demo/HelloEndPoint.class | Bin 0 -> 1243 bytes
.../projects/project-generateContractsDoc/pom.xml | 4 +-
11 files changed, 149 insertions(+), 23 deletions(-)
diff --git a/cli/src/main/java/org/apache/servicecomb/toolkit/cli/DocGenerate.java b/cli/src/main/java/org/apache/servicecomb/toolkit/cli/DocGenerate.java
index 889cd6c..3f880e6 100755
--- a/cli/src/main/java/org/apache/servicecomb/toolkit/cli/DocGenerate.java
+++ b/cli/src/main/java/org/apache/servicecomb/toolkit/cli/DocGenerate.java
@@ -50,7 +50,7 @@ public class DocGenerate implements Runnable {
@Option(name = {"-f", "--format"}, title = "document format", required = false,
description = "format of document, as swagger-ui or asciidoc-html (swagger-ui by default)")
- private String format = "swagger-ui";
+ private String format = "html";
@Option(name = {"-o", "--output"}, title = "output directory",
description = "location of the generated document (current dir by default)")
diff --git a/common/src/main/java/org/apache/servicecomb/toolkit/common/DocumentType.java b/common/src/main/java/org/apache/servicecomb/toolkit/common/DocumentType.java
new file mode 100644
index 0000000..d23f343
--- /dev/null
+++ b/common/src/main/java/org/apache/servicecomb/toolkit/common/DocumentType.java
@@ -0,0 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.servicecomb.toolkit.common;
+
+public enum DocumentType {
+ HTML
+}
diff --git a/common/src/main/java/org/apache/servicecomb/toolkit/common/SourceType.java b/common/src/main/java/org/apache/servicecomb/toolkit/common/SourceType.java
new file mode 100644
index 0000000..c92ea92
--- /dev/null
+++ b/common/src/main/java/org/apache/servicecomb/toolkit/common/SourceType.java
@@ -0,0 +1,23 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.servicecomb.toolkit.common;
+
+public enum SourceType {
+ CODE,
+ CONTRACT
+}
diff --git a/docgen/src/main/java/org/apache/servicecomb/toolkit/docgen/ContractsSwaggerUIGenerator.java b/docgen/src/main/java/org/apache/servicecomb/toolkit/docgen/ContractsSwaggerUIGenerator.java
index 9387d2d..925a371 100755
--- a/docgen/src/main/java/org/apache/servicecomb/toolkit/docgen/ContractsSwaggerUIGenerator.java
+++ b/docgen/src/main/java/org/apache/servicecomb/toolkit/docgen/ContractsSwaggerUIGenerator.java
@@ -41,7 +41,7 @@ public class ContractsSwaggerUIGenerator implements DocGenerator {
private static Pattern variablePattern = Pattern.compile("(?<=\\{\\{)[a-zA-Z0-9_-]*(?=\\}\\})");
- private final static String DOC_FORMAT = "swagger-ui";
+ private final static String DOC_FORMAT = "html";
private final static String DOC_SUFFIX = ".html";
diff --git a/docgen/src/test/java/docgen/DocGeneratorTest.java b/docgen/src/test/java/docgen/DocGeneratorTest.java
index ff21cf3..f7d4de5 100755
--- a/docgen/src/test/java/docgen/DocGeneratorTest.java
+++ b/docgen/src/test/java/docgen/DocGeneratorTest.java
@@ -56,7 +56,7 @@ public class DocGeneratorTest {
Path tempDir = Files.createTempDirectory(null);
Path outputPath = Paths.get(tempDir.toFile().getAbsolutePath()
+ File.separator + "swagger-ui.html");
- DocGenerator docGenerator = GeneratorFactory.getGenerator(DocGenerator.class, "swagger-ui");
+ DocGenerator docGenerator = GeneratorFactory.getGenerator(DocGenerator.class, "html");
Map<String, Object> docGeneratorConfig = new HashMap<>();
docGeneratorConfig.put("contractContent", swagger);
docGeneratorConfig.put("outputPath", outputPath.toFile().getCanonicalPath());
diff --git a/toolkit-maven-plugin/src/main/java/org/apache/servicecomb/toolkit/plugin/GenerateContractsDocMojo.java b/toolkit-maven-plugin/src/main/java/org/apache/servicecomb/toolkit/plugin/GenerateContractsDocMojo.java
index 84ba37d..641441b 100755
--- a/toolkit-maven-plugin/src/main/java/org/apache/servicecomb/toolkit/plugin/GenerateContractsDocMojo.java
+++ b/toolkit-maven-plugin/src/main/java/org/apache/servicecomb/toolkit/plugin/GenerateContractsDocMojo.java
@@ -41,6 +41,7 @@ import org.apache.maven.project.MavenProject;
import org.apache.servicecomb.swagger.SwaggerUtils;
import org.apache.servicecomb.toolkit.DocGenerator;
import org.apache.servicecomb.toolkit.GeneratorFactory;
+import org.apache.servicecomb.toolkit.common.SourceType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -50,34 +51,65 @@ import org.slf4j.LoggerFactory;
)
public class GenerateContractsDocMojo extends AbstractMojo {
- private static Logger LOGGER = LoggerFactory.getLogger(ContractGenerator.class);
+ private static Logger LOGGER = LoggerFactory.getLogger(GenerateContractsDocMojo.class);
@Parameter(defaultValue = "${project}")
private MavenProject project;
+ @Parameter(defaultValue = "code")
+ private String sourceType;
+
@Parameter(defaultValue = "contractLocation")
private String contractLocation;
- @Parameter(defaultValue = "swagger-ui")
- private String docType;
+ @Parameter(defaultValue = "html")
+ private String documentType;
- @Parameter(defaultValue = "docOutput")
- private String docOutput;
+ @Parameter(defaultValue = "documentOutput")
+ private String documentOutput;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
try {
- File file = new File(contractLocation);
- if (!file.exists()) {
- throw new MojoFailureException("contract location is not exists");
- }
- if (Objects.requireNonNull(file.listFiles()).length == 0) {
- throw new MojoFailureException(contractLocation + " has no contract files");
+ switch (SourceType.valueOf(sourceType.toUpperCase())) {
+ case CODE:
+ File tmpFileDir = new File("target/tmp-contract-dir");
+ if (!tmpFileDir.exists()) {
+ if (!tmpFileDir.mkdirs()) {
+ throw new MojoFailureException("fail to create directory to save contract");
+ }
+ }
+
+ Path tmpPath = Files.createTempDirectory(Paths.get(tmpFileDir.toURI()), "");
+ contractLocation = tmpPath.toFile().getCanonicalPath();
+ LOGGER.debug("temporary contract location is " + contractLocation);
+
+ ContractGenerator contractGenerator = new ContractGenerator(project);
+ contractGenerator.generateAndOutput(contractLocation, "");
+ if (Objects.requireNonNull(tmpFileDir.listFiles()).length == 0) {
+ LOGGER.info("no contract in the code");
+ return;
+ }
+
+ break;
+ case CONTRACT:
+ File file = new File(contractLocation);
+ if (!file.exists()) {
+ throw new MojoFailureException("contract location is not exists");
+ }
+
+ if (Objects.requireNonNull(file.listFiles()).length == 0) {
+ throw new MojoFailureException(contractLocation + " has no contract files");
+ }
+
+ break;
+ default:
+ throw new MojoFailureException(sourceType + " is not supported now");
}
- DocGenerator docGenerator = GeneratorFactory.getGenerator(DocGenerator.class, docType);
+ DocGenerator docGenerator = GeneratorFactory.getGenerator(DocGenerator.class, documentType);
if (docGenerator == null) {
throw new MojoFailureException("DocGenerator's implementation is not found");
}
@@ -89,7 +121,7 @@ public class GenerateContractsDocMojo extends AbstractMojo {
Map<String, Object> docGeneratorConfig = new HashMap<>();
docGeneratorConfig.put("contractContent", SwaggerUtils.parseSwagger(file.toUri().toURL()));
- docGeneratorConfig.put("outputPath", docOutput + File.separator
+ docGeneratorConfig.put("outputPath", documentOutput + File.separator
+ file.toFile().getName().substring(0, file.toFile().getName().indexOf(".")));
docGenerator.configure(docGeneratorConfig);
if (!docGenerator.generate()) {
diff --git a/toolkit-maven-plugin/src/test/java/plugin/GenerateContractsDocMojoTest.java b/toolkit-maven-plugin/src/test/java/plugin/GenerateContractsDocMojoTest.java
index a6e5608..1c301df 100755
--- a/toolkit-maven-plugin/src/test/java/plugin/GenerateContractsDocMojoTest.java
+++ b/toolkit-maven-plugin/src/test/java/plugin/GenerateContractsDocMojoTest.java
@@ -24,16 +24,22 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+import static org.mockito.BDDMockito.given;
+import static org.mockito.Mockito.mock;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.testing.MojoRule;
import org.apache.maven.plugin.testing.resources.TestResources;
+import org.apache.maven.project.MavenProject;
import org.junit.Rule;
import org.junit.Test;
@@ -67,19 +73,48 @@ public class GenerateContractsDocMojoTest {
assertTrue((new File(testDocumentDir)).mkdir());
}
+ final MavenProject project = mock(MavenProject.class);
+
+ // code has no contract
+ List<String> runtimeUrlPath = new ArrayList<>();
+ runtimeUrlPath.add(baseDir + "/classes-no-contract");
+ given(project.getRuntimeClasspathElements()).willReturn(runtimeUrlPath);
+ rule.setVariableValueToObject(generateContractsDocMojo, "project", project);
+
try {
- rule.setVariableValueToObject(generateContractsDocMojo, "contractLocation", testContractDir);
- rule.setVariableValueToObject(generateContractsDocMojo, "docOutput", testDocumentDir);
- rule.setVariableValueToObject(generateContractsDocMojo, "docType", "swagger-ui");
+ rule.setVariableValueToObject(generateContractsDocMojo, "sourceType", "code");
+ generateContractsDocMojo.execute();
+
+ assertEquals(0, Objects.requireNonNull(
+ Paths.get(rule.getVariableValueFromObject(generateContractsDocMojo, "contractLocation").toString()).toFile()
+ .listFiles()).length);
+ } catch (MojoFailureException e) {
+ fail();
+ }
+
+ // code has contract
+ runtimeUrlPath.remove(0);
+ runtimeUrlPath.add(baseDir + "/classes");
+ given(project.getRuntimeClasspathElements()).willReturn(runtimeUrlPath);
+ rule.setVariableValueToObject(generateContractsDocMojo, "project", project);
+
+ try {
+ rule.setVariableValueToObject(generateContractsDocMojo, "sourceType", "code");
+ rule.setVariableValueToObject(generateContractsDocMojo, "documentOutput", testDocumentDir);
+ rule.setVariableValueToObject(generateContractsDocMojo, "documentType", "html");
generateContractsDocMojo.execute();
+
assertNotEquals(0, Files.list(Paths.get(testDocumentDir)).count());
} catch (MojoFailureException | IOException e) {
fail();
}
try {
+ rule.setVariableValueToObject(generateContractsDocMojo, "sourceType", "contract");
+ rule.setVariableValueToObject(generateContractsDocMojo, "documentType", "html");
rule.setVariableValueToObject(generateContractsDocMojo, "contractLocation", "");
generateContractsDocMojo.execute();
+
rule.setVariableValueToObject(generateContractsDocMojo, "contractLocation", "nonexitstdir");
generateContractsDocMojo.execute();
} catch (MojoFailureException e) {
@@ -91,6 +126,8 @@ public class GenerateContractsDocMojoTest {
assertTrue((new File(testEmptyDir)).mkdir());
}
try {
+ rule.setVariableValueToObject(generateContractsDocMojo, "sourceType", "contract");
+ rule.setVariableValueToObject(generateContractsDocMojo, "documentType", "html");
rule.setVariableValueToObject(generateContractsDocMojo, "contractLocation", testEmptyDir);
generateContractsDocMojo.execute();
} catch (MojoFailureException e) {
@@ -98,8 +135,20 @@ public class GenerateContractsDocMojoTest {
}
try {
- rule.setVariableValueToObject(generateContractsDocMojo, "contractLocation", testDocumentDir);
- rule.setVariableValueToObject(generateContractsDocMojo, "docType", "nonImpl");
+ rule.setVariableValueToObject(generateContractsDocMojo, "sourceType", "contract");
+ rule.setVariableValueToObject(generateContractsDocMojo, "documentType", "html");
+ rule.setVariableValueToObject(generateContractsDocMojo, "contractLocation", testContractDir);
+ rule.setVariableValueToObject(generateContractsDocMojo, "documentType", "nonImpl");
+ generateContractsDocMojo.execute();
+ } catch (MojoFailureException e) {
+ assertEquals("DocGenerator's implementation is not found", e.getMessage());
+ }
+
+ try {
+ rule.setVariableValueToObject(generateContractsDocMojo, "sourceType", "contract");
+ rule.setVariableValueToObject(generateContractsDocMojo, "documentType", "html");
+ rule.setVariableValueToObject(generateContractsDocMojo, "contractLocation", testContractDir);
+ rule.setVariableValueToObject(generateContractsDocMojo, "documentType", testDocumentDir);
generateContractsDocMojo.execute();
} catch (MojoFailureException e) {
assertEquals("DocGenerator's implementation is not found", e.getMessage());
diff --git a/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/classes-no-contract/demo/Application.class b/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/classes-no-contract/demo/Application.class
new file mode 100644
index 0000000..df126b8
Binary files /dev/null and b/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/classes-no-contract/demo/Application.class differ
diff --git a/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/classes/demo/Application.class b/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/classes/demo/Application.class
new file mode 100644
index 0000000..df126b8
Binary files /dev/null and b/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/classes/demo/Application.class differ
diff --git a/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/classes/demo/HelloEndPoint.class b/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/classes/demo/HelloEndPoint.class
new file mode 100644
index 0000000..c90473d
Binary files /dev/null and b/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/classes/demo/HelloEndPoint.class differ
diff --git a/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/pom.xml b/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/pom.xml
index 464dc22..a2d913e 100755
--- a/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/pom.xml
+++ b/toolkit-maven-plugin/src/test/projects/project-generateContractsDoc/pom.xml
@@ -37,8 +37,8 @@
<extensions>true</extensions>
<configuration>
<contractLocation>./contract</contractLocation>
- <docOutput>./document</docOutput>
- <docType>swagger-ui</docType>
+ <documentOutput>./document</documentOutput>
+ <documentType>html</documentType>
</configuration>
</plugin>
</plugins>