comparison visualizer/Data/test/unit/src/com/sun/hotspot/igv/data/serialization/ParserTest.java @ 4512:015fb895586b

Moved visualizer to new directory.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Tue, 07 Feb 2012 22:41:09 +0100
parents
children
comparison
equal deleted inserted replaced
4511:6cb549627941 4512:015fb895586b
1 /*
2 * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.hotspot.igv.data.serialization;
27
28 import com.sun.hotspot.igv.data.*;
29 import java.io.CharArrayWriter;
30 import java.io.StringReader;
31 import static org.junit.Assert.assertTrue;
32 import static org.junit.Assert.fail;
33 import org.junit.*;
34 import org.xml.sax.InputSource;
35 import org.xml.sax.SAXException;
36
37 /**
38 *
39 * @author Thomas Wuerthinger
40 */
41 public class ParserTest {
42
43 public ParserTest() {
44 }
45
46 @BeforeClass
47 public static void setUpClass() throws Exception {
48 }
49
50 @AfterClass
51 public static void tearDownClass() throws Exception {
52 }
53
54 @Before
55 public void setUp() {
56 }
57
58 @After
59 public void tearDown() {
60 }
61
62 private void test(GraphDocument document) {
63 final Printer printer = new Printer();
64 final CharArrayWriter writer = new CharArrayWriter();
65 printer.export(writer, document);
66 test(document, writer.toString());
67 }
68
69 private void test(GraphDocument document, String xmlString) {
70
71 StringReader sr = new StringReader(xmlString);
72 InputSource is = new InputSource(sr);
73
74 try {
75 Parser parser = new Parser();
76 final GraphDocument parsedDocument = parser.parse(is, null);
77 Util.assertGraphDocumentEquals(document, parsedDocument);
78 } catch (SAXException ex) {
79 fail(ex.toString());
80 }
81 }
82
83 private void testBoth(GraphDocument document, String xmlString) {
84 test(document);
85 test(document, xmlString);
86 }
87
88 /**
89 * Test of graph document serialization
90 */
91 @Test
92 public void testSerialization() {
93 final GraphDocument doc = new GraphDocument();
94
95 test(doc);
96
97 final Group group1 = new Group(doc);
98 doc.addElement(group1);
99 test(doc);
100
101 final Group group2 = new Group(doc);
102 doc.addElement(group2);
103 test(doc);
104
105 final InputGraph graph = new InputGraph("");
106 group1.addElement(graph);
107 test(doc);
108
109 graph.addNode(new InputNode(0));
110 test(doc);
111
112 graph.addNode(new InputNode(1));
113 test(doc);
114
115 graph.addNode(new InputNode(2));
116 test(doc);
117
118 graph.addNode(new InputNode(3));
119 test(doc);
120
121 graph.addEdge(new InputEdge((char)0, (char)0, 0, 1));
122 test(doc);
123
124 graph.addEdge(new InputEdge((char)1, (char)1, 0, 1));
125 test(doc);
126
127 graph.addEdge(new InputEdge((char)0, (char)0, 1, 2));
128 test(doc);
129
130 graph.addEdge(new InputEdge((char)0, (char)0, 2, 3));
131 test(doc);
132
133 group1.setMethod(new InputMethod(group1, "testMethod", "tM", 1));
134 test(doc);
135
136 final InputBlock b1 = graph.addBlock("1");
137 b1.addNode(0);
138 b1.addNode(1);
139
140 final InputBlock b2 = graph.addBlock("2");
141 b2.addNode(2);
142 b2.addNode(3);
143 test(doc);
144
145 final GraphDocument document2 = new GraphDocument();
146 doc.addGraphDocument(document2);
147 test(doc);
148 assertTrue(doc.getElements().size() == 2);
149
150 final Group group3 = new Group(document2);
151 document2.addElement(group3);
152 doc.addGraphDocument(document2);
153 assertTrue(doc.getElements().size() == 3);
154 assertTrue(document2.getElements().size() == 0);
155
156 doc.clear();
157 test(doc);
158 Util.assertGraphDocumentEquals(doc, new GraphDocument());
159 }
160
161 @Test
162 public void testSimpleExport() {
163 GraphDocument document = new GraphDocument();
164 Group g = new Group(document);
165 document.addElement(g);
166
167 InputGraph graph = new InputGraph("TestGraph");
168 g.addElement(graph);
169 graph.getProperties().setProperty("testName", "testValue");
170
171 InputNode n1 = new InputNode(0);
172 InputNode n2 = new InputNode(1);
173 InputEdge e1 = new InputEdge((char)0, 0, 1);
174 InputEdge e2 = new InputEdge((char)1, 0, 1);
175 graph.addNode(n1);
176 graph.addNode(n2);
177 graph.addEdge(e1);
178 graph.addEdge(e2);
179
180 test(document);
181 }
182
183 @Test
184 public void testComplexExport() {
185
186 GraphDocument document = new GraphDocument();
187 Group g = new Group(document);
188 document.addElement(g);
189
190 InputGraph graph = new InputGraph("TestGraph");
191 g.addElement(graph);
192 graph.getProperties().setProperty("testName", "testValue");
193
194 InputNode n1 = new InputNode(0);
195 InputNode n2 = new InputNode(1);
196 InputEdge e1 = new InputEdge((char)0, 0, 0);
197 InputEdge e2 = new InputEdge((char)1, 1, 1);
198 graph.addNode(n1);
199 graph.addNode(n2);
200 graph.addEdge(e1);
201 graph.addEdge(e2);
202
203 InputGraph graph2 = new InputGraph("TestGraph2");
204 g.addElement(graph2);
205 graph2.addNode(n1);
206 InputNode n3 = new InputNode(2);
207 graph2.addNode(n3);
208 InputEdge e3 = new InputEdge((char)0, 0, 2);
209 graph2.addEdge(e3);
210
211 test(document);
212 }
213
214
215 /**
216 * Test of parse method, of class Parser.
217 */
218 @Test
219 public void testParse() {
220 testBoth(new GraphDocument(), "<graphDocument></graphDocument>");
221 }
222
223 }