1 /*
2  * Copyright (c) 2014, 2017, 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 common;
25 
26 import static jaxp.library.JAXPTestUtilities.clearSystemProperty;
27 import static jaxp.library.JAXPTestUtilities.getSystemProperty;
28 import static jaxp.library.JAXPTestUtilities.setSystemProperty;
29 
30 import java.io.FilePermission;
31 import java.io.InputStream;
32 import java.io.StringWriter;
33 
34 import javax.xml.XMLConstants;
35 import javax.xml.parsers.DocumentBuilder;
36 import javax.xml.parsers.DocumentBuilderFactory;
37 import javax.xml.transform.Transformer;
38 import javax.xml.transform.TransformerFactory;
39 import javax.xml.transform.dom.DOMSource;
40 import javax.xml.transform.sax.SAXSource;
41 import javax.xml.transform.stream.StreamResult;
42 import javax.xml.transform.stream.StreamSource;
43 import javax.xml.validation.Schema;
44 import javax.xml.validation.SchemaFactory;
45 import javax.xml.validation.Validator;
46 import javax.xml.xpath.XPath;
47 import javax.xml.xpath.XPathFactory;
48 
49 import jaxp.library.JAXPTestUtilities;
50 
51 import org.testng.Assert;
52 import org.testng.annotations.Listeners;
53 import org.testng.annotations.Test;
54 import org.w3c.dom.Document;
55 import org.xml.sax.InputSource;
56 
57 /*
58  * @test
59  * @bug 6941169
60  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
61  * @run testng/othervm -DrunSecMngr=true common.Bug6941169Test
62  * @run testng/othervm common.Bug6941169Test
63  * @summary Test use-service-mechanism feature.
64  */
65 @Test(singleThreaded = true)
66 @Listeners({ jaxp.library.FilePolicy.class })
67 public class Bug6941169Test {
68     static final String SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
69     static final String SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
70 
71     private static final String DOM_FACTORY_ID = "javax.xml.parsers.DocumentBuilderFactory";
72     private static final String SAX_FACTORY_ID = "javax.xml.parsers.SAXParserFactory";
73 
74     // impl specific feature
75     final String ORACLE_FEATURE_SERVICE_MECHANISM = "http://www.oracle.com/feature/use-service-mechanism";
76 
77     static String _xml = Bug6941169Test.class.getResource("Bug6941169.xml").getPath();
78     static String _xsd = Bug6941169Test.class.getResource("Bug6941169.xsd").getPath();
79 
80     @Test
testValidation_SAX_withoutServiceMech()81     public void testValidation_SAX_withoutServiceMech() {
82         System.out.println("Validation using SAX Source;  Service mechnism is turned off;  SAX Impl should be the default:");
83         InputSource is = new InputSource(Bug6941169Test.class.getResourceAsStream("Bug6941169.xml"));
84         SAXSource ss = new SAXSource(is);
85         setSystemProperty(SAX_FACTORY_ID, "MySAXFactoryImpl");
86         long start = System.currentTimeMillis();
87         try {
88             SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
89             factory.setFeature(ORACLE_FEATURE_SERVICE_MECHANISM, false);
90             Schema schema = factory.newSchema(new StreamSource(_xsd));
91             Validator validator = schema.newValidator();
92             validator.validate(ss, null);
93         } catch (Exception e) {
94             // e.printStackTrace();
95             String error = e.getMessage();
96             if (error.indexOf("javax.xml.parsers.FactoryConfigurationError: Provider MySAXFactoryImpl not found") > 0) {
97                 Assert.fail(e.getMessage());
98             } else {
99                 System.out.println("Default impl is used");
100             }
101 
102             // System.out.println(e.getMessage());
103 
104         }
105         long end = System.currentTimeMillis();
106         double elapsedTime = ((end - start));
107         System.out.println("Time elapsed: " + elapsedTime);
108         clearSystemProperty(SAX_FACTORY_ID);
109     }
110 
111     @Test
testValidation_SAX_withServiceMech()112     public void testValidation_SAX_withServiceMech() {
113         System.out.println("Validation using SAX Source. Using service mechnism (by default) to find SAX Impl:");
114         InputSource is = new InputSource(Bug6941169Test.class.getResourceAsStream("Bug6941169.xml"));
115         SAXSource ss = new SAXSource(is);
116         setSystemProperty(SAX_FACTORY_ID, "MySAXFactoryImpl");
117         long start = System.currentTimeMillis();
118         try {
119             SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
120             Schema schema = factory.newSchema(new StreamSource(_xsd));
121             Validator validator = schema.newValidator();
122             validator.validate(ss, null);
123             Assert.fail("User impl MySAXFactoryImpl should be used.");
124         } catch (Exception e) {
125             String error = e.getMessage();
126             if (error.indexOf("javax.xml.parsers.FactoryConfigurationError: Provider MySAXFactoryImpl not found") > 0) {
127                 // expected
128             }
129             // System.out.println(e.getMessage());
130 
131         }
132         long end = System.currentTimeMillis();
133         double elapsedTime = ((end - start));
134         System.out.println("Time elapsed: " + elapsedTime);
135         clearSystemProperty(SAX_FACTORY_ID);
136     }
137 
138     @Test
testValidation_SAX_withSM()139     public void testValidation_SAX_withSM() throws Exception {
140         if(System.getSecurityManager() == null)
141             return;
142 
143         System.out.println("Validation using SAX Source with security manager:");
144         InputSource is = new InputSource(Bug6941169Test.class.getResourceAsStream("Bug6941169.xml"));
145         SAXSource ss = new SAXSource(is);
146         setSystemProperty(SAX_FACTORY_ID, "MySAXFactoryImpl");
147 
148         long start = System.currentTimeMillis();
149         try {
150             SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
151             factory.setFeature(ORACLE_FEATURE_SERVICE_MECHANISM, false);
152             Schema schema = factory.newSchema(new StreamSource(_xsd));
153             Validator validator = schema.newValidator();
154             validator.validate(ss, null);
155         } catch (Exception e) {
156             String error = e.getMessage();
157             if (error.indexOf("javax.xml.parsers.FactoryConfigurationError: Provider MySAXFactoryImpl not found") > 0) {
158                 Assert.fail(e.getMessage());
159             } else {
160                 System.out.println("Default impl is used");
161             }
162 
163             // System.out.println(e.getMessage());
164 
165         } finally {
166             clearSystemProperty(SAX_FACTORY_ID);
167         }
168         long end = System.currentTimeMillis();
169         double elapsedTime = ((end - start));
170         System.out.println("Time elapsed: " + elapsedTime);
171 
172     }
173 
174     @Test
testTransform_DOM_withoutServiceMech()175     public void testTransform_DOM_withoutServiceMech() {
176         System.out.println("Transform using DOM Source;  Service mechnism is turned off;  Default DOM Impl should be the default:");
177         DOMSource domSource = new DOMSource();
178         domSource.setSystemId(_xml);
179 
180         // DOMSource domSource = new
181         // DOMSource(getDocument(Bug6941169Test.class.getResourceAsStream("Bug6941169.xml")));
182         setSystemProperty(DOM_FACTORY_ID, "MyDOMFactoryImpl");
183         long start = System.currentTimeMillis();
184         try {
185             TransformerFactory factory = TransformerFactory.newInstance();
186             factory.setFeature(ORACLE_FEATURE_SERVICE_MECHANISM, false);
187 
188             Transformer t = factory.newTransformer();
189 
190             StringWriter result = new StringWriter();
191             StreamResult streamResult = new StreamResult(result);
192             t.transform(domSource, streamResult);
193             System.out.println("Writing to " + result.toString());
194 
195         } catch (Exception e) {
196             // e.printStackTrace();
197             String error = e.getMessage();
198             if (error.indexOf("Provider MyDOMFactoryImpl not found") > 0) {
199                 Assert.fail(e.getMessage());
200             } else {
201                 System.out.println("Default impl is used");
202             }
203 
204             // System.out.println(e.getMessage());
205 
206         } catch (Error e) {
207             // e.printStackTrace();
208             String error = e.getMessage();
209             if (error.indexOf("Provider MyDOMFactoryImpl not found") > 0) {
210                 Assert.fail(e.getMessage());
211             } else {
212                 System.out.println("Default impl is used");
213             }
214 
215             // System.out.println(e.getMessage());
216 
217         }
218 
219         long end = System.currentTimeMillis();
220         double elapsedTime = ((end - start));
221         System.out.println("Time elapsed: " + elapsedTime);
222         clearSystemProperty(DOM_FACTORY_ID);
223     }
224 
225     /** this is by default */
226     @Test
testTransform_DOM_withServiceMech()227     public void testTransform_DOM_withServiceMech() {
228         System.out.println("Transform using DOM Source;  By default, the factory uses services mechanism to look up impl:");
229         DOMSource domSource = new DOMSource();
230         domSource.setSystemId(_xml);
231 
232         // DOMSource domSource = new
233         // DOMSource(getDocument(Bug6941169Test.class.getResourceAsStream("Bug6941169.xml")));
234         setSystemProperty(DOM_FACTORY_ID, "MyDOMFactoryImpl");
235         long start = System.currentTimeMillis();
236         try {
237             TransformerFactory factory = TransformerFactory.newInstance();
238             Transformer t = factory.newTransformer();
239 
240             StringWriter result = new StringWriter();
241             StreamResult streamResult = new StreamResult(result);
242             t.transform(domSource, streamResult);
243             System.out.println("Writing to " + result.toString());
244 
245             Assert.fail("User impl MyDOMFactoryImpl should be used.");
246 
247         } catch (Exception e) {
248             String error = e.getMessage();
249             if (error.indexOf("Provider MyDOMFactoryImpl not found") > 0) {
250                 // expected
251             }
252             System.out.println(error);
253 
254         } catch (Error e) {
255             String error = e.getMessage();
256             if (error.indexOf("Provider MyDOMFactoryImpl not found") > 0) {
257                 // expected
258             }
259             System.out.println(error);
260 
261         }
262 
263         long end = System.currentTimeMillis();
264         double elapsedTime = ((end - start));
265         System.out.println("Time elapsed: " + elapsedTime);
266         clearSystemProperty(DOM_FACTORY_ID);
267     }
268 
269     @Test
testTransform_DOM_withSM()270     public void testTransform_DOM_withSM() throws Exception {
271         if(System.getSecurityManager() == null)
272             return;
273         System.out.println("Transform using DOM Source;  Security Manager is set:");
274         DOMSource domSource = new DOMSource();
275         domSource.setSystemId(_xml);
276 
277         // DOMSource domSource = new
278         // DOMSource(getDocument(Bug6941169Test.class.getResourceAsStream("Bug6941169.xml")));
279         setSystemProperty(DOM_FACTORY_ID, "MyDOMFactoryImpl");
280         long start = System.currentTimeMillis();
281         try {
282             TransformerFactory factory = TransformerFactory.newInstance("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl",
283                     TransformerFactory.class.getClassLoader());
284             Transformer t = factory.newTransformer();
285 
286             StringWriter result = new StringWriter();
287             StreamResult streamResult = new StreamResult(result);
288             t.transform(domSource, streamResult);
289             System.out.println("Writing to " + result.toString());
290 
291         } catch (Exception e) {
292             String error = e.getMessage();
293             if (error.indexOf("Provider MyDOMFactoryImpl not found") > 0) {
294                 Assert.fail(e.getMessage());
295             } else {
296                 System.out.println("Default impl is used");
297             }
298 
299             // System.out.println(e.getMessage());
300 
301         } catch (Error e) {
302             String error = e.getMessage();
303             if (error.indexOf("Provider MyDOMFactoryImpl not found") > 0) {
304                 Assert.fail(e.getMessage());
305             } else {
306                 System.out.println("Default impl is used");
307             }
308 
309             // System.out.println(e.getMessage());
310 
311         } finally {
312             clearSystemProperty(DOM_FACTORY_ID);
313         }
314         long end = System.currentTimeMillis();
315         double elapsedTime = ((end - start));
316         System.out.println("Time elapsed: " + elapsedTime);
317 
318     }
319 
320     @Test
testXPath_DOM_withoutServiceMech()321     public void testXPath_DOM_withoutServiceMech() {
322         final String XPATH_EXPRESSION = "/fooTest";
323         System.out.println("Evaluate DOM Source;  Service mechnism is turned off;  Default DOM Impl should be used:");
324         Document doc = getDocument(Bug6941169Test.class.getResourceAsStream("Bug6941169.xml"));
325         setSystemProperty(DOM_FACTORY_ID, "MyDOMFactoryImpl");
326         long start = System.currentTimeMillis();
327         try {
328             XPathFactory xPathFactory = XPathFactory.newInstance();
329             xPathFactory.setFeature(ORACLE_FEATURE_SERVICE_MECHANISM, false);
330 
331             XPath xPath = xPathFactory.newXPath();
332 
333             String xPathResult = xPath.evaluate(XPATH_EXPRESSION, doc);
334 
335         } catch (Exception e) {
336             // e.printStackTrace();
337             String error = e.getMessage();
338             if (error.indexOf("MyDOMFactoryImpl not found") > 0) {
339                 Assert.fail(e.getMessage());
340             } else {
341                 System.out.println("Default impl is used");
342             }
343 
344             // System.out.println(e.getMessage());
345 
346         } catch (Error e) {
347             // e.printStackTrace();
348             String error = e.getMessage();
349             if (error.indexOf("MyDOMFactoryImpl not found") > 0) {
350                 Assert.fail(e.getMessage());
351             } else {
352                 System.out.println("Default impl is used");
353             }
354 
355             // System.out.println(e.getMessage());
356 
357         }
358 
359         long end = System.currentTimeMillis();
360         double elapsedTime = ((end - start));
361         System.out.println("Time elapsed: " + elapsedTime);
362         clearSystemProperty(DOM_FACTORY_ID);
363     }
364 
365     @Test
testXPath_DOM_withServiceMech()366     public void testXPath_DOM_withServiceMech() {
367         /**
368          * This is in conflict with the test testXPath_DOM_withSM where the system
369          * default parser is used when the security manager is present. The test
370          * is therefore skipped when the security manager is present.
371          */
372         if (System.getSecurityManager() != null) {
373             return;
374         }
375         final String XPATH_EXPRESSION = "/fooTest";
376         System.out.println("Evaluate DOM Source;  Service mechnism is on by default;  It would try to use MyDOMFactoryImpl:");
377         InputStream input = getClass().getResourceAsStream("Bug6941169.xml");
378         InputSource source = new InputSource(input);
379         setSystemProperty(DOM_FACTORY_ID, "MyDOMFactoryImpl");
380         long start = System.currentTimeMillis();
381         try {
382             XPathFactory xPathFactory = XPathFactory.newInstance();
383 
384             XPath xPath = xPathFactory.newXPath();
385 
386             String xPathResult = xPath.evaluate(XPATH_EXPRESSION, source);
387             Assert.fail("User impl MyDOMFactoryImpl should be used.");
388 
389         } catch (Exception e) {
390             // e.printStackTrace();
391             String error = e.getMessage();
392             if (error.indexOf("MyDOMFactoryImpl not found") > 0) {
393                 System.out.println("Tried to locate MyDOMFactoryImpl");
394             } else {
395                 Assert.fail(e.getMessage());
396 
397             }
398 
399             // System.out.println(e.getMessage());
400 
401         } catch (Error e) {
402             // e.printStackTrace();
403             String error = e.getMessage();
404             if (error.indexOf("MyDOMFactoryImpl not found") > 0) {
405                 System.out.println("Tried to locate MyDOMFactoryImpl");
406             } else {
407                 Assert.fail(e.getMessage());
408 
409             }
410 
411             // System.out.println(e.getMessage());
412 
413         }
414 
415         long end = System.currentTimeMillis();
416         double elapsedTime = ((end - start));
417         System.out.println("Time elapsed: " + elapsedTime);
418         clearSystemProperty(DOM_FACTORY_ID);
419     }
420 
421     @Test
testXPath_DOM_withSM()422     public void testXPath_DOM_withSM() throws Exception {
423         if(System.getSecurityManager() == null)
424             return;
425         final String XPATH_EXPRESSION = "/fooTest";
426         System.out.println("Evaluate DOM Source;  Security Manager is set:");
427         InputStream input = getClass().getResourceAsStream("Bug6941169.xml");
428         InputSource source = new InputSource(input);
429         setSystemProperty(DOM_FACTORY_ID, "MyDOMFactoryImpl");
430         long start = System.currentTimeMillis();
431         try {
432             XPathFactory xPathFactory = XPathFactory.newInstance("http://java.sun.com/jaxp/xpath/dom",
433                     "com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl", null);
434 
435             XPath xPath = xPathFactory.newXPath();
436 
437             String xPathResult = xPath.evaluate(XPATH_EXPRESSION, source);
438             System.out.println("Use default impl");
439         } catch (Exception e) {
440             // e.printStackTrace();
441             String error = e.getMessage();
442             if (error.indexOf("MyDOMFactoryImpl not found") > 0) {
443                 Assert.fail(e.getMessage());
444             } else {
445                 System.out.println("Default impl should be used");
446             }
447 
448             // System.out.println(e.getMessage());
449 
450         } catch (Error e) {
451             // e.printStackTrace();
452             String error = e.getMessage();
453             if (error.indexOf("MyDOMFactoryImpl not found") > 0) {
454                 Assert.fail(e.getMessage());
455             } else {
456                 System.out.println("Default impl should be used");
457             }
458 
459             // System.out.println(e.getMessage());
460 
461         } finally {
462             clearSystemProperty(DOM_FACTORY_ID);
463         }
464         long end = System.currentTimeMillis();
465         double elapsedTime = ((end - start));
466         System.out.println("Time elapsed: " + elapsedTime);
467 
468     }
469 
470     @Test
testSM()471     public void testSM() {
472         SecurityManager sm = System.getSecurityManager();
473         if (System.getSecurityManager() != null) {
474             System.out.println("Security manager not cleared: " + sm.toString());
475         } else {
476             System.out.println("Security manager cleared: ");
477         }
478     }
479 
getDocument(InputStream in)480     private static Document getDocument(InputStream in) {
481 
482         Document document = null;
483 
484         try {
485             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
486             dbf.setNamespaceAware(true);
487             DocumentBuilder db = dbf.newDocumentBuilder();
488             document = db.parse(in);
489         } catch (Exception e) {
490             e.printStackTrace();
491             Assert.fail(e.toString());
492         }
493 
494         return document;
495     }
496 }
497