Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/lang/invoke/lambda/LogGeneratedClassesTest.java
48795 views
/*1* Copyright (c) 2013, 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* @bug 802352426* @summary tests logging generated classes for lambda27* @library /java/nio/file28* @run testng LogGeneratedClassesTest29*/30import java.io.File;31import java.io.IOException;32import java.util.ArrayList;33import java.util.List;34import java.nio.file.Files;35import java.nio.file.Path;36import java.nio.file.Paths;37import java.nio.file.attribute.PosixFileAttributeView;3839import org.testng.annotations.AfterClass;40import org.testng.annotations.BeforeClass;41import org.testng.annotations.Test;42import org.testng.SkipException;4344import static java.nio.file.attribute.PosixFilePermissions.*;45import static org.testng.Assert.assertEquals;46import static org.testng.Assert.assertFalse;47import static org.testng.Assert.assertTrue;4849public class LogGeneratedClassesTest extends LUtils {50String longFQCN;5152@BeforeClass53public void setup() throws IOException {54final List<String> scratch = new ArrayList<>();55scratch.clear();56scratch.add("package com.example;");57scratch.add("public class TestLambda {");58scratch.add(" interface I {");59scratch.add(" int foo();");60scratch.add(" }");61scratch.add(" public static void main(String[] args) {");62scratch.add(" I lam = () -> 10;");63scratch.add(" Runnable r = () -> {");64scratch.add(" System.out.println(\"Runnable\");");65scratch.add(" };");66scratch.add(" r.run();");67scratch.add(" System.out.println(\"Finish\");");68scratch.add(" }");69scratch.add("}");7071File test = new File("TestLambda.java");72createFile(test, scratch);73compile("-d", ".", test.getName());7475scratch.remove(0);76scratch.remove(0);77scratch.add(0, "public class LongPackageName {");78StringBuilder sb = new StringBuilder("com.example.");79// longer than 255 which exceed max length of most filesystems80for (int i = 0; i < 30; i++) {81sb.append("nonsense.");82}83sb.append("enough");84longFQCN = sb.toString() + ".LongPackageName";85sb.append(";");86sb.insert(0, "package ");87scratch.add(0, sb.toString());88test = new File("LongPackageName.java");89createFile(test, scratch);90compile("-d", ".", test.getName());9192// create target93Files.createDirectory(Paths.get("dump"));94Files.createDirectories(Paths.get("dumpLong/com/example/nonsense"));95Files.createFile(Paths.get("dumpLong/com/example/nonsense/nonsense"));96Files.createFile(Paths.get("file"));97}9899@AfterClass100public void cleanup() throws IOException {101Files.delete(Paths.get("TestLambda.java"));102Files.delete(Paths.get("LongPackageName.java"));103Files.delete(Paths.get("file"));104TestUtil.removeAll(Paths.get("com"));105TestUtil.removeAll(Paths.get("dump"));106TestUtil.removeAll(Paths.get("dumpLong"));107}108109@Test110public void testNotLogging() {111TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),112"-cp", ".",113"-Djava.security.manager",114"com.example.TestLambda");115tr.assertZero("Should still return 0");116}117118@Test119public void testLogging() throws IOException {120assertTrue(Files.exists(Paths.get("dump")));121TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),122"-cp", ".",123"-Djdk.internal.lambda.dumpProxyClasses=dump",124"-Djava.security.manager",125"com.example.TestLambda");126// dump/com/example + 2 class files127assertEquals(Files.walk(Paths.get("dump")).count(), 5, "Two lambda captured");128tr.assertZero("Should still return 0");129}130131@Test132public void testDumpDirNotExist() throws IOException {133assertFalse(Files.exists(Paths.get("notExist")));134TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),135"-cp", ".",136"-Djdk.internal.lambda.dumpProxyClasses=notExist",137"-Djava.security.manager",138"com.example.TestLambda");139assertEquals(tr.testOutput.stream()140.filter(s -> s.startsWith("WARNING"))141.peek(s -> assertTrue(s.contains("does not exist")))142.count(),1431, "only show error once");144tr.assertZero("Should still return 0");145}146147@Test148public void testDumpDirIsFile() throws IOException {149assertTrue(Files.isRegularFile(Paths.get("file")));150TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),151"-cp", ".",152"-Djdk.internal.lambda.dumpProxyClasses=file",153"-Djava.security.manager",154"com.example.TestLambda");155assertEquals(tr.testOutput.stream()156.filter(s -> s.startsWith("WARNING"))157.peek(s -> assertTrue(s.contains("not a directory")))158.count(),1591, "only show error once");160tr.assertZero("Should still return 0");161}162163private static boolean isWriteableDirectory(Path p) {164if (!Files.isDirectory(p)) {165return false;166}167Path test = p.resolve(Paths.get("test"));168try {169Files.createFile(test);170assertTrue(Files.exists(test));171return true;172} catch (IOException e) {173assertFalse(Files.exists(test));174return false;175} finally {176if (Files.exists(test)) {177try {178Files.delete(test);179} catch (IOException e) {180throw new Error(e);181}182}183}184}185186@Test187public void testDumpDirNotWritable() throws IOException {188if (!Files.getFileStore(Paths.get("."))189.supportsFileAttributeView(PosixFileAttributeView.class)) {190// No easy way to setup readonly directory without POSIX191// We would like to skip the test with a cause with192// throw new SkipException("Posix not supported");193// but jtreg will report failure so we just pass the test194// which we can look at if jtreg changed its behavior195System.out.println("WARNING: POSIX is not supported. Skipping testDumpDirNotWritable test.");196return;197}198199Files.createDirectory(Paths.get("readOnly"),200asFileAttribute(fromString("r-xr-xr-x")));201try {202if (isWriteableDirectory(Paths.get("readOnly"))) {203// Skipping the test: it's allowed to write into read-only directory204// (e.g. current user is super user).205System.out.println("WARNING: readOnly directory is writeable. Skipping testDumpDirNotWritable test.");206return;207}208209TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),210"-cp", ".",211"-Djdk.internal.lambda.dumpProxyClasses=readOnly",212"-Djava.security.manager",213"com.example.TestLambda");214assertEquals(tr.testOutput.stream()215.filter(s -> s.startsWith("WARNING"))216.peek(s -> assertTrue(s.contains("not writable")))217.count(),2181, "only show error once");219tr.assertZero("Should still return 0");220} finally {221TestUtil.removeAll(Paths.get("readOnly"));222}223}224225@Test226public void testLoggingException() throws IOException {227assertTrue(Files.exists(Paths.get("dumpLong")));228TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),229"-cp", ".",230"-Djdk.internal.lambda.dumpProxyClasses=dumpLong",231"-Djava.security.manager",232longFQCN);233assertEquals(tr.testOutput.stream()234.filter(s -> s.startsWith("WARNING: Exception"))235.count(),2362, "show error each capture");237// dumpLong/com/example/nosense/nosense238assertEquals(Files.walk(Paths.get("dumpLong")).count(), 5, "Two lambda captured failed to log");239tr.assertZero("Should still return 0");240}241}242243244