001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.databinding.utils.reader;
020:
021: import org.apache.axiom.attachments.ByteArrayDataSource;
022: import org.apache.axiom.om.OMAbstractFactory;
023: import org.apache.axiom.om.OMAttribute;
024: import org.apache.axiom.om.OMElement;
025: import org.apache.axiom.om.OMFactory;
026: import org.apache.axiom.om.OMNamespace;
027: import org.apache.axiom.om.impl.serialize.StreamingOMSerializer;
028: import org.apache.axiom.om.util.Base64;
029: import org.apache.axiom.om.util.StAXUtils;
030: import org.apache.axis2.databinding.ADBBean;
031: import org.apache.axis2.databinding.utils.Constants;
032: import org.apache.axis2.util.StreamWrapper;
033: import org.custommonkey.xmlunit.XMLTestCase;
034: import org.w3c.dom.Document;
035: import org.xml.sax.SAXException;
036:
037: import javax.activation.DataHandler;
038: import javax.xml.namespace.QName;
039: import javax.xml.parsers.DocumentBuilder;
040: import javax.xml.parsers.DocumentBuilderFactory;
041: import javax.xml.parsers.ParserConfigurationException;
042: import javax.xml.stream.XMLStreamException;
043: import javax.xml.stream.XMLStreamReader;
044: import javax.xml.stream.XMLStreamWriter;
045: import java.io.ByteArrayInputStream;
046: import java.io.ByteArrayOutputStream;
047: import java.io.IOException;
048: import java.util.ArrayList;
049: import java.util.List;
050:
051: public class ADBXMLStreamReaderTest extends XMLTestCase {
052:
053: private DocumentBuilder db;
054:
055: protected void setUp() throws Exception {
056: DocumentBuilderFactory dbf = DocumentBuilderFactory
057: .newInstance();
058: dbf.setNamespaceAware(true);
059: db = dbf.newDocumentBuilder();
060: }
061:
062: /** complex array scenario */
063: public void testComplexObjectArrayScenario() {
064: try {
065: String expectedXML = "<ns1:TestComplexStringArrayScenario xmlns:ns1=\"http://testComplexStringArrayScenario.org\">"
066: + "<Foo>Some Text</Foo>"
067: + "<Dependent>"
068: + "<Name>FooTwo</Name>"
069: + "<Age>25</Age>"
070: + "<Sex>Male</Sex>"
071: + "</Dependent>"
072: + "<AdditionalDependent>"
073: + "<Name>FooTwo</Name>"
074: + "<Age>25</Age>"
075: + "<Sex>Male</Sex>"
076: + "</AdditionalDependent>"
077: + "<AdditionalDependent>"
078: + "<Name>FooTwo</Name>"
079: + "<Age>25</Age>"
080: + "<Sex>Male</Sex>"
081: + "</AdditionalDependent>"
082: + "<AdditionalDependent>"
083: + "<Name>FooTwo</Name>"
084: + "<Age>25</Age>"
085: + "<Sex>Male</Sex>"
086: + "</AdditionalDependent>"
087: + "<AdditionalDependent>"
088: + "<Name>FooTwo</Name>"
089: + "<Age>25</Age>"
090: + "<Sex>Male</Sex>"
091: + "</AdditionalDependent>"
092: + "<Bar>Some More Text</Bar><"
093: + "/ns1:TestComplexStringArrayScenario>";
094:
095: ArrayList propertyList = new ArrayList();
096: propertyList.add("Foo");
097: propertyList.add("Some Text");
098: propertyList.add(new QName("Dependent"));
099: DummyADBBean dummyBean = new DummyADBBean();
100: propertyList.add(dummyBean);
101:
102: ADBBean[] adbBeans = new ADBBean[4];
103: for (int i = 0; i < 4; i++) {
104: adbBeans[i] = new DummyADBBean();
105: }
106: for (int i = 0; i < adbBeans.length; i++) {
107: propertyList.add(new QName("AdditionalDependent"));
108: propertyList.add(adbBeans[i]);
109:
110: }
111:
112: propertyList.add("Bar");
113: propertyList.add("Some More Text");
114:
115: XMLStreamReader pullParser = new ADBXMLStreamReaderImpl(
116: new QName(
117: "http://testComplexStringArrayScenario.org",
118: "TestComplexStringArrayScenario", "ns1"),
119: propertyList.toArray(), null);
120: String actualXML = getStringXML(pullParser);
121:
122: assertXMLEqual(newDocument(expectedXML),
123: newDocument(actualXML));
124: } catch (ParserConfigurationException e) {
125: fail("Error has occurred " + e);
126: } catch (SAXException e) {
127: fail("Error has occurred " + e);
128: } catch (IOException e) {
129: fail("Error has occurred " + e);
130: } catch (Exception e) {
131: fail("Error has occurred " + e);
132: }
133: }
134:
135: /** complex array scenario with nulls in between */
136: public void testComplexObjectArrayScenarioWithNulls() {
137: try {
138: String expectedXML = "<ns1:TestComplexStringArrayScenario xmlns:ns1=\"http://testComplexStringArrayScenario.org\">"
139: + "<AdditionalDependent>"
140: + "<Name>FooTwo</Name>"
141: + "<Age>25</Age>"
142: + "<Sex>Male</Sex>"
143: + "</AdditionalDependent>"
144: + "<AdditionalDependent>"
145: + "<Name>FooTwo</Name>"
146: + "<Age>25</Age>"
147: + "<Sex>Male</Sex>"
148: + "</AdditionalDependent>"
149: + "<AdditionalDependent>"
150: + "<Name>FooTwo</Name>"
151: + "<Age>25</Age>"
152: + "<Sex>Male</Sex>"
153: + "</AdditionalDependent>"
154: + "<AdditionalDependent xsi:nil=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
155: + "</AdditionalDependent>"
156: + "<Bar>Some More Text</Bar><"
157: + "/ns1:TestComplexStringArrayScenario>";
158:
159: ArrayList propertyList = new ArrayList();
160:
161: ADBBean[] adbBeans = new ADBBean[4];
162: for (int i = 0; i < 4; i++) {
163: adbBeans[i] = new DummyADBBean();
164: }
165:
166: adbBeans[3] = null;
167:
168: for (int i = 0; i < adbBeans.length; i++) {
169: propertyList.add(new QName("AdditionalDependent"));
170: propertyList.add(adbBeans[i]);
171:
172: }
173:
174: propertyList.add("Bar");
175: propertyList.add("Some More Text");
176:
177: XMLStreamReader pullParser = new ADBXMLStreamReaderImpl(
178: new QName(
179: "http://testComplexStringArrayScenario.org",
180: "TestComplexStringArrayScenario", "ns1"),
181: propertyList.toArray(), null);
182: String actualXML = getStringXML(pullParser);
183:
184: assertXMLEqual(newDocument(expectedXML),
185: newDocument(actualXML));
186: } catch (ParserConfigurationException e) {
187: fail("Error has occurred " + e);
188: } catch (SAXException e) {
189: fail("Error has occurred " + e);
190: } catch (IOException e) {
191: fail("Error has occurred " + e);
192: } catch (Exception e) {
193: fail("Error has occurred " + e);
194: }
195: }
196:
197: /** Empty array */
198: public void testComplexObjectArrayScenarioEmptyArray() {
199: try {
200: String expectedXML = "<ns1:TestComplexStringArrayScenario xmlns:ns1=\"http://testComplexStringArrayScenario.org\">"
201: + "<Foo>Some Text</Foo>"
202: + "<Dependent>"
203: + "<Name>FooTwo</Name>"
204: + "<Age>25</Age>"
205: + "<Sex>Male</Sex>"
206: + "</Dependent>"
207: + "<Bar>Some More Text</Bar><"
208: + "/ns1:TestComplexStringArrayScenario>";
209:
210: ArrayList propertyList = new ArrayList();
211: propertyList.add("Foo");
212: propertyList.add("Some Text");
213: propertyList.add(new QName("Dependent"));
214: DummyADBBean dummyBean = new DummyADBBean();
215: propertyList.add(dummyBean);
216:
217: String[] array = new String[] {};
218: propertyList.add(new QName("AdditionalDependent"));
219: propertyList.add(array);
220:
221: propertyList.add("Bar");
222: propertyList.add("Some More Text");
223:
224: XMLStreamReader pullParser = new ADBXMLStreamReaderImpl(
225: new QName(
226: "http://testComplexStringArrayScenario.org",
227: "TestComplexStringArrayScenario", "ns1"),
228: propertyList.toArray(), null);
229: String actualXML = getStringXML(pullParser);
230: assertXMLEqual(newDocument(expectedXML),
231: newDocument(actualXML));
232:
233: } catch (ParserConfigurationException e) {
234: fail("Error has occurred " + e);
235: } catch (SAXException e) {
236: fail("Error has occurred " + e);
237: } catch (IOException e) {
238: fail("Error has occurred " + e);
239: } catch (Exception e) {
240: fail("Error has occurred " + e);
241: }
242: }
243:
244: /** test a complex array list */
245: public void testComplexArrayList() {
246: try {
247:
248: String exptectedXML = "<Person><Name>FooOne</Name><Organization>Apache</Organization>"
249: + "<Dependent><Name>FooTwo</Name><Age>25</Age><Sex>Male</Sex><Depemdent>"
250: + "<Name>FooTwo</Name><Age>25</Age><Sex>Male</Sex><Depemdent><Name>FooTwo</Name>"
251: + "<Age>25</Age><Sex>Male</Sex></Depemdent></Depemdent></Dependent>"
252: + "<test:Dependent xmlns:test=\"http://whatever.com\"><Name>FooTwo</Name><Age>25</Age>"
253: + "<Sex>Male</Sex><Depemdent><Name>FooTwo</Name><Age>25</Age><Sex>Male</Sex>"
254: + "</Depemdent></test:Dependent></Person>";
255:
256: ArrayList propertyList = new ArrayList();
257: propertyList.add("Name");
258: propertyList.add("FooOne");
259:
260: propertyList.add("Organization");
261: propertyList.add("Apache");
262:
263: propertyList.add(new QName("Dependent"));
264: DummyADBBean dummyBean = new DummyADBBean();
265: DummyADBBean nextdummyBean = dummyBean.addAnotherBean();
266: nextdummyBean.addAnotherBean();
267: propertyList.add(dummyBean);
268:
269: propertyList.add(new QName("http://whatever.com",
270: "Dependent", "test"));
271: dummyBean = new DummyADBBean();
272: dummyBean.addAnotherBean();
273: propertyList.add(dummyBean);
274:
275: QName projectQName = new QName("Person");
276: XMLStreamReader pullParser = new ADBXMLStreamReaderImpl(
277: projectQName, propertyList.toArray(), null);
278:
279: Document actualDom = newDocument(getStringXML(pullParser));
280: Document expectedDocument = newDocument(exptectedXML);
281: assertXMLEqual(actualDom, expectedDocument);
282: } catch (ParserConfigurationException e) {
283: fail("Exception in parsing documents " + e);
284: } catch (SAXException e) {
285: fail("Exception in parsing documents " + e);
286: } catch (IOException e) {
287: fail("Exception in parsing documents " + e);
288: } catch (XMLStreamException e) {
289: fail("Exception in parsing documents " + e);
290: }
291:
292: }
293:
294: public static class DummyADBBean implements ADBBean {
295: ArrayList propertyList = new ArrayList();
296:
297: public DummyADBBean() {
298: propertyList.add("Name");
299: propertyList.add("FooTwo");
300: propertyList.add("Age");
301: propertyList.add("25");
302: propertyList.add("Sex");
303: propertyList.add("Male");
304: }
305:
306: public DummyADBBean addAnotherBean() {
307: propertyList.add(new QName("Depemdent"));
308: DummyADBBean dummyBean = new DummyADBBean();
309: propertyList.add(dummyBean);
310: return dummyBean;
311: }
312:
313: public XMLStreamReader getPullParser(QName adbBeanQName) {
314: return new ADBXMLStreamReaderImpl(adbBeanQName,
315: propertyList.toArray(), null);
316: }
317: }
318:
319: public void testWithOMElements() throws XMLStreamException {
320:
321: String expectedXML = "<OMElementTest><axis2:FirstOMElement xmlns:axis2=\"http://ws.apache.org/namespaces/axis2\">"
322: + "<axis2:SecondOMElement></axis2:SecondOMElement></axis2:FirstOMElement><Foo>Some Text</Foo>"
323: + "<Dependent><Name>FooTwo</Name><Age>25</Age><Sex>Male</Sex></Dependent>"
324: + "<axis2:SecondOMElement xmlns:axis2=\"http://ws.apache.org/namespaces/axis2\">"
325: + "</axis2:SecondOMElement></OMElementTest>";
326:
327: OMFactory factory = OMAbstractFactory.getOMFactory();
328: OMNamespace axis2Namespace = factory.createOMNamespace(
329: org.apache.axis2.Constants.AXIS2_NAMESPACE_URI,
330: org.apache.axis2.Constants.AXIS2_NAMESPACE_PREFIX);
331: OMElement firstElement = factory.createOMElement(
332: "FirstOMElement", axis2Namespace);
333: OMElement secondElement = factory.createOMElement(
334: "SecondOMElement", axis2Namespace, firstElement);
335:
336: ArrayList propertyList = new ArrayList();
337:
338: // add an OMElement
339: propertyList.add(firstElement.getQName());
340: propertyList.add(firstElement);
341:
342: // add some more stuff
343: propertyList.add("Foo");
344: propertyList.add("Some Text");
345: propertyList.add(new QName("Dependent"));
346: DummyADBBean dummyBean = new DummyADBBean();
347: propertyList.add(dummyBean);
348:
349: // lets add one more element
350: propertyList.add(secondElement.getQName());
351: propertyList.add(secondElement);
352:
353: XMLStreamReader pullParser = new ADBXMLStreamReaderImpl(
354: new QName("OMElementTest"), propertyList.toArray(),
355: null);
356: String stringXML = getStringXML(pullParser);
357: try {
358: Document actualDom = newDocument(stringXML);
359: Document expectedDocument = newDocument(expectedXML);
360: assertXMLEqual(actualDom, expectedDocument);
361: } catch (ParserConfigurationException e) {
362: fail("Exception in parsing documents " + e);
363: } catch (SAXException e) {
364: fail("Exception in parsing documents " + e);
365: } catch (IOException e) {
366: fail("Exception in parsing documents " + e);
367: }
368:
369: }
370:
371: /** Test a completely null element */
372: public void testNullableAttribute() {
373: try {
374:
375: /*
376: This is what I expect :
377:
378: */
379: String exptectedXML = "<Person xmlns=\"\"><Name xmlns=\"\">FooOne</Name><DependentOne xmlns=\"\" "
380: + "xsi:nil=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"/>"
381: + "</Person>";
382:
383: ArrayList propertyList = new ArrayList();
384: propertyList.add("Name");
385: propertyList.add("FooOne");
386: propertyList.add(new QName("DependentOne"));
387: propertyList.add(null);
388:
389: QName projectQName = new QName("Person");
390: XMLStreamReader pullParser = new ADBXMLStreamReaderImpl(
391: projectQName, propertyList.toArray(), null);
392:
393: Document actualDom = newDocument(getStringXML(pullParser));
394: Document expectedDocument = newDocument(exptectedXML);
395: assertXMLEqual(actualDom, expectedDocument);
396: } catch (ParserConfigurationException e) {
397: fail("Exception in parsing documents " + e);
398: } catch (SAXException e) {
399: fail("Exception in parsing documents " + e);
400: } catch (IOException e) {
401: fail("Exception in parsing documents " + e);
402: } catch (XMLStreamException e) {
403: fail("Exception in parsing documents " + e);
404: }
405:
406: }
407:
408: /** Test a simple array */
409: public void testSimpleStringArrayScenario() {
410: try {
411: String expectedXML = "<ns1:TestComplexStringArrayScenario xmlns:ns1=\"http://testComplexStringArrayScenario.org\">"
412: + "<StringInfo>Some Text 0</StringInfo>"
413: + "<StringInfo>Some Text 1</StringInfo>"
414: + "<StringInfo>Some Text 2</StringInfo>"
415: + "<StringInfo>Some Text 3</StringInfo>"
416: + "</ns1:TestComplexStringArrayScenario>";
417:
418: ArrayList propertyList = new ArrayList();
419:
420: String[] stringArray = new String[4];
421: for (int i = 0; i < 4; i++) {
422: stringArray[i] = "Some Text " + i;
423: }
424: propertyList.add("StringInfo");
425: propertyList.add(stringArray);
426:
427: XMLStreamReader pullParser = new ADBXMLStreamReaderImpl(
428: new QName(
429: "http://testComplexStringArrayScenario.org",
430: "TestComplexStringArrayScenario", "ns1"),
431: propertyList.toArray(), null);
432: String actualXML = getStringXML(pullParser);
433:
434: assertXMLEqual(newDocument(expectedXML),
435: newDocument(actualXML));
436: } catch (ParserConfigurationException e) {
437: fail("Error has occurred " + e);
438: } catch (SAXException e) {
439: fail("Error has occurred " + e);
440: } catch (IOException e) {
441: fail("Error has occurred " + e);
442: } catch (XMLStreamException e) {
443: fail("Error has occurred " + e);
444: }
445:
446: }
447:
448: /** Test a simple array with null's inbetween */
449: public void testSimpleStringArrayScenarioWithNulls() {
450: try {
451: String expectedXML = "<ns1:TestComplexStringArrayScenario xmlns:ns1=\"http://testComplexStringArrayScenario.org\">"
452: + "<StringInfo>Some Text 0</StringInfo>"
453: + "<StringInfo xsi:nil=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"/>"
454: + "<StringInfo>Some Text 2</StringInfo>"
455: + "<StringInfo>Some Text 3</StringInfo>"
456: + "</ns1:TestComplexStringArrayScenario>";
457:
458: ArrayList propertyList = new ArrayList();
459:
460: String[] stringArray = new String[4];
461: for (int i = 0; i < 4; i++) {
462: stringArray[i] = "Some Text " + i;
463: }
464: stringArray[1] = null;
465:
466: propertyList.add("StringInfo");
467: propertyList.add(stringArray);
468:
469: XMLStreamReader pullParser = new ADBXMLStreamReaderImpl(
470: new QName(
471: "http://testComplexStringArrayScenario.org",
472: "TestComplexStringArrayScenario", "ns1"),
473: propertyList.toArray(), null);
474: String actualXML = getStringXML(pullParser);
475:
476: assertXMLEqual(newDocument(expectedXML),
477: newDocument(actualXML));
478: } catch (ParserConfigurationException e) {
479: fail("Error has occurred " + e);
480: } catch (SAXException e) {
481: fail("Error has occurred " + e);
482: } catch (IOException e) {
483: fail("Error has occurred " + e);
484: } catch (XMLStreamException e) {
485: fail("Error has occurred " + e);
486: }
487:
488: }
489:
490: /** test the mixed content */
491: public void testComplexStringArrayScenarioWithMixedContent() {
492: try {
493: String expectedXML = "<ns1:TestComplexStringArrayScenario xmlns:ns1=\"http://testComplexStringArrayScenario.org\">"
494: + "<Foo>Some Text</Foo>"
495: + "<Dependent>"
496: + "<Name>FooTwo</Name>"
497: + "<Age>25</Age>"
498: + "<Sex>Male</Sex>"
499: + "</Dependent>"
500: + "<StringInfo>Some Text 0</StringInfo>"
501: + "<StringInfo>Some Text 1</StringInfo>"
502: + "<StringInfo>Some Text 2</StringInfo>"
503: + "<StringInfo>Some Text 3</StringInfo>"
504: + "<Bar>Some More Text</Bar>"
505: + "</ns1:TestComplexStringArrayScenario>";
506:
507: ArrayList propertyList = new ArrayList();
508: propertyList.add("Foo");
509: propertyList.add("Some Text");
510: propertyList.add(new QName("Dependent"));
511: DummyADBBean dummyBean = new DummyADBBean();
512: propertyList.add(dummyBean);
513:
514: String[] stringArray = new String[4];
515: for (int i = 0; i < 4; i++) {
516: stringArray[i] = "Some Text " + i;
517: }
518: propertyList.add("StringInfo");
519: propertyList.add(stringArray);
520:
521: propertyList.add("Bar");
522: propertyList.add("Some More Text");
523:
524: XMLStreamReader pullParser = new ADBXMLStreamReaderImpl(
525: new QName(
526: "http://testComplexStringArrayScenario.org",
527: "TestComplexStringArrayScenario", "ns1"),
528: propertyList.toArray(), null);
529: String actualXML = getStringXML(pullParser);
530:
531: assertXMLEqual(newDocument(expectedXML),
532: newDocument(actualXML));
533: } catch (ParserConfigurationException e) {
534: fail("Error has occurred " + e);
535: } catch (SAXException e) {
536: fail("Error has occurred " + e);
537: } catch (IOException e) {
538: fail("Error has occurred " + e);
539: } catch (Exception e) {
540: fail("Error has occurred " + e);
541: }
542:
543: }
544:
545: /** Test a simple array with one element nil */
546: public void testComplexStringArrayScenarioWithNull() {
547: try {
548: String expectedXML = "<ns1:TestComplexStringArrayScenario "
549: + "xmlns:ns1=\"http://testComplexStringArrayScenario.org\" "
550: + ">"
551: + "<StringInfo>Some Text 0</StringInfo>"
552: + "<StringInfo xsi:nil=\"true\" "
553: + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"></StringInfo>"
554: + "<StringInfo>Some Text 2</StringInfo>"
555: + "<StringInfo>Some Text 3</StringInfo>"
556: + "</ns1:TestComplexStringArrayScenario>";
557:
558: ArrayList propertyList = new ArrayList();
559:
560: String[] stringArray = new String[4];
561: for (int i = 0; i < 4; i++) {
562: if (i != 1)
563: stringArray[i] = "Some Text " + i;
564: }
565: stringArray[1] = null;
566:
567: propertyList.add("StringInfo");
568: propertyList.add(stringArray);
569:
570: XMLStreamReader pullParser = new ADBXMLStreamReaderImpl(
571: new QName(
572: "http://testComplexStringArrayScenario.org",
573: "TestComplexStringArrayScenario", "ns1"),
574: propertyList.toArray(), null);
575: String actualXML = getStringXML(pullParser);
576:
577: assertXMLEqual(newDocument(expectedXML),
578: newDocument(actualXML));
579: } catch (ParserConfigurationException e) {
580: fail("Error has occurred " + e);
581: } catch (SAXException e) {
582: fail("Error has occurred " + e);
583: } catch (IOException e) {
584: fail("Error has occurred " + e);
585: } catch (XMLStreamException e) {
586: fail("Error has occurred " + e);
587: }
588:
589: }
590:
591: /**
592: * Test multiple unqulified attributes
593: *
594: * @throws XMLStreamException
595: */
596: public void testAttributes() throws XMLStreamException {
597:
598: String expectedXML = "<emp:Employee xmlns:emp=\"http://ec.org/software\" Attr2=\"Value 2\" "
599: + "Attr3=\"Value 3\" Attr1=\"Value 1\" Attr5=\"Value 5\" Attr4=\"Value 4\"></emp:Employee>";
600:
601: OMFactory factory = OMAbstractFactory.getOMFactory();
602: QName elementQName = new QName("http://ec.org/software",
603: "Employee", "emp");
604: OMAttribute[] attribute = new OMAttribute[5];
605:
606: for (int i = 0; i < 5; i++) {
607: attribute[i] = factory.createOMAttribute("Attr" + (i + 1),
608: null, "Value " + (i + 1));
609: }
610:
611: List omAttribList = new ArrayList();
612: for (int i = 0; i < attribute.length; i++) {
613: omAttribList.add(Constants.OM_ATTRIBUTE_KEY);
614: omAttribList.add(attribute[i]);
615: }
616:
617: String stringXML = getStringXML(new ADBXMLStreamReaderImpl(
618: elementQName, null, omAttribList.toArray()));
619: try {
620: Document actualDom = newDocument(stringXML);
621: Document expectedDocument = newDocument(expectedXML);
622: assertXMLEqual(actualDom, expectedDocument);
623: } catch (ParserConfigurationException e) {
624: fail("Exception in parsing documents " + e);
625: } catch (SAXException e) {
626: fail("Exception in parsing documents " + e);
627: } catch (IOException e) {
628: fail("Exception in parsing documents " + e);
629: }
630:
631: }
632:
633: /** A text only element */
634: public void testElementText() {
635:
636: String expectedXML = "<ns1:testElementText xmlns:ns1=\"http://testElementText.org\">"
637: + "This is some Text for the element</ns1:testElementText>";
638: try {
639: ArrayList properties = new ArrayList();
640: properties.add(ADBXMLStreamReader.ELEMENT_TEXT);
641: properties.add("This is some Text for the element");
642:
643: XMLStreamReader pullParser = new ADBXMLStreamReaderImpl(
644: new QName("http://testElementText.org",
645: "testElementText", "ns1"), properties
646: .toArray(), null);
647:
648: String actualXML = getStringXML(pullParser);
649:
650: assertXMLEqual(newDocument(expectedXML),
651: newDocument(actualXML));
652: } catch (ParserConfigurationException e) {
653: fail("Error has occurred " + e);
654: } catch (SAXException e) {
655: fail("Error has occurred " + e);
656: } catch (IOException e) {
657: fail("Error has occurred " + e);
658: } catch (Exception e) {
659: e.printStackTrace();
660: fail("Error has occurred " + e);
661: }
662: }
663:
664: /// todo Fails due to a bug in WSTX writer
665: // /**
666: // * Test multiple qualified attributes
667: // * @throws XMLStreamException
668: // */
669: // public void testAttributesWithNamespaces() throws XMLStreamException {
670: //
671: // String expectedXML = "<emp:Employee xmlns:emp=\"http://ec.org/software\" " +
672: // "xmlns:attrNS=\"mailto:whoever@whatever.com\" attrNS:Attr2=\"Value 2\" " +
673: // "attrNS:Attr3=\"Value 3\" attrNS:Attr1=\"Value 1\"\n" +
674: // " attrNS:Attr5=\"Value 5\" attrNS:Attr4=\"Value 4\"></emp:Employee>";
675: //
676: // OMFactory factory = OMAbstractFactory.getOMFactory();
677: // QName elementQName = new QName("http://ec.org/software", "Employee", "emp");
678: // OMNamespace attrNS = factory.createOMNamespace("mailto:whoever@whatever.com", "attrNS");
679: //
680: // // add some attributes with namespaces
681: // OMAttribute[] attribute = new OMAttribute[5];
682: // for (int i = 0; i < 5; i++) {
683: // attribute[i] = factory.createOMAttribute("Attr" + (i + 1), attrNS, "Value " + (i + 1));
684: // }
685: //
686: // List omAttribList = new ArrayList();
687: // for (int i = 0; i < attribute.length; i++) {
688: // omAttribList.add(Constants.OM_ATTRIBUTE_KEY);
689: // omAttribList.add(attribute[i]);
690: // }
691: // String stringXML = getStringXML(new ADBXMLStreamReaderImpl(elementQName,
692: // null,
693: // omAttribList.toArray()));
694: // try {
695: // Document actualDom = newDocument(stringXML);
696: // Document expectedDocument = newDocument(expectedXML);
697: // assertXMLEqual(actualDom, expectedDocument);
698: // } catch (ParserConfigurationException e) {
699: // fail("Exception in parsing documents " + e);
700: // } catch (SAXException e) {
701: // fail("Exception in parsing documents " + e);
702: // } catch (IOException e) {
703: // fail("Exception in parsing documents " + e);
704: // }
705: // }
706:
707: /** test for qualified attributes */
708: public void testUnQualifiedAttributes() {
709:
710: String expectedXML = "<ns1:testElementText xmlns:ns1=\"http://testElementText.org\" MyUnQualifiedAttribute=\"MyAttributeValue\">"
711: + "<ns2:QualifiedElement xmlns:ns2=\"http://testQElementText.org\">"
712: + "This is some Text for the element</ns2:QualifiedElement></ns1:testElementText>";
713: try {
714: ArrayList properties = new ArrayList();
715: properties.add(new QName("http://testQElementText.org",
716: "QualifiedElement", "ns2"));
717: properties.add("This is some Text for the element");
718:
719: String[] attributes = new String[2];
720: attributes[0] = "MyUnQualifiedAttribute";
721: attributes[1] = "MyAttributeValue";
722:
723: XMLStreamReader pullParser = new ADBXMLStreamReaderImpl(
724: new QName("http://testElementText.org",
725: "testElementText", "ns1"), properties
726: .toArray(), attributes);
727:
728: String actualXML = getStringXML(pullParser);
729:
730: assertXMLEqual(newDocument(expectedXML),
731: newDocument(actualXML));
732: } catch (ParserConfigurationException e) {
733: fail("Error has occurred " + e);
734: } catch (SAXException e) {
735: fail("Error has occurred " + e);
736: } catch (IOException e) {
737: fail("Error has occurred " + e);
738: } catch (Exception e) {
739: fail("Error has occurred " + e);
740: }
741: }
742:
743: /** test for base64 */
744: public void testBase64EncodedText() {
745:
746: String textTobeSent = "33344MthwrrewrIOTEN)(&**^E(W)EW";
747:
748: String expectedXML = "<ns1:testElementText xmlns:ns1=\"http://testElementText.org\">"
749: + "<ns2:QualifiedElement xmlns:ns2=\"http://testQElementText.org\">"
750: + Base64.encode(textTobeSent.getBytes())
751: + "</ns2:QualifiedElement></ns1:testElementText>";
752: try {
753: ArrayList properties = new ArrayList();
754: properties.add(new QName("http://testQElementText.org",
755: "QualifiedElement", "ns2"));
756: properties.add(new DataHandler(new ByteArrayDataSource(
757: textTobeSent.getBytes())));
758:
759: XMLStreamReader pullParser = new ADBXMLStreamReaderImpl(
760: new QName("http://testElementText.org",
761: "testElementText", "ns1"), properties
762: .toArray(), null);
763:
764: String actualXML = getStringXML(pullParser);
765:
766: assertXMLEqual(newDocument(expectedXML),
767: newDocument(actualXML));
768: } catch (ParserConfigurationException e) {
769: fail("Error has occurred " + e);
770: } catch (SAXException e) {
771: fail("Error has occurred " + e);
772: } catch (IOException e) {
773: fail("Error has occurred " + e);
774: } catch (Exception e) {
775: fail("Error has occurred " + e);
776: }
777: }
778:
779: /** test the qualified elements A qulified element has been associated with a namespace */
780: public void testQualifiedElement() {
781:
782: String expectedXML = "<ns1:testElementText xmlns:ns1=\"http://testElementText.org\">"
783: + "<ns2:QualifiedElement xmlns:ns2=\"http://testQElementText.org\">"
784: + "This is some Text for the element</ns2:QualifiedElement></ns1:testElementText>";
785: try {
786: ArrayList properties = new ArrayList();
787: properties.add(new QName("http://testQElementText.org",
788: "QualifiedElement", "ns2"));
789: properties.add("This is some Text for the element");
790:
791: XMLStreamReader pullParser = new ADBXMLStreamReaderImpl(
792: new QName("http://testElementText.org",
793: "testElementText", "ns1"), properties
794: .toArray(), null);
795:
796: String actualXML = getStringXML(pullParser);
797: assertXMLEqual(newDocument(expectedXML),
798: newDocument(actualXML));
799: } catch (ParserConfigurationException e) {
800: fail("Error has occurred " + e);
801: } catch (SAXException e) {
802: fail("Error has occurred " + e);
803: } catch (IOException e) {
804: fail("Error has occurred " + e);
805: } catch (Exception e) {
806: fail("Error has occurred " + e);
807: }
808: }
809:
810: /**
811: * Util method to convert the pullstream to a string
812: *
813: * @param reader
814: * @return
815: */
816: private String getStringXML(XMLStreamReader reader)
817: throws XMLStreamException {
818: //the returned pullparser starts at an Element rather than the start
819: //document event. This is somewhat disturbing but since an ADBBean
820: //denotes an XMLFragment, it is justifiable to keep the current event
821: //at the Start-element rather than the start document
822: //What it boils down to is that we need to wrap the reader in a
823: //stream wrapper to get a fake start-document event
824:
825: StreamingOMSerializer ser = new StreamingOMSerializer();
826: ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
827: XMLStreamWriter writer = StAXUtils
828: .createXMLStreamWriter(byteArrayOutputStream);
829: ser.serialize(new StreamWrapper(reader), writer);
830: writer.flush();
831: return byteArrayOutputStream.toString();
832: }
833:
834: // /**
835: // * Util method to convert the pullstream to a string
836: // * @param reader
837: // * @return
838: // */
839: // private String getStringXML(XMLStreamReader reader) {
840: // //the returned pullparser starts at an Element rather than the start
841: // //document event. This is somewhat disturbing but since an ADBBean
842: // //denotes an XMLFragment, it is justifiable to keep the current event
843: // //at the Start-element rather than the start document
844: // //What it boils down to is that we need to wrap the reader in a
845: // //stream wrapper to get a fake start-document event
846: // StAXOMBuilder stAXOMBuilder = new StAXOMBuilder(
847: // new StreamWrapper(reader));
848: // //stAXOMBuilder.setDoDebug(true);
849: // OMElement omelement = stAXOMBuilder.getDocumentElement();
850: // return omelement.toString();
851: // }
852:
853: /**
854: * Creates a DOM document from the string
855: *
856: * @param xml
857: * @return
858: * @throws ParserConfigurationException
859: * @throws SAXException
860: * @throws IOException
861: */
862: public Document newDocument(String xml)
863: throws ParserConfigurationException, SAXException,
864: IOException {
865: return db.parse(new ByteArrayInputStream(xml.getBytes()));
866: }
867: }
|