Skip to content

Commit 49ec9c2

Browse files
committed
Make tests independent from OS and PlantUML & Graphviz versions
- Ignore spec test since it required specifying complete SVG code and makes it dependent on PlantUML and Graphviz versions. - Migrate tests cases to plain unit tests instead - Use regular expressions instead of comparing rendered SVG output character by character - Make PlantUML test run on Windows (other tests, especially spec tests, still don't run on Windows due to some file loading issues in ResourceLocation class).
1 parent 6f21f4c commit 49ec9c2

7 files changed

Lines changed: 461 additions & 145 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* This work is made available under the terms of the BSD 2-Clause "Simplified" License.
3+
* The BSD accompanies this distribution (LICENSE.txt).
4+
*
5+
* Copyright © 2026 Advantest Europe GmbH. All rights reserved.
6+
*/
7+
package com.vladsch.flexmark.ext.plantuml;
8+
9+
import java.io.BufferedReader;
10+
import java.io.IOException;
11+
import java.io.InputStreamReader;
12+
import java.util.Collections;
13+
14+
import org.junit.After;
15+
import org.junit.Before;
16+
17+
import com.vladsch.flexmark.html.HtmlRenderer;
18+
import com.vladsch.flexmark.parser.Parser;
19+
import com.vladsch.flexmark.util.data.DataHolder;
20+
import com.vladsch.flexmark.util.data.MutableDataSet;
21+
22+
public abstract class AbstractPlantUmlTest {
23+
24+
private static final DataHolder OPTIONS = new MutableDataSet()
25+
.set(Parser.EXTENSIONS, Collections.singleton(PlantUmlExtension.create()))
26+
.set(HtmlRenderer.INDENT_SIZE, 2)
27+
.set(PlantUmlExtension.KEY_RENDER_FENCED_PLANTUML_CODE_BLOCKS, true)
28+
.toImmutable();
29+
30+
protected Parser parser;
31+
protected HtmlRenderer renderer;
32+
33+
@Before
34+
public void setUp() {
35+
parser = Parser.builder(OPTIONS).build();
36+
renderer = HtmlRenderer.builder(OPTIONS).build();
37+
}
38+
39+
@After
40+
public void cleanUp() {
41+
parser = null;
42+
renderer = null;
43+
}
44+
45+
protected String readFileFromClasspath(String filePath) throws IOException {
46+
StringBuffer contents = new StringBuffer();
47+
try (BufferedReader reader = new BufferedReader(
48+
new InputStreamReader(this.getClass().getResourceAsStream(filePath)))) {
49+
50+
String line = reader.readLine();
51+
while (line != null) {
52+
contents.append(line);
53+
contents.append("\n");
54+
line = reader.readLine();
55+
}
56+
}
57+
return contents.toString();
58+
}
59+
}

flexmark-ext-plantuml/src/test/java/com/vladsch/flexmark/ext/plantuml/PlantUmlCodeBlockSpecTest.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,22 @@
1313
import com.vladsch.flexmark.util.data.DataHolder;
1414
import com.vladsch.flexmark.util.data.MutableDataSet;
1515
import org.jetbrains.annotations.NotNull;
16+
import org.junit.Ignore;
1617
import org.junit.runners.Parameterized;
1718

1819
import java.util.Collections;
1920
import java.util.List;
2021

22+
// Ignore this test, since its spec is highly dependent on the PlantUML and Graphviz versions.
23+
// In addition, the flexmark implementation is not capable of loading files correctly on Windows.
24+
@Ignore
2125
public class PlantUmlCodeBlockSpecTest extends RendererSpecTest {
22-
final private static String SPEC_RESOURCE = "/ext_plantuml_ast_spec.md";
23-
final public static @NotNull ResourceLocation RESOURCE_LOCATION = ResourceLocation.of(SPEC_RESOURCE);
24-
final private static DataHolder OPTIONS = new MutableDataSet()
26+
private static final String SPEC_RESOURCE = "/ext_plantuml_ast_spec.md";
27+
28+
@NotNull
29+
public static final ResourceLocation RESOURCE_LOCATION = ResourceLocation.of(SPEC_RESOURCE);
30+
31+
private static final DataHolder OPTIONS = new MutableDataSet()
2532
.set(Parser.EXTENSIONS, Collections.singleton(PlantUmlExtension.create()))
2633
.set(PlantUmlExtension.KEY_RENDER_FENCED_PLANTUML_CODE_BLOCKS, true)
2734
.toImmutable();
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*
2+
* This work is made available under the terms of the BSD 2-Clause "Simplified" License.
3+
* The BSD accompanies this distribution (LICENSE.txt).
4+
*
5+
* Copyright © 2026 Advantest Europe GmbH. All rights reserved.
6+
*/
7+
package com.vladsch.flexmark.ext.plantuml;
8+
9+
10+
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertNotNull;
12+
import static org.junit.Assert.assertTrue;
13+
14+
import org.junit.Test;
15+
16+
import com.vladsch.flexmark.util.ast.Document;
17+
18+
public class PlantUmlCodeBlockTest extends AbstractPlantUmlTest {
19+
20+
@Test
21+
public void simplePlantUmlCodeBlockRendered() {
22+
final String markdownCode = """
23+
@startuml
24+
class ArrayList
25+
interface List
26+
27+
ArrayList ..|> List
28+
@enduml
29+
""";
30+
final String expectedRegex = """
31+
<figure>
32+
<svg [^<>]+>[\\s\\S]+
33+
</svg>
34+
</figure>
35+
""";
36+
37+
Document document = parser.parse(markdownCode);
38+
String resultHtml = renderer.render(document);
39+
40+
assertNotNull(resultHtml);
41+
assertTrue(resultHtml.matches(expectedRegex));
42+
}
43+
44+
@Test
45+
public void simplePlantUmlCodeBlockWithEmptyLineFollowingRendered() {
46+
final String markdownCode = """
47+
@startuml
48+
class ArrayList
49+
interface List
50+
51+
ArrayList ..|> List
52+
@enduml
53+
54+
""";
55+
final String expectedRegex = """
56+
<figure>
57+
<svg [^<>]+>[\\s\\S]+
58+
</svg>
59+
</figure>
60+
""";
61+
62+
Document document = parser.parse(markdownCode);
63+
String resultHtml = renderer.render(document);
64+
65+
assertNotNull(resultHtml);
66+
assertTrue(resultHtml.matches(expectedRegex));
67+
}
68+
69+
@Test
70+
public void invalidPlantUmlCodeBlockInHtml() {
71+
final String markdownCode = """
72+
@startuml
73+
class ArrayList
74+
interface List
75+
76+
ArrayList ..|> List
77+
@endsomething
78+
""";
79+
final String expected = """
80+
<p>@startuml
81+
class ArrayList
82+
interface List</p>
83+
<pre><code>ArrayList ..|&gt; List
84+
</code></pre>
85+
<p>@endsomething</p>
86+
""";
87+
88+
Document document = parser.parse(markdownCode);
89+
String resultHtml = renderer.render(document);
90+
91+
assertNotNull(resultHtml);
92+
assertEquals(resultHtml, expected);
93+
}
94+
95+
@Test
96+
public void simplePlantUmlCodeBlockWithContextRendered() {
97+
final String markdownCode = """
98+
# Heading
99+
100+
Some text
101+
in a paragraph.
102+
103+
@startuml
104+
class ArrayList
105+
interface List
106+
107+
ArrayList ..|> List
108+
@enduml
109+
110+
Another paragraph.
111+
""";
112+
final String expectedRegex = """
113+
<h1>Heading</h1>
114+
<p>Some text
115+
in a paragraph\\.</p>
116+
<figure>
117+
<svg [^<>]+>[\\s\\S]+
118+
</svg>
119+
</figure>
120+
<p>Another paragraph\\.</p>
121+
""";
122+
123+
Document document = parser.parse(markdownCode);
124+
String resultHtml = renderer.render(document);
125+
126+
assertNotNull(resultHtml);
127+
assertTrue(resultHtml.matches(expectedRegex));
128+
}
129+
130+
@Test
131+
public void timingDiagramRendered() {
132+
final String markdownCode = """
133+
# Timing diagram example
134+
135+
@startuml
136+
robust "DNS Resolver" as DNS
137+
robust "Web Browser" as WB
138+
concise "Web User" as WU
139+
140+
@0
141+
WU is Idle
142+
WB is Idle
143+
DNS is Idle
144+
145+
@+100
146+
WU -> WB : URL
147+
WU is Waiting
148+
WB is Processing
149+
150+
@+200
151+
WB is Waiting
152+
WB -> DNS@+50 : Resolve URL
153+
154+
@+100
155+
DNS is Processing
156+
157+
@+300
158+
DNS is Idle
159+
@enduml
160+
161+
A paragraph following.
162+
""";
163+
final String expectedRegex = """
164+
<h1>Timing diagram example</h1>
165+
<figure>
166+
<svg [^<>]+>[\\s\\S]+
167+
</svg>
168+
</figure>
169+
<p>A paragraph following.</p>
170+
""";
171+
172+
Document document = parser.parse(markdownCode);
173+
String resultHtml = renderer.render(document);
174+
175+
assertNotNull(resultHtml);
176+
assertTrue(resultHtml.matches(expectedRegex));
177+
}
178+
}

0 commit comments

Comments
 (0)