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