Path: blob/master/test/jdk/java/lang/Class/forName/NonJavaNames.java
41153 views
/*1* Copyright (c) 2003, 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*/2223import java.io.IOException;24import java.nio.file.Files;25import java.nio.file.Path;2627import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;28import org.testng.annotations.BeforeClass;29import org.testng.annotations.DataProvider;30import org.testng.annotations.Test;3132/*33* @test34* @bug 495255835* @library /test/lib36* @run testng/othervm NonJavaNames37* @summary Verify names that aren't legal Java names are accepted by forName.38*/3940public class NonJavaNames {41public static class Baz {42public Baz(){}43}4445public static interface myInterface {46}4748NonJavaNames.myInterface create() {49// With target 1.5, this class's name will include a '+'50// instead of a '$'.51class Baz2 implements NonJavaNames.myInterface {52public Baz2() { }53}5455return new Baz2();56}5758private static final String SRC_DIR = System.getProperty("test.src");59private static final Path TEST_SRC = Path.of(SRC_DIR, "classes");60private static final Path TEST_CLASSES = Path.of(System.getProperty("test.classes", "."));6162@BeforeClass63public void createInvalidNameClasses() throws IOException {64Path hyphenPath = TEST_SRC.resolve("hyphen.class");65Path commaPath = TEST_SRC.resolve("comma.class");66Path periodPath = TEST_SRC.resolve("period.class");67Path leftsquarePath = TEST_SRC.resolve("left-square.class");68Path rightsquarePath = TEST_SRC.resolve("right-square.class");69Path plusPath = TEST_SRC.resolve("plus.class");70Path semicolonPath = TEST_SRC.resolve("semicolon.class");71Path zeroPath = TEST_SRC.resolve("0.class");72Path threePath = TEST_SRC.resolve("3.class");73Path zadePath = TEST_SRC.resolve("Z.class");7475Path dhyphenPath = TEST_CLASSES.resolve("-.class");76Path dcommaPath = TEST_CLASSES.resolve(",.class");77Path dperiodPath = TEST_CLASSES.resolve("..class");78Path dleftsquarePath = TEST_CLASSES.resolve("[.class");79Path drightsquarePath = TEST_CLASSES.resolve("].class");80Path dplusPath = TEST_CLASSES.resolve("+.class");81Path dsemicolonPath = TEST_CLASSES.resolve(";.class");82Path dzeroPath = TEST_CLASSES.resolve("0.class");83Path dthreePath = TEST_CLASSES.resolve("3.class");84Path dzadePath = TEST_CLASSES.resolve("Z.class");8586Files.copy(hyphenPath, dhyphenPath, REPLACE_EXISTING);87Files.copy(commaPath, dcommaPath, REPLACE_EXISTING);88Files.copy(periodPath, dperiodPath, REPLACE_EXISTING);89Files.copy(leftsquarePath, dleftsquarePath, REPLACE_EXISTING);90Files.copy(rightsquarePath, drightsquarePath, REPLACE_EXISTING);91Files.copy(plusPath, dplusPath, REPLACE_EXISTING);92Files.copy(semicolonPath, dsemicolonPath, REPLACE_EXISTING);93Files.copy(zeroPath, dzeroPath, REPLACE_EXISTING);94Files.copy(threePath, dthreePath, REPLACE_EXISTING);95Files.copy(zadePath, dzadePath, REPLACE_EXISTING);96}9798@Test99public void testForNameReturnsSameClass() throws ClassNotFoundException {100NonJavaNames.Baz bz = new NonJavaNames.Baz();101String name;102103if (Class.forName(name=bz.getClass().getName()) != NonJavaNames.Baz.class) {104System.err.println("Class object from forName does not match object.class.");105System.err.println("Failures for class ``" + name + "''.");106throw new RuntimeException();107}108109NonJavaNames.myInterface bz2 = (new NonJavaNames()).create();110if (Class.forName(name=bz2.getClass().getName()) != bz2.getClass()) {111System.err.println("Class object from forName does not match getClass.");112System.err.println("Failures for class ``" + name + "''.");113throw new RuntimeException();114}115}116117@Test(dataProvider = "goodNonJavaClassNames")118public void testGoodNonJavaClassNames(String name) throws ClassNotFoundException {119System.out.println("Testing good class name ``" + name + "''");120Class.forName(name);121}122123@Test(dataProvider = "badNonJavaClassNames")124public void testBadNonJavaClassNames(String name) {125System.out.println("Testing bad class name ``" + name + "''");126try {127Class.forName(name);128} catch (ClassNotFoundException e) {129// Expected behavior130return;131}132throw new RuntimeException("Bad class name ``" + name + "'' accepted.");133}134135@DataProvider(name = "goodNonJavaClassNames")136Object[][] getGoodNonJavaClassNames() {137return new Object[][] {138{","},139{"+"},140{"-"},141{"0"},142{"3"},143// ":", These names won't work under windows.144// "<",145// ">",146{"Z"},147{"]"}148};149}150151@DataProvider(name = "badNonJavaClassNames")152Object[][] getBadNonJavaClassNames() {153return new Object[][] {154{";"},155{"["},156{"."}157};158}159}160161162