Skip to content

Commit 3777327

Browse files
author
Niall Pemberton
committed
Create a version of the XmlStreamReaderUtilitiesTest to run against the original XmlStreamReader implementation (Sanity check to make sure all the refactoring hasn't screwed things up)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1004857 13f79535-47bb-0310-9956-ffa450edef68
1 parent 389eebc commit 3777327

3 files changed

Lines changed: 70 additions & 72 deletions

File tree

src/test/java/org/apache/commons/io/input/XmlStreamReaderUtilitiesTest.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,15 @@ private void checkRawEncoding(String expected,
166166
builder.append("xmlGuessEnc=[").append(xmlGuessEnc).append("], ");
167167
builder.append("xmlEnc=[").append(xmlEnc).append("], ");
168168
builder.append("defaultEncoding=[").append(defaultEncoding).append("],");
169-
MockXmlStreamReader mock = new MockXmlStreamReader(defaultEncoding);
170-
String encoding = mock.calculateRawEncoding(bomEnc,xmlGuessEnc,xmlEnc);
169+
String encoding = calculateRawEncoding(bomEnc,xmlGuessEnc,xmlEnc, defaultEncoding);
171170
assertEquals(builder.toString(), expected, encoding);
172171
}
172+
protected String calculateRawEncoding(String bomEnc, String xmlGuessEnc, String xmlEnc,
173+
String defaultEncoding) throws IOException {
174+
MockXmlStreamReader mock = new MockXmlStreamReader(defaultEncoding);
175+
String encoding = mock.calculateRawEncoding(bomEnc, xmlGuessEnc, xmlEnc);
176+
return encoding;
177+
}
173178
private void checkRawError(String msgSuffix,
174179
String bomEnc, String xmlGuessEnc, String xmlEnc, String defaultEncoding) {
175180
try {
@@ -212,20 +217,25 @@ public void testCalculateHttpEncoding() throws IOException {
212217
checkHttpEncoding("UTF-8", false, APPXML_UTF8, null, null, null, null);
213218
checkHttpEncoding("UTF-8", false, APPXML_UTF8, "UTF-16BE", "UTF-16BE", "UTF-16BE", "UTF-16BE");
214219
}
215-
private void checkHttpEncoding(String expected, boolean lenienet, String httpContentType,
220+
private void checkHttpEncoding(String expected, boolean lenient, String httpContentType,
216221
String bomEnc, String xmlGuessEnc, String xmlEnc, String defaultEncoding) throws IOException {
217222
StringBuilder builder = new StringBuilder();
218223
builder.append("HttpEncoding: ").append(bomEnc).append("], ");
219-
builder.append("lenienet=[").append(lenienet).append("], ");
224+
builder.append("lenient=[").append(lenient).append("], ");
220225
builder.append("httpContentType=[").append(httpContentType).append("], ");
221226
builder.append("bomEnc=[").append(bomEnc).append("], ");
222227
builder.append("xmlGuessEnc=[").append(xmlGuessEnc).append("], ");
223228
builder.append("xmlEnc=[").append(xmlEnc).append("], ");
224229
builder.append("defaultEncoding=[").append(defaultEncoding).append("],");
225-
MockXmlStreamReader mock = new MockXmlStreamReader(defaultEncoding);
226-
String encoding = mock.calculateHttpEncoding(httpContentType, bomEnc, xmlGuessEnc, xmlEnc, lenienet);
230+
String encoding = calculateHttpEncoding(httpContentType, bomEnc, xmlGuessEnc, xmlEnc, lenient, defaultEncoding);
227231
assertEquals(builder.toString(), expected, encoding);
228232
}
233+
protected String calculateHttpEncoding(String httpContentType, String bomEnc, String xmlGuessEnc,
234+
String xmlEnc, boolean lenient, String defaultEncoding) throws IOException {
235+
MockXmlStreamReader mock = new MockXmlStreamReader(defaultEncoding);
236+
String encoding = mock.calculateHttpEncoding(httpContentType, bomEnc, xmlGuessEnc, xmlEnc, lenient);
237+
return encoding;
238+
}
229239
private void checkHttpError(String msgSuffix, boolean lenienet, String httpContentType,
230240
String bomEnc, String xmlGuessEnc, String xmlEnc, String defaultEncoding) {
231241
try {

src/test/java/org/apache/commons/io/input/compatibility/XmlStreamReaderException.java

Lines changed: 3 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.apache.commons.io.input.compatibility;
1818

1919
import java.io.InputStream;
20-
import java.io.IOException;
2120

2221
/**
2322
* The XmlStreamReaderException is thrown by the XmlStreamReader constructors if
@@ -32,16 +31,9 @@
3231
* @author Alejandro Abdelnur
3332
* @version $Id$
3433
*/
35-
public class XmlStreamReaderException extends IOException {
36-
private final String bomEncoding;
34+
public class XmlStreamReaderException extends org.apache.commons.io.input.XmlStreamReaderException {
3735

38-
private final String xmlGuessEncoding;
39-
40-
private final String xmlEncoding;
41-
42-
private final String contentTypeMime;
43-
44-
private final String contentTypeEncoding;
36+
private static final long serialVersionUID = 1L;
4537

4638
private final InputStream is;
4739

@@ -78,65 +70,10 @@ public XmlStreamReaderException(String msg, String bomEnc,
7870
*/
7971
public XmlStreamReaderException(String msg, String ctMime, String ctEnc,
8072
String bomEnc, String xmlGuessEnc, String xmlEnc, InputStream is) {
81-
super(msg);
82-
contentTypeMime = ctMime;
83-
contentTypeEncoding = ctEnc;
84-
bomEncoding = bomEnc;
85-
xmlGuessEncoding = xmlGuessEnc;
86-
xmlEncoding = xmlEnc;
73+
super(msg, ctMime, ctEnc, bomEnc, xmlGuessEnc, xmlEnc);
8774
this.is = is;
8875
}
8976

90-
/**
91-
* Returns the BOM encoding found in the InputStream.
92-
*
93-
* @return the BOM encoding, null if none.
94-
*/
95-
public String getBomEncoding() {
96-
return bomEncoding;
97-
}
98-
99-
/**
100-
* Returns the encoding guess based on the first bytes of the InputStream.
101-
*
102-
* @return the encoding guess, null if it couldn't be guessed.
103-
*/
104-
public String getXmlGuessEncoding() {
105-
return xmlGuessEncoding;
106-
}
107-
108-
/**
109-
* Returns the encoding found in the XML prolog of the InputStream.
110-
*
111-
* @return the encoding of the XML prolog, null if none.
112-
*/
113-
public String getXmlEncoding() {
114-
return xmlEncoding;
115-
}
116-
117-
/**
118-
* Returns the MIME type in the content-type used to attempt determining the
119-
* encoding.
120-
*
121-
* @return the MIME type in the content-type, null if there was not
122-
* content-type or the encoding detection did not involve HTTP.
123-
*/
124-
public String getContentTypeMime() {
125-
return contentTypeMime;
126-
}
127-
128-
/**
129-
* Returns the encoding in the content-type used to attempt determining the
130-
* encoding.
131-
*
132-
* @return the encoding in the content-type, null if there was not
133-
* content-type, no encoding in it or the encoding detection did not
134-
* involve HTTP.
135-
*/
136-
public String getContentTypeEncoding() {
137-
return contentTypeEncoding;
138-
}
139-
14077
/**
14178
* Returns the unconsumed InputStream to allow the application to do an
14279
* alternate encoding detection on the InputStream.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.io.input.compatibility;
18+
19+
import java.io.ByteArrayInputStream;
20+
import java.io.IOException;
21+
22+
import org.apache.commons.io.input.XmlStreamReaderUtilitiesTest;
23+
24+
/**
25+
* Test compatibility of the original XmlStreamReader (before all the refactoring).
26+
*/
27+
public class XmlStreamReaderUtilitiesCompatibilityTest extends XmlStreamReaderUtilitiesTest {
28+
29+
protected String calculateRawEncoding(String bomEnc, String xmlGuessEnc, String xmlEnc,
30+
String defaultEncoding) throws IOException {
31+
MockXmlStreamReader mock = new MockXmlStreamReader(defaultEncoding);
32+
String encoding = mock.calculateRawEncoding(bomEnc, xmlGuessEnc, xmlEnc, null);
33+
return encoding;
34+
}
35+
protected String calculateHttpEncoding(String httpContentType, String bomEnc, String xmlGuessEnc,
36+
String xmlEnc, boolean lenient, String defaultEncoding) throws IOException {
37+
MockXmlStreamReader mock = new MockXmlStreamReader(defaultEncoding);
38+
String encoding = mock.calculateHttpEncoding(
39+
XmlStreamReader.getContentTypeMime(httpContentType),
40+
XmlStreamReader.getContentTypeEncoding(httpContentType),
41+
bomEnc, xmlGuessEnc, xmlEnc, null, lenient);
42+
return encoding;
43+
}
44+
45+
/** Mock {@link XmlStreamReader} implementation */
46+
private static class MockXmlStreamReader extends XmlStreamReader {
47+
MockXmlStreamReader(String defaultEncoding) throws IOException {
48+
super(new ByteArrayInputStream("".getBytes()), null, true, defaultEncoding);
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)