8381882: Convert test/jaxp/javax/xml/jaxp/unittest/stream tests to JUnit#30643
8381882: Convert test/jaxp/javax/xml/jaxp/unittest/stream tests to JUnit#30643david-beaumont wants to merge 3 commits intoopenjdk:masterfrom
Conversation
|
👋 Welcome back dbeaumont! A progress list of the required criteria for merging this PR into |
|
❗ This change is not yet ready to be integrated. |
|
@david-beaumont The following label will be automatically applied to this pull request:
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. |
david-beaumont
left a comment
There was a problem hiding this comment.
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")); |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
No need for synchronization on factories.
There was a problem hiding this comment.
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"; |
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
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")); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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: " |
There was a problem hiding this comment.
I've left this in, but realistically only one or these sub-clauses will ever be true.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Not actually asserting anything.
Webrevs
|
Marcono1234
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
| * @run junit/othervm stream.AttributeLocalNameTest.AttributeLocalNameTest | |
| * @run junit/othervm ${test.main.class} |
| 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; | ||
| } |
There was a problem hiding this comment.
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()) { |
There was a problem hiding this comment.
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 forexpected) 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?
| 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())); | ||
| } | ||
| } |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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 + "')"); |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
Split into two asserts (assertFalse)? (applies also to the other test methods)
| String v; | ||
| v = xsr.getAttributeValue(nameSpace, attrName); |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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")); |
There was a problem hiding this comment.
Can simplify this?
| 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); |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
Replace println with assertNotNull, if that was the intention? (or add it in addition?)
| assertEquals("Expected getText() and getTextCharacters() to return same value for event of type (" + tokenTypeDesc(sr.getEventType()) + ")", | ||
| text, text2); |
There was a problem hiding this comment.
Message comes last, currently it is the 'expected' argument.
But how has this assertion passed then before? Is it even reachable?
| byte[] data = content.getBytes("UTF-8"); | ||
| return constructStreamReader(f, data); |
There was a problem hiding this comment.
Avoid try-catch by using StandardCharsets?
A tip also to other reviewers: When you open this PR in |
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
Issue
Reviewing
Using
gitCheckout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/30643/head:pull/30643$ git checkout pull/30643Update a local copy of the PR:
$ git checkout pull/30643$ git pull https://git.openjdk.org/jdk.git pull/30643/headUsing Skara CLI tools
Checkout this PR locally:
$ git pr checkout 30643View PR using the GUI difftool:
$ git pr show -t 30643Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/30643.diff
Using Webrev
Link to Webrev Comment