Skip to content

Commit 6ea15b8

Browse files
committed
add tests
Signed-off-by: Stefan Niederhauser <[email protected]>
1 parent 4a3354e commit 6ea15b8

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

graphviz-java/src/test/java/guru/nidi/graphviz/engine/RendererTest.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@
2222
import org.junit.jupiter.params.ParameterizedTest;
2323
import org.junit.jupiter.params.provider.CsvSource;
2424

25-
import java.io.File;
26-
import java.io.IOException;
25+
import javax.imageio.ImageIO;
26+
import java.io.*;
2727
import java.nio.file.Files;
2828

29+
import static guru.nidi.graphviz.engine.Format.PNG;
30+
import static guru.nidi.graphviz.engine.Format.SVG;
2931
import static guru.nidi.graphviz.model.Factory.graph;
3032
import static guru.nidi.graphviz.model.Factory.node;
33+
import static java.nio.charset.StandardCharsets.UTF_8;
3134
import static org.hamcrest.MatcherAssert.assertThat;
32-
import static org.hamcrest.Matchers.greaterThan;
35+
import static org.hamcrest.Matchers.*;
3336
import static org.junit.jupiter.api.Assertions.assertTrue;
3437

3538
class RendererTest {
@@ -53,7 +56,7 @@ void testFile(File giveFile, File expectedFile) throws IOException {
5356
Files.deleteIfExists(expectedFile.getParentFile().toPath());
5457

5558
final Graph graph = graph("example1").directed().with(node("a").link(node("b")));
56-
Graphviz.fromGraph(graph).width(200).render(Format.PNG).toFile(giveFile);
59+
Graphviz.fromGraph(graph).width(200).render(PNG).toFile(giveFile);
5760

5861
assertTrue(expectedFile.exists() && expectedFile.isFile());
5962
}
@@ -64,18 +67,35 @@ void toFileNoParentFolder() throws Exception {
6467
Files.deleteIfExists(file.toPath());
6568
try {
6669
final Graph graph = graph("example1").directed().with(node("a").link(node("b")));
67-
Graphviz.fromGraph(graph).width(200).render(Format.PNG).toFile(file);
70+
Graphviz.fromGraph(graph).width(200).render(PNG).toFile(file);
6871
assertTrue(file.exists());
6972
} finally {
7073
Files.deleteIfExists(file.toPath());
7174
}
7275
}
7376

77+
@Test
78+
void outputStream() throws IOException {
79+
final Graphviz g = Graphviz.fromGraph(graph().with(node("a")));
80+
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
81+
g.render(SVG).toOutputStream(baos);
82+
assertThat(new String(baos.toByteArray(), UTF_8), allOf(startsWith("<svg"), endsWith("</svg>\n")));
83+
}
84+
85+
@Test
86+
void toImage() throws IOException {
87+
final Graphviz g = Graphviz.fromGraph(graph().with(node("a")));
88+
final File file = new File("target/toImage.png");
89+
file.delete();
90+
ImageIO.write(g.render(PNG).toImage(), "png", file);
91+
assertTrue(file.exists());
92+
}
93+
7494
@Test
7595
void image() throws IOException {
7696
final File out = new File("target/image.png");
7797
final Graphviz g = Graphviz.fromGraph(graph().with(node(" ").with(Image.of("graphviz.png"))));
78-
g.basedir(new File("example")).render(Format.PNG).toFile(out);
98+
g.basedir(new File("example")).render(PNG).toFile(out);
7999
assertThat((int) out.length(), greaterThan(19000));
80100
}
81101

0 commit comments

Comments
 (0)