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;
020:
021: import junit.framework.TestCase;
022: import org.apache.axiom.om.util.StAXUtils;
023:
024: import javax.xml.namespace.QName;
025: import javax.xml.stream.XMLStreamConstants;
026: import javax.xml.stream.XMLStreamException;
027: import javax.xml.stream.XMLStreamReader;
028: import java.io.StringReader;
029:
030: public class SimpleArrayReaderStateMachineTest extends TestCase {
031:
032: public void testStateMachine() throws Exception {
033: String xmlDoc = "<wrapper><myIntVal>200</myIntVal><myIntVal>200</myIntVal><myIntVal>200</myIntVal>"
034: + "<myIntVal>200</myIntVal><myIntVal>200</myIntVal><myIntVal>200</myIntVal></wrapper>";
035: XMLStreamReader reader = StAXUtils
036: .createXMLStreamReader(new StringReader(xmlDoc));
037: SimpleArrayReaderStateMachine sm = new SimpleArrayReaderStateMachine();
038: sm.setElementNameToTest(new QName("myIntVal"));
039: try {
040: sm.read(reader);
041: } catch (XMLStreamException e) {
042: e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
043: }
044:
045: assertEquals(6, sm.getTextArray().length);
046:
047: }
048:
049: public void testStateMachine3() throws Exception {
050: String xmlDoc = "<wrapper><myIntVal>200</myIntVal><myIntVal>200</myIntVal><myIntVal>200</myIntVal>"
051: + "<myIntVal>200</myIntVal><myIntVal>200</myIntVal><myIntVal>200</myIntVal></wrapper>";
052: XMLStreamReader reader = StAXUtils
053: .createXMLStreamReader(new StringReader(xmlDoc));
054:
055: while (reader.hasNext()) {
056: int event = reader.next();
057: if (event == XMLStreamConstants.START_ELEMENT
058: && "myIntVal".equals(reader.getLocalName())) {
059: break;
060: }
061:
062: }
063:
064: SimpleArrayReaderStateMachine sm = new SimpleArrayReaderStateMachine();
065: sm.setElementNameToTest(new QName("myIntVal"));
066: try {
067: sm.read(reader);
068: } catch (XMLStreamException e) {
069: e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
070: }
071:
072: assertEquals(6, sm.getTextArray().length);
073:
074: }
075:
076: public void testStateMachine2() throws Exception {
077: String xmlDoc = "<wrapper><myIntVal>200</myIntVal></wrapper>";
078: XMLStreamReader reader = StAXUtils
079: .createXMLStreamReader(new StringReader(xmlDoc));
080: SimpleArrayReaderStateMachine sm = new SimpleArrayReaderStateMachine();
081: sm.setElementNameToTest(new QName("myIntVal"));
082: try {
083: sm.read(reader);
084: } catch (XMLStreamException e) {
085: e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
086: }
087:
088: assertEquals("200", sm.getTextArray()[0]);
089: }
090:
091: public void testStateMachineEmptyArray() throws Exception {
092: String xmlDoc = "<wrapper></wrapper>";
093: XMLStreamReader reader = StAXUtils
094: .createXMLStreamReader(new StringReader(xmlDoc));
095: SimpleArrayReaderStateMachine sm = new SimpleArrayReaderStateMachine();
096: sm.setElementNameToTest(new QName("myIntVal"));
097: try {
098: sm.read(reader);
099: } catch (Exception e) {
100:
101: }
102:
103: assertEquals(0, sm.getTextArray().length);
104:
105: }
106:
107: }
|