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: */package org.apache.cxf.aegis.xml.stax;
019:
020: import java.io.ByteArrayOutputStream;
021: import java.io.File;
022: import java.io.StringReader;
023:
024: import org.apache.cxf.aegis.util.jdom.StaxBuilder;
025: import org.apache.cxf.aegis.xml.MessageWriter;
026: import org.apache.cxf.aegis.xml.jdom.JDOMWriter;
027: import org.apache.cxf.test.AbstractCXFTest;
028: import org.jdom.Document;
029: import org.jdom.Element;
030: import org.jdom.output.DOMOutputter;
031: import org.junit.After;
032: import org.junit.Before;
033: import org.junit.Test;
034:
035: /**
036: * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
037: * @since Nov 4, 2004
038: */
039: public class WriterTest extends AbstractCXFTest {
040: File output;
041:
042: @Before
043: public void setUp() throws Exception {
044: super .setUpBus();
045:
046: output = File.createTempFile("writetest", ".xml");
047: }
048:
049: @After
050: public void tearDown() {
051: if (output.exists()) {
052: output.delete();
053: }
054: }
055:
056: @Test
057: public void testLiteral() throws Exception {
058: ByteArrayOutputStream bos = new ByteArrayOutputStream();
059: ElementWriter writer = new ElementWriter(bos, "root",
060: "urn:test");
061:
062: write(writer);
063:
064: writer.flush();
065: bos.close();
066:
067: // System.out.println(bos.toString());
068: StaxBuilder builder = new StaxBuilder();
069: Document doc = builder.build(new StringReader(bos.toString()));
070:
071: testWrite(doc);
072: }
073:
074: @Test
075: public void testJDOM() throws Exception {
076: Document doc = new Document(new Element("root", "urn:test"));
077:
078: write(new JDOMWriter(doc.getRootElement()));
079:
080: testWrite(doc);
081: }
082:
083: public void write(MessageWriter writer) {
084: MessageWriter nons = writer.getElementWriter("nons");
085: nons.writeValue("nons");
086: nons.close();
087:
088: MessageWriter intval = writer.getElementWriter("int");
089: intval.writeValueAsInt(new Integer(10000));
090: intval.close();
091:
092: MessageWriter child1 = writer.getElementWriter("child1",
093: "urn:child1");
094: MessageWriter att1 = child1.getAttributeWriter("att1");
095: att1.writeValue("att1");
096: att1.close();
097: MessageWriter att2 = child1.getAttributeWriter("att2", "");
098: att2.writeValue("att2");
099: att2.close();
100: MessageWriter att3 = child1.getAttributeWriter("att3",
101: "urn:att3");
102: att3.writeValue("att3");
103: att3.close();
104: MessageWriter att4 = child1.getAttributeWriter("att4", null);
105: att4.writeValue("att4");
106: att4.close();
107:
108: child1.close();
109:
110: writer.close();
111: }
112:
113: public void testWrite(Document jdoc) throws Exception {
114: org.w3c.dom.Document doc = new DOMOutputter().output(jdoc);
115: addNamespace("t", "urn:test");
116: addNamespace("c", "urn:child1");
117: addNamespace("a", "urn:att3");
118:
119: assertValid("/t:root/t:nons[text()='nons']", doc);
120: assertValid("/t:root/t:int[text()='10000']", doc);
121: assertValid("/t:root/c:child1", doc);
122: assertValid("/t:root/c:child1[@c:att1]", doc);
123: assertValid("/t:root/c:child1[@att2]", doc);
124: assertValid("/t:root/c:child1[@a:att3]", doc);
125: assertValid("/t:root/c:child1[@att4]", doc);
126: }
127: }
|