From 2007b3c02e84a1e46c5954a17b518455065eea4e Mon Sep 17 00:00:00 2001 From: Denis Kononenko Date: Wed, 16 Nov 2016 18:11:56 +0300 Subject: [PATCH] 8167240: Write new tests to cover functionality of existing 'jimage' options New tests were implemented Reviewed-by: jlaskey, anazarov, shurailine --- jdk/test/tools/jimage/JImageBasicsTest.java | 89 +++++++ jdk/test/tools/jimage/JImageCliTest.java | 120 ++++++++++ jdk/test/tools/jimage/JImageExtractTest.java | 181 +++++++++++++++ jdk/test/tools/jimage/JImageInfoTest.java | 74 ++++++ jdk/test/tools/jimage/JImageListTest.java | 229 +++++++++++++++++++ jdk/test/tools/jimage/JImageToolTest.java | 14 +- jdk/test/tools/jimage/JImageVerifyTest.java | 94 ++++++++ 7 files changed, 792 insertions(+), 9 deletions(-) create mode 100644 jdk/test/tools/jimage/JImageBasicsTest.java create mode 100644 jdk/test/tools/jimage/JImageCliTest.java create mode 100644 jdk/test/tools/jimage/JImageExtractTest.java create mode 100644 jdk/test/tools/jimage/JImageInfoTest.java create mode 100644 jdk/test/tools/jimage/JImageListTest.java create mode 100644 jdk/test/tools/jimage/JImageVerifyTest.java diff --git a/jdk/test/tools/jimage/JImageBasicsTest.java b/jdk/test/tools/jimage/JImageBasicsTest.java new file mode 100644 index 00000000000..adfc8106d89 --- /dev/null +++ b/jdk/test/tools/jimage/JImageBasicsTest.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @summary Tests to verify jimage basic options, i.e. --version, -h + * @library /test/lib + * @modules jdk.jlink/jdk.tools.jimage + * @build jdk.test.lib.Asserts + * @run main JImageBasicsTest + */ + +import static jdk.test.lib.Asserts.assertTrue; + +public class JImageBasicsTest extends JImageCliTest { + + public void testVersion() { + jimage("--version") + .assertSuccess() + .resultChecker(r -> { + assertTrue(r.output.contains(System.getProperty("java.version")), "Contains java version."); + }); + } + + public void testFullVersion() { + jimage("--full-version") + .assertSuccess() + .resultChecker(r -> { + assertTrue(r.output.contains(System.getProperty("java.version")), "Contains java version."); + }); + } + + public void testHelp() { + jimage("--help") + .assertSuccess() + .resultChecker(r -> verifyHelpOutput(r.output)); + } + + public void testShortHelp() { + jimage("-h") + .assertSuccess() + .resultChecker(r -> verifyHelpOutput(r.output)); + } + + public void testUnknownAction() { + jimage("unknown") + .assertFailure() + .assertShowsError(); + } + + public void testUnknownOption() { + jimage("--unknown") + .assertFailure() + .assertShowsError(); + } + + private void verifyHelpOutput(String output) { + assertTrue(output.startsWith("Usage: jimage"), "Usage is printed."); + assertTrue(output.contains("extract"), "Option 'extract' is found."); + assertTrue(output.contains("info"), "Option 'info' is found."); + assertTrue(output.contains("list"), "Option 'list' is found."); + assertTrue(output.contains("verify"), "Option 'verify' is found."); + } + + public static void main(String[] args) throws Throwable { + new JImageBasicsTest().runTests(); + } +} + diff --git a/jdk/test/tools/jimage/JImageCliTest.java b/jdk/test/tools/jimage/JImageCliTest.java new file mode 100644 index 00000000000..da924d5ff94 --- /dev/null +++ b/jdk/test/tools/jimage/JImageCliTest.java @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.io.PrintWriter; +import java.lang.reflect.Method; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.function.Consumer; +import java.util.regex.Pattern; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static jdk.test.lib.Asserts.assertFalse; +import static jdk.test.lib.Asserts.assertTrue; +import static jdk.test.lib.Asserts.fail; + +/** + * This class is intended to be a base class for classes which are about to test + * command line interface of jimage. + */ +public class JImageCliTest { + + private String bootImagePath; + + public JImageCliTest() { + Path imagePath = Paths.get(System.getProperty("java.home"), "lib", "modules"); + if (Files.exists(imagePath)) { + this.bootImagePath = imagePath.toAbsolutePath().toString(); + } + } + + public void assertMatches(String regex, String output) { + Pattern pattern = Pattern.compile(regex); + if (!pattern.matcher(output).find()) { + fail(String.format("Expected to find a string match for [%s] in output \n[\n%s\n]\n.", + pattern, output)); + } + } + + /** + * Returns a path to a tested image to share it across tests. By default it returns a path to the boot image + * of tested JDK. This behavior can be redefined by descendants. + */ + public String getImagePath() { + return bootImagePath; + } + + /** + * Runs jimage task with the supplied arguments. + */ + protected static JImageResult jimage(String... args) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + System.out.println("jimage " + Arrays.asList(args)); + int exitCode = jdk.tools.jimage.Main.run(args, new PrintWriter(ps)); + return new JImageResult(exitCode, new String(baos.toByteArray(), UTF_8)); + } + + protected static class JImageResult { + final int exitCode; + final String output; + + JImageResult(int exitCode, String output) { + this.exitCode = exitCode; + this.output = output; + } + + JImageResult assertSuccess() { assertTrue(exitCode == 0, output); return this; } + JImageResult assertFailure() { assertFalse(exitCode == 0, output); return this; } + + // a helper to ensure the error output doesn't exhibit implementation details + JImageResult assertShowsError() { + assertTrue(output.contains("Error"), + String.format("Output contains error, output=[%s]\n", output)); + assertFalse(output.contains("Exception"), + String.format("Output doesn't contain a stacktrace, output=[%s]\n", output)); + return this; + } + + JImageResult resultChecker(Consumer r) { r.accept(this); return this; } + } + + protected final void runTests() throws Throwable { + if (getImagePath() != null) { + for (Method m : getClass().getDeclaredMethods()) { + if (m.getName().startsWith("test")) { + System.out.printf("Invoking %s\n", m.getName()); + m.invoke(this); + } + } + } else { + System.out.println("This is not an image build. Skipping."); + } + } + +} + diff --git a/jdk/test/tools/jimage/JImageExtractTest.java b/jdk/test/tools/jimage/JImageExtractTest.java new file mode 100644 index 00000000000..9c015471414 --- /dev/null +++ b/jdk/test/tools/jimage/JImageExtractTest.java @@ -0,0 +1,181 @@ +/* + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @summary Tests to verify jimage 'extract' action + * @library /test/lib + * @modules jdk.jlink/jdk.tools.jimage + * @build jdk.test.lib.Asserts + * @run main/othervm JImageExtractTest + */ + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.attribute.FileAttribute; +import java.nio.file.attribute.PosixFilePermission; +import java.nio.file.attribute.PosixFilePermissions; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; +import java.util.stream.Collectors; + +import static jdk.test.lib.Asserts.assertEquals; +import static jdk.test.lib.Asserts.assertTrue; + +public class JImageExtractTest extends JImageCliTest { + public void testExtract() throws IOException { + jimage("extract", getImagePath()) + .assertSuccess() + .resultChecker(r -> { + assertTrue(r.output.isEmpty(), "Output is not expected"); + }); + verifyExplodedImage(Paths.get(".")); + } + + public void testExtractHelp() { + for (String opt : Arrays.asList("-h", "--help")) { + jimage("extract", "--help") + .assertSuccess() + .resultChecker(r -> { + // extract - descriptive text + assertMatches("\\s+extract\\s+-\\s+.*", r.output); + }); + } + } + + public void testExtractToDir() throws IOException { + Path tmp = Files.createTempDirectory(Paths.get("."), getClass().getName()); + jimage("extract", "--dir", tmp.toString(), getImagePath()) + .assertSuccess() + .resultChecker(r -> { + assertTrue(r.output.isEmpty(), "Output is not expected"); + }); + verifyExplodedImage(tmp); + } + + public void testExtractNoImageSpecified() { + jimage("extract", "") + .assertFailure() + .assertShowsError(); + } + + public void testExtractNotAnImage() throws IOException { + Path tmp = Files.createTempFile(Paths.get("."), getClass().getName(), "not_an_image"); + jimage("extract", tmp.toString()) + .assertFailure() + .assertShowsError(); + } + + public void testExtractNotExistingImage() throws IOException { + Path tmp = Paths.get(".", "not_existing_image"); + Files.deleteIfExists(tmp); + jimage("extract", tmp.toString()) + .assertFailure() + .assertShowsError(); + } + + public void testExtractToUnspecifiedDir() { + jimage("extract", "--dir", "--", getImagePath()) + .assertFailure() + .assertShowsError(); + } + + public void testExtractToNotExistingDir() throws IOException { + Path tmp = Files.createTempDirectory(Paths.get("."), getClass().getName()); + Files.delete(tmp); + jimage("extract", "--dir", tmp.toString(), getImagePath()) + .assertSuccess() + .resultChecker(r -> { + assertTrue(r.output.isEmpty(), "Output is not expected"); + }); + verifyExplodedImage(tmp); + } + + public void testExtractFromDir() { + Path imagePath = Paths.get(getImagePath()); + Path imageDirPath = imagePath.subpath(0, imagePath.getNameCount() - 1); + jimage("extract", imageDirPath.toString()) + .assertFailure() + .assertShowsError(); + } + + public void testExtractToDirBySymlink() throws IOException { + Path tmp = Files.createTempDirectory(Paths.get("."), getClass().getName()); + try { + Path symlink = Files.createSymbolicLink(Paths.get(".", "symlink"), tmp); + jimage("extract", "--dir", symlink.toString(), getImagePath()) + .assertSuccess() + .resultChecker(r -> { + assertTrue(r.output.isEmpty(), "Output is not expected"); + }); + verifyExplodedImage(tmp); + } catch (UnsupportedOperationException e) { + // symlinks are not supported + // nothing to test + } + } + + public void testExtractToReadOnlyDir() throws IOException { + Set perms = PosixFilePermissions.fromString("r-xr--r--"); + FileAttribute> atts = PosixFilePermissions.asFileAttribute(perms); + Path tmp = Files.createTempDirectory(Paths.get("."), getClass().getName(), atts); + jimage("extract", "--dir", tmp.toString(), getImagePath()) + .assertFailure() + .assertShowsError(); + } + + public void testExtractToNotEmptyDir() throws IOException { + Path tmp = Files.createTempDirectory(Paths.get("."), getClass().getName()); + Files.createFile(Paths.get(tmp.toString(), ".not_empty")); + jimage("extract", "--dir", tmp.toString(), getImagePath()) + .assertFailure() + .assertShowsError(); + } + + public void testExtractToFile() throws IOException { + Path tmp = Files.createTempFile(Paths.get("."), getClass().getName(), "not_a_dir"); + jimage("extract", "--dir", tmp.toString(), getImagePath()) + .assertFailure() + .assertShowsError(); + } + + private void verifyExplodedImage(Path imagePath) throws IOException { + Set allModules = Files.walk(imagePath, 1).collect(Collectors.toSet()); + assertTrue(allModules.stream().anyMatch(p -> "java.base".equals(p.getFileName().toString())), + "Exploded image contains java.base module."); + + Set badModules = allModules.stream() + .filter(p -> !Files.exists(p.resolve("module-info.class"))) + .collect(Collectors.toSet()); + assertEquals(badModules, new HashSet() {{ add(imagePath); }}, + "There are no exploded modules with missing 'module-info.class'"); + } + + public static void main(String[] args) throws Throwable { + new JImageExtractTest().runTests(); + } +} + diff --git a/jdk/test/tools/jimage/JImageInfoTest.java b/jdk/test/tools/jimage/JImageInfoTest.java new file mode 100644 index 00000000000..45eaece46bb --- /dev/null +++ b/jdk/test/tools/jimage/JImageInfoTest.java @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @summary Tests to verify jimage 'info' action + * @library /test/lib + * @modules jdk.jlink/jdk.tools.jimage + * @build jdk.test.lib.Asserts + * @run main JImageInfoTest + */ + +import java.util.Arrays; + +public class JImageInfoTest extends JImageCliTest { + public void testInfo() { + jimage("info", getImagePath()) + .assertSuccess() + .resultChecker(r -> { + assertMatches("(?m)^\\s+Major Version: +[1-9]\\d*$.*", r.output); + assertMatches("(?m)^\\s+Minor Version: +\\d+$.*", r.output); + assertMatches("(?m)^\\s+Flags: +\\d+$.*", r.output); + assertMatches("(?m)^\\s+Resource Count: +\\d+$.*", r.output); + assertMatches("(?m)^\\s+Table Length: +\\d+$.*", r.output); + assertMatches("(?m)^\\s+Offsets Size: +\\d+$.*", r.output); + assertMatches("(?m)^\\s+Redirects Size: +\\d+$.*", r.output); + assertMatches("(?m)^\\s+Locations Size: +\\d+$.*", r.output); + assertMatches("(?m)^\\s+Strings Size: +\\d+$.*", r.output); + assertMatches("(?m)^\\s+Index Size: +\\d+$.*", r.output); + }); + } + + public void testInfoHelp() { + for (String opt : Arrays.asList("-h", "--help")) { + jimage("info", opt) + .assertSuccess() + .resultChecker(r -> { + // info - descriptive text + assertMatches("\\s+info\\s+-\\s+.*", r.output); + }); + } + } + + public void testInfoUnknownOption() { + jimage("info", "--unknown") + .assertFailure() + .assertShowsError(); + } + + public static void main(String[] args) throws Throwable { + new JImageInfoTest().runTests(); + } +} + diff --git a/jdk/test/tools/jimage/JImageListTest.java b/jdk/test/tools/jimage/JImageListTest.java new file mode 100644 index 00000000000..e49e14e2ccb --- /dev/null +++ b/jdk/test/tools/jimage/JImageListTest.java @@ -0,0 +1,229 @@ +/* + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @summary Tests to verify jimage 'list' action + * @library /test/lib + * @modules jdk.jlink/jdk.tools.jimage + * @build jdk.test.lib.Asserts + * @run main JImageListTest + */ + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static jdk.test.lib.Asserts.assertEquals; +import static jdk.test.lib.Asserts.assertFalse; +import static jdk.test.lib.Asserts.assertTrue; + +public class JImageListTest extends JImageCliTest { + public void testList() { + jimage("list", getImagePath()) + .assertSuccess() + .resultChecker(r -> { + String[] lines = r.output.split(System.lineSeparator()); + assertTrue(lines.length > 0, "Option --list has output."); + assertTrue(lines[0].startsWith("jimage: " + getImagePath()), + "Output should start with jimage path."); + + List modules = Stream.of(lines) + .filter(s -> s.startsWith("Module: ")) + .map(s -> s.substring(s.indexOf(':') + 1).trim()) + .collect(Collectors.toList()); + assertTrue(modules.size() > 0, "Image contains at least one module."); + assertTrue(modules.indexOf("java.base") > 0, "Module java.base found."); + }); + } + + public void testListHelp() { + for (String opt : Arrays.asList("-h", "--help")) { + jimage("list", opt) + .assertSuccess() + .resultChecker(r -> { + // list - descriptive text + assertMatches("\\s+list\\s+-\\s+.*", r.output); + }); + } + } + + public void testListVerbose() { + jimage("list", "--verbose", getImagePath()) + .assertSuccess() + .resultChecker(r -> { + assertMatches("Offset\\s+Size\\s+Compressed\\s+Entry", r.output); + + String[] lines = r.output.split("[" + System.lineSeparator() + "]+"); + assertTrue(lines.length > 0, "Option --list has output."); + assertTrue(lines[0].startsWith("jimage: " + getImagePath()), + "Output should start with jimage path."); + + List modules = Stream.of(lines) + .filter(s -> s.startsWith("Module: ")) + .map(s -> s.substring(s.indexOf(':') + 1).trim()) + .collect(Collectors.toList()); + assertTrue(modules.size() > 0, "Image contains at least one module."); + assertTrue(modules.indexOf("java.base") > 0, "Module java.base found."); + + Set entries = Stream.of(lines) + .filter(s -> { return !s.startsWith("Module: ") && !s.startsWith("Offset"); }) + // Offset \d+ Size \d+ Compressed \d+ Entry \.* + .filter(s -> !s.matches("\\s+\\d+\\s+\\d+\\s+\\d+\\s+.*")) + .collect(Collectors.toSet()); + assertEquals(entries, new HashSet<>() {{ add("jimage: " + getImagePath()); }}, + "All entries should be in format: Offset Size Compressed Entry"); + }); + } + + public void testListIncludeAllWithGlob() { + JImageResult listAll = jimage("list", getImagePath()).assertSuccess(); + JImageResult listAllGlob = jimage("list", "--include", "**", getImagePath()).assertSuccess(); + assertEquals(listAllGlob.output, listAll.output, "--include ** should produce the same output"); + } + + public void testListIncludeWithGlob() { + JImageResult listAll = jimage("list", getImagePath()).assertSuccess(); + Set expected = Stream.of(listAll.output.split("[" + System.lineSeparator() + "]+")) + .map(String::trim) + .filter(s -> s.startsWith("java/util/zip")) + .collect(Collectors.toSet()); + + JImageResult listJavaUtil = jimage("list", "--include", "/java.base/java/util/zip/**", getImagePath()).assertSuccess(); + Set actual = Stream.of(listJavaUtil.output.split("[" + System.lineSeparator() + "]+")) + .map(String::trim) + .filter(s -> !s.startsWith("jimage:") && !s.startsWith("Module:")) + .collect(Collectors.toSet()); + assertEquals(actual, expected, "All java.util.zip classes are listed"); + } + + public void testListIncludeNoMatchWithGlob() { + JImageResult listNotMatching = jimage("list", "--include", "not_matching", getImagePath()).assertSuccess(); + Set entries = Stream.of(listNotMatching.output.split("["+ System.lineSeparator() + "]+")) + .map(String::trim) + .filter(s -> !s.startsWith("jimage:") && !s.startsWith("Module:")) + .collect(Collectors.toSet()); + assertEquals(entries, Collections.emptySet(), "No java.util classes are listed"); + } + + public void testListIncludeAllWithExplicitGlob() { + JImageResult listAll = jimage("list", getImagePath()).assertSuccess(); + JImageResult listAllGlob = jimage("list", "--include", "glob:**", getImagePath()).assertSuccess(); + assertEquals(listAllGlob.output, listAll.output, "--include glob:** should produce the same output"); + } + + public void testListIncludeAllWithRegex() { + JImageResult listAll = jimage("list", getImagePath()).assertSuccess(); + JImageResult listAllRegex = jimage("list", "--include", "regex:.*", getImagePath()).assertSuccess(); + assertEquals(listAllRegex.output, listAll.output, "--include regex:.* should produce the same output"); + } + + public void testListIncludeWithRegex() { + JImageResult listAll = jimage("list", getImagePath()).assertSuccess(); + Set expected = Stream.of(listAll.output.split("[" + System.lineSeparator() + "]+")) + .map(String::trim) + .filter(s -> s.startsWith("java/text/")) + .collect(Collectors.toSet()); + assertFalse(expected.isEmpty(), "There should be classes from java.text package"); + + JImageResult listJavaText = jimage("list", "--include", "regex:/java.base/java/text/.*", getImagePath()).assertSuccess(); + Set actual = Stream.of(listJavaText.output.split("[" + System.lineSeparator() + "]+")) + .map(String::trim) + .filter(s -> !s.startsWith("jimage:") && !s.startsWith("Module:")) + .collect(Collectors.toSet()); + + assertEquals(actual, expected, "All java.text classes are listed"); + } + + public void testListIncludeNoMatchWithRegex() { + JImageResult listNotMatching = jimage("list", "--include", "regex:not_matching", + getImagePath()).assertSuccess(); + Set entries = Stream.of(listNotMatching.output.split("[" + System.lineSeparator() + "]+")) + .map(String::trim) + .filter(s -> !s.startsWith("jimage:") && !s.startsWith("Module:")) + .collect(Collectors.toSet()); + assertEquals(entries, Collections.emptySet(), "No classes are listed"); + } + + public void testListIncludeMultiplePatterns() throws IOException { + JImageResult listAll = jimage("list", getImagePath()).assertSuccess(); + Set expected = Stream.of(listAll.output.split("[" + System.lineSeparator() + "]+")) + .map(String::trim) + .filter(s -> s.startsWith("java/time/") || s.startsWith("java/util/zip")) + .collect(Collectors.toSet()); + assertFalse(expected.isEmpty(), "There should be classes from java.time and java.io packages"); + + JImageResult listMatched = jimage("list", "--include", "glob:/java.base/java/time/**,regex:/java.base/java/util/zip/.*", + getImagePath()).assertSuccess(); + Set actual = Stream.of(listMatched.output.split("[" + System.lineSeparator() + "]+")) + .map(String::trim) + .filter(s -> !s.startsWith("jimage:") && !s.startsWith("Module:")) + .collect(Collectors.toSet()); + + assertEquals(actual, expected, "All java.time and java.util.zip classes are listed"); + } + + public void testListNoImageSpecified() { + jimage("list", "") + .assertFailure() + .assertShowsError(); + } + + public void testListEmptyFile() throws IOException { + Path tmp = Files.createTempFile(Paths.get("."), getClass().getName(), "empty_file"); + jimage("list", tmp.toString()) + .assertFailure() + .assertShowsError(); + } + + public void testListNotAnImage() throws IOException { + Path tmp = Files.createTempFile(Paths.get("."), getClass().getName(), "not_an_image"); + Files.write(tmp, "This is not an image".getBytes()); + jimage("list", tmp.toString()) + .assertFailure() + .assertShowsError(); + } + + public void testListNotExistingImage() throws IOException { + Path tmp = Paths.get(".", "not_existing_image"); + Files.deleteIfExists(tmp); + jimage("list", tmp.toString()) + .assertFailure() + .assertShowsError(); + } + + public void testListWithUnknownOption() { + jimage("list", "--unknown") + .assertFailure() + .assertShowsError(); + } + + public static void main(String[] args) throws Throwable { + new JImageListTest().runTests(); + } +} + diff --git a/jdk/test/tools/jimage/JImageToolTest.java b/jdk/test/tools/jimage/JImageToolTest.java index da6cb9f740e..c508eba0cfc 100644 --- a/jdk/test/tools/jimage/JImageToolTest.java +++ b/jdk/test/tools/jimage/JImageToolTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,8 +23,8 @@ /* * @test * @library /lib/testlibrary - * @build jdk.testlibrary.* - * @summary Test to see if jimage tool extracts and recreates correctly. + * @build jdk.testlibrary.ProcessTools + * @summary Test to check if jimage tool exists and is working * @run main/timeout=360 JImageToolTest */ @@ -42,7 +42,7 @@ import jdk.testlibrary.ProcessTools; public class JImageToolTest { private static void jimage(String... jimageArgs) throws Exception { ArrayList args = new ArrayList<>(); - args.add("-ms8m"); + args.add("-ms64m"); args.add("jdk.tools.jimage.Main"); args.addAll(Arrays.asList(jimageArgs)); @@ -61,11 +61,7 @@ public class JImageToolTest { if (Files.exists(jimagePath) && Files.exists(modulesimagePath)) { String jimage = jimagePath.toAbsolutePath().toString(); - String bootimage = modulesimagePath.toAbsolutePath().toString(); - String extractDir = Paths.get(".", "extract").toAbsolutePath().toString(); - jimage("list", bootimage); - jimage("verify", bootimage); - jimage("extract", "--dir", extractDir, bootimage); + jimage("--version"); System.out.println("Test successful"); } else { System.out.println("Test skipped, not an images build"); diff --git a/jdk/test/tools/jimage/JImageVerifyTest.java b/jdk/test/tools/jimage/JImageVerifyTest.java new file mode 100644 index 00000000000..bb763c45704 --- /dev/null +++ b/jdk/test/tools/jimage/JImageVerifyTest.java @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @summary Tests to verify jimage 'verify' action + * @library /test/lib + * @modules jdk.jlink/jdk.tools.jimage + * @build jdk.test.lib.Asserts + * @run main JImageVerifyTest + */ + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; + +import static jdk.test.lib.Asserts.assertTrue; + +public class JImageVerifyTest extends JImageCliTest { + + public void testVerify() { + jimage("verify", getImagePath()) + .assertSuccess() + .resultChecker(r -> { + assertTrue(r.output.startsWith("jimage: " + getImagePath()), + "Contains verified image's path"); + }); + } + + public void testVerifyHelp() { + for (String opt : Arrays.asList("-h", "--help")) { + jimage("verify", opt) + .assertSuccess() + .resultChecker(r -> { + // verify - descriptive text + assertMatches("\\s+verify\\s+-\\s+.*", r.output); + }); + } + } + + public void testVerifyImageNotSpecified() { + jimage("verify", "") + .assertFailure() + .assertShowsError(); + } + + public void testVerifyNotAnImage() throws IOException { + Path tmp = Files.createTempFile(Paths.get("."), getClass().getName(), "not_an_image"); + jimage("verify", tmp.toString()) + .assertFailure() + .assertShowsError(); + } + + public void testVerifyNotExistingImage() throws IOException { + Path tmp = Paths.get(".", "not_existing_image"); + Files.deleteIfExists(tmp); + jimage("verify", "") + .assertFailure() + .assertShowsError(); + } + + public void testVerifyWithUnknownOption() { + jimage("verify", "--unknown") + .assertFailure() + .assertShowsError(); + } + + public static void main(String[] args) throws Throwable { + new JImageVerifyTest().runTests(); + } + +}