001: package com.mockrunner.test.jdbc;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.InputStream;
005: import java.io.OutputStream;
006: import java.io.Reader;
007: import java.io.StringReader;
008: import java.io.Writer;
009: import java.sql.SQLException;
010:
011: import javax.xml.parsers.DocumentBuilder;
012: import javax.xml.parsers.DocumentBuilderFactory;
013: import javax.xml.stream.XMLStreamConstants;
014: import javax.xml.stream.XMLStreamReader;
015: import javax.xml.stream.XMLStreamWriter;
016: import javax.xml.transform.dom.DOMResult;
017: import javax.xml.transform.dom.DOMSource;
018: import javax.xml.transform.sax.SAXResult;
019: import javax.xml.transform.sax.SAXSource;
020: import javax.xml.transform.stax.StAXResult;
021: import javax.xml.transform.stax.StAXSource;
022: import javax.xml.transform.stream.StreamResult;
023: import javax.xml.transform.stream.StreamSource;
024:
025: import junit.framework.TestCase;
026:
027: import org.jdom.Document;
028: import org.jdom.input.DOMBuilder;
029: import org.jdom.input.SAXBuilder;
030: import org.jdom.output.Format;
031: import org.jdom.output.XMLOutputter;
032: import org.xml.sax.ContentHandler;
033: import org.xml.sax.helpers.AttributesImpl;
034:
035: import com.mockrunner.mock.jdbc.MockSQLXML;
036: import com.mockrunner.util.common.StreamUtil;
037:
038: public class MockSQLXMLTest extends TestCase {
039: private MockSQLXML stringSQLXML;
040: private MockSQLXML readerSQLXML;
041: private MockSQLXML inputStreamSQLXML;
042: private MockSQLXML w3cDocumentSQLXML;
043:
044: private void prepareTestSQLXMLObjects() throws Exception {
045: stringSQLXML = new MockSQLXML(getFirstTestXMLAsString());
046: readerSQLXML = new MockSQLXML(
047: getTestXMLAsReader(getFirstTestXMLAsString()));
048: inputStreamSQLXML = new MockSQLXML(
049: getTestXMLAsInputStream(getFirstTestXMLAsString()));
050: w3cDocumentSQLXML = new MockSQLXML(
051: getTestXMLAsW3CDocument(getFirstTestXMLAsString()));
052: }
053:
054: private Reader getTestXMLAsReader(String xml) throws Exception {
055: return new StringReader(xml);
056: }
057:
058: private InputStream getTestXMLAsInputStream(String xml)
059: throws Exception {
060: return new ByteArrayInputStream(xml.getBytes("UTF-8"));
061: }
062:
063: private org.w3c.dom.Document getTestXMLAsW3CDocument(String xml)
064: throws Exception {
065: DocumentBuilder builder = DocumentBuilderFactory.newInstance()
066: .newDocumentBuilder();
067: return builder.parse(new ByteArrayInputStream(xml
068: .getBytes("ISO-8859-1")));
069: }
070:
071: private void prepareSecondTestXMLForSAX(SAXResult saxResult)
072: throws Exception {
073: ContentHandler contentHandler = saxResult.getHandler();
074: contentHandler.startDocument();
075: AttributesImpl attributes = new AttributesImpl();
076: attributes.addAttribute("", "att1", "att1", "CDATA", "1");
077: attributes.addAttribute("", "att2", "att2", "CDATA", "2");
078: contentHandler.startElement("", "xyz", "xyz", attributes);
079: contentHandler.characters("Hello World".toCharArray(), 0,
080: "Hello World".toCharArray().length);
081: contentHandler.endElement("", "xyz", "xyz");
082: contentHandler.endDocument();
083: }
084:
085: private void prepareSecondTestXMLForStAX(StAXResult staxResult)
086: throws Exception {
087: XMLStreamWriter streamWriter = staxResult.getXMLStreamWriter();
088: streamWriter.writeStartDocument();
089: streamWriter.writeStartElement("xyz");
090: streamWriter.writeAttribute("att1", "1");
091: streamWriter.writeAttribute("att2", "2");
092: streamWriter.writeCharacters("Hello World");
093: streamWriter.writeEndElement();
094: streamWriter.writeEndDocument();
095: }
096:
097: private String getFirstTestXMLAsString() {
098: return "<test>" + "<subelement att=\"1\">" + "text"
099: + "</subelement>" + "</test>";
100: }
101:
102: private String getSecondTestXMLAsString() {
103: return "<xyz att1=\"1\" att2=\"2\">" + "Hello World" + "</xyz>";
104: }
105:
106: private XMLOutputter getCompareOutputter() {
107: Format format = Format.getCompactFormat();
108: format.setOmitDeclaration(true);
109: format.setOmitEncoding(true);
110: XMLOutputter outputter = new XMLOutputter(format);
111: return outputter;
112: }
113:
114: private void assertXMLEqualsTestXML(String xml, String testXML)
115: throws Exception {
116: SAXBuilder builder = new SAXBuilder();
117: Document expected = builder.build(new StringReader(testXML));
118: Document testee = builder.build(new StringReader(xml));
119: XMLOutputter outputter = getCompareOutputter();
120: String expectedString = outputter.outputString(expected);
121: String testeeString = outputter.outputString(testee);
122: assertEquals(expectedString, testeeString);
123: }
124:
125: private void assertXMLEqualsTestXML(org.w3c.dom.Document xml,
126: String testXML) throws Exception {
127: SAXBuilder saxBuilder = new SAXBuilder();
128: Document expected = saxBuilder.build(new StringReader(testXML));
129: DOMBuilder domBuilder = new DOMBuilder();
130: Document testee = domBuilder.build(xml);
131: XMLOutputter outputter = getCompareOutputter();
132: String expectedString = outputter.outputString(expected);
133: String testeeString = outputter.outputString(testee);
134: assertEquals(expectedString, testeeString);
135: }
136:
137: private void assertXMLEqualsFirstTestXML(XMLStreamReader xml)
138: throws Exception {
139: xml.nextTag();
140: assertEquals("test", xml.getLocalName());
141: assertEquals(XMLStreamConstants.START_ELEMENT, xml
142: .getEventType());
143: assertEquals(0, xml.getAttributeCount());
144: xml.nextTag();
145: assertEquals("subelement", xml.getLocalName());
146: assertEquals(XMLStreamConstants.START_ELEMENT, xml
147: .getEventType());
148: assertEquals(1, xml.getAttributeCount());
149: assertEquals("1", xml.getAttributeValue(null, "att"));
150: xml.next();
151: assertEquals("text", xml.getText());
152: assertEquals(XMLStreamConstants.CHARACTERS, xml.getEventType());
153: xml.nextTag();
154: assertEquals("subelement", xml.getLocalName());
155: assertEquals(XMLStreamConstants.END_ELEMENT, xml.getEventType());
156: xml.nextTag();
157: assertEquals("test", xml.getLocalName());
158: assertEquals(XMLStreamConstants.END_ELEMENT, xml.getEventType());
159: }
160:
161: private void assertXMLEqualsSecondTestXML(XMLStreamReader xml)
162: throws Exception {
163: xml.nextTag();
164: assertEquals("xyz", xml.getLocalName());
165: assertEquals(XMLStreamConstants.START_ELEMENT, xml
166: .getEventType());
167: assertEquals(2, xml.getAttributeCount());
168: assertEquals("1", xml.getAttributeValue(null, "att1"));
169: assertEquals("2", xml.getAttributeValue(null, "att2"));
170: xml.next();
171: assertEquals("Hello World", xml.getText());
172: assertEquals(XMLStreamConstants.CHARACTERS, xml.getEventType());
173: xml.nextTag();
174: assertEquals("xyz", xml.getLocalName());
175: assertEquals(XMLStreamConstants.END_ELEMENT, xml.getEventType());
176: }
177:
178: public void testGetMethods() throws Exception {
179: prepareTestSQLXMLObjects();
180: doTestGetString(stringSQLXML, getFirstTestXMLAsString());
181: doTestGetString(readerSQLXML, getFirstTestXMLAsString());
182: doTestGetString(inputStreamSQLXML, getFirstTestXMLAsString());
183: doTestGetString(w3cDocumentSQLXML, getFirstTestXMLAsString());
184: prepareTestSQLXMLObjects();
185: doTestGetCharacterStream(stringSQLXML,
186: getFirstTestXMLAsString());
187: doTestGetCharacterStream(readerSQLXML,
188: getFirstTestXMLAsString());
189: doTestGetCharacterStream(inputStreamSQLXML,
190: getFirstTestXMLAsString());
191: doTestGetCharacterStream(w3cDocumentSQLXML,
192: getFirstTestXMLAsString());
193: prepareTestSQLXMLObjects();
194: doTestGetBinaryStream(stringSQLXML, getFirstTestXMLAsString());
195: doTestGetBinaryStream(readerSQLXML, getFirstTestXMLAsString());
196: doTestGetBinaryStream(inputStreamSQLXML,
197: getFirstTestXMLAsString());
198: doTestGetBinaryStream(w3cDocumentSQLXML,
199: getFirstTestXMLAsString());
200: prepareTestSQLXMLObjects();
201: doTestGetContentAsDocument(stringSQLXML,
202: getFirstTestXMLAsString());
203: doTestGetContentAsDocument(readerSQLXML,
204: getFirstTestXMLAsString());
205: doTestGetContentAsDocument(inputStreamSQLXML,
206: getFirstTestXMLAsString());
207: doTestGetContentAsDocument(w3cDocumentSQLXML,
208: getFirstTestXMLAsString());
209: prepareTestSQLXMLObjects();
210: doTestGetStreamSource(stringSQLXML, getFirstTestXMLAsString());
211: doTestGetStreamSource(readerSQLXML, getFirstTestXMLAsString());
212: doTestGetStreamSource(inputStreamSQLXML,
213: getFirstTestXMLAsString());
214: doTestGetStreamSource(w3cDocumentSQLXML,
215: getFirstTestXMLAsString());
216: prepareTestSQLXMLObjects();
217: doTestGetDOMSource(stringSQLXML, getFirstTestXMLAsString());
218: doTestGetDOMSource(readerSQLXML, getFirstTestXMLAsString());
219: doTestGetDOMSource(inputStreamSQLXML, getFirstTestXMLAsString());
220: doTestGetDOMSource(w3cDocumentSQLXML, getFirstTestXMLAsString());
221: prepareTestSQLXMLObjects();
222: doTestGetSAXSource(stringSQLXML, getFirstTestXMLAsString());
223: doTestGetSAXSource(readerSQLXML, getFirstTestXMLAsString());
224: doTestGetSAXSource(inputStreamSQLXML, getFirstTestXMLAsString());
225: doTestGetSAXSource(w3cDocumentSQLXML, getFirstTestXMLAsString());
226: prepareTestSQLXMLObjects();
227: doTestGetStAXSource(stringSQLXML, true);
228: doTestGetStAXSource(readerSQLXML, true);
229: doTestGetStAXSource(inputStreamSQLXML, true);
230: doTestGetStAXSource(w3cDocumentSQLXML, true);
231: }
232:
233: public void testSetString() throws Exception {
234: prepareTestSQLXMLObjects();
235: assertTrue(stringSQLXML.isWriteable());
236: assertTrue(readerSQLXML.isWriteable());
237: assertTrue(inputStreamSQLXML.isWriteable());
238: assertTrue(w3cDocumentSQLXML.isWriteable());
239: stringSQLXML.setString(getSecondTestXMLAsString());
240: readerSQLXML.setString(getSecondTestXMLAsString());
241: inputStreamSQLXML.setString(getSecondTestXMLAsString());
242: w3cDocumentSQLXML.setString(getSecondTestXMLAsString());
243: doTestGetString(stringSQLXML, getSecondTestXMLAsString());
244: doTestGetString(readerSQLXML, getSecondTestXMLAsString());
245: doTestGetString(inputStreamSQLXML, getSecondTestXMLAsString());
246: doTestGetString(w3cDocumentSQLXML, getSecondTestXMLAsString());
247: assertFalse(stringSQLXML.isWriteable());
248: assertFalse(readerSQLXML.isWriteable());
249: assertFalse(inputStreamSQLXML.isWriteable());
250: assertFalse(w3cDocumentSQLXML.isWriteable());
251: }
252:
253: public void testSetBinaryStream() throws Exception {
254: prepareTestSQLXMLObjects();
255: assertTrue(stringSQLXML.isWriteable());
256: assertTrue(readerSQLXML.isWriteable());
257: assertTrue(inputStreamSQLXML.isWriteable());
258: assertTrue(w3cDocumentSQLXML.isWriteable());
259: OutputStream stringStream = stringSQLXML.setBinaryStream();
260: OutputStream readerStream = readerSQLXML.setBinaryStream();
261: OutputStream inputStreamStream = inputStreamSQLXML
262: .setBinaryStream();
263: OutputStream w3cDocumentStream = w3cDocumentSQLXML
264: .setBinaryStream();
265: stringStream
266: .write(getSecondTestXMLAsString().getBytes("UTF-8"));
267: readerStream
268: .write(getSecondTestXMLAsString().getBytes("UTF-8"));
269: inputStreamStream.write(getSecondTestXMLAsString().getBytes(
270: "UTF-8"));
271: w3cDocumentStream.write(getSecondTestXMLAsString().getBytes(
272: "UTF-8"));
273: doTestGetDOMSource(stringSQLXML, getSecondTestXMLAsString());
274: doTestGetDOMSource(readerSQLXML, getSecondTestXMLAsString());
275: doTestGetDOMSource(inputStreamSQLXML,
276: getSecondTestXMLAsString());
277: doTestGetDOMSource(w3cDocumentSQLXML,
278: getSecondTestXMLAsString());
279: assertFalse(stringSQLXML.isWriteable());
280: assertFalse(readerSQLXML.isWriteable());
281: assertFalse(inputStreamSQLXML.isWriteable());
282: assertFalse(w3cDocumentSQLXML.isWriteable());
283: }
284:
285: public void testSetCharacterStream() throws Exception {
286: prepareTestSQLXMLObjects();
287: assertTrue(stringSQLXML.isWriteable());
288: assertTrue(readerSQLXML.isWriteable());
289: assertTrue(inputStreamSQLXML.isWriteable());
290: assertTrue(w3cDocumentSQLXML.isWriteable());
291: Writer stringWriter = stringSQLXML.setCharacterStream();
292: Writer readerWriter = readerSQLXML.setCharacterStream();
293: Writer inputStreamWriter = inputStreamSQLXML
294: .setCharacterStream();
295: Writer w3cDocumentWriter = w3cDocumentSQLXML
296: .setCharacterStream();
297: stringWriter.write(getSecondTestXMLAsString());
298: readerWriter.write(getSecondTestXMLAsString());
299: inputStreamWriter.write(getSecondTestXMLAsString());
300: w3cDocumentWriter.write(getSecondTestXMLAsString());
301: doTestGetBinaryStream(stringSQLXML, getSecondTestXMLAsString());
302: doTestGetBinaryStream(readerSQLXML, getSecondTestXMLAsString());
303: doTestGetBinaryStream(inputStreamSQLXML,
304: getSecondTestXMLAsString());
305: doTestGetBinaryStream(w3cDocumentSQLXML,
306: getSecondTestXMLAsString());
307: assertFalse(stringSQLXML.isWriteable());
308: assertFalse(readerSQLXML.isWriteable());
309: assertFalse(inputStreamSQLXML.isWriteable());
310: assertFalse(w3cDocumentSQLXML.isWriteable());
311: }
312:
313: public void testSetStreamResult() throws Exception {
314: prepareTestSQLXMLObjects();
315: assertTrue(stringSQLXML.isWriteable());
316: assertTrue(readerSQLXML.isWriteable());
317: assertTrue(inputStreamSQLXML.isWriteable());
318: assertTrue(w3cDocumentSQLXML.isWriteable());
319: StreamResult stringResult = (StreamResult) stringSQLXML
320: .setResult(StreamResult.class);
321: StreamResult readerResult = (StreamResult) readerSQLXML
322: .setResult(StreamResult.class);
323: StreamResult inputStreamResult = (StreamResult) inputStreamSQLXML
324: .setResult(StreamResult.class);
325: StreamResult w3cDocumentResult = (StreamResult) w3cDocumentSQLXML
326: .setResult(StreamResult.class);
327: stringResult.getOutputStream().write(
328: getSecondTestXMLAsString().getBytes("UTF-8"));
329: readerResult.getOutputStream().write(
330: getSecondTestXMLAsString().getBytes("UTF-8"));
331: inputStreamResult.getOutputStream().write(
332: getSecondTestXMLAsString().getBytes("UTF-8"));
333: w3cDocumentResult.getOutputStream().write(
334: getSecondTestXMLAsString().getBytes("UTF-8"));
335: doTestGetSAXSource(stringSQLXML, getSecondTestXMLAsString());
336: doTestGetSAXSource(readerSQLXML, getSecondTestXMLAsString());
337: doTestGetSAXSource(inputStreamSQLXML,
338: getSecondTestXMLAsString());
339: doTestGetSAXSource(w3cDocumentSQLXML,
340: getSecondTestXMLAsString());
341: assertFalse(stringSQLXML.isWriteable());
342: assertFalse(readerSQLXML.isWriteable());
343: assertFalse(inputStreamSQLXML.isWriteable());
344: assertFalse(w3cDocumentSQLXML.isWriteable());
345: }
346:
347: public void testSetDOMResultSetNode() throws Exception {
348: prepareTestSQLXMLObjects();
349: assertTrue(stringSQLXML.isWriteable());
350: assertTrue(readerSQLXML.isWriteable());
351: assertTrue(inputStreamSQLXML.isWriteable());
352: assertTrue(w3cDocumentSQLXML.isWriteable());
353: DOMResult stringResult = (DOMResult) stringSQLXML
354: .setResult(DOMResult.class);
355: DOMResult readerResult = (DOMResult) readerSQLXML
356: .setResult(DOMResult.class);
357: DOMResult inputStreamResult = (DOMResult) inputStreamSQLXML
358: .setResult(DOMResult.class);
359: DOMResult w3cDocumentResult = (DOMResult) w3cDocumentSQLXML
360: .setResult(DOMResult.class);
361: stringResult
362: .setNode(getTestXMLAsW3CDocument(getSecondTestXMLAsString()));
363: readerResult.setNode(getTestXMLAsW3CDocument(
364: getSecondTestXMLAsString()).getFirstChild());
365: inputStreamResult
366: .setNode(getTestXMLAsW3CDocument(getSecondTestXMLAsString()));
367: w3cDocumentResult.setNode(getTestXMLAsW3CDocument(
368: getSecondTestXMLAsString()).getFirstChild());
369: doTestGetStreamSource(stringSQLXML, getSecondTestXMLAsString());
370: doTestGetStreamSource(readerSQLXML, getSecondTestXMLAsString());
371: doTestGetStreamSource(inputStreamSQLXML,
372: getSecondTestXMLAsString());
373: doTestGetStreamSource(w3cDocumentSQLXML,
374: getSecondTestXMLAsString());
375: assertFalse(stringSQLXML.isWriteable());
376: assertFalse(readerSQLXML.isWriteable());
377: assertFalse(inputStreamSQLXML.isWriteable());
378: assertFalse(w3cDocumentSQLXML.isWriteable());
379: }
380:
381: public void testSetDOMResultGetNode() throws Exception {
382: prepareTestSQLXMLObjects();
383: assertTrue(stringSQLXML.isWriteable());
384: assertTrue(readerSQLXML.isWriteable());
385: assertTrue(inputStreamSQLXML.isWriteable());
386: assertTrue(w3cDocumentSQLXML.isWriteable());
387: DOMResult stringResult = (DOMResult) stringSQLXML
388: .setResult(DOMResult.class);
389: DOMResult readerResult = (DOMResult) readerSQLXML
390: .setResult(DOMResult.class);
391: DOMResult inputStreamResult = (DOMResult) inputStreamSQLXML
392: .setResult(DOMResult.class);
393: DOMResult w3cDocumentResult = (DOMResult) w3cDocumentSQLXML
394: .setResult(DOMResult.class);
395: org.w3c.dom.Document stringDocument = (org.w3c.dom.Document) stringResult
396: .getNode();
397: org.w3c.dom.Document readerDocument = (org.w3c.dom.Document) readerResult
398: .getNode();
399: org.w3c.dom.Document inputStreamDocument = (org.w3c.dom.Document) inputStreamResult
400: .getNode();
401: org.w3c.dom.Document w3cDocumentDocument = (org.w3c.dom.Document) w3cDocumentResult
402: .getNode();
403: stringDocument.appendChild(stringDocument.importNode(
404: getTestXMLAsW3CDocument(getSecondTestXMLAsString())
405: .getFirstChild(), true));
406: readerDocument.appendChild(readerDocument.importNode(
407: getTestXMLAsW3CDocument(getSecondTestXMLAsString())
408: .getFirstChild(), true));
409: inputStreamDocument.appendChild(inputStreamDocument.importNode(
410: getTestXMLAsW3CDocument(getSecondTestXMLAsString())
411: .getFirstChild(), true));
412: w3cDocumentDocument.appendChild(w3cDocumentDocument.importNode(
413: getTestXMLAsW3CDocument(getSecondTestXMLAsString())
414: .getFirstChild(), true));
415: doTestGetStAXSource(stringSQLXML, false);
416: doTestGetStAXSource(readerSQLXML, false);
417: doTestGetStAXSource(inputStreamSQLXML, false);
418: doTestGetStAXSource(w3cDocumentSQLXML, false);
419: assertFalse(stringSQLXML.isWriteable());
420: assertFalse(readerSQLXML.isWriteable());
421: assertFalse(inputStreamSQLXML.isWriteable());
422: assertFalse(w3cDocumentSQLXML.isWriteable());
423: }
424:
425: public void testSetSAXResult() throws Exception {
426: prepareTestSQLXMLObjects();
427: assertTrue(stringSQLXML.isWriteable());
428: assertTrue(readerSQLXML.isWriteable());
429: assertTrue(inputStreamSQLXML.isWriteable());
430: assertTrue(w3cDocumentSQLXML.isWriteable());
431: SAXResult stringResult = (SAXResult) stringSQLXML
432: .setResult(SAXResult.class);
433: SAXResult readerResult = (SAXResult) readerSQLXML
434: .setResult(SAXResult.class);
435: SAXResult inputStreamResult = (SAXResult) inputStreamSQLXML
436: .setResult(SAXResult.class);
437: SAXResult w3cDocumentResult = (SAXResult) w3cDocumentSQLXML
438: .setResult(SAXResult.class);
439: prepareSecondTestXMLForSAX(stringResult);
440: prepareSecondTestXMLForSAX(readerResult);
441: prepareSecondTestXMLForSAX(inputStreamResult);
442: prepareSecondTestXMLForSAX(w3cDocumentResult);
443: doTestGetCharacterStream(stringSQLXML,
444: getSecondTestXMLAsString());
445: doTestGetCharacterStream(readerSQLXML,
446: getSecondTestXMLAsString());
447: doTestGetCharacterStream(inputStreamSQLXML,
448: getSecondTestXMLAsString());
449: doTestGetCharacterStream(w3cDocumentSQLXML,
450: getSecondTestXMLAsString());
451: assertFalse(stringSQLXML.isWriteable());
452: assertFalse(readerSQLXML.isWriteable());
453: assertFalse(inputStreamSQLXML.isWriteable());
454: assertFalse(w3cDocumentSQLXML.isWriteable());
455: }
456:
457: public void testSetStAXResult() throws Exception {
458: prepareTestSQLXMLObjects();
459: assertTrue(stringSQLXML.isWriteable());
460: assertTrue(readerSQLXML.isWriteable());
461: assertTrue(inputStreamSQLXML.isWriteable());
462: assertTrue(w3cDocumentSQLXML.isWriteable());
463: StAXResult stringResult = (StAXResult) stringSQLXML
464: .setResult(StAXResult.class);
465: StAXResult readerResult = (StAXResult) readerSQLXML
466: .setResult(StAXResult.class);
467: StAXResult inputStreamResult = (StAXResult) inputStreamSQLXML
468: .setResult(StAXResult.class);
469: StAXResult w3cDocumentResult = (StAXResult) w3cDocumentSQLXML
470: .setResult(StAXResult.class);
471: prepareSecondTestXMLForStAX(stringResult);
472: prepareSecondTestXMLForStAX(readerResult);
473: prepareSecondTestXMLForStAX(inputStreamResult);
474: prepareSecondTestXMLForStAX(w3cDocumentResult);
475: doTestGetContentAsDocument(stringSQLXML,
476: getSecondTestXMLAsString());
477: doTestGetContentAsDocument(readerSQLXML,
478: getSecondTestXMLAsString());
479: doTestGetContentAsDocument(inputStreamSQLXML,
480: getSecondTestXMLAsString());
481: doTestGetContentAsDocument(w3cDocumentSQLXML,
482: getSecondTestXMLAsString());
483: assertFalse(stringSQLXML.isWriteable());
484: assertFalse(readerSQLXML.isWriteable());
485: assertFalse(inputStreamSQLXML.isWriteable());
486: assertFalse(w3cDocumentSQLXML.isWriteable());
487: }
488:
489: public void testEquals() throws Exception {
490: prepareTestSQLXMLObjects();
491: assertFalse(stringSQLXML.equals(null));
492: assertTrue(stringSQLXML.equals(stringSQLXML));
493: assertTrue(stringSQLXML.equals(w3cDocumentSQLXML));
494: assertEquals(stringSQLXML.hashCode(), w3cDocumentSQLXML
495: .hashCode());
496: assertTrue(readerSQLXML.equals(inputStreamSQLXML));
497: assertEquals(readerSQLXML.hashCode(), inputStreamSQLXML
498: .hashCode());
499: stringSQLXML.setString(getSecondTestXMLAsString());
500: assertFalse(stringSQLXML.equals(inputStreamSQLXML));
501: assertFalse(inputStreamSQLXML.equals(stringSQLXML));
502: Writer writer = inputStreamSQLXML.setCharacterStream();
503: writer.write(getSecondTestXMLAsString());
504: assertTrue(stringSQLXML.equals(inputStreamSQLXML));
505: assertTrue(inputStreamSQLXML.equals(stringSQLXML));
506: assertEquals(stringSQLXML.hashCode(), inputStreamSQLXML
507: .hashCode());
508: stringSQLXML.getString();
509: assertFalse(stringSQLXML.isReadable());
510: assertTrue(inputStreamSQLXML.isReadable());
511: assertTrue(stringSQLXML.equals(inputStreamSQLXML));
512: assertTrue(inputStreamSQLXML.equals(stringSQLXML));
513: assertEquals(stringSQLXML.hashCode(), inputStreamSQLXML
514: .hashCode());
515: stringSQLXML.free();
516: assertFalse(stringSQLXML.equals(inputStreamSQLXML));
517: assertFalse(inputStreamSQLXML.equals(stringSQLXML));
518: inputStreamSQLXML.free();
519: assertTrue(stringSQLXML.equals(inputStreamSQLXML));
520: assertTrue(inputStreamSQLXML.equals(stringSQLXML));
521: assertEquals(stringSQLXML.hashCode(), inputStreamSQLXML
522: .hashCode());
523: }
524:
525: public void testClone() throws Exception {
526: prepareTestSQLXMLObjects();
527: inputStreamSQLXML.getString();
528: MockSQLXML clone = (MockSQLXML) inputStreamSQLXML.clone();
529: assertFalse(clone.isReadable());
530: assertTrue(clone.isWriteable());
531: assertFalse(clone.wasFreeCalled());
532: assertNotSame(inputStreamSQLXML, clone);
533: assertTrue(inputStreamSQLXML.equals(clone));
534: }
535:
536: public void testFree() throws Exception {
537: prepareTestSQLXMLObjects();
538: w3cDocumentSQLXML.free();
539: try {
540: w3cDocumentSQLXML.getString();
541: fail();
542: } catch (SQLException exc) {
543: //expected exception
544: }
545: try {
546: w3cDocumentSQLXML.setString(getSecondTestXMLAsString());
547: fail();
548: } catch (SQLException exc) {
549: //expected exception
550: }
551: assertFalse(w3cDocumentSQLXML.isReadable());
552: assertFalse(w3cDocumentSQLXML.isWriteable());
553: assertTrue(w3cDocumentSQLXML.wasFreeCalled());
554: assertXMLEqualsTestXML(w3cDocumentSQLXML.getContentAsString(),
555: getFirstTestXMLAsString());
556: String xml = new String(StreamUtil
557: .getStreamAsByteArray(w3cDocumentSQLXML
558: .getContentAsInputStream()), "UTF-8");
559: assertXMLEqualsTestXML(xml, getFirstTestXMLAsString());
560: xml = StreamUtil.getReaderAsString(w3cDocumentSQLXML
561: .getContentAsReader());
562: assertXMLEqualsTestXML(xml, getFirstTestXMLAsString());
563: assertXMLEqualsTestXML(w3cDocumentSQLXML
564: .getContentAsW3CDocument(), getFirstTestXMLAsString());
565: }
566:
567: public void testNoContent() throws Exception {
568: MockSQLXML emptySQLXML = new MockSQLXML();
569: assertNull(emptySQLXML.getContentAsString());
570: assertNull(emptySQLXML.getContentAsW3CDocument());
571: assertNull(emptySQLXML.getContentAsInputStream());
572: assertNull(emptySQLXML.getContentAsReader());
573: try {
574: emptySQLXML.getString();
575: fail();
576: } catch (SQLException exc) {
577: //expected exception
578: }
579: emptySQLXML.setString(getFirstTestXMLAsString());
580: assertXMLEqualsTestXML(emptySQLXML.getString(),
581: getFirstTestXMLAsString());
582: }
583:
584: private void doTestGetString(MockSQLXML sqlXML, String testXML)
585: throws Exception {
586: assertTrue(sqlXML.isReadable());
587: assertXMLEqualsTestXML(sqlXML.getContentAsString(), testXML);
588: assertTrue(sqlXML.isReadable());
589: assertXMLEqualsTestXML(sqlXML.getString(), testXML);
590: assertFalse(sqlXML.isReadable());
591: assertXMLEqualsTestXML(sqlXML.getContentAsString(), testXML);
592: try {
593: sqlXML.getString();
594: fail();
595: } catch (SQLException exc) {
596: //expected exception
597: }
598: }
599:
600: private void doTestGetCharacterStream(MockSQLXML sqlXML,
601: String testXML) throws Exception {
602: assertTrue(sqlXML.isReadable());
603: String xml = StreamUtil.getReaderAsString(sqlXML
604: .getContentAsReader());
605: assertXMLEqualsTestXML(xml, testXML);
606: assertTrue(sqlXML.isReadable());
607: xml = StreamUtil.getReaderAsString(sqlXML.getCharacterStream());
608: assertXMLEqualsTestXML(xml, testXML);
609: assertFalse(sqlXML.isReadable());
610: xml = StreamUtil.getReaderAsString(sqlXML.getContentAsReader());
611: assertXMLEqualsTestXML(xml, testXML);
612: try {
613: sqlXML.getCharacterStream();
614: fail();
615: } catch (SQLException exc) {
616: //expected exception
617: }
618: }
619:
620: private void doTestGetBinaryStream(MockSQLXML sqlXML, String testXML)
621: throws Exception {
622: assertTrue(sqlXML.isReadable());
623: String xml = new String(StreamUtil.getStreamAsByteArray(sqlXML
624: .getContentAsInputStream()), "UTF-8");
625: assertXMLEqualsTestXML(xml, testXML);
626: assertTrue(sqlXML.isReadable());
627: xml = new String(StreamUtil.getStreamAsByteArray(sqlXML
628: .getBinaryStream()), "UTF-8");
629: assertXMLEqualsTestXML(xml, testXML);
630: assertFalse(sqlXML.isReadable());
631: xml = new String(StreamUtil.getStreamAsByteArray(sqlXML
632: .getContentAsInputStream()), "UTF-8");
633: assertXMLEqualsTestXML(xml, testXML);
634: try {
635: sqlXML.getBinaryStream();
636: fail();
637: } catch (SQLException exc) {
638: //expected exception
639: }
640: }
641:
642: private void doTestGetContentAsDocument(MockSQLXML sqlXML,
643: String testXML) throws Exception {
644: assertTrue(sqlXML.isReadable());
645: assertXMLEqualsTestXML(sqlXML.getContentAsW3CDocument(),
646: testXML);
647: assertTrue(sqlXML.isReadable());
648: }
649:
650: private void doTestGetStreamSource(MockSQLXML sqlXML, String testXML)
651: throws Exception {
652: assertTrue(sqlXML.isReadable());
653: StreamSource source = (StreamSource) sqlXML
654: .getSource(StreamSource.class);
655: String xml = new String(StreamUtil.getStreamAsByteArray(source
656: .getInputStream()), "UTF-8");
657: assertXMLEqualsTestXML(xml, testXML);
658: assertFalse(sqlXML.isReadable());
659: try {
660: sqlXML.getSource(StreamSource.class);
661: fail();
662: } catch (SQLException exc) {
663: //expected exception
664: }
665: }
666:
667: private void doTestGetDOMSource(MockSQLXML sqlXML, String testXML)
668: throws Exception {
669: assertTrue(sqlXML.isReadable());
670: DOMSource source = (DOMSource) sqlXML
671: .getSource(DOMSource.class);
672: assertXMLEqualsTestXML((org.w3c.dom.Document) source.getNode(),
673: testXML);
674: assertFalse(sqlXML.isReadable());
675: try {
676: sqlXML.getSource(DOMSource.class);
677: fail();
678: } catch (SQLException exc) {
679: //expected exception
680: }
681: }
682:
683: private void doTestGetSAXSource(MockSQLXML sqlXML, String testXML)
684: throws Exception {
685: assertTrue(sqlXML.isReadable());
686: SAXSource source = (SAXSource) sqlXML
687: .getSource(SAXSource.class);
688: InputStream stream = source.getInputSource().getByteStream();
689: String xml = new String(
690: StreamUtil.getStreamAsByteArray(stream), "UTF-8");
691: assertXMLEqualsTestXML(xml, testXML);
692: assertFalse(sqlXML.isReadable());
693: try {
694: sqlXML.getSource(SAXSource.class);
695: fail();
696: } catch (SQLException exc) {
697: //expected exception
698: }
699: }
700:
701: private void doTestGetStAXSource(MockSQLXML sqlXML,
702: boolean testFirst) throws Exception {
703: assertTrue(sqlXML.isReadable());
704: StAXSource source = (StAXSource) sqlXML
705: .getSource(StAXSource.class);
706: if (testFirst) {
707: assertXMLEqualsFirstTestXML(source.getXMLStreamReader());
708: } else {
709: assertXMLEqualsSecondTestXML(source.getXMLStreamReader());
710: }
711: assertFalse(sqlXML.isReadable());
712: try {
713: sqlXML.getSource(StAXSource.class);
714: fail();
715: } catch (SQLException exc) {
716: //expected exception
717: }
718: }
719: }
|