From 9ffd6cee41d2ce246313d25c524ebb7b80762c99 Mon Sep 17 00:00:00 2001 From: Sjoerd Talsma Date: Thu, 15 Sep 2022 21:35:46 +0200 Subject: [PATCH 01/12] Extract PlantUML generation into separate package. Signed-off-by: Sjoerd Talsma --- pom.xml | 2 +- .../talsmasoftware/umldoclet/uml/Diagram.java | 19 +++++--- .../uml/plantuml/PlantumlGenerator.java | 43 +++++++++++++++++++ 3 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java diff --git a/pom.xml b/pom.xml index e1600c71f..d1c3a500b 100644 --- a/pom.xml +++ b/pom.xml @@ -156,8 +156,8 @@ .gitignore .codebeatignore .codacy.yml - .lift.toml .github/** + .lift/** .mvn/** mvnw* lib/** diff --git a/src/main/java/nl/talsmasoftware/umldoclet/uml/Diagram.java b/src/main/java/nl/talsmasoftware/umldoclet/uml/Diagram.java index 2f2f0a67f..5e97ef902 100644 --- a/src/main/java/nl/talsmasoftware/umldoclet/uml/Diagram.java +++ b/src/main/java/nl/talsmasoftware/umldoclet/uml/Diagram.java @@ -16,15 +16,18 @@ package nl.talsmasoftware.umldoclet.uml; import net.sourceforge.plantuml.FileFormat; -import net.sourceforge.plantuml.FileFormatOption; -import net.sourceforge.plantuml.SourceStringReader; import nl.talsmasoftware.umldoclet.configuration.Configuration; import nl.talsmasoftware.umldoclet.configuration.ImageConfig; import nl.talsmasoftware.umldoclet.logging.Message; import nl.talsmasoftware.umldoclet.rendering.indent.IndentingPrintWriter; import nl.talsmasoftware.umldoclet.rendering.writers.StringBufferingWriter; +import nl.talsmasoftware.umldoclet.uml.plantuml.PlantumlGenerator; -import java.io.*; +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.nio.file.Files; import java.util.Objects; import java.util.stream.Stream; @@ -37,6 +40,8 @@ */ public abstract class Diagram extends UMLNode { + private static final PlantumlGenerator PLANTUML_GENERATOR = PlantumlGenerator.autodetect(); + private final Configuration config; private final FileFormat[] formats; private File diagramBaseFile; @@ -79,7 +84,7 @@ public Configuration getConfiguration() { * Determine the physical file location for the plantuml output. * *

This will even be called if {@code -createPumlFiles} is not enabled, - * to determine the {@linkplain #getDiagramBaseFile()}. + * to determine the {@code diagram base file}. * * @return The physical file for the plantuml output. */ @@ -156,15 +161,15 @@ private String writePlantumlSourceToFile() throws IOException { private StringBufferingWriter createBufferingPlantumlFileWriter(File pumlFile) throws IOException { return new StringBufferingWriter( new OutputStreamWriter( - new FileOutputStream(pumlFile), config.umlCharset())); + Files.newOutputStream(pumlFile.toPath()), config.umlCharset())); } private void renderDiagramFile(String plantumlSource, FileFormat format) throws IOException { final File diagramFile = getDiagramFile(format); config.logger().info(Message.INFO_GENERATING_FILE, diagramFile); ensureParentDir(diagramFile); - try (OutputStream out = new FileOutputStream(diagramFile)) { - new SourceStringReader(plantumlSource).outputImage(out, new FileFormatOption(format)); + try (OutputStream out = Files.newOutputStream(diagramFile.toPath())) { + PLANTUML_GENERATOR.generatePlantumlDiagramFromSource(plantumlSource, format, out); } } diff --git a/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java b/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java new file mode 100644 index 000000000..323762c97 --- /dev/null +++ b/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java @@ -0,0 +1,43 @@ +/* + * Copyright 2016-2022 Talsma ICT + * + * Licensed 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 nl.talsmasoftware.umldoclet.uml.plantuml; + +import net.sourceforge.plantuml.FileFormat; +import net.sourceforge.plantuml.FileFormatOption; +import net.sourceforge.plantuml.SourceStringReader; + +import java.io.IOException; +import java.io.OutputStream; + +public abstract class PlantumlGenerator { + + private PlantumlGenerator() { + } + + public static PlantumlGenerator autodetect() { + return new BuiltinPlantumlGenerator(); + } + + public abstract void generatePlantumlDiagramFromSource(String plantumlSource, FileFormat format, OutputStream out) throws IOException; + + private static final class BuiltinPlantumlGenerator extends PlantumlGenerator { + @Override + public void generatePlantumlDiagramFromSource(String plantumlSource, FileFormat format, OutputStream out) throws IOException { + new SourceStringReader(plantumlSource).outputImage(out, new FileFormatOption(format)); + } + } + +} From a57880aad637350f6c5b2c9daffe55b675cd4214 Mon Sep 17 00:00:00 2001 From: Sjoerd Talsma Date: Thu, 15 Sep 2022 21:35:46 +0200 Subject: [PATCH 02/12] Extract PlantUML generation into separate package. Signed-off-by: Sjoerd Talsma --- .../talsmasoftware/umldoclet/uml/Diagram.java | 16 +++---- .../uml/plantuml/PlantumlGenerator.java | 43 +++++++++++++++++++ 2 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java diff --git a/src/main/java/nl/talsmasoftware/umldoclet/uml/Diagram.java b/src/main/java/nl/talsmasoftware/umldoclet/uml/Diagram.java index 6814a6375..010a7f0c2 100644 --- a/src/main/java/nl/talsmasoftware/umldoclet/uml/Diagram.java +++ b/src/main/java/nl/talsmasoftware/umldoclet/uml/Diagram.java @@ -16,20 +16,17 @@ package nl.talsmasoftware.umldoclet.uml; import net.sourceforge.plantuml.FileFormat; -import net.sourceforge.plantuml.FileFormatOption; -import net.sourceforge.plantuml.SourceStringReader; import nl.talsmasoftware.umldoclet.configuration.Configuration; import nl.talsmasoftware.umldoclet.configuration.ImageConfig; import nl.talsmasoftware.umldoclet.logging.Message; import nl.talsmasoftware.umldoclet.rendering.indent.IndentingPrintWriter; import nl.talsmasoftware.umldoclet.rendering.writers.StringBufferingWriter; +import nl.talsmasoftware.umldoclet.uml.plantuml.PlantumlGenerator; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; -import java.util.List; import java.util.Objects; import java.util.stream.Stream; @@ -43,6 +40,9 @@ * Abstract class corresponding to a single UML diagram. */ public abstract class Diagram extends UMLNode { + + private static final PlantumlGenerator PLANTUML_GENERATOR = PlantumlGenerator.autodetect(); + private final Configuration config; private final FileFormat[] formats; private File diagramBaseFile; @@ -90,7 +90,7 @@ public Configuration getConfiguration() { * Determine the physical file location for the plantuml output. * *

This will even be called if {@code -createPumlFiles} is not enabled, - * to determine the {@linkplain #getDiagramBaseFile()}. + * to determine the {@code diagram base file}. * * @return The physical file for the plantuml output. */ @@ -167,15 +167,15 @@ private String writePlantumlSourceToFile() throws IOException { private StringBufferingWriter createBufferingPlantumlFileWriter(File pumlFile) throws IOException { return new StringBufferingWriter( new OutputStreamWriter( - new FileOutputStream(pumlFile), config.umlCharset())); + Files.newOutputStream(pumlFile.toPath()), config.umlCharset())); } private void renderDiagramFile(String plantumlSource, FileFormat format) throws IOException { final File diagramFile = getDiagramFile(format); config.logger().info(Message.INFO_GENERATING_FILE, diagramFile); ensureParentDir(diagramFile); - try (OutputStream out = new FileOutputStream(diagramFile)) { - new SourceStringReader(plantumlSource).outputImage(out, new FileFormatOption(format)); + try (OutputStream out = Files.newOutputStream(diagramFile.toPath())) { + PLANTUML_GENERATOR.generatePlantumlDiagramFromSource(plantumlSource, format, out); } } diff --git a/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java b/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java new file mode 100644 index 000000000..323762c97 --- /dev/null +++ b/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java @@ -0,0 +1,43 @@ +/* + * Copyright 2016-2022 Talsma ICT + * + * Licensed 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 nl.talsmasoftware.umldoclet.uml.plantuml; + +import net.sourceforge.plantuml.FileFormat; +import net.sourceforge.plantuml.FileFormatOption; +import net.sourceforge.plantuml.SourceStringReader; + +import java.io.IOException; +import java.io.OutputStream; + +public abstract class PlantumlGenerator { + + private PlantumlGenerator() { + } + + public static PlantumlGenerator autodetect() { + return new BuiltinPlantumlGenerator(); + } + + public abstract void generatePlantumlDiagramFromSource(String plantumlSource, FileFormat format, OutputStream out) throws IOException; + + private static final class BuiltinPlantumlGenerator extends PlantumlGenerator { + @Override + public void generatePlantumlDiagramFromSource(String plantumlSource, FileFormat format, OutputStream out) throws IOException { + new SourceStringReader(plantumlSource).outputImage(out, new FileFormatOption(format)); + } + } + +} From 4d6e62e32b43a38babe71f7c73bb1712538e5dde Mon Sep 17 00:00:00 2001 From: Sjoerd Talsma Date: Fri, 18 Nov 2022 16:04:10 +0100 Subject: [PATCH 03/12] Commit dangling change from local branch pre-merge Signed-off-by: Sjoerd Talsma --- src/main/java/nl/talsmasoftware/umldoclet/uml/Diagram.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/nl/talsmasoftware/umldoclet/uml/Diagram.java b/src/main/java/nl/talsmasoftware/umldoclet/uml/Diagram.java index 010a7f0c2..4f4458b8b 100644 --- a/src/main/java/nl/talsmasoftware/umldoclet/uml/Diagram.java +++ b/src/main/java/nl/talsmasoftware/umldoclet/uml/Diagram.java @@ -27,6 +27,8 @@ import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; +import java.nio.file.Files; +import java.util.List; import java.util.Objects; import java.util.stream.Stream; @@ -37,7 +39,7 @@ import static nl.talsmasoftware.umldoclet.util.FileUtils.withoutExtension; /** - * Abstract class corresponding to a single UML diagram. + * Abstract UML Diagram class. */ public abstract class Diagram extends UMLNode { From 8aa3dc3fd21ec2689d4728e9223d0b1039ebc794 Mon Sep 17 00:00:00 2001 From: Sjoerd Talsma Date: Sun, 11 Dec 2022 12:20:01 +0100 Subject: [PATCH 04/12] Change PlantumlGenerator class to proper interface. Signed-off-by: Sjoerd Talsma --- .../plantuml/BuiltinPlantumlGenerator.java | 30 +++++++++++++++++++ .../uml/plantuml/PlantumlGenerator.java | 18 ++--------- 2 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/BuiltinPlantumlGenerator.java diff --git a/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/BuiltinPlantumlGenerator.java b/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/BuiltinPlantumlGenerator.java new file mode 100644 index 000000000..3c251e2bb --- /dev/null +++ b/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/BuiltinPlantumlGenerator.java @@ -0,0 +1,30 @@ +/* + * Copyright 2016-2022 Talsma ICT + * + * Licensed 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 nl.talsmasoftware.umldoclet.uml.plantuml; + +import net.sourceforge.plantuml.FileFormat; +import net.sourceforge.plantuml.FileFormatOption; +import net.sourceforge.plantuml.SourceStringReader; + +import java.io.IOException; +import java.io.OutputStream; + +public final class BuiltinPlantumlGenerator implements PlantumlGenerator { + @Override + public void generatePlantumlDiagramFromSource(String plantumlSource, FileFormat format, OutputStream out) throws IOException { + new SourceStringReader(plantumlSource).outputImage(out, new FileFormatOption(format)); + } +} diff --git a/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java b/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java index 323762c97..bd7ce60a6 100644 --- a/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java +++ b/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java @@ -16,28 +16,16 @@ package nl.talsmasoftware.umldoclet.uml.plantuml; import net.sourceforge.plantuml.FileFormat; -import net.sourceforge.plantuml.FileFormatOption; -import net.sourceforge.plantuml.SourceStringReader; import java.io.IOException; import java.io.OutputStream; -public abstract class PlantumlGenerator { +public interface PlantumlGenerator { - private PlantumlGenerator() { - } - - public static PlantumlGenerator autodetect() { + static PlantumlGenerator autodetect() { return new BuiltinPlantumlGenerator(); } - public abstract void generatePlantumlDiagramFromSource(String plantumlSource, FileFormat format, OutputStream out) throws IOException; - - private static final class BuiltinPlantumlGenerator extends PlantumlGenerator { - @Override - public void generatePlantumlDiagramFromSource(String plantumlSource, FileFormat format, OutputStream out) throws IOException { - new SourceStringReader(plantumlSource).outputImage(out, new FileFormatOption(format)); - } - } + void generatePlantumlDiagramFromSource(String plantumlSource, FileFormat format, OutputStream out) throws IOException; } From 0975a4f7e8f91acd6fb21024cd6c6d1b9f16aad1 Mon Sep 17 00:00:00 2001 From: Sjoerd Talsma Date: Sun, 11 Dec 2022 16:02:16 +0100 Subject: [PATCH 05/12] Use TestContainers to develop unit test for RemotePlantumlGeneratorTest. Signed-off-by: Sjoerd Talsma --- pom.xml | 6 ++ .../uml/plantuml/RemotePlantumlGenerator.java | 53 ++++++++++++++++ .../plantuml/RemotePlantumlGeneratorTest.java | 61 +++++++++++++++++++ .../{logback.xml => logback-test.xml} | 1 - 4 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGenerator.java create mode 100644 src/test/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGeneratorTest.java rename src/test/resources/{logback.xml => logback-test.xml} (96%) diff --git a/pom.xml b/pom.xml index 8e4dd424e..27e68b6d9 100644 --- a/pom.xml +++ b/pom.xml @@ -325,6 +325,12 @@ ${junit.version} test + + org.testcontainers + junit-jupiter + 1.17.6 + test + org.hamcrest hamcrest diff --git a/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGenerator.java b/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGenerator.java new file mode 100644 index 000000000..c149ec796 --- /dev/null +++ b/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGenerator.java @@ -0,0 +1,53 @@ +/* + * Copyright 2016-2022 Talsma ICT + * + * Licensed 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 nl.talsmasoftware.umldoclet.uml.plantuml; + +import net.sourceforge.plantuml.FileFormat; +import net.sourceforge.plantuml.code.ArobaseStringCompressor; +import net.sourceforge.plantuml.code.AsciiEncoder; +import net.sourceforge.plantuml.code.CompressionZlib; +import net.sourceforge.plantuml.code.Transcoder; +import net.sourceforge.plantuml.code.TranscoderImpl; + +import java.io.IOException; +import java.io.OutputStream; + +import static java.util.Objects.requireNonNull; + +public class RemotePlantumlGenerator implements PlantumlGenerator { + private final String baseUrl; + private final Transcoder transcoder = + TranscoderImpl.utf8(new AsciiEncoder(), new ArobaseStringCompressor(), new CompressionZlib()); + + public RemotePlantumlGenerator(final String baseUrl) { + this.baseUrl = requireNonNull(baseUrl, "Base URL for remote PlantUML server url is ."); + } + + @Override + public void generatePlantumlDiagramFromSource(String plantumlSource, FileFormat format, OutputStream out) { + final String encodedDiagram = encodeDiagram(plantumlSource); + System.out.println(encodedDiagram); + } + + private String encodeDiagram(final String diagramSource) { + try { + return transcoder.encode(requireNonNull(diagramSource, "UML diagram source was .")); + } catch (IOException ioe) { + throw new IllegalStateException("Error encoding diagram: " + ioe.getMessage(), ioe); + } + } + +} diff --git a/src/test/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGeneratorTest.java b/src/test/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGeneratorTest.java new file mode 100644 index 000000000..cc316fe9a --- /dev/null +++ b/src/test/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGeneratorTest.java @@ -0,0 +1,61 @@ +/* + * Copyright 2016-2022 Talsma ICT + * + * Licensed 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 nl.talsmasoftware.umldoclet.uml.plantuml; + +import net.sourceforge.plantuml.FileFormat; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.utility.DockerImageName; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.greaterThan; + +@Testcontainers +class RemotePlantumlGeneratorTest { + static final String testUml = "@startuml\r\nBob -> Alice : hello\r\n@enduml"; + + @Container + GenericContainer plantumlServer = new GenericContainer(DockerImageName.parse("plantuml/plantuml-server")) + .withExposedPorts(8080); + + PlantumlGenerator subject; + + @BeforeEach + void setUp() { + subject = new RemotePlantumlGenerator(String.format("https://%s:%s/plantuml/", + plantumlServer.getHost(), plantumlServer.getMappedPort(8080))); + } + + @Test + void simpleDiagramCanBeGenerated() throws IOException { + // prepare + final ByteArrayOutputStream bos = new ByteArrayOutputStream(); + + // execute + subject.generatePlantumlDiagramFromSource(testUml, FileFormat.SVG, bos); + + // verify + final byte[] bytes = bos.toByteArray(); + assertThat("Diagram size", bytes.length, greaterThan(0)); + } + +} diff --git a/src/test/resources/logback.xml b/src/test/resources/logback-test.xml similarity index 96% rename from src/test/resources/logback.xml rename to src/test/resources/logback-test.xml index 5c2c51bd4..5131db2ad 100644 --- a/src/test/resources/logback.xml +++ b/src/test/resources/logback-test.xml @@ -1,7 +1,6 @@ - true From 1822fa34197b6e3e4810a48262d4d0f0826281ca Mon Sep 17 00:00:00 2001 From: Sjoerd Talsma Date: Fri, 16 Dec 2022 15:24:53 +0100 Subject: [PATCH 06/12] Implement a RemotePlantumlGenerator that uses a PlantUML server base url. Signed-off-by: Sjoerd Talsma --- .../uml/plantuml/PlantumlGenerator.java | 1 + .../uml/plantuml/RemotePlantumlGenerator.java | 24 +++++++++++++++++-- .../plantuml/RemotePlantumlGeneratorTest.java | 23 +++++++++++------- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java b/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java index bd7ce60a6..3c212690e 100644 --- a/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java +++ b/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java @@ -23,6 +23,7 @@ public interface PlantumlGenerator { static PlantumlGenerator autodetect() { +// return new RemotePlantumlGenerator(); return new BuiltinPlantumlGenerator(); } diff --git a/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGenerator.java b/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGenerator.java index c149ec796..cde43e23e 100644 --- a/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGenerator.java +++ b/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGenerator.java @@ -23,27 +23,47 @@ import net.sourceforge.plantuml.code.TranscoderImpl; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; +import java.net.URL; +import java.util.Objects; import static java.util.Objects.requireNonNull; public class RemotePlantumlGenerator implements PlantumlGenerator { + private static final String DEFAULT_PLANTUML_BASE_URL = "https://www.plantuml.com/plantuml/"; + private final String baseUrl; private final Transcoder transcoder = TranscoderImpl.utf8(new AsciiEncoder(), new ArobaseStringCompressor(), new CompressionZlib()); + public RemotePlantumlGenerator() { + this(null); + } + public RemotePlantumlGenerator(final String baseUrl) { - this.baseUrl = requireNonNull(baseUrl, "Base URL for remote PlantUML server url is ."); + String url = Objects.toString(baseUrl, DEFAULT_PLANTUML_BASE_URL); + if (!url.endsWith("/")) url += "/"; + this.baseUrl = url; } @Override public void generatePlantumlDiagramFromSource(String plantumlSource, FileFormat format, OutputStream out) { final String encodedDiagram = encodeDiagram(plantumlSource); - System.out.println(encodedDiagram); + final String diagramUrl = baseUrl + format.name().toLowerCase() + '/' + encodedDiagram; + try (InputStream in = new URL(diagramUrl).openConnection().getInputStream()) { + final byte[] buf = new byte[4096]; + for (int read = in.read(buf); read >= 0; read = in.read(buf)) { + out.write(buf, 0, read); + } + } catch (IOException | RuntimeException e) { + throw new RuntimeException(e); + } } private String encodeDiagram(final String diagramSource) { try { + // TODO internalize transcoder to be able to remove PlantUML dependency altogether. return transcoder.encode(requireNonNull(diagramSource, "UML diagram source was .")); } catch (IOException ioe) { throw new IllegalStateException("Error encoding diagram: " + ioe.getMessage(), ioe); diff --git a/src/test/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGeneratorTest.java b/src/test/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGeneratorTest.java index cc316fe9a..fed92e7af 100644 --- a/src/test/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGeneratorTest.java +++ b/src/test/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGeneratorTest.java @@ -23,11 +23,13 @@ import org.testcontainers.junit.jupiter.Testcontainers; import org.testcontainers.utility.DockerImageName; -import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.is; @Testcontainers class RemotePlantumlGeneratorTest { @@ -41,21 +43,26 @@ class RemotePlantumlGeneratorTest { @BeforeEach void setUp() { - subject = new RemotePlantumlGenerator(String.format("https://%s:%s/plantuml/", - plantumlServer.getHost(), plantumlServer.getMappedPort(8080))); +// subject = new RemotePlantumlGenerator(String.format("http://%s:%s/", +// plantumlServer.getHost(), plantumlServer.getMappedPort(8080))); + subject = new RemotePlantumlGenerator("https://www.plantuml.com/plantuml/"); } @Test void simpleDiagramCanBeGenerated() throws IOException { // prepare - final ByteArrayOutputStream bos = new ByteArrayOutputStream(); + final File testDiagram = new File("target/test-classes/" + + getClass().getPackageName().replace('.', '/') + + "/testUml.svg"); + testDiagram.delete(); // execute - subject.generatePlantumlDiagramFromSource(testUml, FileFormat.SVG, bos); + try (OutputStream out = new FileOutputStream(testDiagram)) { + subject.generatePlantumlDiagramFromSource(testUml, FileFormat.SVG, out); + } // verify - final byte[] bytes = bos.toByteArray(); - assertThat("Diagram size", bytes.length, greaterThan(0)); + assertThat(testDiagram.isFile(), is(true)); } } From de150685a82c09460a3eccc6ca31237ffd469c6f Mon Sep 17 00:00:00 2001 From: Sjoerd Talsma Date: Mon, 19 Dec 2022 21:50:31 +0100 Subject: [PATCH 07/12] Add setting for plantUmlBaseUrl in Configuration. Signed-off-by: Sjoerd Talsma --- .../configuration/Configuration.java | 21 ++++++++++++++++++- .../umldoclet/javadoc/DocletConfig.java | 7 +++++++ .../plantuml/RemotePlantumlGeneratorTest.java | 3 ++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/main/java/nl/talsmasoftware/umldoclet/configuration/Configuration.java b/src/main/java/nl/talsmasoftware/umldoclet/configuration/Configuration.java index c2ad885a8..cbe7b221c 100644 --- a/src/main/java/nl/talsmasoftware/umldoclet/configuration/Configuration.java +++ b/src/main/java/nl/talsmasoftware/umldoclet/configuration/Configuration.java @@ -31,7 +31,26 @@ public interface Configuration { /** - * Tha name of the doclet to delegate main documentation to + * The base URL of the PlantUML server to generate diagrams with. + *

+ * Please note that it is not recommended to use the public, central PlantUML server at + * https://www.plantuml.com/plantuml. + * Although not strictly forbidden by the author of PlantUML, using the central server to generate your + * javadoc diagrams is causing additional load on the central server and is a lot slower than running your own + * local server. + *

+ * Using docker to run a local PlantUML server can be a simple as: + *

{@code
+     * docker run -d -p 8080:8080 plantuml/plantuml-server:latest
+     * }
+ * After that, you can run the UMLDoclet with {@code plantUmlBaseUrl = "http://localhost:8080/"} + * + * @return The base URL of the PlantUML online server to use. + */ + Optional plantUmlBaseUrl(); + + /** + * The name of the doclet to delegate main documentation to * or {@link Optional#empty} if no delegation is wanted. * * @return The name of the doclet to delegate main documentation to diff --git a/src/main/java/nl/talsmasoftware/umldoclet/javadoc/DocletConfig.java b/src/main/java/nl/talsmasoftware/umldoclet/javadoc/DocletConfig.java index ca400fc8a..5f8981aa5 100644 --- a/src/main/java/nl/talsmasoftware/umldoclet/javadoc/DocletConfig.java +++ b/src/main/java/nl/talsmasoftware/umldoclet/javadoc/DocletConfig.java @@ -52,6 +52,8 @@ public class DocletConfig implements Configuration { private final UMLOptions options; private volatile LocalizedReporter reporter; + String plantUmlBaseUrl = null; + /** * The name of the delegate doclet to use for the main documentation task. *

@@ -139,6 +141,11 @@ public Set mergeOptionsWith(Set standard return options.mergeWith(standardOptions); } + @Override + public Optional plantUmlBaseUrl() { + return Optional.ofNullable(plantUmlBaseUrl); + } + @Override public Optional delegateDocletName() { return Optional.ofNullable(delegateDoclet).filter(name -> !"false".equalsIgnoreCase(name)); diff --git a/src/test/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGeneratorTest.java b/src/test/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGeneratorTest.java index fed92e7af..f19794b33 100644 --- a/src/test/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGeneratorTest.java +++ b/src/test/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGeneratorTest.java @@ -45,7 +45,8 @@ class RemotePlantumlGeneratorTest { void setUp() { // subject = new RemotePlantumlGenerator(String.format("http://%s:%s/", // plantumlServer.getHost(), plantumlServer.getMappedPort(8080))); - subject = new RemotePlantumlGenerator("https://www.plantuml.com/plantuml/"); +// subject = new RemotePlantumlGenerator("https://www.plantuml.com/plantuml/"); + subject = new RemotePlantumlGenerator("http://localhost:8080/"); } @Test From b697d2eaa78491bec5326d0da913a6bba84eeb42 Mon Sep 17 00:00:00 2001 From: Sjoerd Talsma Date: Fri, 23 Dec 2022 16:31:03 +0100 Subject: [PATCH 08/12] Restore remote plantuml generator test to use testcontainer. Signed-off-by: Sjoerd Talsma --- .../umldoclet/uml/plantuml/RemotePlantumlGeneratorTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGeneratorTest.java b/src/test/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGeneratorTest.java index f19794b33..3529aa3e7 100644 --- a/src/test/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGeneratorTest.java +++ b/src/test/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGeneratorTest.java @@ -43,10 +43,10 @@ class RemotePlantumlGeneratorTest { @BeforeEach void setUp() { -// subject = new RemotePlantumlGenerator(String.format("http://%s:%s/", -// plantumlServer.getHost(), plantumlServer.getMappedPort(8080))); + subject = new RemotePlantumlGenerator(String.format("http://%s:%s/", + plantumlServer.getHost(), plantumlServer.getMappedPort(8080))); // subject = new RemotePlantumlGenerator("https://www.plantuml.com/plantuml/"); - subject = new RemotePlantumlGenerator("http://localhost:8080/"); +// subject = new RemotePlantumlGenerator("http://localhost:8080/"); } @Test From 4de42ebe21cae7d8d8cc7fc3cddc20e2a16180fb Mon Sep 17 00:00:00 2001 From: Sjoerd Talsma Date: Fri, 23 Dec 2022 16:44:39 +0100 Subject: [PATCH 09/12] Allow plantuml generator to determine the generator based on the Configuration. Signed-off-by: Sjoerd Talsma --- src/main/java/nl/talsmasoftware/umldoclet/uml/Diagram.java | 6 +++--- .../umldoclet/uml/plantuml/PlantumlGenerator.java | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/nl/talsmasoftware/umldoclet/uml/Diagram.java b/src/main/java/nl/talsmasoftware/umldoclet/uml/Diagram.java index 4f4458b8b..517683e58 100644 --- a/src/main/java/nl/talsmasoftware/umldoclet/uml/Diagram.java +++ b/src/main/java/nl/talsmasoftware/umldoclet/uml/Diagram.java @@ -43,15 +43,15 @@ */ public abstract class Diagram extends UMLNode { - private static final PlantumlGenerator PLANTUML_GENERATOR = PlantumlGenerator.autodetect(); - private final Configuration config; + private final PlantumlGenerator plantumlGenerator; private final FileFormat[] formats; private File diagramBaseFile; protected Diagram(Configuration config) { super(null); this.config = requireNonNull(config, "Configuration is "); + this.plantumlGenerator = PlantumlGenerator.getPlantumlGenerator(config); this.formats = config.images().formats().stream() .map(this::toFileFormat).filter(Objects::nonNull) .toArray(FileFormat[]::new); @@ -177,7 +177,7 @@ private void renderDiagramFile(String plantumlSource, FileFormat format) throws config.logger().info(Message.INFO_GENERATING_FILE, diagramFile); ensureParentDir(diagramFile); try (OutputStream out = Files.newOutputStream(diagramFile.toPath())) { - PLANTUML_GENERATOR.generatePlantumlDiagramFromSource(plantumlSource, format, out); + plantumlGenerator.generatePlantumlDiagramFromSource(plantumlSource, format, out); } } diff --git a/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java b/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java index 3c212690e..dec4b9cd3 100644 --- a/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java +++ b/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java @@ -16,13 +16,14 @@ package nl.talsmasoftware.umldoclet.uml.plantuml; import net.sourceforge.plantuml.FileFormat; +import nl.talsmasoftware.umldoclet.configuration.Configuration; import java.io.IOException; import java.io.OutputStream; public interface PlantumlGenerator { - static PlantumlGenerator autodetect() { + static PlantumlGenerator getPlantumlGenerator(Configuration configuration) { // return new RemotePlantumlGenerator(); return new BuiltinPlantumlGenerator(); } From a8cd276088e23b2f9c82e23b4ffc65aaecb8a664 Mon Sep 17 00:00:00 2001 From: Sjoerd Talsma Date: Fri, 23 Dec 2022 16:57:56 +0100 Subject: [PATCH 10/12] Add javadoc option `--plantuml-server-url`. Signed-off-by: Sjoerd Talsma --- .../umldoclet/configuration/Configuration.java | 4 ++-- .../nl/talsmasoftware/umldoclet/javadoc/DocletConfig.java | 6 +++--- .../nl/talsmasoftware/umldoclet/javadoc/UMLOptions.java | 2 ++ .../nl/talsmasoftware/umldoclet/UMLDoclet.properties | 2 ++ 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/java/nl/talsmasoftware/umldoclet/configuration/Configuration.java b/src/main/java/nl/talsmasoftware/umldoclet/configuration/Configuration.java index cbe7b221c..c8e80d3ba 100644 --- a/src/main/java/nl/talsmasoftware/umldoclet/configuration/Configuration.java +++ b/src/main/java/nl/talsmasoftware/umldoclet/configuration/Configuration.java @@ -43,11 +43,11 @@ public interface Configuration { *

{@code
      * docker run -d -p 8080:8080 plantuml/plantuml-server:latest
      * }
- * After that, you can run the UMLDoclet with {@code plantUmlBaseUrl = "http://localhost:8080/"} + * After that, you can run the UMLDoclet with {@code plantumlServerUrl = "http://localhost:8080/"} * * @return The base URL of the PlantUML online server to use. */ - Optional plantUmlBaseUrl(); + Optional plantumlServerUrl(); /** * The name of the doclet to delegate main documentation to diff --git a/src/main/java/nl/talsmasoftware/umldoclet/javadoc/DocletConfig.java b/src/main/java/nl/talsmasoftware/umldoclet/javadoc/DocletConfig.java index 5f8981aa5..85983f3a3 100644 --- a/src/main/java/nl/talsmasoftware/umldoclet/javadoc/DocletConfig.java +++ b/src/main/java/nl/talsmasoftware/umldoclet/javadoc/DocletConfig.java @@ -52,7 +52,7 @@ public class DocletConfig implements Configuration { private final UMLOptions options; private volatile LocalizedReporter reporter; - String plantUmlBaseUrl = null; + String plantumlServerUrl = null; /** * The name of the delegate doclet to use for the main documentation task. @@ -142,8 +142,8 @@ public Set mergeOptionsWith(Set standard } @Override - public Optional plantUmlBaseUrl() { - return Optional.ofNullable(plantUmlBaseUrl); + public Optional plantumlServerUrl() { + return Optional.ofNullable(plantumlServerUrl); } @Override diff --git a/src/main/java/nl/talsmasoftware/umldoclet/javadoc/UMLOptions.java b/src/main/java/nl/talsmasoftware/umldoclet/javadoc/UMLOptions.java index 4c8ef7525..ed7da611d 100644 --- a/src/main/java/nl/talsmasoftware/umldoclet/javadoc/UMLOptions.java +++ b/src/main/java/nl/talsmasoftware/umldoclet/javadoc/UMLOptions.java @@ -80,6 +80,8 @@ private UMLOptions(DocletConfig config, Set standardOpt this.options.add(new Option("-d", 1, Kind.OTHER, args -> config.destDirName = args.get(0))); // Our own options + this.options.add(new Option("--plantuml-server-url -plantumlServerUrl", 1, Kind.STANDARD, + args -> config.plantumlServerUrl = args.get(0))); this.options.add(new Option("--delegate-doclet -delegateDoclet", 1, Kind.STANDARD, args -> config.delegateDoclet = args.get(0))); this.options.add(new Option("--create-puml-files -createPumlFiles", 0, Kind.STANDARD, args -> config.renderPumlFile = true)); diff --git a/src/main/resources/nl/talsmasoftware/umldoclet/UMLDoclet.properties b/src/main/resources/nl/talsmasoftware/umldoclet/UMLDoclet.properties index 70377b444..5312763b5 100644 --- a/src/main/resources/nl/talsmasoftware/umldoclet/UMLDoclet.properties +++ b/src/main/resources/nl/talsmasoftware/umldoclet/UMLDoclet.properties @@ -23,6 +23,8 @@ error.unanticipated.error.generating.diagrams=Unanticipated error generating dia error.unanticipated.error.postprocessing.html=Unanticipated error post-processing HTML: {0} # Usage +doclet.usage.plantuml-server-url.description=Base URL for the PlantUML server\nExamples: http://localhost:8080/, https://www.plantuml.com/plantuml/ +doclet.usage.plantuml-server-url.parameters= doclet.usage.delegate-doclet.description=The delegate doclet providing the main documentation\nDefaults to 'jdk.javadoc.doclet.StandardDoclet'\nSpecify 'false' to disable delegation doclet.usage.delegate-doclet.parameters= doclet.usage.create-puml-files.description=Create PlantUML '.puml' files From 45411a0f985899a554532a31ddb05c9465bdf3fa Mon Sep 17 00:00:00 2001 From: Sjoerd Talsma Date: Fri, 23 Dec 2022 17:07:33 +0100 Subject: [PATCH 11/12] Use RemotePlantumlGenerator if --plantuml-server-url option is specified. Signed-off-by: Sjoerd Talsma --- .../umldoclet/uml/plantuml/PlantumlGenerator.java | 6 ++++-- .../uml/plantuml/RemotePlantumlGenerator.java | 6 +++--- .../umldoclet/uml/DependencyDiagramTest.java | 14 ++++++++++++-- .../talsmasoftware/umldoclet/uml/DiagramTest.java | 1 + .../umldoclet/uml/NamespaceTest.java | 2 ++ 5 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java b/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java index dec4b9cd3..29b765b2c 100644 --- a/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java +++ b/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java @@ -24,8 +24,10 @@ public interface PlantumlGenerator { static PlantumlGenerator getPlantumlGenerator(Configuration configuration) { -// return new RemotePlantumlGenerator(); - return new BuiltinPlantumlGenerator(); + return configuration.plantumlServerUrl() + .map(RemotePlantumlGenerator::new) + .map(PlantumlGenerator.class::cast) + .orElseGet(BuiltinPlantumlGenerator::new); } void generatePlantumlDiagramFromSource(String plantumlSource, FileFormat format, OutputStream out) throws IOException; diff --git a/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGenerator.java b/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGenerator.java index cde43e23e..1bc1140dd 100644 --- a/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGenerator.java +++ b/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGenerator.java @@ -32,10 +32,10 @@ public class RemotePlantumlGenerator implements PlantumlGenerator { private static final String DEFAULT_PLANTUML_BASE_URL = "https://www.plantuml.com/plantuml/"; + private static final Transcoder TRANSCODER = + TranscoderImpl.utf8(new AsciiEncoder(), new ArobaseStringCompressor(), new CompressionZlib()); private final String baseUrl; - private final Transcoder transcoder = - TranscoderImpl.utf8(new AsciiEncoder(), new ArobaseStringCompressor(), new CompressionZlib()); public RemotePlantumlGenerator() { this(null); @@ -64,7 +64,7 @@ public void generatePlantumlDiagramFromSource(String plantumlSource, FileFormat private String encodeDiagram(final String diagramSource) { try { // TODO internalize transcoder to be able to remove PlantUML dependency altogether. - return transcoder.encode(requireNonNull(diagramSource, "UML diagram source was .")); + return TRANSCODER.encode(requireNonNull(diagramSource, "UML diagram source was .")); } catch (IOException ioe) { throw new IllegalStateException("Error encoding diagram: " + ioe.getMessage(), ioe); } diff --git a/src/test/java/nl/talsmasoftware/umldoclet/uml/DependencyDiagramTest.java b/src/test/java/nl/talsmasoftware/umldoclet/uml/DependencyDiagramTest.java index 7673b0879..70a32d599 100644 --- a/src/test/java/nl/talsmasoftware/umldoclet/uml/DependencyDiagramTest.java +++ b/src/test/java/nl/talsmasoftware/umldoclet/uml/DependencyDiagramTest.java @@ -30,8 +30,17 @@ import static java.util.Collections.singleton; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; -import static org.hamcrest.Matchers.*; -import static org.mockito.Mockito.*; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.hasToString; +import static org.hamcrest.Matchers.is; +import static org.mockito.Mockito.atLeast; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; public class DependencyDiagramTest { private ImageConfig mockImages; @@ -54,6 +63,7 @@ public void prepareMocksDiagramAndExclusions() { public void verifyMocks() { verify(mockConfig, atLeastOnce()).images(); verify(mockImages, atLeastOnce()).formats(); + verify(mockConfig, atLeast(0)).plantumlServerUrl(); verify(mockConfig, atLeast(0)).excludedPackageDependencies(); verify(mockConfig, atLeast(0)).indentation(); verifyNoMoreInteractions(mockConfig, mockImages); diff --git a/src/test/java/nl/talsmasoftware/umldoclet/uml/DiagramTest.java b/src/test/java/nl/talsmasoftware/umldoclet/uml/DiagramTest.java index 8e11b618f..94643b1b9 100644 --- a/src/test/java/nl/talsmasoftware/umldoclet/uml/DiagramTest.java +++ b/src/test/java/nl/talsmasoftware/umldoclet/uml/DiagramTest.java @@ -71,6 +71,7 @@ public void setUp() { @AfterEach public void tearDown() { + verify(config, atLeast(0)).plantumlServerUrl(); verify(config, atLeast(0)).images(); verify(config, atLeast(0)).destinationDirectory(); verify(config, atLeast(0)).logger(); diff --git a/src/test/java/nl/talsmasoftware/umldoclet/uml/NamespaceTest.java b/src/test/java/nl/talsmasoftware/umldoclet/uml/NamespaceTest.java index b00c11e3b..28035d9d5 100644 --- a/src/test/java/nl/talsmasoftware/umldoclet/uml/NamespaceTest.java +++ b/src/test/java/nl/talsmasoftware/umldoclet/uml/NamespaceTest.java @@ -52,10 +52,12 @@ public void verifyMocks() { public void testEquals() { PackageDiagram packageUml = new PackageDiagram(config, "a.b.c", randomString()); Namespace namespace = new Namespace(packageUml, "a.b.c", randomString()); + assertThat(namespace.equals(namespace), is(true)); assertThat(namespace, is(equalTo(new Namespace(null, "a.b.c", randomString())))); assertThat(namespace, is(equalTo(new Namespace(packageUml, "a.b.c", randomString())))); assertThat(namespace, is(not(equalTo(new Namespace(packageUml, "A.B.C", randomString()))))); + verify(config, atLeastOnce()).plantumlServerUrl(); } } From 2e77acf371c3302c7280febb44dd7db634e836d5 Mon Sep 17 00:00:00 2001 From: Sjoerd Talsma Date: Fri, 30 Dec 2022 17:04:37 +0100 Subject: [PATCH 12/12] Only allow http(s) base-url configurations for PlantUML server. Signed-off-by: Sjoerd Talsma --- .../umldoclet/uml/plantuml/PlantumlGenerator.java | 6 ++++-- .../umldoclet/uml/plantuml/RemotePlantumlGenerator.java | 4 ---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java b/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java index 29b765b2c..ae82784de 100644 --- a/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java +++ b/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/PlantumlGenerator.java @@ -20,13 +20,15 @@ import java.io.IOException; import java.io.OutputStream; +import java.util.regex.Pattern; public interface PlantumlGenerator { + Pattern HTTP_URLS = Pattern.compile("^https?://"); static PlantumlGenerator getPlantumlGenerator(Configuration configuration) { return configuration.plantumlServerUrl() - .map(RemotePlantumlGenerator::new) - .map(PlantumlGenerator.class::cast) + .filter(url -> HTTP_URLS.matcher(url).find()) + .map(url -> (PlantumlGenerator) new RemotePlantumlGenerator(url)) .orElseGet(BuiltinPlantumlGenerator::new); } diff --git a/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGenerator.java b/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGenerator.java index 1bc1140dd..491c1825c 100644 --- a/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGenerator.java +++ b/src/main/java/nl/talsmasoftware/umldoclet/uml/plantuml/RemotePlantumlGenerator.java @@ -37,10 +37,6 @@ public class RemotePlantumlGenerator implements PlantumlGenerator { private final String baseUrl; - public RemotePlantumlGenerator() { - this(null); - } - public RemotePlantumlGenerator(final String baseUrl) { String url = Objects.toString(baseUrl, DEFAULT_PLANTUML_BASE_URL); if (!url.endsWith("/")) url += "/";