Skip to content

Commit e3a4caa

Browse files
Amos ShiGoeLin
Amos Shi
authored andcommitted
8216408: XMLStreamWriter setDefaultNamespace(null) throws NullPointerException
Backport-of: 1ebe11a
1 parent 4543378 commit e3a4caa

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

src/java.xml/share/classes/com/sun/xml/internal/stream/writers/XMLStreamWriterImpl.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -44,6 +44,7 @@
4444
import java.util.Iterator;
4545
import java.util.List;
4646
import java.util.Map;
47+
import java.util.Objects;
4748
import java.util.Random;
4849
import java.util.Set;
4950
import javax.xml.XMLConstants;
@@ -1729,12 +1730,7 @@ private void addAttrNamespace(String prefix, String uri) {
17291730
*/
17301731
private boolean isDefaultNamespace(String uri) {
17311732
String defaultNamespace = fInternalNamespaceContext.getURI(DEFAULT_PREFIX);
1732-
1733-
if (uri.equals(defaultNamespace)) {
1734-
return true;
1735-
}
1736-
1737-
return false;
1733+
return Objects.equals(uri, defaultNamespace);
17381734
}
17391735

17401736
/**

test/jaxp/javax/xml/jaxp/unittest/stream/XMLStreamWriterTest/XMLStreamWriterTest.java

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2019 Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -42,11 +42,11 @@
4242

4343
/*
4444
* @test
45-
* @bug 6347190 8139584
45+
* @bug 6347190 8139584 8216408
4646
* @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
4747
* @run testng/othervm -DrunSecMngr=true stream.XMLStreamWriterTest.XMLStreamWriterTest
4848
* @run testng/othervm stream.XMLStreamWriterTest.XMLStreamWriterTest
49-
* @summary Test StAX Writer won't insert comment into element inside.
49+
* @summary Tests XMLStreamWriter.
5050
*/
5151
@Listeners({jaxp.library.BasePolicy.class})
5252
public class XMLStreamWriterTest {
@@ -94,12 +94,14 @@ public void testCreateStartDocument_DOMWriter()
9494
}
9595

9696
/**
97-
* Test of main method, of class TestXMLStreamWriter.
97+
* Verifies that the StAX Writer won't insert comment into the element tag.
9898
*/
9999
@Test
100100
public void testWriteComment() {
101101
try {
102-
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><a:html href=\"http://java.sun.com\"><!--This is comment-->java.sun.com</a:html>";
102+
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
103+
+ "<a:html href=\"http://java.sun.com\">"
104+
+ "<!--This is comment-->java.sun.com</a:html>";
103105
XMLOutputFactory f = XMLOutputFactory.newInstance();
104106
// f.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES,
105107
// Boolean.TRUE);
@@ -122,4 +124,18 @@ public void testWriteComment() {
122124
}
123125
}
124126

127+
/**
128+
* @bug 8216408
129+
* Verifies that setDefaultNamespace accepts null.
130+
*
131+
* @throws Exception
132+
*/
133+
@Test
134+
public void testSetDefaultNamespace() throws Exception {
135+
XMLOutputFactory f = XMLOutputFactory.newFactory();
136+
f.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
137+
StringWriter sw = new StringWriter();
138+
XMLStreamWriter xsw = f.createXMLStreamWriter(sw);
139+
xsw.setDefaultNamespace(null);
140+
}
125141
}

0 commit comments

Comments
 (0)