1/*
2 * Copyright (c) 2013, 2017, 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 * @modules jdk.compiler
30 *          jdk.zipfs
31 * @run testng LogGeneratedClassesTest
32 */
33import java.io.File;
34import java.io.IOException;
35import java.util.ArrayList;
36import java.util.List;
37import java.util.function.Predicate;
38import java.nio.file.Files;
39import java.nio.file.Path;
40import java.nio.file.Paths;
41import java.nio.file.attribute.PosixFileAttributeView;
42
43import org.testng.annotations.AfterClass;
44import org.testng.annotations.BeforeClass;
45import org.testng.annotations.Test;
46import org.testng.SkipException;
47
48import static java.nio.file.attribute.PosixFilePermissions.*;
49import static org.testng.Assert.assertEquals;
50import static org.testng.Assert.assertFalse;
51import static org.testng.Assert.assertTrue;
52
53public class LogGeneratedClassesTest extends LUtils {
54    String longFQCN;
55
56    @BeforeClass
57    public void setup() throws IOException {
58        final List<String> scratch = new ArrayList<>();
59        scratch.clear();
60        scratch.add("package com.example;");
61        scratch.add("public class TestLambda {");
62        scratch.add("    interface I {");
63        scratch.add("        int foo();");
64        scratch.add("    }");
65        scratch.add("    public static void main(String[] args) {");
66        scratch.add("        System.setSecurityManager(new SecurityManager());");
67        scratch.add("        I lam = () -> 10;");
68        scratch.add("        Runnable r = () -> {");
69        scratch.add("            System.out.println(\"Runnable\");");
70        scratch.add("        };");
71        scratch.add("        r.run();");
72        scratch.add("        System.out.println(\"Finish\");");
73        scratch.add("    }");
74        scratch.add("}");
75
76        File test = new File("TestLambda.java");
77        createFile(test, scratch);
78        compile("-d", ".", test.getName());
79
80        scratch.remove(0);
81        scratch.remove(0);
82        scratch.add(0, "public class LongPackageName {");
83        StringBuilder sb = new StringBuilder("com.example.");
84        // longer than 255 which exceed max length of most filesystems
85        for (int i = 0; i < 30; i++) {
86            sb.append("nonsense.");
87        }
88        sb.append("enough");
89        longFQCN = sb.toString() + ".LongPackageName";
90        sb.append(";");
91        sb.insert(0, "package ");
92        scratch.add(0, sb.toString());
93        test = new File("LongPackageName.java");
94        createFile(test, scratch);
95        compile("-d", ".", test.getName());
96
97        // create target
98        Files.createDirectory(Paths.get("dump"));
99        Files.createDirectories(Paths.get("dumpLong/com/example/nonsense"));
100        Files.createFile(Paths.get("dumpLong/com/example/nonsense/nonsense"));
101        Files.createFile(Paths.get("file"));
102    }
103
104    @AfterClass
105    public void cleanup() throws IOException {
106        Files.delete(Paths.get("TestLambda.java"));
107        Files.delete(Paths.get("LongPackageName.java"));
108        Files.delete(Paths.get("file"));
109        TestUtil.removeAll(Paths.get("com"));
110        TestUtil.removeAll(Paths.get("dump"));
111        TestUtil.removeAll(Paths.get("dumpLong"));
112    }
113
114    @Test
115    public void testNotLogging() {
116        TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
117                               "-cp", ".",
118                               "com.example.TestLambda");
119        tr.assertZero("Should still return 0");
120    }
121
122    @Test
123    public void testLogging() throws IOException {
124        assertTrue(Files.exists(Paths.get("dump")));
125        TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
126                               "-cp", ".",
127                               "-Djdk.internal.lambda.dumpProxyClasses=dump",
128                               "com.example.TestLambda");
129        // 2 our own class files. We don't care about the others
130        assertEquals(Files.find(
131                        Paths.get("dump"),
132                        99,
133                        (p, a) -> p.startsWith(Paths.get("dump/com/example"))
134                                && a.isRegularFile()).count(),
135                2, "Two lambda captured");
136        tr.assertZero("Should still return 0");
137    }
138
139    @Test
140    public void testDumpDirNotExist() throws IOException {
141        assertFalse(Files.exists(Paths.get("notExist")));
142        TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
143                               "-cp", ".",
144                               "-Djdk.internal.lambda.dumpProxyClasses=notExist",
145                               "com.example.TestLambda");
146        assertEquals(tr.testOutput.stream()
147                                  .filter(s -> s.startsWith("WARNING"))
148                                  .peek(s -> assertTrue(s.contains("does not exist")))
149                                  .count(),
150                     1, "only show error once");
151        tr.assertZero("Should still return 0");
152    }
153
154    @Test
155    public void testDumpDirIsFile() throws IOException {
156        assertTrue(Files.isRegularFile(Paths.get("file")));
157        TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
158                               "-cp", ".",
159                               "-Djdk.internal.lambda.dumpProxyClasses=file",
160                               "com.example.TestLambda");
161        assertEquals(tr.testOutput.stream()
162                                  .filter(s -> s.startsWith("WARNING"))
163                                  .peek(s -> assertTrue(s.contains("not a directory")))
164                                  .count(),
165                     1, "only show error once");
166        tr.assertZero("Should still return 0");
167    }
168
169    private static boolean isWriteableDirectory(Path p) {
170        if (!Files.isDirectory(p)) {
171            return false;
172        }
173        Path test = p.resolve(Paths.get("test"));
174        try {
175            Files.createFile(test);
176            assertTrue(Files.exists(test));
177            return true;
178        } catch (IOException e) {
179            assertFalse(Files.exists(test));
180            return false;
181        } finally {
182            if (Files.exists(test)) {
183                try {
184                    Files.delete(test);
185                } catch (IOException e) {
186                    throw new Error(e);
187                }
188            }
189        }
190    }
191
192    @Test
193    public void testDumpDirNotWritable() throws IOException {
194        if (!Files.getFileStore(Paths.get("."))
195                  .supportsFileAttributeView(PosixFileAttributeView.class)) {
196            // No easy way to setup readonly directory without POSIX
197            // We would like to skip the test with a cause with
198            //     throw new SkipException("Posix not supported");
199            // but jtreg will report failure so we just pass the test
200            // which we can look at if jtreg changed its behavior
201            System.out.println("WARNING: POSIX is not supported. Skipping testDumpDirNotWritable test.");
202            return;
203        }
204
205        Files.createDirectory(Paths.get("readOnly"),
206                              asFileAttribute(fromString("r-xr-xr-x")));
207        try {
208            if (isWriteableDirectory(Paths.get("readOnly"))) {
209                // Skipping the test: it's allowed to write into read-only directory
210                // (e.g. current user is super user).
211                System.out.println("WARNING: readOnly directory is writeable. Skipping testDumpDirNotWritable test.");
212                return;
213            }
214
215            TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
216                                   "-cp", ".",
217                                   "-Djdk.internal.lambda.dumpProxyClasses=readOnly",
218                                   "com.example.TestLambda");
219            assertEquals(tr.testOutput.stream()
220                                      .filter(s -> s.startsWith("WARNING"))
221                                      .peek(s -> assertTrue(s.contains("not writable")))
222                                      .count(),
223                         1, "only show error once");
224            tr.assertZero("Should still return 0");
225        } finally {
226            TestUtil.removeAll(Paths.get("readOnly"));
227        }
228    }
229
230    @Test
231    public void testLoggingException() throws IOException {
232        assertTrue(Files.exists(Paths.get("dumpLong")));
233        TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
234                               "-cp", ".",
235                               "-Djdk.internal.lambda.dumpProxyClasses=dumpLong",
236                               longFQCN);
237        assertEquals(tr.testOutput.stream()
238                                  .filter(s -> s.startsWith("WARNING: Exception"))
239                                  .count(),
240                     2, "show error each capture");
241        // dumpLong/com/example/nonsense/nonsense
242        Path dumpPath = Paths.get("dumpLong/com/example/nonsense");
243        Predicate<Path> filter = p -> p.getParent() == null || dumpPath.startsWith(p) || p.startsWith(dumpPath);
244        boolean debug = true;
245        if (debug) {
246           Files.walk(Paths.get("dumpLong"))
247                .forEachOrdered(p -> {
248                    if (filter.test(p)) {
249                        System.out.println("accepted: " + p.toString());
250                    } else {
251                        System.out.println("filetered out: " + p.toString());
252                    }
253                 });
254        }
255        assertEquals(Files.walk(Paths.get("dumpLong"))
256                .filter(filter)
257                .count(), 5, "Two lambda captured failed to log");
258        tr.assertZero("Should still return 0");
259    }
260}
261