File: NumArgsTest.java

package info (click to toggle)
libnb-javaparser-java 9%2B2018-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 65,172 kB
  • sloc: java: 440,096; xml: 6,359; sh: 865; makefile: 314
file content (268 lines) | stat: -rw-r--r-- 9,934 bytes parent folder | download | duplicates (18)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 * Copyright (c) 2013, 2015, 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.
 */

import com.sun.tools.javac.api.*;
import com.sun.tools.javac.file.*;
import java.io.*;
import java.util.*;
import javax.tools.*;

// More general parameter limit testing framework, and designed so
// that it could be expanded into a general limits-testing framework
// in the future.
public class NumArgsTest {

    private static final NumArgsTest.NestingDef[] NO_NESTING = {};

    // threshold is named as such because "threshold" args is expected
    // to pass, and "threshold" + 1 args is expected to fail.
    private final int threshold;
    private final boolean isStaticMethod;
    private final String result;
    private final String testName;
    private final String methodName;
    private final NestingDef[] nesting;
    private final File testdir;
    private final JavacTool tool = JavacTool.create();
    private final JavacFileManager fm =
        tool.getStandardFileManager(null, null, null);
    private int errors = 0;

    public NumArgsTest(final int threshold,
                       final boolean isStaticMethod,
                       final String result,
                       final String methodName,
                       final String testName,
                       final NestingDef[] nesting) {
        this.threshold = threshold;
        this.isStaticMethod = isStaticMethod;
        this.result = result;
        this.methodName = methodName;
        this.testName = testName;
        this.nesting = nesting;
        testdir = new File(testName);
        testdir.mkdir();
    }

    public NumArgsTest(final int threshold,
                       final boolean isStaticMethod,
                       final String result,
                       final String methodName,
                       final String testName) {
        this(threshold, isStaticMethod, result, methodName,
             testName, NO_NESTING);
    }

    public NumArgsTest(final int threshold,
                       final String result,
                       final String methodName,
                       final String testName,
                       final NestingDef[] nesting) {
        this(threshold, false, result, methodName, testName, nesting);
    }

    public NumArgsTest(final int threshold,
                       final String result,
                       final String methodName,
                       final String testName) {
        this(threshold, false, result, methodName, testName, NO_NESTING);
    }

    public NumArgsTest(final int threshold,
                       final String testName,
                       final NestingDef[] nesting) {
        this(threshold, null, null, testName, nesting);
    }

    public NumArgsTest(final int threshold,
                       final String testName) {
        this(threshold, null, null, testName, NO_NESTING);
    }

    public NumArgsTest(final int threshold,
                       final String testName,
                       final String constructorName,
                       final NestingDef[] nesting) {
        this(threshold, null, constructorName, testName, nesting);
    }

    protected void writeArgs(final int num, final PrintWriter stream)
        throws IOException {
        stream.print("int x1");
        for(int i = 1; i < num; i++)
            stream.print(", int x" + (i + 1));
    }

    protected void writeMethod(final int num,
                               final String name,
                               final PrintWriter stream)
        throws IOException {
        stream.write("public ");
        if (isStaticMethod) stream.write("static ");
        if (result == null)
            stream.write("");
        else {
            stream.write(result);
            stream.write(" ");
        }
        stream.write(name);
        stream.write("(");
        writeArgs(num, stream);
        stream.write(") {}\n");
    }

    protected void writeJavaFile(final int num,
                                 final boolean pass,
                                 final PrintWriter stream)
        throws IOException {
        final String fullName = testName + (pass ? "Pass" : "Fail");
        stream.write("public class ");
        stream.write(fullName);
        stream.write(" {\n");
        for(int i = 0; i < nesting.length; i++)
            nesting[i].writeBefore(stream);
        if (null == methodName)
            writeMethod(num, fullName, stream);
        else
            writeMethod(num, methodName, stream);
        for(int i = nesting.length - 1; i >= 0; i--)
            nesting[i].writeAfter(stream);
        stream.write("}\n");
    }

    public void runTest() throws Exception {
        // Run the pass test
        final String passTestName = testName + "Pass.java";
        final StringWriter passBody = new StringWriter();
        final PrintWriter passStream = new PrintWriter(passBody);
        final File passFile = new File(testdir, passTestName);
        final FileWriter passWriter = new FileWriter(passFile);

        writeJavaFile(threshold, true, passStream);
        passStream.close();
        passWriter.write(passBody.toString());
        passWriter.close();

        final StringWriter passSW = new StringWriter();
        final String[] passArgs = { passFile.toString() };
        final Iterable<? extends JavaFileObject> passFiles =
            fm.getJavaFileObjectsFromFiles(Arrays.asList(passFile));
        final JavaCompiler.CompilationTask passTask =
            tool.getTask(passSW, fm, null, null, null, passFiles);

        if (!passTask.call()) {
            errors++;
            System.err.println("Compilation unexpectedly failed. Body:\n" +
                               passBody);
            System.err.println("Output:\n" + passSW.toString());
        }

        // Run the fail test
        final String failTestName = testName + "Fail.java";
        final StringWriter failBody = new StringWriter();
        final PrintWriter failStream = new PrintWriter(failBody);
        final File failFile = new File(testdir, failTestName);
        final FileWriter failWriter = new FileWriter(failFile);

        writeJavaFile(threshold + 1, false, failStream);
        failStream.close();
        failWriter.write(failBody.toString());
        failWriter.close();

        final StringWriter failSW = new StringWriter();
        final TestDiagnosticHandler failDiag =
            new TestDiagnosticHandler("compiler.err.limit.parameters");
        final Iterable<? extends JavaFileObject> failFiles =
            fm.getJavaFileObjectsFromFiles(Arrays.asList(failFile));
        final JavaCompiler.CompilationTask failTask =
            tool.getTask(failSW,
                         tool.getStandardFileManager(null, null, null),
                         failDiag,
                         null,
                         null,
                         failFiles);

        if (failTask.call()) {
            errors++;
            System.err.println("Compilation unexpectedly succeeded.");
            System.err.println("Input:\n" + failBody);
        }

        if (!failDiag.sawError) {
            errors++;
            System.err.println("Did not see expected compile error.");
        }

        if (errors != 0)
            throw new RuntimeException("Test failed with " +
                                       errors + " errors");
    }

    public static NestingDef classNesting(final String name) {
        return new NestedClassBuilder(name, false);
    }

    public static NestingDef classNesting(final String name,
                                          final boolean isStatic) {
        return new NestedClassBuilder(name, isStatic);
    }

    protected interface NestingDef {
        public abstract void writeBefore(final PrintWriter stream);
        public abstract void writeAfter(final PrintWriter stream);
    }

    private static class NestedClassBuilder implements NestingDef {
        private final String name;
        private final boolean isStatic;
        public NestedClassBuilder(final String name, final boolean isStatic) {
            this.name = name;
            this.isStatic = isStatic;
        }
        public void writeBefore(final PrintWriter stream) {
            stream.write("public ");
            if (isStatic) stream.write("static");
            stream.write(" class ");
            stream.write(name);
            stream.write(" {\n");
        }
        public void writeAfter(final PrintWriter stream) {
            stream.write("}\n");
        }
    }

    public class TestDiagnosticHandler<T> implements DiagnosticListener<T> {
        public boolean sawError;
        public final String target;

        public TestDiagnosticHandler(final String target) {
            this.target = target;
        }

        public void report(final Diagnostic<? extends T> diag) {
            if (diag.getCode().equals(target))
                sawError = true;
        }
    }

}