Skip to content

Commit 6e3acf2

Browse files
nkutschecowtowncoder
authored andcommitted
Unit test for Issue#91 (#98)
Adds unit test case for issue #91
1 parent f11c033 commit 6e3acf2

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package stax2.stream;
2+
3+
import org.codehaus.stax2.LocationInfo;
4+
import org.codehaus.stax2.XMLStreamReader2;
5+
import stax2.BaseStax2Test;
6+
7+
import javax.xml.stream.*;
8+
import javax.xml.transform.stream.StreamSource;
9+
import java.io.StringReader;
10+
11+
/**
12+
* Set of unit tests that checks that the {@link LocationInfo} implementation
13+
* works as expected, provides proper values or -1 to indicate "don't know".
14+
*/
15+
public class TestExtLocationInfo
16+
extends BaseStax2Test
17+
{
18+
final static String URI = "main.xml";
19+
final static String INCL_URI = "include.xml";
20+
/**
21+
* This document fragment tries ensure that external entities works ok
22+
*/
23+
final static String TEST_EXT_ENT =
24+
"<?xml version='1.0'?>"
25+
+"<!DOCTYPE main [\n" // first char: 21; row 1
26+
+"<!ENTITY incl SYSTEM '" + INCL_URI + "'>\n" // fc: 38; row 2
27+
+"]>\n" // fc: 74; row 3
28+
+"<main>" // fc: 77; row 4
29+
+"&incl;" // fc: 83; row 4
30+
+"</main>"; // fc: 89; row 4
31+
// EOF, fc: 98; row 7
32+
33+
/**
34+
* This document fragment is used to be included from TEST_EXT_ENT
35+
*/
36+
final static String TEST_EXT_ENT_INCL =
37+
"<include></include>"; // first char: 0; row 1
38+
// EOF, fc: 19; row 1
39+
40+
41+
42+
public void testLocationsWithExtEntity()
43+
throws XMLStreamException
44+
{
45+
46+
XMLResolver resolver = new XMLResolver() {
47+
@Override
48+
public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace) throws XMLStreamException {
49+
if (INCL_URI.equals(systemID)){
50+
StreamSource src = new StreamSource(new StringReader(TEST_EXT_ENT_INCL), systemID);
51+
return src;
52+
}
53+
fail("Unexpected systemID to resolve: " + systemID);
54+
return null;
55+
}
56+
};
57+
XMLStreamReader2 sr = getReader(TEST_EXT_ENT, URI, resolver);
58+
59+
assertOffset(sr, 0, 21);
60+
61+
assertTokenType(DTD, sr.next());
62+
assertTokenType(START_ELEMENT, sr.next());
63+
assertEquals("main", sr.getLocalName());
64+
assertOffset(sr, 77, 83, URI);
65+
66+
assertTokenType(START_ELEMENT, sr.next());
67+
assertEquals("include", sr.getLocalName());
68+
assertOffset(sr, 0, 9, INCL_URI);
69+
70+
71+
72+
assertTokenType(END_ELEMENT, sr.next());
73+
assertOffset(sr, 9, 19, INCL_URI);
74+
75+
76+
assertTokenType(END_ELEMENT, sr.next());
77+
assertOffset(sr, 89, 96, URI);
78+
79+
sr.close();
80+
}
81+
82+
/*
83+
////////////////////////////////////////
84+
// Private methods
85+
////////////////////////////////////////
86+
*/
87+
88+
private void assertOffset(XMLStreamReader2 sr, int startOffset, int endOffset, String systemId)
89+
throws XMLStreamException
90+
{
91+
LocationInfo li = sr.getLocationInfo();
92+
Location startLoc = li.getStartLocation();
93+
assertEquals("Incorrect starting systemID for event " + tokenTypeDesc(sr.getEventType()), systemId, startLoc.getSystemId());
94+
Location endLoc = li.getEndLocation();
95+
assertEquals("Incorrect ending systemID for event " + tokenTypeDesc(sr.getEventType()), systemId, endLoc.getSystemId());
96+
assertOffset(sr, startOffset, endOffset);
97+
}
98+
private void assertOffset(XMLStreamReader2 sr, int startOffset, int endOffset)
99+
throws XMLStreamException
100+
{
101+
LocationInfo li = sr.getLocationInfo();
102+
Location startLoc = li.getStartLocation();
103+
assertEquals("Incorrect starting offset for event "+tokenTypeDesc(sr.getEventType()), startOffset, startLoc.getCharacterOffset());
104+
Location endLoc = li.getEndLocation();
105+
assertEquals("Incorrect ending offset for event "+tokenTypeDesc(sr.getEventType()), endOffset, endLoc.getCharacterOffset());
106+
}
107+
108+
109+
private XMLStreamReader2 getReader(String contents, String systemId, XMLResolver xmlResolver)
110+
throws XMLStreamException
111+
{
112+
XMLInputFactory f = getInputFactory();
113+
114+
if(xmlResolver != null){
115+
f.setXMLResolver(xmlResolver);
116+
}
117+
118+
setCoalescing(f, false); // shouldn't really matter
119+
setNamespaceAware(f, true);
120+
setSupportExternalEntities(f, true);
121+
setReplaceEntities(f, true);
122+
123+
124+
// No need to validate, just need entities
125+
setValidating(f, false);
126+
127+
return (XMLStreamReader2) f.createXMLStreamReader(new StreamSource(new StringReader(contents), systemId));
128+
}
129+
}

0 commit comments

Comments
 (0)