View Javadoc
1   /*
2    * Copyright (c) 2008, The JUNG Authors
3    *
4    * All rights reserved.
5    *
6    * This software is open-source under the BSD license; see either
7    * "license.txt" or
8    * https://github.com/jrtom/jung/blob/master/LICENSE for a description.
9    */
10  
11  package edu.uci.ics.jung.io.graphml.parser;
12  
13  import org.junit.Assert;
14  import org.junit.Test;
15  
16  import edu.uci.ics.jung.io.GraphIOException;
17  import edu.uci.ics.jung.io.graphml.NodeMetadata;
18  
19  public class TestNodeElementParser extends AbstractParserTest {
20  
21      @Test(expected= GraphIOException.class)
22      public void testNoId() throws Exception {
23          
24          String xml = 
25              "<node/>";
26          
27          readObject(xml);
28      }
29  
30      @Test
31      public void testId() throws Exception {
32          
33          String xml = 
34              "<node id=\"1\"/>";
35          
36          NodeMetadata node = (NodeMetadata) readObject(xml);
37          Assert.assertNotNull(node);
38          Assert.assertEquals("1", node.getId());
39          Assert.assertEquals(null, node.getDescription());
40          Assert.assertEquals(0, node.getPorts().size());
41      }
42  
43      @Test
44      public void testDesc() throws Exception {
45          
46          String xml = 
47              "<node id=\"1\">" +
48                  "<desc>this is my node</desc>" +
49              "</node>";
50          
51          NodeMetadata node = (NodeMetadata) readObject(xml);
52          Assert.assertNotNull(node);
53          Assert.assertEquals("1", node.getId());
54          Assert.assertEquals("this is my node", node.getDescription());
55          Assert.assertEquals(0, node.getPorts().size());
56      }
57  
58      @Test
59      public void testPort() throws Exception {
60                      
61          String xml = 
62              "<node id=\"1\">" +
63                  "<desc>this is my node</desc>" +
64                  "<port name=\"p1\">" +
65                      "<desc>port 1</desc>" +
66                  "</port>" +
67              "</node>";
68          
69          NodeMetadata node = (NodeMetadata) readObject(xml);
70          Assert.assertNotNull(node);
71          Assert.assertEquals("1", node.getId());
72          Assert.assertEquals("this is my node", node.getDescription());
73          Assert.assertEquals(1, node.getPorts().size());
74          Assert.assertEquals("p1", node.getPorts().get(0).getName());
75      }
76  
77      @Test
78      public void testMultiPort() throws Exception {
79          
80          String xml = 
81              "<node id=\"1\">" +
82                  "<desc>this is my node</desc>" +
83                  "<port name=\"p1\"/>" +
84                  "<port name=\"p2\"/>" +
85                  "<port name=\"p3\"/>" +
86                  "<port name=\"p4\"/>" +
87              "</node>";
88          
89          NodeMetadata node = (NodeMetadata) readObject(xml);
90          Assert.assertNotNull(node);
91          Assert.assertEquals("1", node.getId());
92          Assert.assertEquals("this is my node", node.getDescription());
93          Assert.assertEquals(4, node.getPorts().size());
94          Assert.assertEquals("p1", node.getPorts().get(0).getName());
95          Assert.assertEquals("p2", node.getPorts().get(1).getName());
96          Assert.assertEquals("p3", node.getPorts().get(2).getName());
97          Assert.assertEquals("p4", node.getPorts().get(3).getName());
98      }
99  
100     @Test
101     public void testUserAttributes() throws Exception {
102         
103         String xml = 
104             "<node id=\"1\" bob=\"abc123\"/>";
105         
106         NodeMetadata node = (NodeMetadata) readObject(xml);
107         Assert.assertNotNull(node);
108         Assert.assertEquals("1", node.getId());
109         Assert.assertEquals(1, node.getProperties().size());
110         Assert.assertEquals("abc123", node.getProperty("bob"));
111         Assert.assertEquals(0, node.getPorts().size());
112     }
113 
114     @Test
115     public void testData() throws Exception {
116         
117         String xml = 
118             "<node id=\"1\">" +
119                 "<data key=\"d1\">value1</data>" +
120                 "<data key=\"d2\">value2</data>" +
121             "</node>";
122         
123         NodeMetadata node = (NodeMetadata) readObject(xml);
124         Assert.assertNotNull(node);
125         Assert.assertEquals("1", node.getId());
126         Assert.assertEquals(2, node.getProperties().size());
127         Assert.assertEquals("value1", node.getProperty("d1"));
128         Assert.assertEquals("value2", node.getProperty("d2"));
129         Assert.assertEquals(0, node.getPorts().size());
130     }
131 }