LogGeneratedClassesTest.java revision 10022:0d9f2bdf6dc9
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 */
31import java.io.File;
32import java.io.IOException;
33import java.util.ArrayList;
34import java.util.List;
35import java.nio.file.Files;
36import java.nio.file.Path;
37import java.nio.file.Paths;
38import java.nio.file.attribute.PosixFileAttributeView;
39
40import org.testng.annotations.AfterClass;
41import org.testng.annotations.BeforeClass;
42import org.testng.annotations.Test;
43import org.testng.SkipException;
44
45import static java.nio.file.attribute.PosixFilePermissions.*;
46import static org.testng.Assert.assertEquals;
47import static org.testng.Assert.assertFalse;
48import static org.testng.Assert.assertTrue;
49
50public 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.createDirectories(Paths.get("dumpLong/com/example/nonsense"));
96        Files.createFile(Paths.get("dumpLong/com/example/nonsense/nonsense"));
97        Files.createFile(Paths.get("file"));
98    }
99
100    @AfterClass
101    public void cleanup() throws IOException {
102        Files.delete(Paths.get("TestLambda.java"));
103        Files.delete(Paths.get("LongPackageName.java"));
104        Files.delete(Paths.get("file"));
105        TestUtil.removeAll(Paths.get("com"));
106        TestUtil.removeAll(Paths.get("dump"));
107        TestUtil.removeAll(Paths.get("dumpLong"));
108    }
109
110    @Test
111    public void testNotLogging() {
112        TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
113                               "-cp", ".",
114                               "-Djava.security.manager",
115                               "com.example.TestLambda");
116        tr.assertZero("Should still return 0");
117    }
118
119    @Test
120    public void testLogging() throws IOException {
121        assertTrue(Files.exists(Paths.get("dump")));
122        TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
123                               "-cp", ".",
124                               "-Djdk.internal.lambda.dumpProxyClasses=dump",
125                               "-Djava.security.manager",
126                               "com.example.TestLambda");
127        // dump/com/example + 2 class files
128        assertEquals(Files.walk(Paths.get("dump")).count(), 5, "Two lambda captured");
129        tr.assertZero("Should still return 0");
130    }
131
132    @Test
133    public void testDumpDirNotExist() throws IOException {
134        assertFalse(Files.exists(Paths.get("notExist")));
135        TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
136                               "-cp", ".",
137                               "-Djdk.internal.lambda.dumpProxyClasses=notExist",
138                               "-Djava.security.manager",
139                               "com.example.TestLambda");
140        assertEquals(tr.testOutput.stream()
141                                  .filter(s -> s.startsWith("WARNING"))
142                                  .peek(s -> assertTrue(s.contains("does not exist")))
143                                  .count(),
144                     1, "only show error once");
145        tr.assertZero("Should still return 0");
146    }
147
148    @Test
149    public void testDumpDirIsFile() throws IOException {
150        assertTrue(Files.isRegularFile(Paths.get("file")));
151        TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
152                               "-cp", ".",
153                               "-Djdk.internal.lambda.dumpProxyClasses=file",
154                               "-Djava.security.manager",
155                               "com.example.TestLambda");
156        assertEquals(tr.testOutput.stream()
157                                  .filter(s -> s.startsWith("WARNING"))
158                                  .peek(s -> assertTrue(s.contains("not a directory")))
159                                  .count(),
160                     1, "only show error once");
161        tr.assertZero("Should still return 0");
162    }
163
164    private static boolean isWriteableDirectory(Path p) {
165        if (!Files.isDirectory(p)) {
166            return false;
167        }
168        Path test = p.resolve(Paths.get("test"));
169        try {
170            Files.createFile(test);
171            assertTrue(Files.exists(test));
172            return true;
173        } catch (IOException e) {
174            assertFalse(Files.exists(test));
175            return false;
176        } finally {
177            if (Files.exists(test)) {
178                try {
179                    Files.delete(test);
180                } catch (IOException e) {
181                    throw new Error(e);
182                }
183            }
184        }
185    }
186
187    @Test
188    public void testDumpDirNotWritable() throws IOException {
189        if (!Files.getFileStore(Paths.get("."))
190                  .supportsFileAttributeView(PosixFileAttributeView.class)) {
191            // No easy way to setup readonly directory without POSIX
192            // We would like to skip the test with a cause with
193            //     throw new SkipException("Posix not supported");
194            // but jtreg will report failure so we just pass the test
195            // which we can look at if jtreg changed its behavior
196            System.out.println("WARNING: POSIX is not supported. Skipping testDumpDirNotWritable test.");
197            return;
198        }
199
200        Files.createDirectory(Paths.get("readOnly"),
201                              asFileAttribute(fromString("r-xr-xr-x")));
202        try {
203            if (isWriteableDirectory(Paths.get("readOnly"))) {
204                // Skipping the test: it's allowed to write into read-only directory
205                // (e.g. current user is super user).
206                System.out.println("WARNING: readOnly directory is writeable. Skipping testDumpDirNotWritable test.");
207                return;
208            }
209
210            TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
211                                   "-cp", ".",
212                                   "-Djdk.internal.lambda.dumpProxyClasses=readOnly",
213                                   "-Djava.security.manager",
214                                   "com.example.TestLambda");
215            assertEquals(tr.testOutput.stream()
216                                      .filter(s -> s.startsWith("WARNING"))
217                                      .peek(s -> assertTrue(s.contains("not writable")))
218                                      .count(),
219                         1, "only show error once");
220            tr.assertZero("Should still return 0");
221        } finally {
222            TestUtil.removeAll(Paths.get("readOnly"));
223        }
224    }
225
226    @Test
227    public void testLoggingException() throws IOException {
228        assertTrue(Files.exists(Paths.get("dumpLong")));
229        TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
230                               "-cp", ".",
231                               "-Djdk.internal.lambda.dumpProxyClasses=dumpLong",
232                               "-Djava.security.manager",
233                               longFQCN);
234        assertEquals(tr.testOutput.stream()
235                                  .filter(s -> s.startsWith("WARNING: Exception"))
236                                  .count(),
237                     2, "show error each capture");
238        // dumpLong/com/example/nosense/nosense
239        assertEquals(Files.walk(Paths.get("dumpLong")).count(), 5, "Two lambda captured failed to log");
240        tr.assertZero("Should still return 0");
241    }
242}
243