-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathIoTest.java
More file actions
74 lines (60 loc) · 2.02 KB
/
Copy pathIoTest.java
File metadata and controls
74 lines (60 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package de.adrianwilke.javayed;
import java.io.File;
import org.junit.Assert;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* Tests {@link Io}.
*
* @author Adrian Wilke
*/
public class IoTest {
public static final boolean DELETE_FILES_ON_EXIT = true;
@Test
public void testWriter() throws Exception {
File file = File.createTempFile(IoTest.class.getName() + ".", ".yEd.graphml");
System.out.println("Writing test file: " + file.getPath());
if (DELETE_FILES_ON_EXIT) {
file.deleteOnExit();
}
YedDoc yedDoc = new YedDoc().initialize();
yedDoc.createEdge(yedDoc.createNode("a"), yedDoc.createNode("b"));
Io.write(yedDoc.getDocument(), file);
Assert.assertTrue("File created", file.exists());
Assert.assertTrue("File not empty", file.length() > 0);
}
@Test
public void testReader() throws Exception {
// Create file
File file = File.createTempFile(IoTest.class.getName() + ".", ".yEd.graphml");
System.out.println("Reading test file: " + file.getPath());
if (DELETE_FILES_ON_EXIT) {
file.deleteOnExit();
}
// Write
YedDoc yedDoc = new YedDoc().initialize();
yedDoc.createEdge(yedDoc.createNode("a"), yedDoc.createNode("b"));
Io.write(yedDoc.getDocument(), file);
// Read and check graphml element
Document document = Io.read(file);
Node graphmlNode = document.getFirstChild();
Assert.assertEquals("Got graphml node", "graphml", graphmlNode.getNodeName());
// Check graph element
Element graphNode = null;
NodeList graphmlChildNodes = graphmlNode.getChildNodes();
for (int i = 0; i < graphmlChildNodes.getLength(); i++) {
if (graphmlChildNodes.item(i).getNodeName().equals("graph")) {
if (graphmlChildNodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
graphNode = (Element) graphmlChildNodes.item(i);
break;
}
}
}
Assert.assertNotNull(graphNode);
// Check number of nodes
Assert.assertEquals("2 nodes", 2, graphNode.getElementsByTagName("node").getLength());
}
}