1 /* 2 * Copyright (c) 2013, 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 8023524 27 * @summary tests logging generated classes for lambda 28 * @library /java/nio/file 29 * @run testng LogGeneratedClassesTest 30 */ 31 import java.io.File; 32 import java.io.IOException; 33 import java.util.ArrayList; 34 import java.util.List; 35 import java.nio.file.Files; 36 import java.nio.file.Path; 37 import java.nio.file.Paths; 38 import java.nio.file.LinkOption; 39 import java.util.stream.Stream; 40 41 import org.testng.annotations.AfterClass; 42 import org.testng.annotations.BeforeClass; 43 import org.testng.annotations.Test; 44 45 import static java.nio.file.attribute.PosixFilePermissions.*; 46 import static org.testng.Assert.assertEquals; 47 import static org.testng.Assert.assertFalse; 48 import static org.testng.Assert.assertTrue; 49 50 public class LogGeneratedClassesTest extends LUtils { 51 String longFQCN; 52 53 @BeforeClass 54 public void setup() throws IOException { 55 final List<String> scratch = new ArrayList<>(); 56 scratch.clear(); 57 scratch.add("package com.example;"); 58 scratch.add("public class TestLambda {"); 59 scratch.add(" interface I {"); 60 scratch.add(" int foo();"); 61 scratch.add(" }"); 62 scratch.add(" public static void main(String[] args) {"); 63 scratch.add(" I lam = () -> 10;"); 64 scratch.add(" Runnable r = () -> {"); 65 scratch.add(" System.out.println(\"Runnable\");"); 66 scratch.add(" };"); 67 scratch.add(" r.run();"); 68 scratch.add(" System.out.println(\"Finish\");"); 69 scratch.add(" }"); 70 scratch.add("}"); 71 72 File test = new File("TestLambda.java"); 73 createFile(test, scratch); 74 compile("-d", ".", test.getName()); 75 76 scratch.remove(0); 77 scratch.remove(0); 78 scratch.add(0, "public class LongPackageName {"); 79 StringBuilder sb = new StringBuilder("com.example"); 80 // longer than 255 which exceed max length of most filesystems 81 for (int i = 0; i < 30; i++) { 82 sb.append("nonsense."); 83 } 84 sb.append("enough"); 85 longFQCN = sb.toString() + ".LongPackageName"; 86 sb.append(";"); 87 sb.insert(0, "package "); 88 scratch.add(0, sb.toString()); 89 test = new File("LongPackageName.java"); 90 createFile(test, scratch); 91 compile("-d", ".", test.getName()); 92 93 // create target 94 Files.createDirectory(Paths.get("dump")); 95 Files.createDirectory(Paths.get("dumpLong")); 96 Files.createFile(Paths.get("file")); 97 Files.createDirectory(Paths.get("readOnly"), 98 asFileAttribute(fromString("r-xr-xr-x"))); 99 } 100 101 @AfterClass 102 public void cleanup() throws IOException { 103 Files.delete(Paths.get("TestLambda.java")); 104 Files.delete(Paths.get("LongPackageName.java")); 105 Files.delete(Paths.get("file")); 106 TestUtil.removeAll(Paths.get("com")); 107 TestUtil.removeAll(Paths.get("dump")); 108 TestUtil.removeAll(Paths.get("dumpLong")); 109 TestUtil.removeAll(Paths.get("readOnly")); 110 } 111 112 @Test 113 public void testNotLogging() { 114 TestResult tr = doExec(JAVA_CMD.getAbsolutePath(), 115 "-cp", ".", 116 "com.example.TestLambda"); 117 tr.assertZero("Should still return 0"); 118 } 119 120 @Test 121 public void testLogging() throws IOException { 122 assertTrue(Files.exists(Paths.get("dump"))); 123 TestResult tr = doExec(JAVA_CMD.getAbsolutePath(), 124 "-cp", ".", 125 "-Djdk.internal.lambda.dumpProxyClasses=dump", 126 "com.example.TestLambda"); 127 assertEquals(Files.list(Paths.get("dump")).count(), 2, "Two lambda captured"); 128 tr.assertZero("Should still return 0"); 129 } 130 131 @Test 132 public void testDumpDirNotExist() throws IOException { 133 assertFalse(Files.exists(Paths.get("notExist"))); 134 TestResult tr = doExec(JAVA_CMD.getAbsolutePath(), 135 "-cp", ".", 136 "-Djdk.internal.lambda.dumpProxyClasses=notExist", 137 "com.example.TestLambda"); 138 assertEquals(tr.testOutput.stream() 139 .filter(s -> s.startsWith("WARNING")) 140 .peek(s -> assertTrue(s.endsWith("does not exist"))) 141 .count(), 142 1, "only show error once"); 143 tr.assertZero("Should still return 0"); 144 } 145 146 @Test 147 public void testDumpDirIsFile() throws IOException { 148 assertTrue(Files.isRegularFile(Paths.get("file"))); 149 TestResult tr = doExec(JAVA_CMD.getAbsolutePath(), 150 "-cp", ".", 151 "-Djdk.internal.lambda.dumpProxyClasses=file", 152 "com.example.TestLambda"); 153 assertEquals(tr.testOutput.stream() 154 .filter(s -> s.startsWith("WARNING")) 155 .peek(s -> assertTrue(s.endsWith("not a directory"))) 156 .count(), 157 1, "only show error once"); 158 tr.assertZero("Should still return 0"); 159 } 160 161 @Test 162 public void testDumpDirNotWritable() throws IOException { 163 assertTrue(Files.isRegularFile(Paths.get("file"))); 164 TestResult tr = doExec(JAVA_CMD.getAbsolutePath(), 165 "-cp", ".", 166 "-Djdk.internal.lambda.dumpProxyClasses=readOnly", 167 "com.example.TestLambda"); 168 assertEquals(tr.testOutput.stream() 169 .filter(s -> s.startsWith("WARNING")) 170 .peek(s -> assertTrue(s.endsWith("not writable"))) 171 .count(), 172 1, "only show error once"); 173 tr.assertZero("Should still return 0"); 174 } 175 176 @Test 177 public void testLoggingException() throws IOException { 178 assertTrue(Files.exists(Paths.get("dumpLong"))); 179 TestResult tr = doExec(JAVA_CMD.getAbsolutePath(), 180 "-cp", ".", 181 "-Djdk.internal.lambda.dumpProxyClasses=dumpLong", 182 longFQCN); 183 assertEquals(tr.testOutput.stream() 184 .filter(s -> s.startsWith("WARNING: Exception")) 185 .count(), 186 2, "show error each capture"); 187 assertEquals(Files.list(Paths.get("dumpLong")).count(), 0, "Two lambda captured failed to log"); 188 tr.assertZero("Should still return 0"); 189 } 190 }