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 stream;
25
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.FileNotFoundException;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.net.URL;
32 import java.net.URLClassLoader;
33 import java.util.Properties;
34
35 import javax.xml.stream.XMLInputFactory;
36 import javax.xml.stream.XMLOutputFactory;
37
38 import org.testng.Assert;
39 import org.testng.annotations.BeforeClass;
40 import org.testng.annotations.Test;
41
42 /*
43 * @summary Test SaTX factory using factory property and using ContextClassLoader.
44 */
45 public class FactoryFindTest {
46
47 boolean myClassLoaderUsed = false;
48
49 final static String FACTORY_KEY = "javax.xml.stream.XMLInputFactory";
50
51 @BeforeClass
52 public void setup(){
53 policy.PolicyUtil.changePolicy(getClass().getResource("FactoryFindTest.policy").getFile());
54 }
55
56 @Test
57 public void testFactoryFindUsingStaxProperties() {
58 // If property is defined, will take precendence so this test
59 // is ignored :(
60 if (System.getProperty(FACTORY_KEY) != null) {
61 return;
62 }
63
64 Properties props = new Properties();
65 String configFile = System.getProperty("java.home") + File.separator + "lib" + File.separator + "stax.properties";
66
67 File f = new File(configFile);
68 if (f.exists()) {
69 try {
70 FileInputStream fis = new FileInputStream(f);
71 props.load(fis);
72 fis.close();
73 } catch (FileNotFoundException e) {
74 return;
75 } catch (IOException e) {
76 return;
90 }
91
92 XMLInputFactory factory = XMLInputFactory.newInstance();
93 Assert.assertTrue(factory.getClass().getName().equals(props.getProperty(FACTORY_KEY)));
94 }
95
96 @Test
97 public void testFactoryFind() {
98 try {
99 // System.setProperty("jaxp.debug", "true");
100
101 XMLInputFactory factory = XMLInputFactory.newInstance();
102 Assert.assertTrue(factory.getClass().getClassLoader() == null);
103
104 Thread.currentThread().setContextClassLoader(null);
105 factory = XMLInputFactory.newInstance();
106 Assert.assertTrue(factory.getClass().getClassLoader() == null);
107
108 Thread.currentThread().setContextClassLoader(new MyClassLoader());
109 factory = XMLInputFactory.newInstance();
110 if (System.getSecurityManager() == null)
111 Assert.assertTrue(myClassLoaderUsed);
112 else
113 Assert.assertFalse(myClassLoaderUsed);
114
115 XMLOutputFactory ofactory = XMLOutputFactory.newInstance();
116 Assert.assertTrue(ofactory.getClass().getClassLoader() == null);
117
118 Thread.currentThread().setContextClassLoader(null);
119 ofactory = XMLOutputFactory.newInstance();
120 Assert.assertTrue(ofactory.getClass().getClassLoader() == null);
121
122 Thread.currentThread().setContextClassLoader(new MyClassLoader());
123 ofactory = XMLOutputFactory.newInstance();
124 if (System.getSecurityManager() == null)
125 Assert.assertTrue(myClassLoaderUsed);
126 else
127 Assert.assertFalse(myClassLoaderUsed);
128 } catch (Exception ex) {
129 throw new RuntimeException(ex);
|
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 stream;
25
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.FileNotFoundException;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.net.URL;
32 import java.net.URLClassLoader;
33 import java.util.Properties;
34
35 import javax.xml.stream.XMLInputFactory;
36 import javax.xml.stream.XMLOutputFactory;
37
38 import org.testng.Assert;
39 import org.testng.annotations.Listeners;
40 import org.testng.annotations.Test;
41
42 /*
43 * @summary Test SaTX factory using factory property and using ContextClassLoader.
44 */
45 @Listeners({jaxp.library.FilePolicy.class})
46 public class FactoryFindTest {
47
48 boolean myClassLoaderUsed = false;
49
50 final static String FACTORY_KEY = "javax.xml.stream.XMLInputFactory";
51
52 // @BeforeClass
53 // public void setup(){
54 // policy.PolicyUtil.changePolicy(getClass().getResource("FactoryFindTest.policy").getFile());
55 // }
56
57 @Test(enabled=false)
58 public void testFactoryFindUsingStaxProperties() {
59 // If property is defined, will take precendence so this test
60 // is ignored :(
61 if (System.getProperty(FACTORY_KEY) != null) {
62 return;
63 }
64
65 Properties props = new Properties();
66 String configFile = System.getProperty("java.home") + File.separator + "lib" + File.separator + "stax.properties";
67
68 File f = new File(configFile);
69 if (f.exists()) {
70 try {
71 FileInputStream fis = new FileInputStream(f);
72 props.load(fis);
73 fis.close();
74 } catch (FileNotFoundException e) {
75 return;
76 } catch (IOException e) {
77 return;
91 }
92
93 XMLInputFactory factory = XMLInputFactory.newInstance();
94 Assert.assertTrue(factory.getClass().getName().equals(props.getProperty(FACTORY_KEY)));
95 }
96
97 @Test
98 public void testFactoryFind() {
99 try {
100 // System.setProperty("jaxp.debug", "true");
101
102 XMLInputFactory factory = XMLInputFactory.newInstance();
103 Assert.assertTrue(factory.getClass().getClassLoader() == null);
104
105 Thread.currentThread().setContextClassLoader(null);
106 factory = XMLInputFactory.newInstance();
107 Assert.assertTrue(factory.getClass().getClassLoader() == null);
108
109 Thread.currentThread().setContextClassLoader(new MyClassLoader());
110 factory = XMLInputFactory.newInstance();
111 // because it's decided by having sm or not in FactoryFind code
112 if (System.getSecurityManager() == null)
113 Assert.assertTrue(myClassLoaderUsed);
114 else
115 Assert.assertFalse(myClassLoaderUsed);
116
117 XMLOutputFactory ofactory = XMLOutputFactory.newInstance();
118 Assert.assertTrue(ofactory.getClass().getClassLoader() == null);
119
120 Thread.currentThread().setContextClassLoader(null);
121 ofactory = XMLOutputFactory.newInstance();
122 Assert.assertTrue(ofactory.getClass().getClassLoader() == null);
123
124 Thread.currentThread().setContextClassLoader(new MyClassLoader());
125 ofactory = XMLOutputFactory.newInstance();
126 if (System.getSecurityManager() == null)
127 Assert.assertTrue(myClassLoaderUsed);
128 else
129 Assert.assertFalse(myClassLoaderUsed);
130 } catch (Exception ex) {
131 throw new RuntimeException(ex);
|