1 /*
   2  * Copyright (c) 2016, 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  * @library /test/lib
  27  * @modules java.base/jdk.internal.misc
  28  *          jdk.compiler
  29  *          jdk.jartool
  30  * @build jdk.test.lib.JDKToolFinder jdk.test.lib.Utils
  31  * @build MRTestBase
  32  * @run testng Basic1
  33  */
  34 
  35 import org.testng.annotations.*;
  36 
  37 import java.nio.file.*;
  38 import java.util.*;
  39 
  40 public class Basic1 extends MRTestBase {
  41 
  42     @BeforeTest
  43     public void setup() throws Throwable {
  44         String test = "test01";
  45         Path classes = Paths.get("classes");
  46 
  47         Path base = classes.resolve("base");
  48         Files.createDirectories(base);
  49         Path source = Paths.get(src, "data", test, "base", "version");
  50         javac(base, source.resolve("Main.java"), source.resolve("Version.java"));
  51 
  52         Path v9 = classes.resolve("v9");
  53         Files.createDirectories(v9);
  54         source = Paths.get(src, "data", test, "v9", "version");
  55         javac(v9, source.resolve("Version.java"));
  56 
  57         Path v10 = classes.resolve("v10");
  58         Files.createDirectories(v10);
  59         source = Paths.get(src, "data", test, "v10", "version");
  60         javac(v10, source.resolve("Version.java"));
  61 
  62         Path v10_1 = classes.resolve("v10_1").resolve("META-INF").resolve("versions").resolve("v10");
  63         Files.createDirectories(v10_1);
  64         source = Paths.get(src, "data", test, "v10", "version");
  65         javac(v10_1, source.resolve("Version.java"));
  66     }
  67 
  68     @Test
  69     public void test() throws Throwable {
  70         String jarfile = "test.jar";
  71         Path classes = Paths.get("classes");
  72 
  73         Path base = classes.resolve("base");
  74         Path v9 = classes.resolve("v9");
  75         Path v10 = classes.resolve("v10");
  76 
  77         jar("cf", jarfile, "-C", base.toString(), ".",
  78                 "--release", "9", "-C", v9.toString(), ".",
  79                 "--release", "10", "-C", v10.toString(), ".")
  80                 .shouldHaveExitValue(SUCCESS);
  81 
  82         checkMultiRelease(jarfile, true);
  83 
  84         Map<String, String[]> names = Map.of(
  85                 "version/Main.class",
  86                 new String[]{"base", "version", "Main.class"},
  87 
  88                 "version/Version.class",
  89                 new String[]{"base", "version", "Version.class"},
  90 
  91             "META-INF/versions/9/version/Version.class",
  92             new String[] {"v9", "version", "Version.class"},
  93 
  94             "META-INF/versions/10/version/Version.class",
  95             new String[] {"v10", "version", "Version.class"}
  96         );
  97 
  98         compare(jarfile, names);
  99     }
 100 
 101     @Test
 102     public void testFail() throws Throwable {
 103         String jarfile = "test.jar";
 104         Path classes = Paths.get("classes");
 105         Path base = classes.resolve("base");
 106         Path v10 = classes.resolve("v10_1");
 107 
 108         jar("cf", jarfile, "-C", base.toString(), ".",
 109                 "--release", "10", "-C", v10.toString(), ".")
 110                 .shouldNotHaveExitValue(SUCCESS)
 111                 .shouldContain("unexpected versioned entry META-INF/versions/");
 112     }
 113 }