1/*
2 * Copyright (c) 1998, 2015, 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
26package com.sun.hotspot.igv.data.serialization;
27
28import com.sun.hotspot.igv.data.*;
29import java.io.*;
30import java.nio.channels.Channels;
31import static org.junit.Assert.assertTrue;
32import static org.junit.Assert.fail;
33import org.junit.*;
34import org.openide.util.Exceptions;
35import org.xml.sax.InputSource;
36import org.xml.sax.SAXException;
37
38/**
39 *
40 * @author Thomas Wuerthinger
41 */
42public class ParserTest {
43
44    public ParserTest() {
45    }
46
47    @BeforeClass
48    public static void setUpClass() throws Exception {
49    }
50
51    @AfterClass
52    public static void tearDownClass() throws Exception {
53    }
54
55    @Before
56    public void setUp() {
57    }
58
59    @After
60    public void tearDown() {
61    }
62
63    private void test(GraphDocument document) {
64        final Printer printer = new Printer();
65        final CharArrayWriter writer = new CharArrayWriter();
66        printer.export(writer, document);
67        test(document, writer.toString());
68    }
69
70    private void test(GraphDocument document, String xmlString) {
71        try {
72            InputStream in = new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
73            try {
74                Parser parser = new Parser(Channels.newChannel(in));
75                final GraphDocument parsedDocument = parser.parse();
76                Util.assertGraphDocumentEquals(document, parsedDocument);
77            } catch (SAXException ex) {
78                fail(ex.toString());
79            }
80        } catch (UnsupportedEncodingException ex) {
81            Exceptions.printStackTrace(ex);
82        }
83    }
84
85    private void testBoth(GraphDocument document, String xmlString) {
86        test(document);
87        test(document, xmlString);
88    }
89
90    /**
91     * Test of graph document serialization
92     */
93    @Test
94    public void testSerialization() {
95        final GraphDocument doc = new GraphDocument();
96
97        test(doc);
98
99        final Group group1 = new Group(doc);
100        doc.addElement(group1);
101        test(doc);
102
103        final Group group2 = new Group(doc);
104        doc.addElement(group2);
105        test(doc);
106
107        final InputGraph graph = new InputGraph("");
108        group1.addElement(graph);
109        test(doc);
110
111        graph.addNode(new InputNode(0));
112        test(doc);
113
114        graph.addNode(new InputNode(1));
115        test(doc);
116
117        graph.addNode(new InputNode(2));
118        test(doc);
119
120        graph.addNode(new InputNode(3));
121        test(doc);
122
123        graph.addEdge(new InputEdge((char)0, (char)0, 0, 1));
124        test(doc);
125
126        graph.addEdge(new InputEdge((char)1, (char)1, 0, 1));
127        test(doc);
128
129        graph.addEdge(new InputEdge((char)0, (char)0, 1, 2));
130        test(doc);
131
132        graph.addEdge(new InputEdge((char)0, (char)0, 2, 3));
133        test(doc);
134
135        group1.setMethod(new InputMethod(group1, "testMethod", "tM", 1));
136        test(doc);
137
138        final InputBlock b1 = graph.addBlock("1");
139        b1.addNode(0);
140        b1.addNode(1);
141
142        final InputBlock b2 = graph.addBlock("2");
143        b2.addNode(2);
144        b2.addNode(3);
145        test(doc);
146
147        final GraphDocument document2 = new GraphDocument();
148        doc.addGraphDocument(document2);
149        test(doc);
150        assertTrue(doc.getElements().size() == 2);
151
152        final Group group3 = new Group(document2);
153        document2.addElement(group3);
154        doc.addGraphDocument(document2);
155        assertTrue(doc.getElements().size() == 3);
156        assertTrue(document2.getElements().size() == 0);
157
158        doc.clear();
159        test(doc);
160        Util.assertGraphDocumentEquals(doc, new GraphDocument());
161    }
162
163    @Test
164    public void testSimpleExport() {
165        GraphDocument document = new GraphDocument();
166        Group g = new Group(document);
167        document.addElement(g);
168
169        InputGraph graph = new InputGraph("TestGraph");
170                g.addElement(graph);
171        graph.getProperties().setProperty("testName", "testValue");
172
173        InputNode n1 = new InputNode(0);
174        InputNode n2 = new InputNode(1);
175        InputEdge e1 = new InputEdge((char)0, 0, 1);
176        InputEdge e2 = new InputEdge((char)1, 0, 1);
177        graph.addNode(n1);
178        graph.addNode(n2);
179        graph.addEdge(e1);
180        graph.addEdge(e2);
181
182        test(document);
183    }
184
185    @Test
186    public void testComplexExport() {
187
188        GraphDocument document = new GraphDocument();
189        Group g = new Group(document);
190        document.addElement(g);
191
192        InputGraph graph = new InputGraph("TestGraph");
193                g.addElement(graph);
194        graph.getProperties().setProperty("testName", "testValue");
195
196        InputNode n1 = new InputNode(0);
197        InputNode n2 = new InputNode(1);
198        InputEdge e1 = new InputEdge((char)0, 0, 0);
199        InputEdge e2 = new InputEdge((char)1, 1, 1);
200        graph.addNode(n1);
201        graph.addNode(n2);
202        graph.addEdge(e1);
203        graph.addEdge(e2);
204
205        InputGraph graph2 = new InputGraph("TestGraph2");
206                g.addElement(graph2);
207        graph2.addNode(n1);
208        InputNode n3 = new InputNode(2);
209        graph2.addNode(n3);
210        InputEdge e3 = new InputEdge((char)0, 0, 2);
211        graph2.addEdge(e3);
212
213        test(document);
214    }
215
216
217    /**
218     * Test of parse method, of class Parser.
219     */
220    @Test
221    public void testParse() {
222        testBoth(new GraphDocument(), "<graphDocument></graphDocument>");
223    }
224
225}
226