aboutsummaryrefslogtreecommitdiff
path: root/test/java/lang/invoke/lambda/LogGeneratedClassesTest.java
blob: dc34bba24b98d6463d72be127fac2ccba5c71814 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 8023524
 * @summary tests logging generated classes for lambda
 * @library /java/nio/file
 * @run testng LogGeneratedClassesTest
 */
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFileAttributeView;
import java.util.stream.Stream;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.SkipException;

import static java.nio.file.attribute.PosixFilePermissions.*;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

public class LogGeneratedClassesTest extends LUtils {
    String longFQCN;

    @BeforeClass
    public void setup() throws IOException {
        final List<String> scratch = new ArrayList<>();
        scratch.clear();
        scratch.add("package com.example;");
        scratch.add("public class TestLambda {");
        scratch.add("    interface I {");
        scratch.add("        int foo();");
        scratch.add("    }");
        scratch.add("    public static void main(String[] args) {");
        scratch.add("        I lam = () -> 10;");
        scratch.add("        Runnable r = () -> {");
        scratch.add("            System.out.println(\"Runnable\");");
        scratch.add("        };");
        scratch.add("        r.run();");
        scratch.add("        System.out.println(\"Finish\");");
        scratch.add("    }");
        scratch.add("}");

        File test = new File("TestLambda.java");
        createFile(test, scratch);
        compile("-d", ".", test.getName());

        scratch.remove(0);
        scratch.remove(0);
        scratch.add(0, "public class LongPackageName {");
        StringBuilder sb = new StringBuilder("com.example.");
        // longer than 255 which exceed max length of most filesystems
        for (int i = 0; i < 30; i++) {
            sb.append("nonsense.");
        }
        sb.append("enough");
        longFQCN = sb.toString() + ".LongPackageName";
        sb.append(";");
        sb.insert(0, "package ");
        scratch.add(0, sb.toString());
        test = new File("LongPackageName.java");
        createFile(test, scratch);
        compile("-d", ".", test.getName());

        // create target
        Files.createDirectory(Paths.get("dump"));
        Files.createDirectories(Paths.get("dumpLong/com/example/nonsense"));
        Files.createFile(Paths.get("dumpLong/com/example/nonsense/nonsense"));
        Files.createFile(Paths.get("file"));
    }

    @AfterClass
    public void cleanup() throws IOException {
        Files.delete(Paths.get("TestLambda.java"));
        Files.delete(Paths.get("LongPackageName.java"));
        Files.delete(Paths.get("file"));
        TestUtil.removeAll(Paths.get("com"));
        TestUtil.removeAll(Paths.get("dump"));
        TestUtil.removeAll(Paths.get("dumpLong"));
    }

    @Test
    public void testNotLogging() {
        TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
                               "-cp", ".",
                               "-Djava.security.manager",
                               "com.example.TestLambda");
        tr.assertZero("Should still return 0");
    }

    @Test
    public void testLogging() throws IOException {
        assertTrue(Files.exists(Paths.get("dump")));
        TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
                               "-cp", ".",
                               "-Djdk.internal.lambda.dumpProxyClasses=dump",
                               "-Djava.security.manager",
                               "com.example.TestLambda");
        // dump/com/example + 2 class files
        assertEquals(Files.walk(Paths.get("dump")).count(), 5, "Two lambda captured");
        tr.assertZero("Should still return 0");
    }

    @Test
    public void testDumpDirNotExist() throws IOException {
        assertFalse(Files.exists(Paths.get("notExist")));
        TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
                               "-cp", ".",
                               "-Djdk.internal.lambda.dumpProxyClasses=notExist",
                               "-Djava.security.manager",
                               "com.example.TestLambda");
        assertEquals(tr.testOutput.stream()
                                  .filter(s -> s.startsWith("WARNING"))
                                  .peek(s -> assertTrue(s.contains("does not exist")))
                                  .count(),
                     1, "only show error once");
        tr.assertZero("Should still return 0");
    }

    @Test
    public void testDumpDirIsFile() throws IOException {
        assertTrue(Files.isRegularFile(Paths.get("file")));
        TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
                               "-cp", ".",
                               "-Djdk.internal.lambda.dumpProxyClasses=file",
                               "-Djava.security.manager",
                               "com.example.TestLambda");
        assertEquals(tr.testOutput.stream()
                                  .filter(s -> s.startsWith("WARNING"))
                                  .peek(s -> assertTrue(s.contains("not a directory")))
                                  .count(),
                     1, "only show error once");
        tr.assertZero("Should still return 0");
    }

    @Test
    public void testDumpDirNotWritable() throws IOException {
        if (! Files.getFileStore(Paths.get("."))
                   .supportsFileAttributeView(PosixFileAttributeView.class)) {
            // No easy way to setup readonly directory without POSIX
            // We would like to skip the test with a cause with
            //     throw new SkipException("Posix not supported");
            // but jtreg will report failure so we just pass the test
            // which we can look at if jtreg changed its behavior
            return;
        }

        Files.createDirectory(Paths.get("readOnly"),
                              asFileAttribute(fromString("r-xr-xr-x")));

        TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
                               "-cp", ".",
                               "-Djdk.internal.lambda.dumpProxyClasses=readOnly",
                               "-Djava.security.manager",
                               "com.example.TestLambda");
        assertEquals(tr.testOutput.stream()
                                  .filter(s -> s.startsWith("WARNING"))
                                  .peek(s -> assertTrue(s.contains("not writable")))
                                  .count(),
                     1, "only show error once");
        tr.assertZero("Should still return 0");

        TestUtil.removeAll(Paths.get("readOnly"));
    }

    @Test
    public void testLoggingException() throws IOException {
        assertTrue(Files.exists(Paths.get("dumpLong")));
        TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
                               "-cp", ".",
                               "-Djdk.internal.lambda.dumpProxyClasses=dumpLong",
                               "-Djava.security.manager",
                               longFQCN);
        assertEquals(tr.testOutput.stream()
                                  .filter(s -> s.startsWith("WARNING: Exception"))
                                  .count(),
                     2, "show error each capture");
        // dumpLong/com/example/nosense/nosense
        assertEquals(Files.walk(Paths.get("dumpLong")).count(), 5, "Two lambda captured failed to log");
        tr.assertZero("Should still return 0");
    }
}