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