package org.enoir.graphvizapi.test;

import org.enoir.graphvizapi.*;
import org.junit.Test;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by frank on 2014/11/21.
 */
public class TestGraphviz {

    @Test
    public void testGenImage() {
        Graphviz gz = new Graphviz("/usr/bin/dot","./");
                Graph graph = new Graph("g1", GraphType.DIGRAPH);
        graph.addAttributes(new Attributes(  "rankdir","LR"));
        Node n1 = new Node("N");
        n1.addAttributes(new Attributes("label","\" Node1 \""));
        Node n2 = new Node("N2");
        Node n3 = new Node("N3");

        graph.addNode(n1);
        graph.addNode(n2);
        graph.addNode(n3);
        graph.addEdge(new Edge("", n1, n2));
        graph.addEdge(new Edge("", n2, n3));
        graph.addEdge(new Edge("",n3,n1));
        graph.addEdge(new Edge("",n3,n1));

        String type = "png";
        byte[] bytearray = gz.getGraphByteArray(graph, type, "100");
        String md5str = getByteArrayMd5(bytearray);
        //Assert.assertEquals(md5str,"ee8629a6a222af4601d506d005dade81");
    }

    private String getByteArrayMd5(byte[] bytes){
        String ret = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(bytes);
            ret = getChecksumString(md.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
        return ret;
    }

    private String getChecksumString(byte[] mdbyte){
        String result = "";
        for (int i=0; i < mdbyte.length; i++) {
            result += Integer.toString( ( mdbyte[i] & 0xff ) + 0x100, 16).substring( 1 );
        }
        return result;
    }

}

@@ -26,24 +26,55 @@
 zapi.test;%0A%0A
+import junit.framework.Assert;%0A
 import org.e
@@ -369,32 +369,24 @@
 dot%22,%22./%22);%0A
-        
         Grap
@@ -399,274 +399,270 @@
 h = 
-new Graph(%22g1%22, GraphType.DIGRAPH);%0A        graph.addAttributes(new Attributes(  %22rankdir%22,%22LR%22));%0A        Node n1 = new Node(%22N%22);%0A        n1.addAttributes(new Attributes(%22label%22,%22%5C%22 Node1 %5C%22%22)
+genSimpleGraph();%0A        String type = %22png%22;%0A        byte%5B%5D bytearray = gz.getGraphByteArray(graph, type, %22100%22);%0A        Assert.assertTrue(bytearray.length%3E0
 );%0A    
+%7D%0A%0A
     
-Node n2 = new Node(%22N2%22);%0A        Node n3 = new Node(%22N3%22
+@Test%0A    public void testGenImageWithDefault() %7B%0A        Graphviz gz = new Graphviz(
 );%0A
-%0A
     
@@ -670,169 +670,265 @@
    g
-raph.addNode(n1);%0A        graph.addNode(n2);%0A        graph.addNode(n3);%0A        graph.addEdge(new Edge(%22%22, n1, n2));%0A        graph.addEdge(new Edge(%22%22, n2, n3));
+z.setTmpPath(%22./%22);%0A        Graph graph = genSimpleGraph();%0A        String type = %22png%22;%0A        byte%5B%5D bytearray = gz.getGraphByteArray(graph, type, %22100%22);%0A        Assert.assertTrue(bytearray.length%3E0);%0A    %7D%0A    @Test%0A    public void testGenImageFail() %7B
 %0A   
@@ -936,87 +936,107 @@
     
-g
+G
 raph
-.addEdge(new Edge(%22%22,n3,n1));%0A        graph.addEdge(new Edge(%22%22,n3,n1)
+viz gz = new Graphviz(%22/dot%22,%22./%22);%0A        Graph graph = new Graph(%22g1%22, GraphType.DIGRAPH
 );%0A
-%0A
     
@@ -1141,123 +1141,41 @@
     
-String md5str = getByteArrayMd5(bytearray);%0A        //Assert.assertEquals(md5str,%22ee8629a6a222af4601d506d005dade81%22
+Assert.assertTrue(bytearray==null
 );%0A 
@@ -1550,32 +1550,644 @@
 urn ret;%0A    %7D%0A%0A
+    private Graph genSimpleGraph() %7B%0A%0A        Graph graph = new Graph(%22g1%22, GraphType.DIGRAPH);%0A        graph.addAttributes(new Attributes(  %22rankdir%22,%22LR%22));%0A        Node n1 = new Node(%22N%22);%0A        n1.addAttributes(new Attributes(%22label%22,%22%5C%22 Node1 %5C%22%22));%0A        Node n2 = new Node(%22N2%22);%0A        Node n3 = new Node(%22N3%22);%0A%0A        graph.addNode(n1);%0A        graph.addNode(n2);%0A        graph.addNode(n3);%0A        graph.addEdge(new Edge(%22%22, n1, n2));%0A        graph.addEdge(new Edge(%22%22, n2, n3));%0A        graph.addEdge(new Edge(%22%22,n3,n1));%0A        graph.addEdge(new Edge(%22%22,n3,n1));%0A        return graph;%0A    %7D%0A%0A
     private Stri

Add new test case to test default dot .