File: XmlTest.java

package info (click to toggle)
libpgjava 8.4-701-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,532 kB
  • ctags: 4,162
  • sloc: java: 33,948; xml: 3,158; makefile: 14; sh: 10
file content (297 lines) | stat: -rw-r--r-- 9,094 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*-------------------------------------------------------------------------
*
* Copyright (c) 2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
*   $PostgreSQL: pgjdbc/org/postgresql/test/jdbc4/XmlTest.java,v 1.1 2008/10/08 18:24:05 jurka Exp $
*
*-------------------------------------------------------------------------
*/
package org.postgresql.test.jdbc4;

import java.sql.*;

import junit.framework.TestCase;

import org.postgresql.test.TestUtil;

import java.io.StringReader;
import java.io.StringWriter;
import java.io.Reader;
import java.io.InputStream;
import java.io.IOException;

import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stax.StAXSource;
import org.w3c.dom.Node;

import javax.xml.transform.Transformer;
import javax.xml.transform.ErrorListener;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;

import javax.xml.transform.Result;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stax.StAXResult;


public class XmlTest extends TestCase {

    private Connection _conn;
    private final Transformer _xslTransformer;
    private final Transformer _identityTransformer;
    private final static String _xsl = "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:output method=\"text\" indent=\"no\" /><xsl:template match=\"/a\"><xsl:for-each select=\"/a/b\">B<xsl:value-of select=\".\" /></xsl:for-each></xsl:template></xsl:stylesheet>";
    private final static String _xmlDocument = "<a><b>1</b><b>2</b></a>";
    private final static String _xmlFragment = "<a>f</a><b>g</b>";



    public XmlTest(String name) throws Exception {
        super(name);
        TransformerFactory factory = TransformerFactory.newInstance();
        _xslTransformer = factory.newTransformer(new StreamSource(new StringReader(_xsl)));
        _xslTransformer.setErrorListener(new Ignorer());
        _identityTransformer = factory.newTransformer();
    }

    protected void setUp() throws Exception {
        _conn = TestUtil.openDB();
        Statement stmt = _conn.createStatement();
        stmt.execute("CREATE TEMP TABLE xmltest(val xml)");
        stmt.execute("INSERT INTO xmltest VALUES ('" + _xmlDocument + "')");
        stmt.execute("INSERT INTO xmltest VALUES ('" + _xmlFragment + "')");
        stmt.close();
    }

    protected void tearDown() throws SQLException {
        Statement stmt = _conn.createStatement();
        stmt.execute("DROP TABLE xmltest");
        stmt.close();
        TestUtil.closeDB(_conn);
    }

    private ResultSet getRS() throws SQLException {
        Statement stmt = _conn.createStatement();
        return stmt.executeQuery("SELECT val FROM xmltest");
    }

    public void testDOMParse() throws SQLException {
        ResultSet rs = getRS();

        assertTrue(rs.next());
        SQLXML xml = rs.getSQLXML(1);
        DOMSource source = xml.getSource(DOMSource.class);
        Node doc = source.getNode();
        Node root = doc.getFirstChild();
        assertEquals("a", root.getNodeName());
        Node first = root.getFirstChild();
        assertEquals("b", first.getNodeName());
        assertEquals("1", first.getTextContent());
        Node last = root.getLastChild();
        assertEquals("b", last.getNodeName());
        assertEquals("2", last.getTextContent());

        assertTrue(rs.next());
        try {
            xml = rs.getSQLXML(1);
            source = xml.getSource(DOMSource.class);
            fail("Can't retrieve a fragment.");
        } catch (SQLException sqle) {
        }
    }

    private void transform(Source source) throws Exception {
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        _xslTransformer.transform(source, result);
        assertEquals("B1B2", writer.toString());
    }

    private <T extends Source> void testRead(Class<T> sourceClass) throws Exception
    {
        ResultSet rs = getRS();

        assertTrue(rs.next());
        SQLXML xml = rs.getSQLXML(1);
        Source source = xml.getSource(sourceClass);
        transform(source);

        assertTrue(rs.next());
        xml = rs.getSQLXML(1);
        try {
            source = xml.getSource(sourceClass);
            transform(source);
            fail("Can't transform a fragment.");
        } catch (Exception sqle) { }
    }

    public void testDOMRead() throws Exception
    {
        testRead(DOMSource.class);
    }

    public void testSAXRead() throws Exception
    {
        testRead(SAXSource.class);
    }

    public void testStAXRead() throws Exception
    {
        testRead(StAXSource.class);
    }

    public void testStreamRead() throws Exception
    {
        testRead(StreamSource.class);
    }

    private <T extends Result> void testWrite(Class<T> resultClass) throws Exception
    {
        Statement stmt = _conn.createStatement();
        stmt.execute("DELETE FROM xmltest");
        stmt.close();

        PreparedStatement ps = _conn.prepareStatement("INSERT INTO xmltest VALUES (?)");
        SQLXML xml = _conn.createSQLXML();
        Result result = xml.setResult(resultClass);

        Source source = new StreamSource(new StringReader(_xmlDocument));
        _identityTransformer.transform(source, result);

        ps.setSQLXML(1, xml);
        ps.executeUpdate();
        ps.close();

        ResultSet rs = getRS();
        assertTrue(rs.next());

        // DOMResults tack on the additional <?xml ...?> header.
        //
        String header = "";
        if (DOMResult.class.equals(resultClass)) {
            header = "<?xml version=\"1.0\" standalone=\"no\"?>";
        }

        assertEquals(header + _xmlDocument, rs.getString(1));
        xml = rs.getSQLXML(1);
        assertEquals(header + _xmlDocument, xml.getString());

        assertTrue(!rs.next());
    }

    public void testDomWrite() throws Exception
    {
        testWrite(DOMResult.class);
    }

    public void testStAXWrite() throws Exception
    {
        testWrite(StAXResult.class);
    }

    public void testStreamWrite() throws Exception
    {
        testWrite(StreamResult.class);
    }

    public void testSAXWrite() throws Exception
    {
        testWrite(SAXResult.class);
    }

    public void testFree() throws SQLException
    {
        ResultSet rs = getRS();
        assertTrue(rs.next());
        SQLXML xml = rs.getSQLXML(1);
        xml.free();
        xml.free();
        try {
            xml.getString();
            fail("Not freed.");
        } catch (SQLException sqle) { }
    }

    public void testGetObject() throws SQLException
    {
        ResultSet rs = getRS();
        assertTrue(rs.next());
        SQLXML xml = (SQLXML)rs.getObject(1);
    }

    public void testSetNull() throws SQLException
    {
        Statement stmt = _conn.createStatement();
        stmt.execute("DELETE FROM xmltest");
        stmt.close();

        PreparedStatement ps = _conn.prepareStatement("INSERT INTO xmltest VALUES (?)");
        ps.setNull(1, Types.SQLXML);
        ps.executeUpdate();
        ps.setObject(1, null, Types.SQLXML);
        ps.executeUpdate();
        SQLXML xml = _conn.createSQLXML();
        xml.setString(null);
        ps.setObject(1, xml);
        ps.executeUpdate();
        ps.close();

        ResultSet rs = getRS();
        assertTrue(rs.next());
        assertNull(rs.getObject(1));
        assertTrue(rs.next());
        assertNull(rs.getSQLXML(1));
        assertTrue(rs.next());
        assertNull(rs.getSQLXML("val"));
        assertTrue(!rs.next());
    }

    public void testEmpty() throws SQLException, IOException {
        SQLXML xml = _conn.createSQLXML();

        try {
            xml.getString();
            fail("Cannot retrieve data from an uninitialized object.");
        } catch (SQLException sqle) { }

        try {
            xml.getSource(null);
            fail("Cannot retrieve data from an uninitialized object.");
        } catch (SQLException sqle) { }
    }

    public void testDoubleSet() throws SQLException
    {
        SQLXML xml = _conn.createSQLXML();

        xml.setString("");

        try {
            xml.setString("");
            fail("Can't set a value after its been initialized.");
        } catch (SQLException sqle) { }

        ResultSet rs = getRS();
        assertTrue(rs.next());
        xml = rs.getSQLXML(1);
        try {
            xml.setString("");
            fail("Can't set a value after its been initialized.");
        } catch (SQLException sqle) { }
    }

    // Don't print warning and errors to System.err, it just
    // clutters the display.
    static class Ignorer implements ErrorListener {
        public void error(TransformerException t) { }
        public void fatalError(TransformerException t) { }
        public void warning(TransformerException t) { }
    }

}