KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > stax > StaxTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.stax;
23
24 import org.jboss.test.JBossTestCase;
25
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.net.URL JavaDoc;
29
30 import javax.xml.stream.XMLEventFactory;
31 import javax.xml.stream.XMLEventReader;
32 import javax.xml.stream.XMLEventWriter;
33 import javax.xml.stream.XMLInputFactory;
34 import javax.xml.stream.XMLOutputFactory;
35 import javax.xml.stream.XMLStreamReader;
36 import javax.xml.stream.XMLStreamWriter;
37 import javax.xml.stream.events.Characters;
38 import javax.xml.stream.events.StartDocument;
39 import javax.xml.stream.events.XMLEvent;
40 import javax.xml.stream.util.XMLEventAllocator;
41
42
43 /**
44  * Test the StAX implementation
45  *
46  * @author jason.greene@jboss.com
47  */

48 public class StaxTestCase extends JBossTestCase
49 {
50    /**
51     * Construct the test case with a given name
52     */

53    public StaxTestCase(String JavaDoc name)
54    {
55       super(name);
56    }
57
58    private static String JavaDoc getXmlUrl(String JavaDoc name)
59    {
60       return getTCLResource(name).getFile();
61    }
62
63    private static URL JavaDoc getTCLResource(String JavaDoc name)
64    {
65       URL JavaDoc xmlUrl = Thread.currentThread().getContextClassLoader().getResource(name);
66       if(xmlUrl == null)
67       {
68          throw new IllegalStateException JavaDoc(name + " not found");
69       }
70       return xmlUrl;
71    }
72
73    public void testStreamReader() throws Exception JavaDoc
74    {
75       FileInputStream JavaDoc stream = new FileInputStream JavaDoc(getXmlUrl("stax/test.xml"));
76       XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(stream);
77       reader.next();
78    }
79
80    public void testEventReader() throws Exception JavaDoc
81    {
82       FileInputStream JavaDoc stream = new FileInputStream JavaDoc(getXmlUrl("stax/test.xml"));
83       XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(stream);
84       XMLEvent event = reader.nextEvent();
85       assertTrue("Expected document start, got:" + event.getClass(), event instanceof StartDocument);
86       event = reader.nextEvent();
87       assertTrue("Expected characters, got:" + event.getClass(), event instanceof Characters);
88    }
89
90    public void testStreamWriter() throws Exception JavaDoc
91    {
92       ByteArrayOutputStream JavaDoc stream = new ByteArrayOutputStream JavaDoc();
93       XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(stream);
94       writer.writeStartDocument();
95       writer.writeComment("hello");
96       writer.close();
97       assertEquals("<?xml version='1.0' encoding='utf-8'?><!--hello-->", stream.toString());
98    }
99
100    public void testEventWriter() throws Exception JavaDoc
101    {
102       ByteArrayOutputStream JavaDoc stream = new ByteArrayOutputStream JavaDoc();
103       XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(stream);
104       XMLEventFactory factory = XMLEventFactory.newInstance();
105       writer.add(factory.createStartDocument());
106       writer.add(factory.createComment("hello"));
107       writer.close();
108       assertEquals("<?xml version='1.0' encoding='UTF-8'?><!--hello-->", stream.toString());
109    }
110 }
111
Popular Tags