Skip to content

8381882: Convert test/jaxp/javax/xml/jaxp/unittest/stream tests to JUnit#30643

Open
david-beaumont wants to merge 3 commits intoopenjdk:masterfrom
david-beaumont:jdk_8381882_jaxp_stream
Open

8381882: Convert test/jaxp/javax/xml/jaxp/unittest/stream tests to JUnit#30643
david-beaumont wants to merge 3 commits intoopenjdk:masterfrom
david-beaumont:jdk_8381882_jaxp_stream

Conversation

@david-beaumont
Copy link
Copy Markdown
Contributor

@david-beaumont david-beaumont commented Apr 9, 2026

Convert and tidy-up unit tests in test/jaxp/javax/xml/jaxp/unittest/stream.

It is recommended to set the diff settings in github to hide whitespace (cog-wheel menu).

There are a lot of cases where manual catch blocks with explicit fail are removed to just allow the test to fail idiomatically by throwing the exception normally. This causes thousands of lines to be un-indented (with no other change).

About 200 explicit uses of fail() were either removed or converted to appropriate JUnit asserts.

There are also several non-trivial changes in this PR related to bad tests where, for example, catching and ignoring exceptions allowed the test to pass under all situations.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8381882: Convert test/jaxp/javax/xml/jaxp/unittest/stream tests to JUnit (Sub-task - P4)

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/30643/head:pull/30643
$ git checkout pull/30643

Update a local copy of the PR:
$ git checkout pull/30643
$ git pull https://git.openjdk.org/jdk.git pull/30643/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 30643

View PR using the GUI difftool:
$ git pr show -t 30643

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/30643.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link
Copy Markdown

bridgekeeper bot commented Apr 9, 2026

👋 Welcome back dbeaumont! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link
Copy Markdown

openjdk bot commented Apr 9, 2026

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk openjdk bot added the core-libs core-libs-dev@openjdk.org label Apr 9, 2026
@openjdk
Copy link
Copy Markdown

openjdk bot commented Apr 9, 2026

@david-beaumont The following label will be automatically applied to this pull request:

  • core-libs

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

Copy link
Copy Markdown
Contributor Author

@david-beaumont david-beaumont left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some "preloaded" comments explaining non-trivial changes.

Assert.assertEquals(s1.getEventType(), XMLStreamConstants.START_DOCUMENT);
s1.next();
s1.next(); // advance to <TITLE>
Assert.assertTrue(s1.getLocalName().equals("TITLE"));
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of assertions have been rewritten to be idiomatic. Mostly this is handled via IntelliJ analysis, but not always.

public class Bug6509774 {

@Test
public void test0() {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was obviously cut & pasted with alternating blank lines (making it much less readable), so I reformatted it to remove unwanted empty lines.

}
}

public static/* synchronized */XMLStreamReader getReader(InputStream is) throws Exception {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for synchronization on factories.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even worse, based on the @summary it sounds like the whole purpose of this test is to verify that these methods can be called concurrently, so the /* synchronized */ was quite misleading?

*/
public class CoalesceTest {

String countryElementContent = "START India CS}}}}}} India END";
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As well as making these constants, I could switch to UPPER_SNAKE_CASE. Your (reviewer) call.

eventType = streamReader.next();
if (eventType == XMLStreamConstants.CHARACTERS) {
String text = streamReader.getText();
if (!text.equals(descriptionElementContent)) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of the debug output is needed if assertEquals() is used.

public void testDuplicateNamespaceURI() throws Exception {

xmlStreamWriter.writeStartDocument();
xmlStreamWriter.writeStartElement(new String(""), "localName", new String("nsUri"));
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear why the new String() was being used here. I could revert it if it's felt likely to be important.

public class NamespaceTest {

/** debug output? */
private static final boolean DEBUG = true;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug output is extensive and unnecessary for unit tests. I've checked that no output was also affecting the test itself.

}

Assert.assertTrue(actualOutput.equals(EXPECTED_OUTPUT) || actualOutput.equals(EXPECTED_OUTPUT_2), "Expected: " + EXPECTED_OUTPUT + "\n" + "Actual: "
assertTrue(actualOutput.equals(EXPECTED_OUTPUT) || actualOutput.equals(EXPECTED_OUTPUT_2), "Expected: " + EXPECTED_OUTPUT + "\n" + "Actual: "
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've left this in, but realistically only one or these sub-clauses will ever be true.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of removal of redundant catch clauses in this test... sorry!

XMLStreamWriter w = xof.createXMLStreamWriter(sw);
w.writeStartDocument();
w.writeStartElement("foo", "bar", "zot");
w.writeDefaultNamespace(null);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not actually asserting anything.

@david-beaumont david-beaumont marked this pull request as ready for review April 9, 2026 09:51
@openjdk openjdk bot added the rfr Pull request is ready for review label Apr 9, 2026
@mlbridge
Copy link
Copy Markdown

mlbridge bot commented Apr 9, 2026

Webrevs

Copy link
Copy Markdown

@Marcono1234 Marcono1234 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully these comments are useful, if not please let me know. They are only suggestions; feel free to ignore them, I am not an OpenJDK member.

(This is the first half of the modified files; I will probably have a look at the other half afterwards.)

* @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
* @run testng/othervm stream.AttributeLocalNameTest.AttributeLocalNameTest
* @library /javax/xml/jaxp/unittest
* @run junit/othervm stream.AttributeLocalNameTest.AttributeLocalNameTest
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other JUnit PRs it was suggested to use ${test.main.class} for @run to avoid repetition, would that work here (for all files) as well?

Suggested change
* @run junit/othervm stream.AttributeLocalNameTest.AttributeLocalNameTest
* @run junit/othervm ${test.main.class}

Comment on lines 152 to 157
if (!expectedLine.equals(actualLine)) {
System.out.println("Entityreference expansion failed, line no: " + expectedOutput.getLineNumber());
System.out.println("Expected: " + expectedLine);
System.out.println("Actual : " + actualLine);
return false;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be more convenient to directly use assertEquals(expectedLine, actualLine, "in line " + expectedOutput.getLineNumber()) or similar here?

Or maybe even do an assertEquals(expected.readAllLines(), actual.readAllLines()), unless that leads to too verbose assertions errors or behaves differently than LineNumberReader.

LineNumberReader expectedOutput = new LineNumberReader(expected);
LineNumberReader actualOutput = new LineNumberReader(actual);

while (expectedOutput.ready() && actualOutput.ready()) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These ready() checks seem problematic:

  • if actualOutput.ready() is false, the test passes if there are less lines than expected
  • depending on the type of the given input stream, could ready() return false (even for expected) so no comparison is performed at all?

Maybe it would be better to just have a while (true) loop, which breaks once expectedOutput.readLine() == null && actualOutput.readLine() == null?

Comment on lines -105 to -112
private void testListElems(List l, Class expType) {
Iterator it = l.iterator();
while (it.hasNext()) {
Object o = it.next();
Assert.assertNotNull(o);
Assert.assertTrue(expType.isAssignableFrom(o.getClass()));
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this made the test slightly less strict because this here also included a !list.contains(null) check which is now not performed anymore?

Assert.assertTrue(loc1.getLineNumber() == 1);
Location loc2 = evt.getLocation();
System.out.println("Location 2: " + loc2.getLineNumber() + "," + loc2.getColumnNumber());
evt = er.nextEvent(); // root
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This nextEvent() // root call is gone now it seems. Maybe that was relevant before, to ensure loc1 and loc2 were not affected by it?

(When re-adding it, maybe add a comment to explain this?)

@Test
public void testReadingNamespace() {
public void testReadingNamespace() throws Exception {
is = new java.io.ByteArrayInputStream(getXML().getBytes());
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add import for java.io.ByteArrayInputStream? (applies to the other test methods here as well)

if (eventType == XMLStreamConstants.START_ELEMENT) {
if (sr.getLocalName().equals(rootElement)) {
assertEquals(prefixApple, sr.getNamespacePrefix(0));
assertEquals(namespaceURIApple, sr.getNamespaceURI(0));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For robustness set a foundElement = true and check assertTrue(foundElement) after the loop?

(applies to the other test methods here as well)


Assert.assertTrue((hasNext_1 == hasNext_2) && (hasNext_1 == hasNext_3),
"XMLStreamReader.hasNext() returns inconsistent values for each subsequent call: " + hasNext_1 + ", " + hasNext_2 + ", " + hasNext_3);
boolean hasNext = assertDoesNotThrow(r1::hasNext);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comment why assertDoesNotThrow is needed, that is, because callers call checkHasNext within a assertThrows(XMLStreamException, ...)?

Assert.fail("Exception occured: " + e.getMessage());
}
String version = r.getVersion();
System.out.println("Bug6767322.xml: " + version);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertNotNull(version)? (or even assertEquals?)

inputFactory.createXMLStreamReader(new StringReader(xml));
Assert.fail("Expected an exception for " + msg);
} catch (XMLStreamException ex) { // good
System.out.println("Expected failure: '" + ex.getMessage() + "' " + "(matching message: '" + msg + "')");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should check exception message? While it was not done previously, this previous println suggests assertTrue(message.contains("illegal declaration"), "message: " + message)?

}
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLStreamReader r = xif.createXMLStreamReader(new StringReader(xml));
assertTrue(!r.standaloneSet() && !r.isStandalone());
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split into two asserts (assertFalse)? (applies also to the other test methods)

Comment on lines 63 to 64
String v;
v = xsr.getAttributeValue(nameSpace, attrName);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move assignment to declaration?

String v = xsr.getAttributeValue(nameSpace, attrName);

public void testRootElementNamespace() throws Exception {
XMLInputFactory xif = XMLInputFactory.newInstance();
xif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
InputStream is = new java.io.ByteArrayInputStream(getXML().getBytes());
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add import for java.io.ByteArrayInputStream? (also for the other test methods below)

reset();
public void supportDTD(boolean supportDTD, boolean replaceEntity, int inputType) throws Exception {
print("\n");
print((supportDTD ? "SupportDTD=true" : "SupportDTD=false") + ", " + (replaceEntity ? "replaceEntity=true" : "replaceEntity=false"));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can simplify this?

Suggested change
print((supportDTD ? "SupportDTD=true" : "SupportDTD=false") + ", " + (replaceEntity ? "replaceEntity=true" : "replaceEntity=false"));
print("SupportDTD=" + supportDTD + ", replaceEntity=" + replaceEntity);

XMLStreamWriter w = of.createXMLStreamWriter(new ByteArrayOutputStream());
XMLStreamWriter w1 = of.createXMLStreamWriter(new ByteArrayOutputStream());
assertTrue(w.equals(w) && w.hashCode() == w.hashCode());
assertNotEquals(w1, w);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency explicitly check assertFalse(w1.equals(w)) here, since this test is about the equals implementation?

JUnit is not intended for checking equals implementations, and might take shortcuts.

assertNull(delegate.getAttributeNamespace(0));
assertEquals("http://ns1.java.com", delegate.getAttributeNamespace(1));
assertEquals("ns1", delegate.getAttributePrefix(1));
assertEquals(delegate.getAttributeName(1).toString(), "{" + delegate.getAttributeNamespace(1) + "}" + delegate.getAttributeLocalName(1));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switch arguments here? Is delegate.getAttributeName(1).toString() rather the 'actual' value?

Assert.assertTrue(delegate.getAttributeCount() == 5);
Assert.assertTrue(delegate.getAttributeType(1) == "CDATA");
assertEquals(5, delegate.getAttributeCount());
assertSame("CDATA", delegate.getAttributeType(1));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe assertSame("CDATA", ...) is actually too strict here; does it really have to be the same exact string object?

Though it matches the previous assertion behavior.

while (delegate.hasNext()) {
if (delegate.getEventType() == XMLStreamConstants.START_ELEMENT) {
if (delegate.getLocalName().equals("name") || delegate.getLocalName().equals("price")) {
System.out.println(delegate.getElementText());
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace println with assertNotNull, if that was the intention? (or add it in addition?)

Comment on lines +308 to 309
assertEquals("Expected getText() and getTextCharacters() to return same value for event of type (" + tokenTypeDesc(sr.getEventType()) + ")",
text, text2);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Message comes last, currently it is the 'expected' argument.

But how has this assertion passed then before? Is it even reachable?

Comment on lines 137 to 138
byte[] data = content.getBytes("UTF-8");
return constructStreamReader(f, data);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid try-catch by using StandardCharsets?

@Marcono1234
Copy link
Copy Markdown

It is recommended to set the diff settings in github to hide whitespace (cog-wheel menu).

A tip also to other reviewers: When you open this PR in github.dev (docs) (which opens when you press the . key while viewing a PR), the diff is often better, though the comment UI might not be as nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core-libs core-libs-dev@openjdk.org rfr Pull request is ready for review

Development

Successfully merging this pull request may close these issues.

2 participants