1 /*******************************************************************************
2 * ============LICENSE_START====================================================
4 * * ===========================================================================
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * * ===========================================================================
7 * * Licensed under the Apache License, Version 2.0 (the "License");
8 * * you may not use this file except in compliance with the License.
9 * * You may obtain a copy of the License at
11 * * http://www.apache.org/licenses/LICENSE-2.0
13 * * Unless required by applicable law or agreed to in writing, software
14 * * distributed under the License is distributed on an "AS IS" BASIS,
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * * See the License for the specific language governing permissions and
17 * * limitations under the License.
18 * * ============LICENSE_END====================================================
21 ******************************************************************************/
22 package org.onap.aaf.cadi.wsse.test;
24 import static org.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
29 import java.io.FileInputStream;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.io.PrintWriter;
34 import javax.xml.stream.XMLStreamException;
35 import javax.xml.stream.events.XMLEvent;
37 import org.onap.aaf.cadi.wsse.XEvent;
38 import org.onap.aaf.cadi.wsse.XReader;
40 public class JU_XReader {
42 private final static String TEST_DIR_NAME = "src/test/resources";
43 private final static String TEST_XML_NAME = "test.xml";
44 private static File testXML;
46 private final static String COMMENT = "a comment";
47 private final static String OUTER_TAG = "outerTag";
48 private final static String INNER_TAG = "innerTag";
49 private final static String DATA_TAG = "dataTag";
50 private final static String DATA = "some text that represents data";
51 private final static String SELF_CLOSING_TAG = "selfClosingTag";
52 private final static String PREFIX = "prefix";
53 private final static String SUFFIX = "suffix";
56 public static void setupOnce() throws IOException {
57 testXML = setupXMLFile();
61 public static void tearDownOnce() {
66 public void test() throws XMLStreamException, IOException {
67 FileInputStream fis = new FileInputStream(TEST_DIR_NAME + '/' + TEST_XML_NAME);
69 XReader xr = new XReader(fis);
70 assertThat(xr.hasNext(), is(true));
73 xe = getNextEvent(xr);
74 assertThat(xe.getEventType(), is(XMLEvent.START_DOCUMENT));
76 xe = getNextEvent(xr);
77 assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
79 xe = getNextEvent(xr);
80 assertThat(xe.getEventType(), is(XMLEvent.COMMENT));
81 assertThat(((XEvent.Comment)xe).value, is(COMMENT));
83 xe = getNextEvent(xr);
84 assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
85 assertThat(xe.asStartElement().getName().toString(), is(OUTER_TAG));
87 xe = getNextEvent(xr);
88 assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
89 assertThat(xe.asStartElement().getName().toString(), is(INNER_TAG));
91 xe = getNextEvent(xr);
92 assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
93 assertThat(xe.asStartElement().getName().toString(), is(DATA_TAG));
95 xe = getNextEvent(xr);
96 assertThat(xe.getEventType(), is(XMLEvent.CHARACTERS));
97 assertThat(xe.asCharacters().getData().toString(), is(DATA));
99 xe = getNextEvent(xr);
100 assertThat(xe.getEventType(), is(XMLEvent.END_ELEMENT));
101 assertThat(xe.asEndElement().getName().toString(), is(DATA_TAG));
103 xe = getNextEvent(xr);
104 assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
105 assertThat(xe.asStartElement().getName().toString(), is(SELF_CLOSING_TAG));
107 xe = getNextEvent(xr);
108 assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
109 assertThat(xe.asStartElement().getName().toString(), is(SUFFIX));
111 xe = getNextEvent(xr);
112 assertThat(xe.getEventType(), is(XMLEvent.END_ELEMENT));
113 assertThat(xe.asEndElement().getName().toString(), is(INNER_TAG));
115 xe = getNextEvent(xr);
116 assertThat(xe.getEventType(), is(XMLEvent.END_ELEMENT));
117 assertThat(xe.asEndElement().getName().toString(), is(OUTER_TAG));
119 assertThat(xr.hasNext(), is(false));
127 // public void tagTest() {
128 // String prefix = "prefix";
129 // String name = "name";
130 // String value = "value";
131 // XReader.Tag tag = new Tag(prefix, name, value);
133 // assertThat(tag.toString(), is(prefix + ':' + name + "=\'" + value + "'"));
137 private static XEvent getNextEvent(XReader xr) throws XMLStreamException {
139 return xr.nextEvent();
144 private static File setupXMLFile() throws IOException {
145 File xmlFile = new File(TEST_DIR_NAME, TEST_XML_NAME);
146 PrintWriter writer = new PrintWriter(xmlFile);
147 writer.println(" "); // Whitespace before the document - this is for coverage
148 writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
149 writer.println("<!DOCTYPE xml>");
150 writer.println("<!--" + COMMENT + "-->");
151 writer.println("<" + OUTER_TAG + ">");
152 writer.println(" <" + INNER_TAG + ">");
153 writer.println(" <" + DATA_TAG + ">" + DATA + "</" + DATA_TAG + ">");
154 writer.println(" <" + SELF_CLOSING_TAG + " withAnAttribute=\"That has nested \\\" marks\" />");
155 writer.println(" <" + PREFIX + ":" + SUFFIX + "/>");
156 writer.println(" </" + INNER_TAG + ">");
157 writer.println("</" + OUTER_TAG + ">");