1 /* 2 * Copyright (c) 2014, 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 jaxp.library; 24 25 import java.io.FilePermission; 26 import java.security.Permission; 27 import java.security.Permissions; 28 import java.security.Policy; 29 import static jaxp.library.JAXPBaseTest.setPolicy; 30 import org.testng.annotations.BeforeClass; 31 32 /** 33 * This is a base class that every test class that need to access local XML 34 * files must extend if it needs to be run with security mode. 35 */ 36 public class JAXPFileBaseTest extends JAXPBaseTest { 37 /* 38 * Install a SecurityManager along with a base Policy to allow testNG to 39 * run when there is a security manager. 40 */ 41 @BeforeClass 42 public static void setUpClass() throws Exception { 43 setPolicy(new FileTestPolicy()); 44 System.setSecurityManager(new SecurityManager()); 45 } 46 47 /* 48 * Utility Method used to set minimal permission in addition to ps. 49 */ 50 protected static void setPermissions(Permission... ps) { 51 Policy.setPolicy(new FileTestPolicy(ps)); 52 } 53 54 /* 55 * Utility Method used to set minimal permission in addition to ps. 56 */ 57 protected static void setPermissions(Permissions ps) { 58 Policy.setPolicy(new FileTestPolicy(ps)); 59 } 60 } 61 62 /** 63 * This policy is only given to tests that need access local files. Additional 64 * permissions for accessing local files have been granted by default. 65 * @author HaiboYan 66 */ 67 class FileTestPolicy extends TestPolicy { 68 /** 69 * Constructor which sets the minimum permissions by default allowing testNG 70 * to work with a SecurityManager. 71 * @param ps permissions to be added. 72 */ 73 public FileTestPolicy(Permissions ps) { 74 super(ps); 75 } 76 77 /** 78 * Constructor which sets the minimum permissions by default allowing testNG 79 * to work with a SecurityManager. 80 * @param ps permission array to be added. 81 */ 82 public FileTestPolicy(Permission... ps) { 83 super(ps); 84 } 85 86 /** 87 * Defines the minimal permissions required by testNG when running these 88 * tests 89 */ 90 @Override 91 protected void setMinimalPermissions() { 92 super.setMinimalPermissions(); 93 permissions.add(new FilePermission(System.getProperty("test.classes") + "/-", 94 "read, write")); 95 permissions.add(new FilePermission(System.getProperty("test.src") + "/-", 96 "read")); 97 } 98 }