nashorn/test/script/trusted/logcoverage.js
changeset 17937 eb32dc973706
parent 17936 2a33a19ee977
parent 17670 8339e359fd41
child 17938 af1b01dfea42
equal deleted inserted replaced
17936:2a33a19ee977 17937:eb32dc973706
     1 /*
       
     2  * Copyright (c) 2010, 2013, 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  * mh_coverage.js
       
    26  * Screen scrape various logs to ensure that we cover enough functionality, 
       
    27  * e.g. method handle instrumentation
       
    28  *
       
    29  * @test
       
    30  * @run
       
    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 
       
    38 function 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 strType = Java.type("java.lang.String");
       
    57             var engine = fac.getScriptEngine(Java.toJavaArray(opts, strType));
       
    58 	    var reader = new java.io.FileReader(name);
       
    59             engine.eval(reader);
       
    60             newErr.flush();
       
    61 	    newOut.flush();
       
    62             return new java.lang.String(baosErr.toByteArray());
       
    63         } finally {
       
    64             // restore System.err to old value
       
    65             System.setErr(oldErr);
       
    66 	    System.setOut(oldOut);
       
    67         }
       
    68     }
       
    69 }
       
    70 
       
    71 var str;
       
    72 
       
    73 var methodsCalled = [
       
    74    'asCollector', 
       
    75    'asType', 
       
    76    'bindTo', 
       
    77    'dropArguments', 
       
    78    'explicitCastArguments', 
       
    79    'filterArguments', 
       
    80    'filterReturnValue', 
       
    81    'findStatic', 
       
    82    'findVirtual',  
       
    83    'foldArguments', 
       
    84    'getter', 
       
    85    'guardWithTest', 
       
    86    'insertArguments', 
       
    87    'methodType', 
       
    88    'setter'
       
    89 ];
       
    90 
       
    91 function check(str, strs) {
       
    92     for each (s in strs) {
       
    93        if (s.indexOf(str) !== -1) {
       
    94 	   continue;
       
    95        }
       
    96        print(method + "not found");
       
    97        return;
       
    98     }
       
    99     print("check ok!");
       
   100 }
       
   101 
       
   102 str = runScriptEngine([ "--log=codegen,compiler=finest,methodhandles=finest,fields=finest" ], __DIR__ + "../basic/NASHORN-19.js");
       
   103 
       
   104 check(str, methodsCalled);
       
   105 check(str, ['return', 'get', 'set', '[fields]']);
       
   106 check(str, ['codegen']);
       
   107 
       
   108 print("hello, world!");