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 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 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 String code3 = "var code3; var x = 'Hello Script'; var x1 = 'Hello Script'; "; 96 final String codeCache = "build/nashorn_code_cache"; 97 final String oldUserDir = System.getProperty("user.dir"); 98 99 private static final String[] ENGINE_OPTIONS_OPT = new String[]{"--persistent-code-cache", "--optimistic-types=true"}; 100 private static final String[] ENGINE_OPTIONS_NOOPT = new String[]{"--persistent-code-cache", "--optimistic-types=false"}; 101 102 @Test 103 public void pathHandlingTest() { 104 System.setProperty("nashorn.persistent.code.cache", codeCache); 105 final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 106 107 fac.getScriptEngine(ENGINE_OPTIONS_NOOPT); 108 109 final Path expectedCodeCachePath = FileSystems.getDefault().getPath(oldUserDir + File.separator + codeCache); 110 final Path actualCodeCachePath = FileSystems.getDefault().getPath(System.getProperty( 111 "nashorn.persistent.code.cache")).toAbsolutePath(); 112 // Check that nashorn code cache is created in current working directory 113 assertEquals(actualCodeCachePath, expectedCodeCachePath); 114 // Check that code cache dir exists and it's not empty 115 final File file = new File(actualCodeCachePath.toUri()); 116 assertTrue(file.exists(), "No code cache directory was created!"); 117 assertTrue(file.isDirectory(), "Code cache location is not a directory!"); 149 e.eval(code3);// less than minimum size for storing 150 // adding code1 and code2. 151 final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath); 152 checkCompiledScripts(stream, 2); 153 } 154 155 @Test 156 public void codeCacheTestOpt() throws ScriptException, IOException { 157 System.setProperty("nashorn.persistent.code.cache", codeCache); 158 final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 159 final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_OPT); 160 final Path codeCachePath = getCodeCachePath(true); 161 e.eval(code1); 162 e.eval(code2); 163 e.eval(code3);// less than minimum size for storing 164 // adding code1 and code2. 165 final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath); 166 checkCompiledScripts(stream, 4); 167 } 168 169 private static Path getCodeCachePath(final boolean optimistic) { 170 final String codeCache = System.getProperty("nashorn.persistent.code.cache"); 171 final Path codeCachePath = FileSystems.getDefault().getPath(codeCache).toAbsolutePath(); 172 final String[] files = codeCachePath.toFile().list(); 173 for (final String file : files) { 174 if (file.endsWith("_opt") == optimistic) { 175 return codeCachePath.resolve(file); 176 } 177 } 178 throw new AssertionError("Code cache path not found: " + codeCachePath.toString()); 179 } 180 181 private static void checkCompiledScripts(final DirectoryStream<Path> stream, final int numberOfScripts) throws IOException { 182 int n = numberOfScripts; 183 for (@SuppressWarnings("unused") final Path file : stream) { 184 n--; 185 } 186 stream.close(); 187 assertEquals(n, 0); 188 } | 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!"); 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 } |