1 /* 2 * Copyright (c) 2014, 2015, 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 24 package transform; 25 26 import java.io.File; 27 28 import javax.xml.parsers.DocumentBuilder; 29 import javax.xml.parsers.DocumentBuilderFactory; 30 import javax.xml.transform.Transformer; 31 import javax.xml.transform.TransformerFactory; 32 import javax.xml.transform.dom.DOMSource; 33 import javax.xml.transform.stream.StreamResult; 34 35 import org.testng.Assert; 36 import org.testng.annotations.Test; 37 import org.w3c.dom.Document; 38 import org.w3c.dom.Element; 39 40 /* 41 * @bug 6551600 42 * @summary Test using UNC path as StreamResult. 43 */ 44 public class CR6551600Test { 45 46 @Test 47 public final void testUNCPath() { 48 String hostName = ""; 49 try { 50 hostName = java.net.InetAddress.getLocalHost().getHostName(); 51 } catch (java.net.UnknownHostException e) { 52 // falls through 53 } 54 55 String path = "\\\\" + hostName + "\\C$\\xslt_unc_test.xml"; 56 String os = System.getProperty("os.name"); 57 if (os.indexOf("Windows") < 0) { 58 path = "///tmp/test.xml"; 59 } 60 else { 61 policy.PolicyUtil.changePolicy(getClass().getResource("CR6551600.policy").getFile()); 62 } 63 64 try { 65 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 66 DocumentBuilder builder = factory.newDocumentBuilder(); 67 Document doc = builder.newDocument(); 68 Element root = doc.createElement("test"); 69 doc.appendChild(root); 70 // create an identity transform 71 Transformer t = TransformerFactory.newInstance().newTransformer(); 72 File f = new File(path); 73 StreamResult result = new StreamResult(f); 74 DOMSource source = new DOMSource(doc); 75 System.out.println("Writing to " + f); 76 t.transform(source, result); 77 } catch (Exception e) { 78 // unexpected failure 79 e.printStackTrace(); 80 Assert.fail(e.toString()); 81 } 82 83 File file = new File(path); 84 if (file.exists()) { 85 file.deleteOnExit(); 86 } 87 } 88 } | 1 /* 2 * Copyright (c) 2014, 2016, 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 24 package transform; 25 26 import java.io.File; 27 import java.io.FilePermission; 28 29 import javax.xml.parsers.DocumentBuilder; 30 import javax.xml.parsers.DocumentBuilderFactory; 31 import javax.xml.transform.Transformer; 32 import javax.xml.transform.TransformerFactory; 33 import javax.xml.transform.dom.DOMSource; 34 import javax.xml.transform.stream.StreamResult; 35 36 import jaxp.library.JAXPTestUtilities; 37 38 import org.testng.Assert; 39 import org.testng.annotations.Listeners; 40 import org.testng.annotations.Test; 41 import org.w3c.dom.Document; 42 import org.w3c.dom.Element; 43 44 /* 45 * @bug 6551600 46 * @summary Test using UNC path as StreamResult. 47 */ 48 @Listeners({ jaxp.library.BasePolicy.class }) 49 public class CR6551600Test { 50 51 @Test 52 public final void testUNCPath() { 53 boolean isWindows = System.getProperty("os.name").contains("Windows"); 54 JAXPTestUtilities.runWithTmpPermission(() -> { 55 String hostName = ""; 56 try { 57 hostName = java.net.InetAddress.getLocalHost().getHostName(); 58 } catch (java.net.UnknownHostException e) { 59 // falls through 60 } 61 62 String path = isWindows ? "\\\\" + hostName + "\\C$\\xslt_unc_test.xml" : "///tmp/test.xml"; 63 64 try { 65 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 66 DocumentBuilder builder = factory.newDocumentBuilder(); 67 Document doc = builder.newDocument(); 68 Element root = doc.createElement("test"); 69 doc.appendChild(root); 70 // create an identity transform 71 Transformer t = TransformerFactory.newInstance().newTransformer(); 72 File f = new File(path); 73 StreamResult result = new StreamResult(f); 74 DOMSource source = new DOMSource(doc); 75 System.out.println("Writing to " + f); 76 t.transform(source, result); 77 } catch (Exception e) { 78 // unexpected failure 79 e.printStackTrace(); 80 Assert.fail(e.toString()); 81 } 82 83 File file = new File(path); 84 if (file.exists()) { 85 file.deleteOnExit(); 86 } 87 }, isWindows ? new FilePermission("//localhost/C$/xslt_unc_test.xml", "read, write, delete") 88 : new FilePermission("///tmp/test.xml", "read, write, delete")); 89 } 90 } |