Path: blob/master/test/jdk/java/lang/module/ModuleNamesTest.java
41149 views
/*1* Copyright (c) 2016, 2018, 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* @modules java.base/jdk.internal.access26* java.base/jdk.internal.module27* @run testng ModuleNamesTest28* @summary Basic test of reading a module-info.class with module names that29* are legal in class files but not legal in the Java Language30*/3132import java.io.ByteArrayOutputStream;33import java.lang.module.InvalidModuleDescriptorException;34import java.lang.module.ModuleDescriptor;35import java.lang.module.ModuleDescriptor.Builder;36import java.lang.module.ModuleDescriptor.Exports;37import java.lang.module.ModuleDescriptor.Opens;38import java.lang.module.ModuleDescriptor.Requires;39import java.nio.ByteBuffer;40import java.util.Optional;41import java.util.Set;4243import jdk.internal.access.SharedSecrets;44import jdk.internal.module.ModuleInfoWriter;4546import org.testng.annotations.DataProvider;47import org.testng.annotations.Test;48import static org.testng.Assert.*;4950@Test51public class ModuleNamesTest {5253@DataProvider(name = "legalModuleNames")54public Object[][] legalModuleNames() {55return new Object[][] {5657{ ".", "." },58{ ".foo", ".foo" },59{ "foo.", "foo." },60{ "foo.bar", "foo.bar" },6162{ "..", ".." },63{ "..foo", "..foo" },64{ "foo..", "foo.." },65{ "foo..bar", "foo..bar" },6667{ "[", "[" },68{ "[foo", "[foo" },69{ "foo[", "foo[" },70{ "foo[bar", "foo[bar" },7172{ ";", ";" },73{ ";foo", ";foo" },74{ "foo;", "foo;" },75{ "foo;bar", "foo;bar" },7677{ "\\\\", "\\" },78{ "\\\\foo", "\\foo" },79{ "foo\\\\", "foo\\" },80{ "foo\\\\bar", "foo\\bar" },8182{ "\\\\\\\\", "\\\\" },83{ "\\\\\\\\foo", "\\\\foo" },84{ "foo\\\\\\\\", "foo\\\\" },85{ "foo\\\\\\\\bar", "foo\\\\bar" },8687{ "\\:", ":" },88{ "\\:foo", ":foo" },89{ "foo\\:", "foo:" },90{ "foo\\:bar", "foo:bar" },9192{ "\\:\\:", "::" },93{ "\\:\\:foo", "::foo" },94{ "foo\\:\\:", "foo::" },95{ "foo\\:\\:bar", "foo::bar" },9697{ "\\@", "@" },98{ "\\@foo", "@foo" },99{ "foo\\@", "foo@" },100{ "foo\\@bar", "foo@bar" },101102{ "\\@\\@", "@@" },103{ "\\@\\@foo", "@@foo" },104{ "foo\\@\\@", "foo@@" },105{ "foo\\@\\@bar", "foo@@bar" },106107{ makeString("", 0x20, ""), " " },108{ makeString("foo", 0x20, ""), "foo " },109{ makeString("", 0x20, "foo"), " foo" },110{ makeString("foo", 0x20, "bar"), "foo bar" },111};112}113114@DataProvider(name = "illegalModuleNames")115public Object[][] illegalModuleNames() {116return new Object[][] {117118{ "", null },119120{ ":", null },121{ ":foo", null },122{ "foo:", null },123{ "foo:bar", null },124125{ "@", null },126{ "@foo", null },127{ "foo@", null },128{ "foo@bar", null },129130{ "\\", null },131{ "\\foo", null },132{ "foo\\", null },133{ "foo\\bar", null },134135{ makeString("", 0x00, ""), null },136{ makeString("", 0x00, "foo"), null },137{ makeString("foo", 0x00, ""), null },138{ makeString("foo", 0x00, "bar"), null },139140{ makeString("", 0x1f, ""), null },141{ makeString("", 0x1f, "foo"), null },142{ makeString("foo", 0x1f, ""), null },143{ makeString("foo", 0x1f, "bar"), null },144145};146}147148@Test(dataProvider = "legalModuleNames")149public void testLegalModuleName(String mn, String expected) throws Exception {150ModuleDescriptor md = newBuilder(mn).requires("java.base").build();151ByteBuffer bb = toBuffer(md);152String name = ModuleDescriptor.read(bb).name();153assertEquals(name, expected);154}155156@Test(dataProvider = "illegalModuleNames",157expectedExceptions = InvalidModuleDescriptorException.class)158public void testIllegalModuleName(String mn, String ignore) throws Exception {159ModuleDescriptor md = newBuilder(mn).requires("java.base").build();160ByteBuffer bb = toBuffer(md);161ModuleDescriptor.read(bb); // throws InvalidModuleDescriptorException162}163164@Test(dataProvider = "legalModuleNames")165public void testLegalRequires(String mn, String expected) throws Exception {166ModuleDescriptor md = newBuilder("m").requires("java.base").requires(mn).build();167ByteBuffer bb = toBuffer(md);168ModuleDescriptor descriptor = ModuleDescriptor.read(bb);169Optional<Requires> requires = descriptor.requires().stream()170.filter(r -> !r.name().equals("java.base"))171.findAny();172assertTrue(requires.isPresent());173assertEquals(requires.get().name(), expected);174}175176@Test(dataProvider = "illegalModuleNames",177expectedExceptions = InvalidModuleDescriptorException.class)178public void testIllegalRequires(String mn, String ignore) throws Exception {179ModuleDescriptor md = newBuilder("m").requires("java.base").requires(mn).build();180ByteBuffer bb = toBuffer(md);181ModuleDescriptor.read(bb); // throws InvalidModuleDescriptorException182}183184@Test(dataProvider = "legalModuleNames")185public void testLegalExports(String mn, String expected) throws Exception {186ModuleDescriptor md = newBuilder("m")187.requires("java.base")188.exports("p", Set.of(mn))189.build();190ByteBuffer bb = toBuffer(md);191ModuleDescriptor descriptor = ModuleDescriptor.read(bb);192Optional<Exports> export = descriptor.exports().stream().findAny();193assertTrue(export.isPresent());194assertTrue(export.get().targets().contains(expected));195}196197@Test(dataProvider = "illegalModuleNames",198expectedExceptions = InvalidModuleDescriptorException.class)199public void testIllegalExports(String mn, String ignore) throws Exception {200ModuleDescriptor md = newBuilder("m")201.requires("java.base")202.exports("p", Set.of(mn))203.build();204ByteBuffer bb = toBuffer(md);205ModuleDescriptor.read(bb); // throws InvalidModuleDescriptorException206}207208@Test(dataProvider = "legalModuleNames")209public void testLegalOpens(String mn, String expected) throws Exception {210ModuleDescriptor md = newBuilder("m")211.requires("java.base")212.opens("p", Set.of(mn))213.build();214ByteBuffer bb = toBuffer(md);215ModuleDescriptor descriptor = ModuleDescriptor.read(bb);216Optional<Opens> opens = descriptor.opens().stream().findAny();217assertTrue(opens.isPresent());218assertTrue(opens.get().targets().contains(expected));219}220221@Test(dataProvider = "illegalModuleNames",222expectedExceptions = InvalidModuleDescriptorException.class)223public void testIllegalOpens(String mn, String ignore) throws Exception {224ModuleDescriptor md = newBuilder("m")225.requires("java.base")226.opens("p", Set.of(mn))227.build();228ByteBuffer bb = toBuffer(md);229ModuleDescriptor.read(bb); // throws InvalidModuleDescriptorException230}231232/**233* Returns a Builder that does not validate module names.234*/235private Builder newBuilder(String mn) {236return SharedSecrets.getJavaLangModuleAccess()237.newModuleBuilder(mn, false, Set.of());238}239240/**241* Returns a {@code ByteBuffer} containing the given module descriptor242* in module-info.class format.243*/244private ByteBuffer toBuffer(ModuleDescriptor descriptor) throws Exception {245ByteArrayOutputStream baos = new ByteArrayOutputStream();246ModuleInfoWriter.write(descriptor, baos);247return ByteBuffer.wrap(baos.toByteArray());248}249250/**251* Returns a string containing a given code point.252*/253private String makeString(String prefix, int codePoint, String suffix) {254StringBuilder sb = new StringBuilder();255sb.append(prefix);256sb.appendCodePoint(codePoint);257sb.append(suffix);258return sb.toString();259}260}261262263