Skip to content

Commit

Permalink
#216 Cleanup: switch to some features of modern Java in test code (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
winfriedgerlach authored Nov 25, 2024
1 parent dd14f34 commit c72367f
Show file tree
Hide file tree
Showing 67 changed files with 313 additions and 440 deletions.
8 changes: 4 additions & 4 deletions src/test/java/failing/BasicSaxTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public class BasicSaxTest
public void testCData() throws Exception
{
SAXParser parser = new WstxSAXParser();
StringBuffer buffer = new StringBuffer("<root><![CDATA[");
StringBuilder builder = new StringBuilder("<root><![CDATA[");
for (int i=0; i<100000; i++) {
buffer.append('a');
builder.append('a');
}
buffer.append("]]></root>");
builder.append("]]></root>");
CDATASectionCounter handler = new CDATASectionCounter();
parser.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
parser.parse(new InputSource(new StringReader(buffer.toString())), handler);
parser.parse(new InputSource(new StringReader(builder.toString())), handler);
// Should get as many cdata sections as text segments
int cdatas = handler.getCDATASectionCount();
int segments = handler.getSegmentCount();
Expand Down
15 changes: 6 additions & 9 deletions src/test/java/failing/ExtLocationInfo91Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,13 @@ public class ExtLocationInfo91Test
public void testLocationsWithExtEntity()
throws XMLStreamException
{
XMLResolver resolver = new XMLResolver() {
@Override
public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace) throws XMLStreamException {
if (INCL_URI.equals(systemID)){
StreamSource src = new StreamSource(new StringReader(TEST_EXT_ENT_INCL), systemID);
return src;
}
fail("Unexpected systemID to resolve: " + systemID);
return null;
XMLResolver resolver = (publicID, systemID, baseURI, namespace) -> {
if (INCL_URI.equals(systemID)){
StreamSource src = new StreamSource(new StringReader(TEST_EXT_ENT_INCL), systemID);
return src;
}
fail("Unexpected systemID to resolve: " + systemID);
return null;
};
XMLStreamReader2 sr = getReader(TEST_EXT_ENT, URI, resolver);

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/failing/RelaxNGTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private void verifyRngFailure(String xml, XMLValidationSchema schema, String fai
} catch (XMLValidationException vex) {
String origMsg = vex.getMessage();
String msg = (origMsg == null) ? "" : origMsg.toLowerCase();
if (msg.indexOf(failPhrase.toLowerCase()) < 0) {
if (!msg.contains(failPhrase.toLowerCase())) {
String actualMsg = "Expected validation exception for "+failMsg+", containing phrase '"+failPhrase+"': got '"+origMsg+"'";
if (strict) {
fail(actualMsg);
Expand Down
10 changes: 2 additions & 8 deletions src/test/java/failing/TestInvalidAttributeValue190.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,10 @@ public void testInvalidAttributeValue() throws Exception
XMLStreamReader2 sr = (XMLStreamReader2)f.createXMLStreamReader(
new StringReader(DOC));

final List<XMLValidationProblem> probs = new ArrayList<XMLValidationProblem>();
final List<XMLValidationProblem> probs = new ArrayList<>();

sr.validateAgainst(schema);
sr.setValidationProblemHandler(new ValidationProblemHandler() {
@Override
public void reportProblem(XMLValidationProblem problem)
throws XMLValidationException {
probs.add(problem);
}
});
sr.setValidationProblemHandler(probs::add);

assertTokenType(START_ELEMENT, sr.next());
assertEquals("root", sr.getLocalName());
Expand Down
16 changes: 4 additions & 12 deletions src/test/java/failing/W3CSchemaNillable179Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,25 +110,17 @@ void testNillable(String xmlDocument, boolean validateReader, boolean validateWr
xmlReader = (XMLStreamReader2) f.createXMLStreamReader(new StringReader(xmlDocument));

if (validateReader) {
xmlReader.setValidationProblemHandler(new ValidationProblemHandler() {
@Override
public void reportProblem(XMLValidationProblem problem)
throws XMLValidationException {
throw new LocalValidationError(problem);
}
xmlReader.setValidationProblemHandler(problem -> {
throw new LocalValidationError(problem);
});
xmlReader.validateAgainst(schema);
}

xmlWriter = (XMLStreamWriter2) getOutputFactory().createXMLStreamWriter(writer);

if (validateWriter) {
xmlWriter.setValidationProblemHandler(new ValidationProblemHandler() {
@Override
public void reportProblem(XMLValidationProblem problem)
throws XMLValidationException {
throw new LocalValidationError(problem);
}
xmlWriter.setValidationProblemHandler(problem -> {
throw new LocalValidationError(problem);
});
xmlWriter.validateAgainst(schema);
}
Expand Down
50 changes: 25 additions & 25 deletions src/test/java/org/codehaus/stax/test/BaseStaxTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@ public abstract class BaseStaxTest
*/
final static String PROP_REPORT_CDATA = "http://java.sun.com/xml/stream/properties/report-cdata-event";

final static HashMap<Integer,String> mTokenTypes = new HashMap<Integer,String>();
final static HashMap<Integer,String> mTokenTypes = new HashMap<>();
static {
mTokenTypes.put(Integer.valueOf(START_ELEMENT), "START_ELEMENT");
mTokenTypes.put(Integer.valueOf(END_ELEMENT), "END_ELEMENT");
mTokenTypes.put(Integer.valueOf(START_DOCUMENT), "START_DOCUMENT");
mTokenTypes.put(Integer.valueOf(END_DOCUMENT), "END_DOCUMENT");
mTokenTypes.put(Integer.valueOf(CHARACTERS), "CHARACTERS");
mTokenTypes.put(Integer.valueOf(CDATA), "CDATA");
mTokenTypes.put(Integer.valueOf(COMMENT), "COMMENT");
mTokenTypes.put(Integer.valueOf(PROCESSING_INSTRUCTION), "PROCESSING_INSTRUCTION");
mTokenTypes.put(Integer.valueOf(DTD), "DTD");
mTokenTypes.put(Integer.valueOf(SPACE), "SPACE");
mTokenTypes.put(Integer.valueOf(ENTITY_REFERENCE), "ENTITY_REFERENCE");
mTokenTypes.put(Integer.valueOf(NAMESPACE), "NAMESPACE_DECLARATION");
mTokenTypes.put(Integer.valueOf(NOTATION_DECLARATION), "NOTATION_DECLARATION");
mTokenTypes.put(Integer.valueOf(ENTITY_DECLARATION), "ENTITY_DECLARATION");
mTokenTypes.put(START_ELEMENT, "START_ELEMENT");
mTokenTypes.put(END_ELEMENT, "END_ELEMENT");
mTokenTypes.put(START_DOCUMENT, "START_DOCUMENT");
mTokenTypes.put(END_DOCUMENT, "END_DOCUMENT");
mTokenTypes.put(CHARACTERS, "CHARACTERS");
mTokenTypes.put(CDATA, "CDATA");
mTokenTypes.put(COMMENT, "COMMENT");
mTokenTypes.put(PROCESSING_INSTRUCTION, "PROCESSING_INSTRUCTION");
mTokenTypes.put(DTD, "DTD");
mTokenTypes.put(SPACE, "SPACE");
mTokenTypes.put(ENTITY_REFERENCE, "ENTITY_REFERENCE");
mTokenTypes.put(NAMESPACE, "NAMESPACE_DECLARATION");
mTokenTypes.put(NOTATION_DECLARATION, "NOTATION_DECLARATION");
mTokenTypes.put(ENTITY_DECLARATION, "ENTITY_DECLARATION");
}

/*
Expand Down Expand Up @@ -185,7 +185,7 @@ protected XMLStreamReader constructNsStreamReader(String content, boolean coal)
protected static boolean isCoalescing(XMLInputFactory f)
throws XMLStreamException
{
return ((Boolean) f.getProperty(XMLInputFactory.IS_COALESCING)).booleanValue();
return (Boolean) f.getProperty(XMLInputFactory.IS_COALESCING);
}

protected static void setCoalescing(XMLInputFactory f, boolean state)
Expand All @@ -200,7 +200,7 @@ protected static void setCoalescing(XMLInputFactory f, boolean state)
protected static boolean isValidating(XMLInputFactory f)
throws XMLStreamException
{
return ((Boolean) f.getProperty(XMLInputFactory.IS_VALIDATING)).booleanValue();
return (Boolean) f.getProperty(XMLInputFactory.IS_VALIDATING);
}

protected static void setValidating(XMLInputFactory f, boolean state)
Expand All @@ -219,7 +219,7 @@ protected static void setValidating(XMLInputFactory f, boolean state)
protected static boolean isNamespaceAware(XMLInputFactory f)
throws XMLStreamException
{
return ((Boolean) f.getProperty(XMLInputFactory.IS_NAMESPACE_AWARE)).booleanValue();
return (Boolean) f.getProperty(XMLInputFactory.IS_NAMESPACE_AWARE);
}

/**
Expand Down Expand Up @@ -269,7 +269,7 @@ protected static boolean setSupportExternalEntities(XMLInputFactory f, boolean s
try {
f.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, b);
Object act = f.getProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES);
return (act instanceof Boolean) && ((Boolean) act).booleanValue() == state;
return (act instanceof Boolean) && (Boolean) act == state;
} catch (IllegalArgumentException e) {
/* Let's assume, then, that the property (or specific value for it)
* is NOT supported...
Expand Down Expand Up @@ -360,7 +360,7 @@ protected static String getAndVerifyText(XMLStreamReader sr)
protected static String getAllText(XMLStreamReader sr)
throws XMLStreamException
{
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
while (true) {
int tt = sr.getEventType();
if (tt != CHARACTERS && tt != SPACE && tt != CDATA) {
Expand All @@ -375,7 +375,7 @@ protected static String getAllText(XMLStreamReader sr)
protected static String getAllCData(XMLStreamReader sr)
throws XMLStreamException
{
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
while (true) {
/* Note: CDATA sections CAN be reported as CHARACTERS, but
* not as SPACE
Expand Down Expand Up @@ -524,7 +524,7 @@ protected void verifyException(Throwable e, String match)
String msg = e.getMessage();
String lmsg = msg.toLowerCase();
String lmatch = match.toLowerCase();
if (lmsg.indexOf(lmatch) < 0) {
if (!lmsg.contains(lmatch)) {
fail("Expected an exception with sub-string \""+match+"\": got one with message \""+msg+"\"");
}
}
Expand All @@ -550,7 +550,7 @@ protected static String stripXmlDecl(String xml) {

protected static String tokenTypeDesc(int tt)
{
String desc = (String) mTokenTypes.get(Integer.valueOf(tt));
String desc = (String) mTokenTypes.get(tt);
if (desc == null) {
return "["+tt+"]";
}
Expand Down Expand Up @@ -603,7 +603,7 @@ protected static String printable(char ch)
return "_";
}
if (ch > 127 || ch < 32) {
StringBuffer sb = new StringBuffer(6);
StringBuilder sb = new StringBuilder(6);
sb.append("\\u");
String hex = Integer.toHexString((int)ch);
for (int i = 0, len = 4 - hex.length(); i < len; i++) {
Expand All @@ -622,7 +622,7 @@ protected static String printable(String str)
}

int len = str.length();
StringBuffer sb = new StringBuffer(len + 64);
StringBuilder sb = new StringBuilder(len + 64);
for (int i = 0; i < len; ++i) {
char c = str.charAt(i);
String res = printable(c);
Expand Down
4 changes: 1 addition & 3 deletions src/test/java/org/codehaus/stax/test/evt/TestEventDTD.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,7 @@ private XMLEventReader getReader(String contents, boolean nsAware,

private void testListElems(List<?> l, Class<?> expType)
{
Iterator<?> it = l.iterator();
while (it.hasNext()) {
Object o = it.next();
for (Object o : l) {
assertNotNull(o);
assertTrue(expType.isAssignableFrom(o.getClass()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public void testStartElementWithAttrs()
final String NS_URI = "http://foo";

XMLEventFactory f = getEventFactory();
ArrayList<Attribute> attrs = new ArrayList<Attribute>();
ArrayList<Attribute> attrs = new ArrayList<>();
Attribute attr1 = f.createAttribute(new QName("attr1"), "value");
testEventWritability(attr1);
attrs.add(attr1);
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/org/codehaus/stax/test/evt/TestEventWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public void testNonRepairingNsWrite()

XMLEventFactory evtf = getEventFactory();

ArrayList<Attribute> attrs = new ArrayList<Attribute>();
ArrayList<Attribute> attrs = new ArrayList<>();
attrs.add(evtf.createAttribute("attr", "value"));
attrs.add(evtf.createAttribute("ns", "uri", "attr2", "value2"));
ArrayList<Namespace> ns = new ArrayList<Namespace>();
ArrayList<Namespace> ns = new ArrayList<>();
ns.add(evtf.createNamespace("ns", "uri"));
StartElement elem = evtf.createStartElement("", "", "root", attrs.iterator(), ns.iterator());

Expand Down Expand Up @@ -137,7 +137,7 @@ public void testPassThrough()

private <T> List<T> fetchElems(Iterator<T> it)
{
ArrayList<T> l = new ArrayList<T>();
ArrayList<T> l = new ArrayList<>();
while (it.hasNext()) {
l.add(it.next());
}
Expand Down Expand Up @@ -178,7 +178,7 @@ private XMLEventReader getEventReader(String contents, boolean nsAware,
private List<XMLEvent> collectEvents(XMLEventReader er)
throws XMLStreamException
{
ArrayList<XMLEvent> events = new ArrayList<XMLEvent>();
ArrayList<XMLEvent> events = new ArrayList<>();
while (er.hasNext()) {
events.add(er.nextEvent());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ private void assertAttr11Value(StartElement elem, String localName, String expVa

private String get11AttrDoc()
{
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
sb.append("<root");
for (int i = 0; i < ATTR11_NAMES.length; ++i) {
sb.append(' ');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ public void testInvalidAccessByIndex()

private String get11AttrDoc()
{
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
sb.append("<root");
for (int i = 0; i < ATTR11_NAMES.length; ++i) {
sb.append(' ');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class TestCDataRead
final static String CDATA1;
final static String CDATA2;
static {
StringBuffer sb1 = new StringBuffer(8000);
StringBuffer sb2 = new StringBuffer(8000);
StringBuilder sb1 = new StringBuilder(8000);
StringBuilder sb2 = new StringBuilder(8000);

sb1.append("...");
sb2.append("\n \n\n ");
Expand Down Expand Up @@ -92,7 +92,7 @@ public void testCDataNonCoalescing()
CDATA, type);
}

StringBuffer sb = new StringBuffer(16000);
StringBuilder sb = new StringBuilder(16000);
do {
sb.append(getAndVerifyText(sr));
type = sr.next();
Expand Down Expand Up @@ -144,7 +144,7 @@ public void testInvalidNestedCData()
XMLStreamReader sr = getReader(XML, coal);
assertTokenType(START_ELEMENT, sr.next());
// Ok, now should get an exception...
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
int type = -1;
try {
while (true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ private void doTestProperties(boolean nsAware, boolean dtd)
break;
case 4:
method = "getNamespaceCount";
result = Integer.valueOf(sr.getNamespaceCount());
result = sr.getNamespaceCount();
break;
case 5:
method = "getAttributeCount";
result = Integer.valueOf(sr.getAttributeCount());
result = sr.getAttributeCount();
break;
case 6:
method = "getPITarget";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,11 @@ private void doTestProperties(boolean nsAware)
break;
case 4:
method = "getNamespaceCount";
result = Integer.valueOf(sr.getNamespaceCount());
result = sr.getNamespaceCount();
break;
case 5:
method = "getAttributeCount";
result = Integer.valueOf(sr.getAttributeCount());
result = sr.getAttributeCount();
break;
case 6:
method = "getPITarget";
Expand All @@ -230,11 +230,11 @@ private void doTestProperties(boolean nsAware)
break;
case 9:
method = "getTextStart";
result = Integer.valueOf(sr.getTextStart());
result = sr.getTextStart();
break;
case 10:
method = "getTextLength";
result = Integer.valueOf(sr.getTextLength());
result = sr.getTextLength();
break;
}
fail("Expected IllegalStateException, when calling "
Expand Down
Loading

0 comments on commit c72367f

Please sign in to comment.