CodeStoreAndPathTest.java revision 1239:77609e069f9f
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 */ 25package jdk.nashorn.internal.runtime.test; 26 27import static org.testng.Assert.assertEquals; 28import static org.testng.Assert.assertFalse; 29import java.io.File; 30import java.io.IOException; 31import java.nio.file.DirectoryStream; 32import java.nio.file.FileSystems; 33import java.nio.file.Files; 34import java.nio.file.Path; 35import javax.script.ScriptEngine; 36import javax.script.ScriptException; 37import jdk.nashorn.api.scripting.NashornScriptEngineFactory; 38import org.testng.annotations.Test; 39 40/** 41 * @test 42 * @bug 8039185 8039403 43 * @summary Test for persistent code cache and path handling 44 * @run testng jdk.nashorn.internal.runtime.CodeStoreAndPathTest 45 */ 46@SuppressWarnings("javadoc") 47public class CodeStoreAndPathTest { 48 49 final String code1 = "var code1; var x = 'Hello Script'; var x1 = 'Hello Script'; " 50 + "var x2 = 'Hello Script'; var x3 = 'Hello Script'; " 51 + "var x4 = 'Hello Script'; var x5 = 'Hello Script';" 52 + "var x6 = 'Hello Script'; var x7 = 'Hello Script'; " 53 + "var x8 = 'Hello Script'; var x9 = 'Hello Script'; " 54 + "var x10 = 'Hello Script';" 55 + "function f() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 56 + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 57 + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 58 + "var x10 = 'Hello Script';}" 59 + "function g() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 60 + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 61 + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 62 + "var x10 = 'Hello Script';}" 63 + "function h() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 64 + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 65 + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 66 + "var x10 = 'Hello Script';}" 67 + "function i() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 68 + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 69 + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 70 + "var x10 = 'Hello Script';}"; 71 final String code2 = "var code2; var x = 'Hello Script'; var x1 = 'Hello Script'; " 72 + "var x2 = 'Hello Script'; var x3 = 'Hello Script'; " 73 + "var x4 = 'Hello Script'; var x5 = 'Hello Script';" 74 + "var x6 = 'Hello Script'; var x7 = 'Hello Script'; " 75 + "var x8 = 'Hello Script'; var x9 = 'Hello Script'; " 76 + "var x10 = 'Hello Script';" 77 + "function f() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 78 + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 79 + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 80 + "var x10 = 'Hello Script';}" 81 + "function g() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 82 + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 83 + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 84 + "var x10 = 'Hello Script';}" 85 + "function h() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 86 + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 87 + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 88 + "var x10 = 'Hello Script';}" 89 + "function i() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 90 + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 91 + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 92 + "var x10 = 'Hello Script';}"; 93 // Script size < Default minimum size for storing a compiled script class 94 final String code3 = "var code3; var x = 'Hello Script'; var x1 = 'Hello Script'; "; 95 final String codeCache = "build/nashorn_code_cache"; 96 final String oldUserDir = System.getProperty("user.dir"); 97 98 private static final String[] ENGINE_OPTIONS_OPT = new String[]{"--persistent-code-cache", "--optimistic-types=true"}; 99 private static final String[] ENGINE_OPTIONS_NOOPT = new String[]{"--persistent-code-cache", "--optimistic-types=false"}; 100 101 @Test 102 public void pathHandlingTest() { 103 System.setProperty("nashorn.persistent.code.cache", codeCache); 104 final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 105 106 fac.getScriptEngine(ENGINE_OPTIONS_NOOPT); 107 108 final Path expectedCodeCachePath = FileSystems.getDefault().getPath(oldUserDir + File.separator + codeCache); 109 final Path actualCodeCachePath = FileSystems.getDefault().getPath(System.getProperty( 110 "nashorn.persistent.code.cache")).toAbsolutePath(); 111 // Check that nashorn code cache is created in current working directory 112 assertEquals(actualCodeCachePath, expectedCodeCachePath); 113 // Check that code cache dir exists and it's not empty 114 final File file = new File(actualCodeCachePath.toUri()); 115 assertFalse(!file.isDirectory(), "No code cache directory was created!"); 116 assertFalse(file.list().length == 0, "Code cache directory is empty!"); 117 } 118 119 @Test 120 public void changeUserDirTest() throws ScriptException, IOException { 121 System.setProperty("nashorn.persistent.code.cache", codeCache); 122 final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 123 final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_NOOPT); 124 final Path codeCachePath = getCodeCachePath(false); 125 final String newUserDir = "build/newUserDir"; 126 // Now changing current working directory 127 System.setProperty("user.dir", System.getProperty("user.dir") + File.separator + newUserDir); 128 try { 129 // Check that a new compiled script is stored in existing code cache 130 e.eval(code1); 131 final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath); 132 checkCompiledScripts(stream, 1); 133 // Setting to default current working dir 134 } finally { 135 System.setProperty("user.dir", oldUserDir); 136 } 137 } 138 139 @Test 140 public void codeCacheTest() throws ScriptException, IOException { 141 System.setProperty("nashorn.persistent.code.cache", codeCache); 142 final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 143 final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_NOOPT); 144 final Path codeCachePath = getCodeCachePath(false); 145 e.eval(code1); 146 e.eval(code2); 147 e.eval(code3);// less than minimum size for storing 148 // adding code1 and code2. 149 final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath); 150 checkCompiledScripts(stream, 2); 151 } 152 153 @Test 154 public void codeCacheTestOpt() throws ScriptException, IOException { 155 System.setProperty("nashorn.persistent.code.cache", codeCache); 156 final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 157 final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_OPT); 158 final Path codeCachePath = getCodeCachePath(true); 159 e.eval(code1); 160 e.eval(code2); 161 e.eval(code3);// less than minimum size for storing 162 // adding code1 and code2. 163 final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath); 164 checkCompiledScripts(stream, 2); 165 } 166 167 private static Path getCodeCachePath(final boolean optimistic) { 168 final String codeCache = System.getProperty("nashorn.persistent.code.cache"); 169 final Path codeCachePath = FileSystems.getDefault().getPath(codeCache).toAbsolutePath(); 170 final String[] files = codeCachePath.toFile().list(); 171 for (final String file : files) { 172 if (file.endsWith("_opt") == optimistic) { 173 return codeCachePath.resolve(file); 174 } 175 } 176 throw new AssertionError("Code cache path not found"); 177 } 178 179 private static void checkCompiledScripts(final DirectoryStream<Path> stream, final int numberOfScripts) throws IOException { 180 int n = numberOfScripts; 181 for (@SuppressWarnings("unused") final Path file : stream) { 182 n--; 183 } 184 stream.close(); 185 assertEquals(n, 0); 186 } 187 188} 189