1 /* 2 * Copyright (c) 2019, 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 package jdk.jpackage.test; 24 25 import java.io.File; 26 import java.io.IOException; 27 import java.nio.file.Files; 28 import java.nio.file.Path; 29 import java.util.List; 30 import java.util.Set; 31 import java.util.function.Supplier; 32 import java.util.stream.Collectors; 33 import java.util.stream.Stream; 34 35 public class Test { 36 37 static final Path TEST_SRC_ROOT = new Supplier<Path>() { 38 @Override 39 public Path get() { 40 Path root = Path.of(System.getProperty("test.src")); 41 42 for (int i = 0; i != 10; ++i) { 43 if (root.resolve("apps").toFile().isDirectory()) { 44 return root.toAbsolutePath(); 45 } 46 root = root.resolve(".."); 47 } 48 49 throw new RuntimeException("Failed to locate apps directory"); 50 } 51 }.get(); 52 53 static Path workDir() { 54 return Path.of("."); 55 } 56 57 static Path defaultInputDir() { 58 return workDir().resolve("input"); 59 } 60 61 static Path defaultOutputDir() { 62 return workDir().resolve("output"); 63 } 64 65 static private void log(String v) { 66 System.err.println(v); 67 } 68 69 public static void trace(String v) { 70 if (TRACE) { 71 log("TRACE: " + v); 72 } 73 } 74 75 private static void traceAssert(String v) { 76 if (TRACE_ASSERTS) { 77 log("TRACE: " + v); 78 } 79 } 80 81 public static void error(String v) { 82 log("ERROR: " + v); 83 throw new AssertionError(v); 84 } 85 86 static Path createTempDirectory() throws IOException { 87 return Files.createTempDirectory("jpackage_"); 88 } 89 90 static Path createTempFile(String suffix) throws IOException { 91 return File.createTempFile("jpackage_", suffix).toPath(); 92 } 93 94 private static String concatMessages(String msg, String msg2) { 95 if (msg2 != null && !msg2.isBlank()) { 96 return msg + ": " + msg2; 97 } 98 return msg; 99 } 100 101 public static void assertEquals(int expected, int actual, String msg) { 102 if (expected != actual) { 103 error(concatMessages(String.format( 104 "Expected [%d]. Actual [%d]", expected, actual), 105 msg)); 106 } 107 108 traceAssert(String.format("assertEquals(%d): %s", expected, msg)); 109 } 110 111 public static void assertEquals(String expected, String actual, String msg) { 112 if (expected == null && actual == null) { 113 return; 114 } 115 116 if (actual == null || !expected.equals(actual)) { 117 error(concatMessages(String.format( 118 "Expected [%s]. Actual [%s]", expected, actual), 119 msg)); 120 } 121 122 traceAssert(String.format("assertEquals(%s): %s", expected, msg)); 123 } 124 125 public static void assertNotEquals(int expected, int actual, String msg) { 126 if (expected == actual) { 127 error(concatMessages(String.format("Unexpected [%d] value", actual), 128 msg)); 129 } 130 131 traceAssert(String.format("assertNotEquals(%d, %d): %s", expected, 132 actual, msg)); 133 } 134 135 public static void assertTrue(boolean actual, String msg) { 136 if (!actual) { 137 error(concatMessages("Unexpected FALSE", msg)); 138 } 139 140 traceAssert(String.format("assertTrue(): %s", msg)); 141 } 142 143 public static void assertFalse(boolean actual, String msg) { 144 if (actual) { 145 error(concatMessages("Unexpected TRUE", msg)); 146 } 147 148 traceAssert(String.format("assertFalse(): %s", msg)); 149 } 150 151 public static void assertUnexpected(String msg) { 152 error(concatMessages("Unexpected", msg)); 153 } 154 155 private static final boolean TRACE; 156 private static final boolean TRACE_ASSERTS; 157 158 static { 159 String val = System.getProperty("jpackage.test.suppress-logging"); 160 if (val == null) { 161 TRACE = true; 162 TRACE_ASSERTS = true; 163 } else { 164 Set<String> logOptions = Set.of(val.toLowerCase().split(",")); 165 TRACE = !(logOptions.contains("trace") || logOptions.contains("t")); 166 TRACE_ASSERTS = !(logOptions.contains("assert") || logOptions.contains( 167 "a")); 168 } 169 } 170 }