Path: blob/master/test/jdk/tools/jimage/JImageListTest.java
41144 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary Tests to verify jimage 'list' action26* @library /test/lib27* @modules jdk.jlink/jdk.tools.jimage28* @build jdk.test.lib.Asserts29* @run main JImageListTest30*/3132import java.io.IOException;33import java.nio.file.Files;34import java.nio.file.Path;35import java.nio.file.Paths;36import java.util.*;37import java.util.stream.Collectors;38import java.util.stream.Stream;3940import static jdk.test.lib.Asserts.assertEquals;41import static jdk.test.lib.Asserts.assertFalse;42import static jdk.test.lib.Asserts.assertTrue;4344public class JImageListTest extends JImageCliTest {45public void testList() {46jimage("list", getImagePath())47.assertSuccess()48.resultChecker(r -> {49String[] lines = r.output.split(System.lineSeparator());50assertTrue(lines.length > 0, "Option --list has output.");51assertTrue(lines[0].startsWith("jimage: " + getImagePath()),52"Output should start with jimage path.");5354List<String> modules = Stream.of(lines)55.filter(s -> s.startsWith("Module: "))56.map(s -> s.substring(s.indexOf(':') + 1).trim())57.collect(Collectors.toList());58assertTrue(modules.size() > 0, "Image contains at least one module.");59assertTrue(modules.contains("java.base"), "Module java.base found.");60});61}6263public void testListHelp() {64for (String opt : Arrays.asList("-h", "--help")) {65jimage("list", opt)66.assertSuccess()67.resultChecker(r -> {68// list - descriptive text69assertMatches("\\s+list\\s+-\\s+.*", r.output);70});71}72}7374public void testListVerbose() {75jimage("list", "--verbose", getImagePath())76.assertSuccess()77.resultChecker(r -> {78assertMatches("Offset\\s+Size\\s+Compressed\\s+Entry", r.output);7980String[] lines = r.output.split("[" + System.lineSeparator() + "]+");81assertTrue(lines.length > 0, "Option --list has output.");82assertTrue(lines[0].startsWith("jimage: " + getImagePath()),83"Output should start with jimage path.");8485List<String> modules = Stream.of(lines)86.filter(s -> s.startsWith("Module: "))87.map(s -> s.substring(s.indexOf(':') + 1).trim())88.collect(Collectors.toList());89assertTrue(modules.size() > 0, "Image contains at least one module.");90assertTrue(modules.contains("java.base"), "Module java.base found.");9192Set<String> entries = Stream.of(lines)93.filter(s -> { return !s.startsWith("Module: ") && !s.startsWith("Offset"); })94// Offset \d+ Size \d+ Compressed \d+ Entry \.*95.filter(s -> !s.matches("\\s+\\d+\\s+\\d+\\s+\\d+\\s+.*"))96.collect(Collectors.toSet());97assertEquals(entries, new HashSet<>() {{ add("jimage: " + getImagePath()); }},98"All entries should be in format: Offset Size Compressed Entry");99});100}101102public void testListIncludeAllWithGlob() {103JImageResult listAll = jimage("list", getImagePath()).assertSuccess();104JImageResult listAllGlob = jimage("list", "--include", "**", getImagePath()).assertSuccess();105assertEquals(listAllGlob.output, listAll.output, "--include ** should produce the same output");106}107108public void testListIncludeWithGlob() {109JImageResult listAll = jimage("list", getImagePath()).assertSuccess();110Set<String> expected = Stream.of(listAll.output.split("[" + System.lineSeparator() + "]+"))111.map(String::trim)112.filter(s -> s.startsWith("java/util/zip"))113.collect(Collectors.toSet());114115JImageResult listJavaUtil = jimage("list", "--include", "/java.base/java/util/zip/**", getImagePath()).assertSuccess();116Set<String> actual = Stream.of(listJavaUtil.output.split("[" + System.lineSeparator() + "]+"))117.map(String::trim)118.filter(s -> !s.startsWith("jimage:") && !s.startsWith("Module:"))119.collect(Collectors.toSet());120assertEquals(actual, expected, "All java.util.zip classes are listed");121}122123public void testListIncludeNoMatchWithGlob() {124JImageResult listNotMatching = jimage("list", "--include", "not_matching", getImagePath()).assertSuccess();125Set<String> entries = Stream.of(listNotMatching.output.split("["+ System.lineSeparator() + "]+"))126.map(String::trim)127.filter(s -> !s.startsWith("jimage:") && !s.startsWith("Module:"))128.collect(Collectors.toSet());129assertEquals(entries, Collections.emptySet(), "No java.util classes are listed");130}131132public void testListIncludeAllWithExplicitGlob() {133JImageResult listAll = jimage("list", getImagePath()).assertSuccess();134JImageResult listAllGlob = jimage("list", "--include", "glob:**", getImagePath()).assertSuccess();135assertEquals(listAllGlob.output, listAll.output, "--include glob:** should produce the same output");136}137138public void testListIncludeAllWithRegex() {139JImageResult listAll = jimage("list", getImagePath()).assertSuccess();140JImageResult listAllRegex = jimage("list", "--include", "regex:.*", getImagePath()).assertSuccess();141assertEquals(listAllRegex.output, listAll.output, "--include regex:.* should produce the same output");142}143144public void testListIncludeWithRegex() {145JImageResult listAll = jimage("list", getImagePath()).assertSuccess();146Set<String> expected = Stream.of(listAll.output.split("[" + System.lineSeparator() + "]+"))147.map(String::trim)148.filter(s -> s.startsWith("java/text/"))149.collect(Collectors.toSet());150assertFalse(expected.isEmpty(), "There should be classes from java.text package");151152JImageResult listJavaText = jimage("list", "--include", "regex:/java.base/java/text/.*", getImagePath()).assertSuccess();153Set<String> actual = Stream.of(listJavaText.output.split("[" + System.lineSeparator() + "]+"))154.map(String::trim)155.filter(s -> !s.startsWith("jimage:") && !s.startsWith("Module:"))156.collect(Collectors.toSet());157158assertEquals(actual, expected, "All java.text classes are listed");159}160161public void testListIncludeNoMatchWithRegex() {162JImageResult listNotMatching = jimage("list", "--include", "regex:not_matching",163getImagePath()).assertSuccess();164Set<String> entries = Stream.of(listNotMatching.output.split("[" + System.lineSeparator() + "]+"))165.map(String::trim)166.filter(s -> !s.startsWith("jimage:") && !s.startsWith("Module:"))167.collect(Collectors.toSet());168assertEquals(entries, Collections.emptySet(), "No classes are listed");169}170171public void testListIncludeMultiplePatterns() throws IOException {172JImageResult listAll = jimage("list", getImagePath()).assertSuccess();173Set<String> expected = Stream.of(listAll.output.split("[" + System.lineSeparator() + "]+"))174.map(String::trim)175.filter(s -> s.startsWith("java/time/") || s.startsWith("java/util/zip"))176.collect(Collectors.toSet());177assertFalse(expected.isEmpty(), "There should be classes from java.time and java.io packages");178179JImageResult listMatched = jimage("list", "--include", "glob:/java.base/java/time/**,regex:/java.base/java/util/zip/.*",180getImagePath()).assertSuccess();181Set<String> actual = Stream.of(listMatched.output.split("[" + System.lineSeparator() + "]+"))182.map(String::trim)183.filter(s -> !s.startsWith("jimage:") && !s.startsWith("Module:"))184.collect(Collectors.toSet());185186assertEquals(actual, expected, "All java.time and java.util.zip classes are listed");187}188189public void testListNoImageSpecified() {190jimage("list", "")191.assertFailure()192.assertShowsError();193}194195public void testListEmptyFile() throws IOException {196Path tmp = Files.createTempFile(Paths.get("."), getClass().getName(), "empty_file");197jimage("list", tmp.toString())198.assertFailure()199.assertShowsError();200}201202public void testListNotAnImage() throws IOException {203Path tmp = Files.createTempFile(Paths.get("."), getClass().getName(), "not_an_image");204Files.write(tmp, "This is not an image".getBytes());205jimage("list", tmp.toString())206.assertFailure()207.assertShowsError();208}209210public void testListNotExistingImage() throws IOException {211Path tmp = Paths.get(".", "not_existing_image");212Files.deleteIfExists(tmp);213jimage("list", tmp.toString())214.assertFailure()215.assertShowsError();216}217218public void testListWithUnknownOption() {219jimage("list", "--unknown")220.assertFailure()221.assertShowsError();222}223224public static void main(String[] args) throws Throwable {225new JImageListTest().runTests();226// Just to ensure that jimage files will be unmapped227System.gc();228}229}230231232233