1 /*
   2  * Copyright (c) 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  * JDK-8144113: Nashorn: enable jjs testing.
  26  * @subtest
  27  * @summary test used by all other jjs-option* test cases
  28  */
  29 var javaHome = $ENV.JAVA_HOME,
  30     homeJjs = "${javaHome}/bin/jjs",
  31     altJjs = $EXEC('which jjs').trim(),
  32     homejavac = "${javaHome}/bin/javac",
  33     altjavac = $EXEC('which javac').trim()
  34 
  35 var Files = Java.type('java.nio.file.Files'),
  36     Paths = Java.type('java.nio.file.Paths'),
  37     System = Java.type('java.lang.System')
  38 
  39 // Initialize default values for variables used in different functions
  40 var func_cond_p = <<EOD
  41 $EXIT == 0
  42 EOD
  43 
  44 var func_cond_n = <<EOD
  45 $EXIT != 0
  46 EOD
  47 
  48 var flag_cond_p = <<EOD
  49 out == e_outp
  50 EOD
  51 
  52 var flag_cond_n = <<EOD
  53 out == e_outn
  54 EOD
  55 
  56 var e_outp = "true"
  57 var e_outn = "false"
  58 
  59 // special cases in which arguments used for negative testing also
  60 var args_p = "-scripting"
  61 var args_n = "-scripting"
  62 
  63 // create file to check -flag passing
  64 var path_f = Paths.get("temp-flag.js")
  65 var testflag_file = path_f.toAbsolutePath()
  66 
  67 // create file to check -flag functionality
  68 var path_func = Paths.get("temp-func.js")
  69 var testfunc_file = path_func.toAbsolutePath()
  70 
  71 
  72 function exists(f) {
  73     return Files.exists(Paths.get(f))
  74 }
  75 
  76 var jjs = exists(homeJjs) ? homeJjs : altJjs
  77 var javac = exists(homejavac) ? homejavac : altjavac
  78 
  79 if (!exists(jjs)) {
  80     throw "no jjs executable found; tried ${homeJjs} and ${altJjs}"
  81 }
  82 
  83 // write code to testflag_file
  84 function write_testflag_file() {
  85     Files.write(testflag_file, msg_flag.getBytes())
  86 }
  87 
  88 // write code to testfunc_file
  89 function write_testfunc_file() {
  90     Files.write(testfunc_file, msg_func.getBytes())
  91 }
  92 
  93 function flag_test_pass() {
  94     print("flag test PASSED")
  95 }
  96 
  97 function flag_test_fail(e_out, out) {
  98     print("flag test FAILED expected out:${e_out} found:${out}")
  99 }
 100 
 101 // check functionality of flag,cover both positive and negative cases
 102 function testjjs_opt_func(args, positive) {
 103     $EXEC("${jjs} ${args}")
 104     var out = $OUT.trim(),
 105         err = $ERR.trim()
 106     if (positive) {
 107         if (eval(func_cond_p))
 108             print("functionality test PASSED")
 109         else
 110             print("functionality test FAILED. stdout: ${out} -- stderr: ${err}")
 111     }
 112     else {
 113         if (eval(func_cond_n))
 114             print("functionality test PASSED")
 115         else
 116             print("functionality test FAILED. stdout: ${out} -- stderr: ${err}")
 117     }
 118 
 119 }
 120 
 121 // check if corresponding $OPTIONS._XXX is set for given flag
 122 function testjjs_opt(args, type, func) {
 123     $EXEC("${jjs} ${args}")
 124     var out = $OUT.trim(),
 125         err = $ERR.trim()
 126     if (type) {
 127         if (eval(flag_cond_p)) {
 128             flag_test_pass()
 129             if (func)
 130                 testjjs_opt_func(arg_p, type)
 131         }
 132         else {
 133             flag_test_fail(e_outp, out)
 134         }
 135     }
 136     else {
 137         if (eval(flag_cond_n)) {
 138             flag_test_pass()
 139             if (func)
 140                 testjjs_opt_func(arg_n, type)
 141         }
 142         else {
 143             flag_test_fail(e_outn, out)
 144         }
 145     }
 146 }
 147 
 148 // Main entry point to test both flag and its functionality
 149 function testjjs_flag_and_func(flag, param) {
 150     try {
 151         var args = "${flag}" + "${param}"
 152         write_testflag_file()
 153         write_testfunc_file()
 154         print("${flag} flag positive test:")
 155         testjjs_opt("${args_p} ${args} ${testflag_file}", true, true) // positive test
 156         print("${flag} flag negative test:")
 157         testjjs_opt("${args_n} ${testflag_file}", false, true)        // negative test
 158     } finally {
 159         $EXEC("rm ${testflag_file}")
 160         $EXEC("rm ${testfunc_file}")
 161     }
 162 }
 163 
 164 // Main entry point to check only functionality of given -flag
 165 function testjjs_functionality(flag, param) {
 166     try {
 167         var args = "${flag}" + "${param}"
 168         write_testfunc_file()
 169         print("${flag} flag positive test:")
 170         testjjs_opt_func("${args_p} ${args} ${testfunc_file}", true) // positive test
 171         print("${flag} flag negative test:")
 172         testjjs_opt_func("${args_n} ${testfunc_file}", false)        // negative test
 173     } finally {
 174         $EXEC("rm ${testfunc_file}")
 175     }
 176 }
 177 
 178 // Main entry point to check only -flag settings for given flag
 179 function testjjs_flag(flag, param) {
 180     try {
 181         var args = "${flag}" + "${param}"
 182         write_testflag_file()
 183         print("${flag} flag positive test:")
 184         testjjs_opt("${args_p} ${args} ${testflag_file}", true, false) // positive test
 185         print("${flag} flag negative test:")
 186         testjjs_opt("${args_n} ${testflag_file}", false, false)        // negative test
 187     } finally {
 188         $EXEC("rm ${testflag_file}")
 189     }
 190 }