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.assertThat;
25 import static org.hamcrest.CoreMatchers.is;
28 import java.io.FileInputStream;
29 import java.io.IOException;
30 import java.io.PrintWriter;
32 import javax.xml.stream.XMLStreamException;
33 import javax.xml.stream.events.XMLEvent;
35 import org.junit.AfterClass;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.onap.aaf.cadi.wsse.XEvent;
39 import org.onap.aaf.cadi.wsse.XReader;
41 public class JU_XReader {
43 private final static String TEST_DIR_NAME = "src/test/resources";
44 private final static String TEST_XML_NAME = "test.xml";
45 private static File testXML;
47 private final static String COMMENT = "a comment";
48 private final static String OUTER_TAG = "outerTag";
49 private final static String INNER_TAG = "innerTag";
50 private final static String DATA_TAG = "dataTag";
51 private final static String DATA = "some text that represents data";
52 private final static String SELF_CLOSING_TAG = "selfClosingTag";
53 private final static String PREFIX = "prefix";
54 private final static String SUFFIX = "suffix";
57 public static void setupOnce() throws IOException {
58 testXML = setupXMLFile();
62 public static void tearDownOnce() {
67 public void test() throws XMLStreamException, IOException {
68 FileInputStream fis = new FileInputStream(TEST_DIR_NAME + '/' + TEST_XML_NAME);
70 XReader xr = new XReader(fis);
71 assertThat(xr.hasNext(), is(true));
74 xe = getNextEvent(xr);
75 assertThat(xe.getEventType(), is(XMLEvent.START_DOCUMENT));
77 xe = getNextEvent(xr);
78 assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
80 xe = getNextEvent(xr);
81 assertThat(xe.getEventType(), is(XMLEvent.COMMENT));
82 assertThat(((XEvent.Comment)xe).value, is(COMMENT));
84 xe = getNextEvent(xr);
85 assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
86 assertThat(xe.asStartElement().getName().toString(), is(OUTER_TAG));
88 xe = getNextEvent(xr);
89 assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
90 assertThat(xe.asStartElement().getName().toString(), is(INNER_TAG));
92 xe = getNextEvent(xr);
93 assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
94 assertThat(xe.asStartElement().getName().toString(), is(DATA_TAG));
96 xe = getNextEvent(xr);
97 assertThat(xe.getEventType(), is(XMLEvent.CHARACTERS));
98 assertThat(xe.asCharacters().getData().toString(), is(DATA));
100 xe = getNextEvent(xr);
101 assertThat(xe.getEventType(), is(XMLEvent.END_ELEMENT));
102 assertThat(xe.asEndElement().getName().toString(), is(DATA_TAG));
104 xe = getNextEvent(xr);
105 assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
106 assertThat(xe.asStartElement().getName().toString(), is(SELF_CLOSING_TAG));
108 xe = getNextEvent(xr);
109 assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
110 assertThat(xe.asStartElement().getName().toString(), is(SUFFIX));
112 xe = getNextEvent(xr);
113 assertThat(xe.getEventType(), is(XMLEvent.END_ELEMENT));
114 assertThat(xe.asEndElement().getName().toString(), is(INNER_TAG));
116 xe = getNextEvent(xr);
117 assertThat(xe.getEventType(), is(XMLEvent.END_ELEMENT));
118 assertThat(xe.asEndElement().getName().toString(), is(OUTER_TAG));
120 assertThat(xr.hasNext(), is(false));
127 private static XEvent getNextEvent(XReader xr) throws XMLStreamException {
129 return xr.nextEvent();
134 private static File setupXMLFile() throws IOException {
135 File xmlFile = new File(TEST_DIR_NAME, TEST_XML_NAME);
136 PrintWriter writer = new PrintWriter(xmlFile);
137 writer.println(" "); // Whitespace before the document - this is for coverage
138 writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
139 writer.println("<!DOCTYPE xml>");
140 writer.println("<!--" + COMMENT + "-->");
141 writer.println("<" + OUTER_TAG + ">");
142 writer.println(" <" + INNER_TAG + ">");
143 writer.println(" <" + DATA_TAG + ">" + DATA + "</" + DATA_TAG + ">");
144 writer.println(" <" + SELF_CLOSING_TAG + " withAnAttribute=\"That has nested \\\" marks\" />");
145 writer.println(" <" + PREFIX + ":" + SUFFIX + "/>");
146 writer.println(" </" + INNER_TAG + ">");
147 writer.println("</" + OUTER_TAG + ">");