|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @bug 8341342 |
| 27 | + * @summary Test Elements methods programmatically |
| 28 | + * @modules jdk.compiler |
| 29 | + * @run junit TestElementsProgrammatic |
| 30 | + */ |
| 31 | + |
| 32 | +import com.sun.source.util.JavacTask; |
| 33 | +import java.net.URI; |
| 34 | +import java.util.List; |
| 35 | +import java.util.function.Consumer; |
| 36 | +import javax.tools.JavaCompiler; |
| 37 | +import javax.tools.JavaFileObject; |
| 38 | +import javax.tools.SimpleJavaFileObject; |
| 39 | +import javax.tools.ToolProvider; |
| 40 | + |
| 41 | +import org.junit.jupiter.params.ParameterizedTest; |
| 42 | +import org.junit.jupiter.params.provider.MethodSource; |
| 43 | + |
| 44 | +import static org.junit.jupiter.api.Assertions.*; |
| 45 | + |
| 46 | +/** |
| 47 | + * Programmatically test workings of various methods of Elements. |
| 48 | + */ |
| 49 | +public class TestElementsProgrammatic { |
| 50 | + |
| 51 | + private final JavaCompiler systemCompiler = ToolProvider.getSystemJavaCompiler(); |
| 52 | + |
| 53 | + @ParameterizedTest |
| 54 | + @MethodSource |
| 55 | + public void automaticallyEnter(Consumer<JavacTask> verify) { |
| 56 | + //make sure the get{All,}{Module,Package,Type}Element{s,} methods will automatically enter: |
| 57 | + JavaFileObject input = |
| 58 | + SimpleJavaFileObject.forSource(URI.create("mem://Test.java"), ""); |
| 59 | + List<JavaFileObject> inputs = List.of(input); |
| 60 | + JavacTask task = (JavacTask) systemCompiler.getTask(null, null, null, null, null, inputs); |
| 61 | + verify.accept(task); |
| 62 | + } |
| 63 | + |
| 64 | + private static List<Consumer<JavacTask>> automaticallyEnter() { |
| 65 | + return List.of( |
| 66 | + task -> assertFalse(task.getElements().getAllModuleElements().isEmpty()), |
| 67 | + task -> assertNotNull(task.getElements().getModuleElement("java.base")), |
| 68 | + task -> assertNotNull(task.getElements().getPackageElement("java.lang")), |
| 69 | + task -> assertFalse(task.getElements().getAllPackageElements("java.lang").isEmpty()), |
| 70 | + task -> assertNotNull(task.getElements().getTypeElement("java.lang.Object")), |
| 71 | + task -> assertFalse(task.getElements().getAllTypeElements("java.lang.Object").isEmpty()) |
| 72 | + ); |
| 73 | + } |
| 74 | +} |
0 commit comments