1
+ package com .baeldung .jgrapht ;
2
+
3
+ import static org .junit .Assert .assertTrue ;
4
+ import java .awt .Color ;
5
+ import java .awt .image .BufferedImage ;
6
+ import java .io .File ;
7
+ import java .io .IOException ;
8
+ import javax .imageio .ImageIO ;
9
+ import org .jgrapht .ext .JGraphXAdapter ;
10
+ import org .jgrapht .graph .DefaultDirectedGraph ;
11
+ import org .jgrapht .graph .DefaultEdge ;
12
+ import org .junit .Before ;
13
+ import org .junit .Test ;
14
+ import com .mxgraph .layout .mxCircleLayout ;
15
+ import com .mxgraph .layout .mxIGraphLayout ;
16
+ import com .mxgraph .util .mxCellRenderer ;
17
+
18
+ public class GraphImageGenerationUnitTest {
19
+ static DefaultDirectedGraph <String , DefaultEdge > g ;
20
+
21
+ @ Before
22
+ public void createGraph () throws IOException {
23
+ File imgFile = new File ("src/test/resources/graph.png" );
24
+ imgFile .createNewFile ();
25
+ g = new DefaultDirectedGraph <String , DefaultEdge >(DefaultEdge .class );
26
+ String x1 = "x1" ;
27
+ String x2 = "x2" ;
28
+ String x3 = "x3" ;
29
+ g .addVertex (x1 );
30
+ g .addVertex (x2 );
31
+ g .addVertex (x3 );
32
+ g .addEdge (x1 , x2 );
33
+ g .addEdge (x2 , x3 );
34
+ g .addEdge (x3 , x1 );
35
+ }
36
+
37
+ @ Test
38
+ public void givenAdaptedGraph_whenWriteBufferedImage_ThenFileShouldExist () throws IOException {
39
+ JGraphXAdapter <String , DefaultEdge > graphAdapter = new JGraphXAdapter <String , DefaultEdge >(g );
40
+ mxIGraphLayout layout = new mxCircleLayout (graphAdapter );
41
+ layout .execute (graphAdapter .getDefaultParent ());
42
+ File imgFile = new File ("src/test/resources/graph.png" );
43
+ BufferedImage image = mxCellRenderer .createBufferedImage (graphAdapter , null , 2 , Color .WHITE , true , null );
44
+ ImageIO .write (image , "PNG" , imgFile );
45
+ assertTrue (imgFile .exists ());
46
+ }
47
+ }
0 commit comments