1 /*
   2  * Copyright (c) 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 /*
  25  * @test
  26  * @summary Test Multi-Release jar support in schemagen tool
  27  * @library /test/lib
  28  * @library /lib/testlibrary
  29  * @modules jdk.compiler java.xml.ws
  30  * @build CompilerUtils
  31  * @run testng MultiReleaseJarTest
  32  */
  33 
  34 import jdk.test.lib.JDKToolLauncher;
  35 import jdk.test.lib.Utils;
  36 import jdk.test.lib.process.OutputAnalyzer;
  37 import jdk.test.lib.process.ProcessTools;
  38 import org.testng.annotations.BeforeClass;
  39 import org.testng.annotations.DataProvider;
  40 import org.testng.annotations.Test;
  41 
  42 import java.io.IOException;
  43 import java.nio.file.Files;
  44 import java.nio.file.Path;
  45 import java.nio.file.Paths;
  46 import java.util.stream.Stream;
  47 
  48 import static org.testng.Assert.assertTrue;
  49 
  50 public class MultiReleaseJarTest {
  51     private static final int SUCCESS = 0;
  52 
  53     @DataProvider(name = "jarFiles")
  54     public Object[][] jarFiles() {
  55         return new Object[][]{{"MV_BOTH.jar", 9},
  56                 {"MV_ONLY_9.jar", 9},
  57                 {"NON_MV.jar", 8}};
  58     }
  59 
  60     @BeforeClass
  61     public void setUpTest() throws Throwable {
  62         compile();
  63         Path classes = Paths.get("classes");
  64         jar("cf", "MV_BOTH.jar",
  65                 "-C", classes.resolve("base").toString(), ".",
  66                 "--release", "9", "-C", classes.resolve("v9").toString(), ".",
  67                 "--release", "10", "-C", classes.resolve("v10").toString(), ".")
  68                 .shouldHaveExitValue(SUCCESS);
  69 
  70         jar("cf", "MV_ONLY_9.jar",
  71                 "-C", classes.resolve("base").toString(), ".",
  72                 "--release", "9", "-C", classes.resolve("v9").toString(), ".")
  73                 .shouldHaveExitValue(SUCCESS);
  74         jar("cf", "NON_MV.jar",
  75                 "-C", classes.resolve("base").toString(), ".")
  76                 .shouldHaveExitValue(SUCCESS);
  77     }
  78 
  79     @Test(dataProvider = "jarFiles")
  80     public void testSchemagen(String jar, int mainVer) throws Throwable {
  81         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("schemagen");
  82         Utils.getForwardVmOptions().forEach(launcher::addToolArg);
  83         launcher.addToolArg("-cp").addToolArg(jar)
  84                 .addToolArg("schemagen.Person");
  85         ProcessTools.executeCommand(launcher.getCommand())
  86                 .shouldHaveExitValue(SUCCESS);
  87 
  88         String pattern = "version" + mainVer;
  89         assertTrue(grep(pattern, Paths.get("schema1.xsd")),
  90                 "Pattern " + pattern + " not found in the generated file.");
  91     }
  92 
  93     private boolean grep(String pattern, Path file) throws IOException {
  94         return new String(Files.readAllBytes(file)).contains(pattern);
  95     }
  96 
  97     private OutputAnalyzer jar(String... args) throws Throwable {
  98         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jar");
  99         Stream.of(args).forEach(launcher::addToolArg);
 100         return ProcessTools.executeCommand(launcher.getCommand());
 101     }
 102 
 103     private void compile() throws Throwable {
 104         String[] vers = {"base", "v9", "v10"};
 105         for (String ver : vers) {
 106             Path classes = Paths.get("classes", ver);
 107             Files.createDirectories(classes);
 108             Path source = Paths.get(Utils.TEST_SRC,"data", "mr", ver);
 109             assertTrue(CompilerUtils.compile(source, classes,
 110                     "--add-modules", "java.xml.ws"));
 111         }
 112     }
 113 }