1 /*
 2  * Copyright (c) 2017, 2024, 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  * @build jdk.test.lib.Utils
30  *        jdk.test.lib.Asserts
31  *        jdk.test.lib.JDKToolFinder
32  *        jdk.test.lib.JDKToolLauncher
33  *        jdk.test.lib.Platform
34  *        jdk.test.lib.process.*
35  * @run main CheckSealedTest
36  */
37 
38 import jdk.test.lib.JDKToolFinder;
39 import jdk.test.lib.process.ProcessTools;
40 
41 import java.io.File;
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.List;
47 
48 import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
49 import static jdk.test.lib.process.ProcessTools.createTestJavaProcessBuilder;
50 import static jdk.test.lib.process.ProcessTools.executeCommand;
51 
52 public class CheckSealedTest {
53     private static final String ARCHIVE_NAME = "b.jar";
54     private static final String TEST_NAME = "CheckSealed";
55     public static void main(String[] args)
56             throws Throwable {
57 
58         String baseDir = System.getProperty("user.dir") + File.separator;
59         String javac = JDKToolFinder.getTestJDKTool("javac");
60 
61         setup(baseDir);
62         String srcDir = System.getProperty("test.src");
63         String cp = srcDir + File.separator + "a" + File.pathSeparator
64                 + srcDir + File.separator + "b.jar" + File.pathSeparator
65                 + ".";
66 
67         // Compile
68         ProcessTools.executeCommand(javac, "-cp", cp, "-d", ".",
69                 srcDir + File.separator + TEST_NAME + ".java");
70 
71         List<String[]> allCMDs = List.of(
72                 // Run test the first time
73                 new String[]{
74                         "-cp", cp, TEST_NAME, "1"
75                 },
76                 // Run test the second time
77                 new String[]{
78                         "-cp", cp, TEST_NAME, "2"
79                 }
80         );
81 
82         for (String[] cmd : allCMDs) {
83             executeCommand(createTestJavaProcessBuilder(cmd))
84                         .outputTo(System.out)
85                         .errorTo(System.out)
86                         .shouldHaveExitValue(0);
87         }
88     }
89 
90     private static void setup(String baseDir) throws IOException {
91         Path testJar = Paths.get(System.getProperty("test.src"), ARCHIVE_NAME);
92         Files.copy(testJar, Paths.get(baseDir, ARCHIVE_NAME), REPLACE_EXISTING);
93     }
94 }