|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | +package transform; |
| 24 | + |
| 25 | +import java.io.BufferedWriter; |
| 26 | +import java.io.ByteArrayOutputStream; |
| 27 | +import java.io.File; |
| 28 | +import java.io.FileInputStream; |
| 29 | +import java.io.IOException; |
| 30 | +import java.io.OutputStream; |
| 31 | +import java.io.OutputStreamWriter; |
| 32 | +import java.util.prefs.InvalidPreferencesFormatException; |
| 33 | +import java.util.prefs.Preferences; |
| 34 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 35 | +import javax.xml.parsers.ParserConfigurationException; |
| 36 | +import javax.xml.transform.OutputKeys; |
| 37 | +import javax.xml.transform.Transformer; |
| 38 | +import javax.xml.transform.TransformerException; |
| 39 | +import javax.xml.transform.TransformerFactory; |
| 40 | +import javax.xml.transform.dom.DOMSource; |
| 41 | +import javax.xml.transform.stream.StreamResult; |
| 42 | +import org.testng.Assert; |
| 43 | +import org.testng.annotations.Test; |
| 44 | +import org.w3c.dom.DOMImplementation; |
| 45 | +import org.w3c.dom.Document; |
| 46 | +import org.w3c.dom.DocumentType; |
| 47 | +import org.w3c.dom.Element; |
| 48 | + |
| 49 | +/* |
| 50 | + * @test |
| 51 | + * @bug 8273370 |
| 52 | + * @run testng transform.SerializationTest |
| 53 | + * @summary Verifies that the characters are written correctly during serialization. |
| 54 | + */ |
| 55 | +public class SerializationTest { |
| 56 | + |
| 57 | + private static final String PREFS_DTD_URI |
| 58 | + = "http://java.sun.com/dtd/preferences.dtd"; |
| 59 | + private static String CLS_DIR = System.getProperty("test.classes", "."); |
| 60 | + private static String SRC_DIR = System.getProperty("test.src"); |
| 61 | + |
| 62 | + /** |
| 63 | + * Verifies that the XMLSupport for exportSubtree handles control characters |
| 64 | + * correctly by reporting en error. |
| 65 | + * |
| 66 | + * Note: exportSubtree currently throws AssertionError. It would be more |
| 67 | + * appropriate to throw InvalidPreferencesFormatException as the import |
| 68 | + * method does. Since this is an edge case however, we'll keep it as is to |
| 69 | + * avoid signature change. |
| 70 | + * |
| 71 | + * The following was the original test: |
| 72 | + Preferences p = Preferences.userRoot().node("test"); |
| 73 | + p.put("key", "[\u0018\u0019]"); |
| 74 | + p.exportSubtree(new ByteArrayOutputStream()); |
| 75 | + * |
| 76 | + * The code however, hanged when running in JTReg. This test therefore replaced |
| 77 | + * the above code with the process extracted from the exportSubtree routine. |
| 78 | + * |
| 79 | + * @throws Exception if the test fails |
| 80 | + */ |
| 81 | + @Test |
| 82 | + public void testTrasformer() throws Exception { |
| 83 | + Assert.assertThrows(AssertionError.class, |
| 84 | + () -> export(new ByteArrayOutputStream())); |
| 85 | + } |
| 86 | + |
| 87 | + private void export(OutputStream os) throws IOException { |
| 88 | + Document doc = createPrefsDoc("preferences"); |
| 89 | + Element preferences = doc.getDocumentElement(); |
| 90 | + preferences.setAttribute("EXTERNAL_XML_VERSION", "1.0"); |
| 91 | + Element xmlRoot = (Element) preferences.appendChild(doc.createElement("root")); |
| 92 | + xmlRoot.setAttribute("type", "user"); |
| 93 | + |
| 94 | + Element e = xmlRoot; |
| 95 | + |
| 96 | + e.appendChild(doc.createElement("map")); |
| 97 | + e = (Element) e.appendChild(doc.createElement("node")); |
| 98 | + e.setAttribute("name", "test"); |
| 99 | + |
| 100 | + putPreferencesInXml(e, doc); |
| 101 | + |
| 102 | + writeDoc(doc, os); |
| 103 | + } |
| 104 | + |
| 105 | + private static Document createPrefsDoc(String qname) { |
| 106 | + try { |
| 107 | + DOMImplementation di = DocumentBuilderFactory.newInstance(). |
| 108 | + newDocumentBuilder().getDOMImplementation(); |
| 109 | + DocumentType dt = di.createDocumentType(qname, null, PREFS_DTD_URI); |
| 110 | + return di.createDocument(null, qname, dt); |
| 111 | + } catch (ParserConfigurationException e) { |
| 112 | + throw new AssertionError(e); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private static void putPreferencesInXml(Element elt, Document doc) { |
| 117 | + Element map = (Element) elt.appendChild(doc.createElement("map")); |
| 118 | + Element entry = (Element) map.appendChild(doc.createElement("entry")); |
| 119 | + entry.setAttribute("key", "key"); |
| 120 | + entry.setAttribute("value", "[\u0018\u0019]"); |
| 121 | + } |
| 122 | + |
| 123 | + private void writeDoc(Document doc, OutputStream out) |
| 124 | + throws IOException { |
| 125 | + try { |
| 126 | + TransformerFactory tf = TransformerFactory.newInstance(); |
| 127 | + tf.setAttribute("indent-number", 2); |
| 128 | + Transformer t = tf.newTransformer(); |
| 129 | + t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, doc.getDoctype().getSystemId()); |
| 130 | + t.setOutputProperty(OutputKeys.INDENT, "yes"); |
| 131 | + //Transformer resets the "indent" info if the "result" is a StreamResult with |
| 132 | + //an OutputStream object embedded, creating a Writer object on top of that |
| 133 | + //OutputStream object however works. |
| 134 | + t.transform(new DOMSource(doc), |
| 135 | + new StreamResult(new BufferedWriter(new OutputStreamWriter(out, "UTF-8")))); |
| 136 | + } catch (TransformerException e) { |
| 137 | + throw new AssertionError(e); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Verifies that the XMLSupport for importPreferences handles control |
| 143 | + * characters correctly by reporting en error. |
| 144 | + * |
| 145 | + * Note: this is the existing behavior. This test is here to match with the |
| 146 | + * export method. |
| 147 | + * |
| 148 | + * "preferences.xml" was generated by calling the exportSubtree method |
| 149 | + * before the patch. |
| 150 | + * |
| 151 | + * @throws Exception if the test fails |
| 152 | + */ |
| 153 | + @Test |
| 154 | + public void testParser() throws Exception { |
| 155 | + Assert.assertThrows(InvalidPreferencesFormatException.class, () -> { |
| 156 | + Preferences.importPreferences( |
| 157 | + new FileInputStream(new File(SRC_DIR + "/preferences.xml"))); |
| 158 | + }); |
| 159 | + } |
| 160 | +} |
0 commit comments