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.Key;
18  
19  public class TestKeyElementParser extends AbstractParserTest {
20  
21      @Test(expected= GraphIOException.class)
22      public void testNoId() throws Exception {
23          
24          String xml = 
25              "<key/>";
26          
27          readObject(xml);
28      }
29  
30      @Test
31      public void testId() throws Exception {
32          
33          String xml = 
34              "<key id=\"d1\"/>";
35          
36          Key key = (Key) readObject(xml);
37          Assert.assertNotNull(key);
38          Assert.assertEquals("d1", key.getId());
39          Assert.assertEquals(null, key.getDescription());
40          Assert.assertEquals(null, key.getDefaultValue());
41          Assert.assertEquals(null, key.getAttributeName());
42          Assert.assertEquals(null, key.getAttributeType());
43          Assert.assertEquals(Key.ForType.ALL, key.getForType());
44      }
45  
46      @Test
47      public void testDesc() throws Exception {
48          
49          String xml = 
50              "<key id=\"d1\">" +
51                  "<desc>this is my key</desc>" +
52              "</key>";
53          
54          Key key = (Key) readObject(xml);
55          Assert.assertNotNull(key);
56          Assert.assertEquals("d1", key.getId());
57          Assert.assertEquals("this is my key", key.getDescription());
58          Assert.assertEquals(null, key.getDefaultValue());
59          Assert.assertEquals(null, key.getAttributeName());
60          Assert.assertEquals(null, key.getAttributeType());
61          Assert.assertEquals(Key.ForType.ALL, key.getForType());
62      }
63  
64      @Test
65      public void testDefault() throws Exception {
66          
67          String xml = 
68              "<key id=\"d1\">" +
69                  "<default>yellow</default>" +
70              "</key>";
71          
72          Key key = (Key) readObject(xml);
73          Assert.assertNotNull(key);
74          Assert.assertEquals("d1", key.getId());
75          Assert.assertEquals(null, key.getDescription());
76          Assert.assertEquals("yellow", key.getDefaultValue());
77          Assert.assertEquals(null, key.getAttributeName());
78          Assert.assertEquals(null, key.getAttributeType());
79          Assert.assertEquals(Key.ForType.ALL, key.getForType());
80      }
81  
82      @Test
83      public void testAttrNameType() throws Exception {
84          
85          String xml = 
86              "<key id=\"d1\" attr.name=\"myattr\" attr.type=\"double\">" +
87              "</key>";
88          
89          Key key = (Key) readObject(xml);
90          Assert.assertNotNull(key);
91          Assert.assertEquals("d1", key.getId());
92          Assert.assertEquals(null, key.getDescription());
93          Assert.assertEquals(null, key.getDefaultValue());
94          Assert.assertEquals("myattr", key.getAttributeName());
95          Assert.assertEquals("double", key.getAttributeType());
96          Assert.assertEquals(Key.ForType.ALL, key.getForType());
97      }
98  
99      @Test
100     public void testForNode() throws Exception {
101         
102         String xml = 
103             "<key id=\"d1\" for=\"node\">" +
104             "</key>";
105         
106         Key key = (Key) readObject(xml);
107         Assert.assertNotNull(key);
108         Assert.assertEquals("d1", key.getId());
109         Assert.assertEquals(null, key.getDescription());
110         Assert.assertEquals(null, key.getDefaultValue());
111         Assert.assertEquals(null, key.getAttributeName());
112         Assert.assertEquals(null, key.getAttributeType());
113         Assert.assertEquals(Key.ForType.NODE, key.getForType());
114     }
115 
116     @Test
117     public void testForEdge() throws Exception {
118         
119         String xml = 
120             "<key id=\"d1\" for=\"edge\">" +
121             "</key>";
122         
123         Key key = (Key) readObject(xml);
124         Assert.assertNotNull(key);
125         Assert.assertEquals("d1", key.getId());
126         Assert.assertEquals(null, key.getDescription());
127         Assert.assertEquals(null, key.getDefaultValue());
128         Assert.assertEquals(null, key.getAttributeName());
129         Assert.assertEquals(null, key.getAttributeType());
130         Assert.assertEquals(Key.ForType.EDGE, key.getForType());
131     }
132 
133     @Test
134     public void testForGraph() throws Exception {
135         
136         String xml = 
137             "<key id=\"d1\" for=\"graph\">" +
138             "</key>";
139         
140         Key key = (Key) readObject(xml);
141         Assert.assertNotNull(key);
142         Assert.assertEquals("d1", key.getId());
143         Assert.assertEquals(null, key.getDescription());
144         Assert.assertEquals(null, key.getDefaultValue());
145         Assert.assertEquals(null, key.getAttributeName());
146         Assert.assertEquals(null, key.getAttributeType());
147         Assert.assertEquals(Key.ForType.GRAPH, key.getForType());
148     }
149 
150     @Test
151     public void testForAll() throws Exception {
152         
153         String xml = 
154             "<key id=\"d1\" for=\"all\">" +
155             "</key>";
156         
157         Key key = (Key) readObject(xml);
158         Assert.assertNotNull(key);
159         Assert.assertEquals("d1", key.getId());
160         Assert.assertEquals(null, key.getDescription());
161         Assert.assertEquals(null, key.getDefaultValue());
162         Assert.assertEquals(null, key.getAttributeName());
163         Assert.assertEquals(null, key.getAttributeType());
164         Assert.assertEquals(Key.ForType.ALL, key.getForType());
165     }
166 }