1 /*
  2  * Copyright (c) 2023, 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 import java.util.List;
 25 import java.util.ArrayList;
 26 import java.util.stream.Stream;
 27 
 28 import jdk.test.lib.process.OutputAnalyzer;
 29 
 30 import static org.testng.Assert.*;
 31 
 32 public class TestEnableNativeAccessBase {
 33     static final String MODULE_PATH = System.getProperty("jdk.module.path");
 34 
 35     static final String PANAMA_MAIN_CLS = "org.openjdk.foreigntest.PanamaMainDirect";
 36     static final String PANAMA_MAIN = "panama_module/" + PANAMA_MAIN_CLS;
 37     static final String PANAMA_REFLECTION_CLS = "org.openjdk.foreigntest.PanamaMainReflection";
 38     static final String PANAMA_REFLECTION = "panama_module/" + PANAMA_REFLECTION_CLS;
 39     static final String PANAMA_INVOKE_CLS = "org.openjdk.foreigntest.PanamaMainInvoke";
 40     static final String PANAMA_INVOKE = "panama_module/" + PANAMA_INVOKE_CLS;
 41     static final String PANAMA_JNI_CLS = "org.openjdk.foreigntest.PanamaMainJNI";
 42     static final String PANAMA_JNI = "panama_module/" + PANAMA_JNI_CLS;
 43     static final String UNNAMED = "org.openjdk.foreigntest.unnamed.PanamaMainUnnamedModule";
 44 
 45     /**
 46      * Represents the expected result of a test.
 47      */
 48     static final class Result {
 49         private final boolean success;
 50         private final List<String> expectedOutput = new ArrayList<>();
 51         private final List<String> notExpectedOutput = new ArrayList<>();
 52 
 53         Result(boolean success) {
 54             this.success = success;
 55         }
 56 
 57         Result expect(String msg) {
 58             expectedOutput.add(msg);
 59             return this;
 60         }
 61 
 62         Result doNotExpect(String msg) {
 63             notExpectedOutput.add(msg);
 64             return this;
 65         }
 66 
 67         boolean shouldSucceed() {
 68             return success;
 69         }
 70 
 71         Stream<String> expectedOutput() {
 72             return expectedOutput.stream();
 73         }
 74 
 75         Stream<String> notExpectedOutput() {
 76             return notExpectedOutput.stream();
 77         }
 78 
 79         @Override
 80         public String toString() {
 81             String s = (success) ? "success" : "failure";
 82             for (String msg : expectedOutput) {
 83                 s += "/" + msg;
 84             }
 85             return s;
 86         }
 87 
 88     }
 89 
 90     static Result success() {
 91         return new Result(true);
 92     }
 93 
 94     static Result successNoWarning() {
 95         return success().doNotExpect("WARNING");
 96     }
 97 
 98     static Result successWithWarning(String moduleName) {
 99         return success().expect("WARNING").expect("--enable-native-access=" + moduleName);
100     }
101 
102     static Result failWithWarning(String expectedOutput) {
103         return new Result(false).expect(expectedOutput).expect("WARNING");
104     }
105 
106     static Result failWithError(String expectedOutput) {
107         return new Result(false).expect(expectedOutput);
108     }
109 
110     /**
111      * Checks an expected result with the output captured by the given
112      * OutputAnalyzer.
113      */
114     void checkResult(Result expectedResult, OutputAnalyzer outputAnalyzer) {
115         expectedResult.expectedOutput().forEach(outputAnalyzer::shouldContain);
116         expectedResult.notExpectedOutput().forEach(outputAnalyzer::shouldNotContain);
117         int exitValue = outputAnalyzer.getExitValue();
118         if (expectedResult.shouldSucceed()) {
119             assertTrue(exitValue == 0);
120         } else {
121             assertTrue(exitValue != 0);
122         }
123     }
124 }