|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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 javax.xml.transform.TransformerFactory; |
| 27 | +import javax.xml.transform.stream.StreamSource; |
| 28 | +import java.io.*; |
| 29 | +import java.nio.file.Files; |
| 30 | +import java.nio.file.Path; |
| 31 | +import java.util.stream.Stream; |
| 32 | +import jaxp.library.JUnitTestUtil; |
| 33 | +import org.junit.jupiter.api.BeforeAll; |
| 34 | +import org.junit.jupiter.params.ParameterizedTest; |
| 35 | +import org.junit.jupiter.params.provider.Arguments; |
| 36 | +import org.junit.jupiter.params.provider.MethodSource; |
| 37 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 38 | + |
| 39 | +/* |
| 40 | + * @test |
| 41 | + * @bug 8344925 |
| 42 | + * @summary Transformer properties tests |
| 43 | + * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest |
| 44 | + * @run junit/othervm transform.PropertiesTest |
| 45 | + */ |
| 46 | +public class PropertiesTest { |
| 47 | + private static final String USER_DIR = System.getProperty("user.dir"); |
| 48 | + private static final String TEST_DIR = System.getProperty("test.src"); |
| 49 | + // Test parameters: |
| 50 | + // generate-translet: indicates whether to generate translet |
| 51 | + // translet-name: the name of the translet |
| 52 | + // package-name: the package name |
| 53 | + // destination-directory: the destination |
| 54 | + // expected: the class path |
| 55 | + private static Stream<Arguments> testData() { |
| 56 | + String destination = JUnitTestUtil.CLS_DIR + "/testdir"; |
| 57 | + return Stream.of( |
| 58 | + Arguments.of(true, "MyTranslet", "org.myorg", destination, "/org/myorg/MyTranslet.class"), |
| 59 | + Arguments.of(false, "Translet", "not.generate", destination, "/not/generate/Translet.class"), |
| 60 | + // translet named after the stylesheet |
| 61 | + Arguments.of(true, null, "org.myorg", destination, "/org/myorg/transform.class"), |
| 62 | + // default package name die.verwandlung since JDK 9 |
| 63 | + Arguments.of(true, "MyTranslet", null, destination, "/die/verwandlung/MyTranslet.class"), |
| 64 | + Arguments.of(true, "MyTranslet", "org.myorg", null, "/org/myorg/MyTranslet.class") |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + @BeforeAll |
| 69 | + public static void setup() throws Exception { |
| 70 | + // so that the translet is generated under test.classes |
| 71 | + JUnitTestUtil.copyFile(JUnitTestUtil.SRC_DIR + "/transform.xsl", JUnitTestUtil.CLS_DIR + "/transform.xsl"); |
| 72 | + } |
| 73 | + |
| 74 | + @ParameterizedTest |
| 75 | + @MethodSource("testData") |
| 76 | + public void test(boolean generateTranslet, String name, String packageName, |
| 77 | + String destination, String expected) |
| 78 | + throws Exception { |
| 79 | + TransformerFactory tf = TransformerFactory.newInstance(); |
| 80 | + |
| 81 | + tf.setAttribute("generate-translet", generateTranslet); |
| 82 | + if (name != null) tf.setAttribute("translet-name", name); |
| 83 | + if (packageName != null) tf.setAttribute("package-name", packageName); |
| 84 | + if (destination != null) tf.setAttribute("destination-directory", destination); |
| 85 | + |
| 86 | + String xslFile = JUnitTestUtil.CLS_DIR + "/transform.xsl"; |
| 87 | + String xslSysId = JUnitTestUtil.getSystemId(xslFile); |
| 88 | + StreamSource xsl = new StreamSource(xslSysId); |
| 89 | + tf.newTemplates(xsl); |
| 90 | + |
| 91 | + String path = (destination != null) ? destination + expected : new File(xslFile).getParent() + expected; |
| 92 | + |
| 93 | + if (generateTranslet) { |
| 94 | + //Files.list(Path.of(path)).forEach(System.out::println); |
| 95 | + assertTrue(Files.exists(Path.of(path)), "Translet is expected at " + expected); |
| 96 | + } else { |
| 97 | + assertTrue(Files.notExists(Path.of(path)), "Translet is not to be generated."); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments