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.service.stax;
019:
020: import javax.xml.stream.XMLStreamException;
021: import javax.xml.stream.XMLStreamReader;
022: import javax.xml.stream.XMLStreamWriter;
023:
024: import org.w3c.dom.Node;
025:
026: import org.apache.cxf.databinding.stax.StaxDataBinding;
027: import org.apache.cxf.databinding.stax.StaxDataBindingFeature;
028: import org.apache.cxf.databinding.stax.XMLStreamWriterCallback;
029: import org.apache.cxf.frontend.ServerFactoryBean;
030: import org.apache.cxf.interceptor.Fault;
031: import org.apache.cxf.staxutils.FragmentStreamReader;
032: import org.apache.cxf.test.AbstractCXFTest;
033: import org.apache.cxf.transport.local.LocalTransportFactory;
034: import org.junit.Test;
035:
036: public class StaxDatabindingTest extends AbstractCXFTest {
037: @Test
038: public void testCallback() throws Exception {
039: String address = "local://foo";
040:
041: ServerFactoryBean sf = new ServerFactoryBean();
042: sf.setServiceBean(new CallbackService());
043: sf.setAddress(address);
044: sf.setDataBinding(new StaxDataBinding());
045: sf.getFeatures().add(new StaxDataBindingFeature());
046:
047: sf.create();
048:
049: Node res = invoke(address, LocalTransportFactory.TRANSPORT_ID,
050: "req.xml");
051:
052: assertValid("//bleh", res);
053: }
054:
055: @Test
056: public void testCopy() throws Exception {
057: String address = "local://foo";
058:
059: ServerFactoryBean sf = new ServerFactoryBean();
060: sf.setServiceBean(new CopyService());
061: sf.setAddress(address);
062: sf.setDataBinding(new StaxDataBinding());
063: sf.getFeatures().add(new StaxDataBindingFeature());
064:
065: sf.create();
066:
067: Node res = invoke(address, LocalTransportFactory.TRANSPORT_ID,
068: "req.xml");
069:
070: //DOMUtils.writeXml(res, System.out);
071: addNamespace("a", "http://stax.service.cxf.apache.org/");
072: assertValid("//a:bleh", res);
073: }
074:
075: public static class CallbackService {
076: public XMLStreamWriterCallback invoke(
077: final XMLStreamReader reader) {
078: try {
079: reader.nextTag();
080: } catch (XMLStreamException e) {
081: throw new Fault(e);
082: }
083:
084: return new XMLStreamWriterCallback() {
085:
086: public void write(XMLStreamWriter writer) throws Fault,
087: XMLStreamException {
088: writer.writeEmptyElement("bleh");
089: }
090:
091: };
092: }
093: }
094:
095: public static class CopyService {
096: public XMLStreamReader invoke(final XMLStreamReader reader) {
097: return new FragmentStreamReader(reader);
098: }
099: }
100: }
|