1 /* 2 * Copyright (c) 2014, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package jdk.nashorn.internal.runtime.test; 26 27 import static org.testng.Assert.assertEquals; 28 import static org.testng.Assert.assertFalse; 29 import static org.testng.Assert.assertTrue; 30 import java.io.File; 31 import java.io.IOException; 32 import java.nio.file.DirectoryStream; 33 import java.nio.file.FileSystems; 34 import java.nio.file.Files; 35 import java.nio.file.Path; 36 import javax.script.ScriptEngine; 37 import javax.script.ScriptException; 38 import jdk.nashorn.api.scripting.NashornScriptEngineFactory; 39 import org.testng.annotations.Test; 40 41 /** 42 * @test 43 * @bug 8039185 8039403 44 * @summary Test for persistent code cache and path handling 45 * @run testng jdk.nashorn.internal.runtime.test.CodeStoreAndPathTest 46 */ 47 @SuppressWarnings("javadoc") 48 public class CodeStoreAndPathTest { 49 50 final static String code1 = "var code1; var x = 'Hello Script'; var x1 = 'Hello Script'; " 51 + "var x2 = 'Hello Script'; var x3 = 'Hello Script'; " 52 + "var x4 = 'Hello Script'; var x5 = 'Hello Script';" 53 + "var x6 = 'Hello Script'; var x7 = 'Hello Script'; " 54 + "var x8 = 'Hello Script'; var x9 = 'Hello Script'; " 55 + "var x10 = 'Hello Script';" 56 + "function f() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 57 + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 58 + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 59 + "var x10 = 'Hello Script';}" 60 + "function g() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 61 + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 62 + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 63 + "var x10 = 'Hello Script';}" 64 + "function h() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 65 + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 66 + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 67 + "var x10 = 'Hello Script';}" 68 + "function i() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 69 + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 70 + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 71 + "var x10 = 'Hello Script';}"; 72 final static String code2 = "var code2; var x = 'Hello Script'; var x1 = 'Hello Script'; " 73 + "var x2 = 'Hello Script'; var x3 = 'Hello Script'; " 74 + "var x4 = 'Hello Script'; var x5 = 'Hello Script';" 75 + "var x6 = 'Hello Script'; var x7 = 'Hello Script'; " 76 + "var x8 = 'Hello Script'; var x9 = 'Hello Script'; " 77 + "var x10 = 'Hello Script';" 78 + "function f() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 79 + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 80 + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 81 + "var x10 = 'Hello Script';}" 82 + "function g() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 83 + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 84 + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 85 + "var x10 = 'Hello Script';}" 86 + "function h() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 87 + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 88 + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 89 + "var x10 = 'Hello Script';}" 90 + "function i() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 91 + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 92 + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 93 + "var x10 = 'Hello Script';}"; 94 // Script size < Default minimum size for storing a compiled script class 95 final static String code3 = "var code3; var x = 'Hello Script'; var x1 = 'Hello Script'; "; 96 final static String nestedFunctions = "\n" + 97 "(function outer() { \n" + 98 " var map = null; \n" + 99 " (function inner() { \n" + 100 " var object; \n" + 101 " if (map === null) { \n" + 102 " map = (function() { \n" + 103 " var HashMap = Java.type('java.util.HashMap'); \n" + 104 " map = new HashMap(); \n" + 105 " map.name = 'name'; \n" + 106 " map.id = 1234;\n" + 107 " map.basePath = 'basePath'; \n" + 108 " map.extensionPath = 'extension';\n" + 109 " map.address = 'address'; \n" + 110 " map.name = 'name'; \n" + 111 " map.id = 1234;\n" + 112 " map.basePath = 'basePath'; \n" + 113 " map.extensionPath = 'extension';\n" + 114 " map.address = 'address';\n" + 115 " map.name = 'name'; \n" + 116 " map.id = 1234;\n" + 117 " map.basePath = 'basePath'; \n" + 118 " map.extensionPath = 'extension';\n" + 119 " map.address = 'address'; \n" + 120 " return map; \n" + 121 " }()); \n" + 122 " } \n" + 123 " object = {}; \n" + 124 " return object; \n" + 125 " })(); \n" + 126 "}()); "; 127 final static String codeCache = "build/nashorn_code_cache"; 128 final static String oldUserDir = System.getProperty("user.dir"); 129 130 private static final String[] ENGINE_OPTIONS_OPT = new String[]{"--persistent-code-cache", "--optimistic-types=true"}; 131 private static final String[] ENGINE_OPTIONS_NOOPT = new String[]{"--persistent-code-cache", "--optimistic-types=false"}; 132 133 @Test 134 public void pathHandlingTest() { 135 System.setProperty("nashorn.persistent.code.cache", codeCache); 136 final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 137 138 fac.getScriptEngine(ENGINE_OPTIONS_NOOPT); 139 140 final Path expectedCodeCachePath = FileSystems.getDefault().getPath(oldUserDir + File.separator + codeCache); 141 final Path actualCodeCachePath = FileSystems.getDefault().getPath(System.getProperty( 142 "nashorn.persistent.code.cache")).toAbsolutePath(); 143 // Check that nashorn code cache is created in current working directory 144 assertEquals(actualCodeCachePath, expectedCodeCachePath); 145 // Check that code cache dir exists and it's not empty 146 final File file = new File(actualCodeCachePath.toUri()); 147 assertTrue(file.exists(), "No code cache directory was created!"); 148 assertTrue(file.isDirectory(), "Code cache location is not a directory!"); 149 assertFalse(file.list().length == 0, "Code cache directory is empty!"); 150 } 151 152 @Test 153 public void changeUserDirTest() throws ScriptException, IOException { 154 System.setProperty("nashorn.persistent.code.cache", codeCache); 155 final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 156 final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_NOOPT); 157 final Path codeCachePath = getCodeCachePath(false); 158 final String newUserDir = "build/newUserDir"; 159 // Now changing current working directory 160 System.setProperty("user.dir", System.getProperty("user.dir") + File.separator + newUserDir); 161 try { 162 // Check that a new compiled script is stored in existing code cache 163 e.eval(code1); 164 final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath); 165 checkCompiledScripts(stream, 1); 166 // Setting to default current working dir 167 } finally { 168 System.setProperty("user.dir", oldUserDir); 169 } 170 } 171 172 @Test 173 public void codeCacheTest() throws ScriptException, IOException { 174 System.setProperty("nashorn.persistent.code.cache", codeCache); 175 final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 176 final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_NOOPT); 177 final Path codeCachePath = getCodeCachePath(false); 178 e.eval(code1); 179 e.eval(code2); 180 e.eval(code3);// less than minimum size for storing 181 // adding code1 and code2. 182 final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath); 183 checkCompiledScripts(stream, 2); 184 } 185 186 @Test 187 public void codeCacheTestOpt() throws ScriptException, IOException { 188 System.setProperty("nashorn.persistent.code.cache", codeCache); 189 final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 190 final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_OPT); 191 final Path codeCachePath = getCodeCachePath(true); 192 e.eval(code1); 193 e.eval(code2); 194 e.eval(code3);// less than minimum size for storing 195 // adding code1 and code2. 196 final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath); 197 checkCompiledScripts(stream, 4); 198 } 199 200 @Test 201 public void testNestedFunctions() throws ScriptException, IOException { 202 System.setProperty("nashorn.persistent.code.cache", codeCache); 203 final NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); 204 factory.getScriptEngine(ENGINE_OPTIONS_OPT).eval(nestedFunctions); 205 factory.getScriptEngine(ENGINE_OPTIONS_OPT).eval(nestedFunctions); 206 factory.getScriptEngine(ENGINE_OPTIONS_OPT).eval(nestedFunctions); 207 factory.getScriptEngine(ENGINE_OPTIONS_OPT).eval(nestedFunctions); 208 } 209 210 private static Path getCodeCachePath(final boolean optimistic) { 211 final String codeCache = System.getProperty("nashorn.persistent.code.cache"); 212 final Path codeCachePath = FileSystems.getDefault().getPath(codeCache).toAbsolutePath(); 213 final String[] files = codeCachePath.toFile().list(); 214 for (final String file : files) { 215 if (file.endsWith("_opt") == optimistic) { 216 return codeCachePath.resolve(file); 217 } 218 } 219 throw new AssertionError("Code cache path not found: " + codeCachePath.toString()); 220 } 221 222 private static void checkCompiledScripts(final DirectoryStream<Path> stream, final int numberOfScripts) throws IOException { 223 int n = numberOfScripts; 224 for (@SuppressWarnings("unused") final Path file : stream) { 225 n--; 226 } 227 stream.close(); 228 assertEquals(n, 0); 229 } 230 231 }