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 ******************************************************************************/
23 package org.onap.aaf.cadi.wsse.test;
25 import static org.junit.Assert.assertThat;
26 import static org.hamcrest.CoreMatchers.is;
29 import java.io.FileInputStream;
30 import java.io.IOException;
31 import java.io.PrintWriter;
33 import javax.xml.stream.XMLStreamException;
34 import javax.xml.stream.events.XMLEvent;
36 import org.junit.AfterClass;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.onap.aaf.cadi.wsse.XEvent;
40 import org.onap.aaf.cadi.wsse.XReader;
42 public class JU_XReader {
44 private final static String TEST_DIR_NAME = "src/test/resources";
45 private final static String TEST_XML_NAME = "test.xml";
46 private static File testXML;
48 private final static String COMMENT = "a comment";
49 private final static String OUTER_TAG = "outerTag";
50 private final static String INNER_TAG = "innerTag";
51 private final static String DATA_TAG = "dataTag";
52 private final static String DATA = "some text that represents data";
53 private final static String SELF_CLOSING_TAG = "selfClosingTag";
54 private final static String PREFIX = "prefix";
55 private final static String SUFFIX = "suffix";
58 public static void setupOnce() throws IOException {
59 testXML = setupXMLFile();
63 public static void tearDownOnce() {
68 public void test() throws XMLStreamException, IOException {
69 FileInputStream fis = new FileInputStream(TEST_DIR_NAME + '/' + TEST_XML_NAME);
71 XReader xr = new XReader(fis);
72 assertThat(xr.hasNext(), is(true));
75 xe = getNextEvent(xr);
76 assertThat(xe.getEventType(), is(XMLEvent.START_DOCUMENT));
78 xe = getNextEvent(xr);
79 assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
81 xe = getNextEvent(xr);
82 assertThat(xe.getEventType(), is(XMLEvent.COMMENT));
83 assertThat(((XEvent.Comment)xe).value, is(COMMENT));
85 xe = getNextEvent(xr);
86 assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
87 assertThat(xe.asStartElement().getName().toString(), is(OUTER_TAG));
89 xe = getNextEvent(xr);
90 assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
91 assertThat(xe.asStartElement().getName().toString(), is(INNER_TAG));
93 xe = getNextEvent(xr);
94 assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
95 assertThat(xe.asStartElement().getName().toString(), is(DATA_TAG));
97 xe = getNextEvent(xr);
98 assertThat(xe.getEventType(), is(XMLEvent.CHARACTERS));
99 assertThat(xe.asCharacters().getData().toString(), is(DATA));
101 xe = getNextEvent(xr);
102 assertThat(xe.getEventType(), is(XMLEvent.END_ELEMENT));
103 assertThat(xe.asEndElement().getName().toString(), is(DATA_TAG));
105 xe = getNextEvent(xr);
106 assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
107 assertThat(xe.asStartElement().getName().toString(), is(SELF_CLOSING_TAG));
109 xe = getNextEvent(xr);
110 assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
111 assertThat(xe.asStartElement().getName().toString(), is(SUFFIX));
113 xe = getNextEvent(xr);
114 assertThat(xe.getEventType(), is(XMLEvent.END_ELEMENT));
115 assertThat(xe.asEndElement().getName().toString(), is(INNER_TAG));
117 xe = getNextEvent(xr);
118 assertThat(xe.getEventType(), is(XMLEvent.END_ELEMENT));
119 assertThat(xe.asEndElement().getName().toString(), is(OUTER_TAG));
121 assertThat(xr.hasNext(), is(false));
128 private static XEvent getNextEvent(XReader xr) throws XMLStreamException {
130 return xr.nextEvent();
135 private static File setupXMLFile() throws IOException {
136 File xmlFile = new File(TEST_DIR_NAME, TEST_XML_NAME);
137 PrintWriter writer = new PrintWriter(xmlFile);
138 writer.println(" "); // Whitespace before the document - this is for coverage
139 writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
140 writer.println("<!DOCTYPE xml>");
141 writer.println("<!--" + COMMENT + "-->");
142 writer.println("<" + OUTER_TAG + ">");
143 writer.println(" <" + INNER_TAG + ">");
144 writer.println(" <" + DATA_TAG + ">" + DATA + "</" + DATA_TAG + ">");
145 writer.println(" <" + SELF_CLOSING_TAG + " withAnAttribute=\"That has nested \\\" marks\" />");
146 writer.println(" <" + PREFIX + ":" + SUFFIX + "/>");
147 writer.println(" </" + INNER_TAG + ">");
148 writer.println("</" + OUTER_TAG + ">");