File: RunTest.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 (202 lines) | stat: -rw-r--r-- 6,538 bytes parent folder | download | duplicates (9)
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
/*
 * 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.
 */

/*
 * @test
 * @bug 8006263
 * @summary Supplementary test cases needed for doclint
 * @modules jdk.compiler/com.sun.tools.doclint
 *          jdk.compiler/com.sun.tools.javac.api
 */

import com.sun.source.util.JavacTask;
import com.sun.tools.doclint.DocLint;
import com.sun.tools.doclint.DocLint.BadArgs;
import com.sun.tools.javac.api.JavacTool;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URI;
import java.security.Permission;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;

public class RunTest {
    static class SimpleSecurityManager extends SecurityManager {
        boolean allowExit = false;

        @Override
        public void checkExit(int status) {
            if (!allowExit)
                throw new SecurityException("System.exit(" + status + ")");
        }
        @Override
        public void checkPermission(Permission perm) { }

    }

    public static void main(String... args) throws Exception {
        // if no security manager already installed, install one to
        // prevent System.exit
        SimpleSecurityManager secmgr = null;
        if (System.getSecurityManager() == null) {
            System.setSecurityManager(secmgr = new SimpleSecurityManager() { });
        }

        try {
            new RunTest().run();
        } finally {
            if (secmgr != null)
                secmgr.allowExit = true;
        }
    }

    void run() throws Exception {
        testMain();
        testRun();
        testInit();
        testArgsNoFiles();

        if (errors > 0)
            throw new Exception(errors + " errors found");
    }

    void testMain() {
        System.err.println("test main(String[])");
        testMain(true, "-help");
        testMain(false, "-unknownOption");
    }

    void testMain(boolean expectOK, String... args) {
        try {
            DocLint.main(args);
            if (!expectOK)
                error("expected SecurityException (from System.exit) not thrown");
        } catch (SecurityException e) {
            System.err.println(e);
            if (expectOK)
                error("unexpected SecurityException caught");
        }
    }

    void testRun() throws BadArgs, IOException {
        System.err.println("test run(String[])");
        DocLint dl = new DocLint();
        String[] args = { "-help" };
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(baos);
        PrintStream prev = System.out;
        try {
            System.setOut(ps);
            dl.run(args);
        } finally {
            System.setOut(prev);
        }
        ps.close();
        String stdout = baos.toString();

        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        dl.run(pw, args);
        pw.close();
        String direct = sw.toString();

        if (!stdout.equals(direct)) {
            error("unexpected output");
            System.err.println("EXPECT>>" + direct + "<<");
            System.err.println("FOUND>>" + stdout + "<<");
        }
    }

    void testInit() {
        System.err.println("test init");
        DocLint dl = new DocLint();
        String name = dl.getName();
        if (!Objects.equals(name, "doclint"))
            error("unexpected result for DocLint.getName()");

        List<? extends JavaFileObject> files =
                Arrays.asList(createFile("Test.java", "/** &0; */ class Test{ }"));
        String[] goodArgs = { "-Xmsgs" };
        testInit(true, goodArgs, files);

        String[] badArgs = { "-unknown" };
        testInit(false, badArgs, files);
    }

    void testInit(boolean expectOK, String[] args, List<? extends JavaFileObject> files) {
        JavacTool javac = JavacTool.create();
        JavacTask task = javac.getTask(null, null, null, null, null, files);
        try {
            DocLint dl = new DocLint();
            dl.init(task, args, true);
            if (!expectOK)
                error("expected IllegalArgumentException not thrown");
            task.call();
        } catch (IllegalArgumentException e) {
            System.err.println(e);
            if (expectOK)
                error("unexpected IllegalArgumentException caught");
        }
    }

    void testArgsNoFiles() throws BadArgs, IOException {
        System.err.println("test args, no files");
        DocLint dl = new DocLint();

        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        dl.run(pw, "-Xmsgs");
        pw.close();
        String out = sw.toString();

        String expect = "No files given";
        if (!Objects.equals(out.trim(), expect)) {
            error("unexpected output");
            System.err.println("EXPECT>>" + expect + "<<");
            System.err.println("FOUND>>" + out + "<<");
        }

    }

    JavaFileObject createFile(String name, final String body) {
        return new SimpleJavaFileObject(URI.create(name), JavaFileObject.Kind.SOURCE) {
            @Override
            public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
                return body;
            }
        };
    }

    void error(String msg) {
        System.err.println("Error: " + msg);
        errors++;
    }

    int errors;
}