jdk-24/test/langtools/tools/EnumAccessible.java

125 lines
4.1 KiB
Java

/*
* Copyright (c) 2022, 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
* @bug 8178701
* @summary Check javac can generate code for protected enums accessible through subclassing.
* @library /tools/lib
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
* jdk.compiler/com.sun.tools.javac.util
* @build toolbox.ToolBox toolbox.JavacTask
* @run main EnumAccessible
*/
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;
import toolbox.TestRunner;
import toolbox.JavacTask;
import toolbox.JavaTask;
import toolbox.Task;
import toolbox.ToolBox;
public class EnumAccessible extends TestRunner {
ToolBox tb;
public static void main(String... args) throws Exception {
new EnumAccessible().runTests();
}
EnumAccessible() {
super(System.err);
tb = new ToolBox();
}
public void runTests() throws Exception {
runTests(m -> new Object[] { Paths.get(m.getName()) });
}
@Test
public void testPattern(Path base) throws Exception {
Path current = base.resolve(".");
Path src = current.resolve("src");
Path classes = current.resolve("classes");
tb.writeJavaFiles(src,
"""
package foo;
import bar.D.E;
public class A {
public static class B {
protected enum C {
X, Y, Z
}
}
public static void main(String... args) {
new E().run(B.C.X);
}
}
""",
"""
package bar;
import foo.A.B;
public class D {
public static class E extends B {
public void run(C arg) {
switch (arg) {
default: System.out.println("OK");
}
}
}
}
""");
Files.createDirectories(classes);
new JavacTask(tb)
.options("-doe")
.outdir(classes)
.files(tb.findJavaFiles(src))
.run(Task.Expect.SUCCESS)
.writeAll();
var out = new JavaTask(tb)
.classpath(classes.toString())
.className("foo.A")
.run()
.writeAll()
.getOutputLines(Task.OutputKind.STDOUT);
var expectedOut = List.of("OK");
if (!Objects.equals(expectedOut, out)) {
throw new AssertionError("Incorrect Output, expected: " + expectedOut +
", actual: " + out);
}
}
}