1   /*
2    * Copyright 2006 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.xml.stream;
18  
19  import java.io.InputStream;
20  import java.io.StringReader;
21  import javax.xml.namespace.QName;
22  import javax.xml.stream.XMLInputFactory;
23  import javax.xml.stream.XMLStreamException;
24  import javax.xml.stream.XMLStreamReader;
25  
26  import org.easymock.MockControl;
27  import org.xml.sax.ContentHandler;
28  import org.xml.sax.InputSource;
29  import org.xml.sax.helpers.AttributesImpl;
30  
31  public class StaxStreamXmlReaderTest extends AbstractStaxXmlReaderTestCase {
32  
33      public static final String CONTENT = "<root xmlns='http://springframework.org/spring-ws'><child/></root>";
34  
35      protected AbstractStaxXmlReader createStaxXmlReader(InputStream inputStream) throws XMLStreamException {
36          return new StaxStreamXmlReader(inputFactory.createXMLStreamReader(inputStream));
37      }
38  
39      public void testPartial() throws Exception {
40          XMLInputFactory inputFactory = XMLInputFactory.newInstance();
41          XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(CONTENT));
42          streamReader.nextTag(); // skip to root
43          assertEquals("Invalid element", new QName("http://springframework.org/spring-ws", "root"),
44                  streamReader.getName());
45          streamReader.nextTag(); // skip to child
46          assertEquals("Invalid element", new QName("http://springframework.org/spring-ws", "child"),
47                  streamReader.getName());
48          StaxStreamXmlReader xmlReader = new StaxStreamXmlReader(streamReader);
49  
50          MockControl mockControl = MockControl.createStrictControl(ContentHandler.class);
51          mockControl.setDefaultMatcher(new SaxArgumentMatcher());
52          ContentHandler contentHandlerMock = (ContentHandler) mockControl.getMock();
53  
54          contentHandlerMock.startDocument();
55          contentHandlerMock.startElement("http://springframework.org/spring-ws", "child", "child", new AttributesImpl());
56          contentHandlerMock.endElement("http://springframework.org/spring-ws", "child", "child");
57          contentHandlerMock.endDocument();
58  
59          xmlReader.setContentHandler(contentHandlerMock);
60          mockControl.replay();
61          xmlReader.parse(new InputSource());
62          mockControl.verify();
63      }
64  
65  
66  }