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