< prev index next >
test/jdk/tools/jpackage/helpers/jdk/jpackage/test/Test.java
Print this page
*** 20,45 ****
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.jpackage.test;
! import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardWatchEventKinds;
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
! public class Test {
public static final Path TEST_SRC_ROOT = new Supplier<Path>() {
@Override
public Path get() {
Path root = Path.of(System.getProperty("test.src"));
--- 20,54 ----
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.jpackage.test;
! import java.io.BufferedOutputStream;
! import java.io.FileNotFoundException;
! import java.io.FileOutputStream;
import java.io.IOException;
+ import java.io.PrintStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardWatchEventKinds;
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
+ import java.util.Collection;
+ import java.util.List;
+ import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
+ import java.util.function.Consumer;
import java.util.function.Supplier;
+ import java.util.stream.Collectors;
+ import jdk.jpackage.internal.IOUtils;
! final public class Test {
public static final Path TEST_SRC_ROOT = new Supplier<Path>() {
@Override
public Path get() {
Path root = Path.of(System.getProperty("test.src"));
*** 53,62 ****
--- 62,127 ----
throw new RuntimeException("Failed to locate apps directory");
}
}.get();
+ private static class Instance implements AutoCloseable {
+ Instance(String args[]) {
+ assertCount = 0;
+
+ name = enclosingMainMethodClass().getSimpleName();
+ extraLogStream = openLogStream();
+
+ currentTest = this;
+
+ log(String.format("[ RUN ] %s", name));
+ }
+
+ @Override
+ public void close() {
+ log(String.format("%s %s; checks=%d",
+ success ? "[ OK ]" : "[ FAILED ]", name, assertCount));
+
+ if (extraLogStream != null) {
+ extraLogStream.close();
+ }
+ }
+
+ void notifyAssert() {
+ assertCount++;
+ }
+
+ void notifySuccess() {
+ success = true;
+ }
+
+ private int assertCount;
+ private boolean success;
+ private final String name;
+ private final PrintStream extraLogStream;
+ }
+
+ public static void run(String args[], TestBody action) {
+ if (currentTest != null) {
+ throw new IllegalStateException(
+ "Unexpeced nested or concurrent Test.run() call");
+ }
+
+ try (Instance instance = new Instance(args)) {
+ action.run();
+ instance.notifySuccess();
+ } catch (Exception ex) {
+ throw new RuntimeException(ex);
+ } finally {
+ currentTest = null;
+ }
+ }
+
+ public static interface TestBody {
+ public void run() throws Exception;
+ }
+
public static Path workDir() {
return Path.of(".");
}
static Path defaultInputDir() {
*** 65,74 ****
--- 130,153 ----
static Path defaultOutputDir() {
return workDir().resolve("output");
}
+ static Class enclosingMainMethodClass() {
+ StackTraceElement st[] = Thread.currentThread().getStackTrace();
+ for (StackTraceElement ste : st) {
+ if ("main".equals(ste.getMethodName())) {
+ try {
+ return Class.forName(ste.getClassName());
+ } catch (ClassNotFoundException ex) {
+ throw new RuntimeException(ex);
+ }
+ }
+ }
+ return null;
+ }
+
static boolean isWindows() {
return (OS.contains("win"));
}
static boolean isOSX() {
*** 78,88 ****
static boolean isLinux() {
return ((OS.contains("nix") || OS.contains("nux")));
}
static private void log(String v) {
! System.err.println(v);
}
public static void trace(String v) {
if (TRACE) {
log("TRACE: " + v);
--- 157,199 ----
static boolean isLinux() {
return ((OS.contains("nix") || OS.contains("nux")));
}
static private void log(String v) {
! System.out.println(v);
! if (currentTest != null && currentTest.extraLogStream != null) {
! currentTest.extraLogStream.println(v);
! }
! }
!
! public static Class getTestClass () {
! return enclosingMainMethodClass();
! }
!
! public static void createPropertiesFile(Path propsFilename,
! Collection<Map.Entry<String, String>> props) {
! trace(String.format("Create [%s] properties file...",
! propsFilename.toAbsolutePath().normalize()));
! try {
! Files.write(propsFilename, props.stream().peek(e -> trace(
! String.format("%s=%s", e.getKey(), e.getValue()))).map(
! e -> String.format("%s=%s", e.getKey(), e.getValue())).collect(
! Collectors.toList()));
! } catch (IOException ex) {
! throw new RuntimeException(ex);
! }
! trace("Done");
! }
!
! public static void createPropertiesFile(Path propsFilename,
! Map.Entry<String, String>... props) {
! createPropertiesFile(propsFilename, List.of(props));
! }
!
! public static void createPropertiesFile(Path propsFilename,
! Map<String, String> props) {
! createPropertiesFile(propsFilename, props.entrySet());
}
public static void trace(String v) {
if (TRACE) {
log("TRACE: " + v);
*** 98,113 ****
public static void error(String v) {
log("ERROR: " + v);
throw new AssertionError(v);
}
public static Path createTempDirectory() throws IOException {
! return Files.createTempDirectory("jpackage_");
}
public static Path createTempFile(String suffix) throws IOException {
! return File.createTempFile("jpackage_", suffix).toPath();
}
public static void waitForFileCreated(Path fileToWaitFor,
long timeoutSeconds) throws IOException {
--- 209,266 ----
public static void error(String v) {
log("ERROR: " + v);
throw new AssertionError(v);
}
+ private static final String TEMP_FILE_PREFIX = null;
+
public static Path createTempDirectory() throws IOException {
! return Files.createTempDirectory(workDir(), TEMP_FILE_PREFIX);
}
public static Path createTempFile(String suffix) throws IOException {
! return Files.createTempFile(workDir(), TEMP_FILE_PREFIX, suffix);
! }
!
! public static void withTempFile(String suffix, Consumer<Path> action) {
! Path tempFile = null;
! boolean keepIt = true;
! try {
! tempFile = createTempFile(suffix);
! action.accept(tempFile);
! keepIt = false;
! } catch (IOException ex) {
! throw new RuntimeException(ex);
! } finally {
! if (tempFile != null && !keepIt) {
! try {
! Files.deleteIfExists(tempFile);
! } catch (IOException ex) {
! throw new RuntimeException(ex);
! }
! }
! }
! }
!
! public static void withTempDirectory(Consumer<Path> action) {
! Path tempDir = null;
! boolean keepIt = true;
! try {
! tempDir = createTempDirectory();
! action.accept(tempDir);
! keepIt = false;
! } catch (IOException ex) {
! throw new RuntimeException(ex);
! } finally {
! try {
! if (tempDir != null && tempDir.toFile().isDirectory() && !keepIt) {
! IOUtils.deleteRecursive(tempDir.toFile());
! }
! } catch (IOException ex) {
! throw new RuntimeException(ex);
! }
! }
}
public static void waitForFileCreated(Path fileToWaitFor,
long timeoutSeconds) throws IOException {
*** 164,233 ****
return msg + ": " + msg2;
}
return msg;
}
! public static void assertEquals(int expected, int actual, String msg) {
if (expected != actual) {
error(concatMessages(String.format(
"Expected [%d]. Actual [%d]", expected, actual),
msg));
}
traceAssert(String.format("assertEquals(%d): %s", expected, msg));
}
! public static void assertEquals(String expected, String actual, String msg) {
! if (expected == null && actual == null) {
! return;
}
! if (actual == null || !expected.equals(actual)) {
error(concatMessages(String.format(
"Expected [%s]. Actual [%s]", expected, actual),
msg));
}
traceAssert(String.format("assertEquals(%s): %s", expected, msg));
}
! public static void assertNotEquals(int expected, int actual, String msg) {
! if (expected == actual) {
! error(concatMessages(String.format("Unexpected [%d] value", actual),
! msg));
! }
! traceAssert(String.format("assertNotEquals(%d, %d): %s", expected,
actual, msg));
}
public static void assertNull(Object value, String msg) {
if (value != null) {
error(concatMessages(String.format("Unexpected not null value [%s]",
value), msg));
}
traceAssert(String.format("assertNull(): %s", msg));
}
public static void assertNotNull(Object value, String msg) {
if (value == null) {
error(concatMessages("Unexpected null value", msg));
}
traceAssert(String.format("assertNotNull(%s): %s", value, msg));
}
public static void assertTrue(boolean actual, String msg) {
if (!actual) {
error(concatMessages("Unexpected FALSE", msg));
}
traceAssert(String.format("assertTrue(): %s", msg));
}
public static void assertFalse(boolean actual, String msg) {
if (actual) {
error(concatMessages("Unexpected TRUE", msg));
}
traceAssert(String.format("assertFalse(): %s", msg));
--- 317,403 ----
return msg + ": " + msg2;
}
return msg;
}
! public static void assertEquals(long expected, long actual, String msg) {
! currentTest.notifyAssert();
if (expected != actual) {
error(concatMessages(String.format(
"Expected [%d]. Actual [%d]", expected, actual),
msg));
}
traceAssert(String.format("assertEquals(%d): %s", expected, msg));
}
! public static void assertNotEquals(long expected, long actual, String msg) {
! currentTest.notifyAssert();
! if (expected == actual) {
! error(concatMessages(String.format("Unexpected [%d] value", actual),
! msg));
! }
!
! traceAssert(String.format("assertNotEquals(%d, %d): %s", expected,
! actual, msg));
}
! public static void assertEquals(String expected, String actual, String msg) {
! currentTest.notifyAssert();
! if ((actual != null && !actual.equals(expected))
! || (expected != null && !expected.equals(actual))) {
error(concatMessages(String.format(
"Expected [%s]. Actual [%s]", expected, actual),
msg));
}
traceAssert(String.format("assertEquals(%s): %s", expected, msg));
}
! public static void assertNotEquals(String expected, String actual, String msg) {
! currentTest.notifyAssert();
! if ((actual != null && !actual.equals(expected))
! || (expected != null && !expected.equals(actual))) {
! traceAssert(String.format("assertNotEquals(%s, %s): %s", expected,
actual, msg));
+ return;
+ }
+
+ error(concatMessages(String.format("Unexpected [%s] value", actual), msg));
}
public static void assertNull(Object value, String msg) {
+ currentTest.notifyAssert();
if (value != null) {
error(concatMessages(String.format("Unexpected not null value [%s]",
value), msg));
}
traceAssert(String.format("assertNull(): %s", msg));
}
public static void assertNotNull(Object value, String msg) {
+ currentTest.notifyAssert();
if (value == null) {
error(concatMessages("Unexpected null value", msg));
}
traceAssert(String.format("assertNotNull(%s): %s", value, msg));
}
public static void assertTrue(boolean actual, String msg) {
+ currentTest.notifyAssert();
if (!actual) {
error(concatMessages("Unexpected FALSE", msg));
}
traceAssert(String.format("assertTrue(): %s", msg));
}
public static void assertFalse(boolean actual, String msg) {
+ currentTest.notifyAssert();
if (actual) {
error(concatMessages("Unexpected TRUE", msg));
}
traceAssert(String.format("assertFalse(): %s", msg));
*** 266,291 ****
"Check [%s] file is executable", path));
}
}
public static void assertUnexpected(String msg) {
error(concatMessages("Unexpected", msg));
}
private static final boolean TRACE;
private static final boolean TRACE_ASSERTS;
static {
! String val = System.getProperty("jpackage.test.suppress-logging");
if (val == null) {
TRACE = true;
TRACE_ASSERTS = true;
} else {
Set<String> logOptions = Set.of(val.toLowerCase().split(","));
TRACE = !(logOptions.contains("trace") || logOptions.contains("t"));
TRACE_ASSERTS = !(logOptions.contains("assert") || logOptions.contains(
"a"));
}
}
private static final String OS = System.getProperty("os.name").toLowerCase();
}
--- 436,504 ----
"Check [%s] file is executable", path));
}
}
public static void assertUnexpected(String msg) {
+ currentTest.notifyAssert();
error(concatMessages("Unexpected", msg));
}
+ private static PrintStream openLogStream() {
+ if (LOG_FILE == null) {
+ return null;
+ }
+
+ try {
+ return new PrintStream(new FileOutputStream(LOG_FILE.toFile(), true));
+ } catch (FileNotFoundException ex) {
+ throw new RuntimeException(ex);
+ }
+ }
+
+ private static Instance currentTest;
+
private static final boolean TRACE;
private static final boolean TRACE_ASSERTS;
+ static final boolean VERBOSE_JPACKAGE;
+
+ static String getConfigProperty(String propertyName) {
+ return System.getProperty(getConfigPropertyName(propertyName));
+ }
+
+ static String getConfigPropertyName(String propertyName) {
+ return "jpackage.test." + propertyName;
+ }
+
+ static final Path LOG_FILE = new Supplier<Path>() {
+ @Override
+ public Path get() {
+ String val = getConfigProperty("logfile");
+ if (val == null) {
+ return null;
+ }
+ return Path.of(val);
+ }
+ }.get();
+
static {
! String val = getConfigProperty("suppress-logging");
if (val == null) {
TRACE = true;
TRACE_ASSERTS = true;
+ VERBOSE_JPACKAGE = true;
+ } else if ("all".equals(val.toLowerCase())) {
+ TRACE = false;
+ TRACE_ASSERTS = false;
+ VERBOSE_JPACKAGE = false;
} else {
Set<String> logOptions = Set.of(val.toLowerCase().split(","));
TRACE = !(logOptions.contains("trace") || logOptions.contains("t"));
TRACE_ASSERTS = !(logOptions.contains("assert") || logOptions.contains(
"a"));
+ VERBOSE_JPACKAGE = !(logOptions.contains("jpackage") || logOptions.contains(
+ "jp"));
}
}
private static final String OS = System.getProperty("os.name").toLowerCase();
}
\ No newline at end of file
< prev index next >