logcoverage.js revision 1743:22d2cf722303
1/*
2 * Copyright (c) 2010, 2016, 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 * Screen scrape various logs to ensure that we cover enough functionality,
26 * e.g. method handle instrumentation
27 *
28 * @test
29 * @fork
30 * @option -Dnashorn.debug=true
31 */
32
33/*
34 * creates new script engine initialized with given options and
35 * runs given code on it. Returns standard output captured.
36 */
37
38function runScriptEngine(opts, name) {
39    var imports = new JavaImporter(
40            Packages.jdk.nashorn.api.scripting,
41            java.io, java.lang, java.util);
42
43    with (imports) {
44        var fac = new NashornScriptEngineFactory();
45        // get current System.err
46        var oldErr = System.err;
47        var oldOut = System.out;
48        var baosErr = new ByteArrayOutputStream();
49        var newErr = new PrintStream(baosErr);
50        var baosOut = new ByteArrayOutputStream();
51        var newOut = new PrintStream(baosOut);
52        try {
53            // set new standard err
54            System.setErr(newErr);
55            System.setOut(newOut);
56            var engine = fac.getScriptEngine(Java.to(opts, "java.lang.String[]"));
57            var reader = new java.io.FileReader(name);
58            engine.eval(reader);
59            newErr.flush();
60            newOut.flush();
61            return new java.lang.String(baosErr.toByteArray());
62        } finally {
63            // restore System.err to old value
64            System.setErr(oldErr);
65            System.setOut(oldOut);
66        }
67    }
68}
69
70var str;
71
72var methodsCalled = [
73    'asCollector',
74    'asType',
75    'bindTo',
76    'dropArguments',
77    'explicitCastArguments',
78    'filterArguments',
79    'filterReturnValue',
80    'findStatic',
81    'findVirtual',
82    'foldArguments',
83    'getter',
84    'guardWithTest',
85    'insertArguments',
86    'methodType',
87    'setter'
88];
89
90function check(str, strs) {
91    for each (s in strs) {
92        if (str.indexOf(s) !== -1) {
93            continue;
94        }
95        print(s + " not found");
96        return;
97    }
98    print("check ok!");
99}
100
101str = runScriptEngine(["--log=codegen,compiler=finest,methodhandles=finest,fields=finest"], __DIR__ + "../basic/NASHORN-19.js");
102str += runScriptEngine(["--log=codegen,compiler=finest,methodhandles=finest,fields=finest"], __DIR__ + "../basic/varargs.js");
103
104check(str, methodsCalled);
105check(str, ['return', 'get', 'set', '[fields]']);
106check(str, ['codegen']);
107
108print("hello, world!");
109