test/src/jdk/nashorn/internal/runtime/test/CodeStoreAndPathTest.java

Mon, 06 Apr 2015 16:18:54 +0530

author
sundar
date
Mon, 06 Apr 2015 16:18:54 +0530
changeset 1326
6787fa783196
parent 1087
test/src/jdk/nashorn/internal/runtime/CodeStoreAndPathTest.java@a119a11d49d8
child 1327
fb53538ea56b
permissions
-rw-r--r--

8076646: nashorn tests should avoid using package names used by nashorn sources
Reviewed-by: hannesw, lagergren

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

mercurial