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  * @bug 4244970
  27  * @summary Test to see if sealing violation is detected correctly
  28  * @library /test/lib
  29  * @run main CheckSealedTest
  30  */
  31 
  32 import jdk.test.lib.JDKToolFinder;
  33 import jdk.test.lib.process.ProcessTools;
  34 
  35 import java.io.File;
  36 import java.io.IOException;
  37 import java.nio.file.Files;
  38 import java.nio.file.Path;
  39 import java.nio.file.Paths;
  40 import java.util.List;
  41 
  42 import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
  43 
  44 public class CheckSealedTest {
  45     private static final String ARCHIVE_NAME = "b.jar";
  46     private static final String TEST_NAME = "CheckSealed";
  47     public static void main(String[] args)
  48             throws Throwable {
  49 
  50         String baseDir = System.getProperty("user.dir") + File.separator;
  51         String javac = JDKToolFinder.getTestJDKTool("javac");
  52         String java = JDKToolFinder.getTestJDKTool("java");
  53 
  54         setup(baseDir);
  55         String srcDir = System.getProperty("test.src");
  56         String cp = srcDir + File.separator + "a" + File.pathSeparator
  57                 + srcDir + File.separator + "b.jar" + File.pathSeparator
  58                 + ".";
  59         List<String[]> allCMDs = List.of(
  60                 // Compile command
  61                 new String[]{
  62                         javac, "-cp", cp, "-d", ".",
  63                         srcDir + File.separator + TEST_NAME + ".java"
  64                 },
  65                 // Run test the first time
  66                 new String[]{
  67                         java, "-cp", cp, TEST_NAME, "1"
  68                 },
  69                 // Run test the second time
  70                 new String[]{
  71                         java, "-cp", cp, TEST_NAME, "2"
  72                 }
  73         );
  74 
  75         for (String[] cmd : allCMDs) {
  76             ProcessTools.executeCommand(cmd)
  77                         .outputTo(System.out)
  78                         .errorTo(System.out)
  79                         .shouldHaveExitValue(0);
  80         }
  81     }
  82 
  83     private static void setup(String baseDir) throws IOException {
  84         Path testJar = Paths.get(System.getProperty("test.src"), ARCHIVE_NAME);
  85         Files.copy(testJar, Paths.get(baseDir, ARCHIVE_NAME), REPLACE_EXISTING);
  86     }
  87 }