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 longNestedFunctions = "\n" +
 128             "(function outer() { \n" +
 129             "    var map = null; \n" +
 130             "    (function inner() { \n" +
 131             "        var object; \n" +
 132             "        var HashMap = Java.type('java.util.HashMap'); \n" +
 133             "        map = new HashMap(); \n" +
 134             "        map.name          = 'name'; \n" +
 135             "        map.id            = 1234;\n" +
 136             "        map.basePath      = 'basePath'; \n" +
 137             "        map.extensionPath = 'extension';\n" +
 138             "        map.address       = 'address'; \n" +
 139             "        map.name          = 'name'; \n" +
 140             "        map.id            = 1234;\n" +
 141             "        map.basePath      = 'basePath'; \n" +
 142             "        map.extensionPath = 'extension';\n" +
 143             "        map.address       = 'address';\n" +
 144             "        map.name          = 'name'; \n" +
 145             "        map.id            = 1234;\n" +
 146             "        map.basePath      = 'basePath'; \n" +
 147             "        map.extensionPath = 'extension';\n" +
 148             "        map.address       = 'address'; \n" +
 149             "        map.name          = 'name'; \n" +
 150             "        map.id            = 1234;\n" +
 151             "        map.basePath      = 'basePath'; \n" +
 152             "        map.extensionPath = 'extension';\n" +
 153             "        map.address       = 'address'; \n" +
 154             "        map.name          = 'name'; \n" +
 155             "        map.id            = 1234;\n" +
 156             "        map.basePath      = 'basePath'; \n" +
 157             "        map.extensionPath = 'extension';\n" +
 158             "        map.address       = 'address';\n" +
 159             "        map.name          = 'name'; \n" +
 160             "        map.id            = 1234;\n" +
 161             "        map.basePath      = 'basePath'; \n" +
 162             "        map.extensionPath = 'extension';\n" +
 163             "        map.address       = 'address'; \n" +
 164             "        map.name          = 'name'; \n" +
 165             "        map.id            = 1234;\n" +
 166             "        map.basePath      = 'basePath'; \n" +
 167             "        map.extensionPath = 'extension';\n" +
 168             "        map.address       = 'address'; \n" +
 169             "        map.name          = 'name'; \n" +
 170             "        map.id            = 1234;\n" +
 171             "        map.basePath      = 'basePath'; \n" +
 172             "        map.extensionPath = 'extension';\n" +
 173             "        map.address       = 'address';\n" +
 174             "        map.name          = 'name'; \n" +
 175             "        map.id            = 1234;\n" +
 176             "        map.basePath      = 'basePath'; \n" +
 177             "        map.extensionPath = 'extension';\n" +
 178             "        map.address       = 'address'; \n" +
 179             "        map.name          = 'name'; \n" +
 180             "        map.id            = 1234;\n" +
 181             "        map.basePath      = 'basePath'; \n" +
 182             "        map.extensionPath = 'extension';\n" +
 183             "        map.address       = 'address'; \n" +
 184             "        map.name          = 'name'; \n" +
 185             "        map.id            = 1234;\n" +
 186             "        map.basePath      = 'basePath'; \n" +
 187             "        map.extensionPath = 'extension';\n" +
 188             "        map.address       = 'address';\n" +
 189             "        map.name          = 'name'; \n" +
 190             "        map.id            = 1234;\n" +
 191             "        map.basePath      = 'basePath'; \n" +
 192             "        map.extensionPath = 'extension';\n" +
 193             "        map.address       = 'address'; \n" +
 194             "        map.name          = 'name'; \n" +
 195             "        map.id            = 1234;\n" +
 196             "        map.basePath      = 'basePath'; \n" +
 197             "        map.extensionPath = 'extension';\n" +
 198             "        map.address       = 'address'; \n" +
 199             "        map.name          = 'name'; \n" +
 200             "        map.id            = 1234;\n" +
 201             "        map.basePath      = 'basePath'; \n" +
 202             "        map.extensionPath = 'extension';\n" +
 203             "        map.address       = 'address';\n" +
 204             "        map.name          = 'name'; \n" +
 205             "        map.id            = 1234;\n" +
 206             "        map.basePath      = 'basePath'; \n" +
 207             "        map.extensionPath = 'extension';\n" +
 208             "        map.address       = 'address'; \n" +
 209             "        map.name          = 'name'; \n" +
 210             "        map.id            = 1234;\n" +
 211             "        map.basePath      = 'basePath'; \n" +
 212             "        map.extensionPath = 'extension';\n" +
 213             "        map.address       = 'address'; \n" +
 214             "        map.name          = 'name'; \n" +
 215             "        map.id            = 1234;\n" +
 216             "        map.basePath      = 'basePath'; \n" +
 217             "        map.extensionPath = 'extension';\n" +
 218             "        map.address       = 'address';\n" +
 219             "        map.name          = 'name'; \n" +
 220             "        map.id            = 1234;\n" +
 221             "        map.basePath      = 'basePath'; \n" +
 222             "        map.extensionPath = 'extension';\n" +
 223             "        map.address       = 'address'; \n" +
 224             "        map.name          = 'name'; \n" +
 225             "        map.id            = 1234;\n" +
 226             "        map.basePath      = 'basePath'; \n" +
 227             "        map.extensionPath = 'extension';\n" +
 228             "        map.address       = 'address'; \n" +
 229             "        map.name          = 'name'; \n" +
 230             "        map.id            = 1234;\n" +
 231             "        map.basePath      = 'basePath'; \n" +
 232             "        map.extensionPath = 'extension';\n" +
 233             "        map.address       = 'address';\n" +
 234             "        map.name          = 'name'; \n" +
 235             "        map.id            = 1234;\n" +
 236             "        map.basePath      = 'basePath'; \n" +
 237             "        map.extensionPath = 'extension';\n" +
 238             "        map.address       = 'address'; \n" +
 239             "        map.name          = 'name'; \n" +
 240             "        map.id            = 1234;\n" +
 241             "        map.basePath      = 'basePath'; \n" +
 242             "        map.extensionPath = 'extension';\n" +
 243             "        map.address       = 'address'; \n" +
 244             "        map.name          = 'name'; \n" +
 245             "        map.id            = 1234;\n" +
 246             "        map.basePath      = 'basePath'; \n" +
 247             "        map.extensionPath = 'extension';\n" +
 248             "        map.address       = 'address';\n" +
 249             "        map.name          = 'name'; \n" +
 250             "        map.id            = 1234;\n" +
 251             "        map.basePath      = 'basePath'; \n" +
 252             "        map.extensionPath = 'extension';\n" +
 253             "        map.address       = 'address'; \n" +
 254             "        map.name          = 'name'; \n" +
 255             "        map.id            = 1234;\n" +
 256             "        map.basePath      = 'basePath'; \n" +
 257             "        map.extensionPath = 'extension';\n" +
 258             "        map.address       = 'address'; \n" +
 259             "        map.name          = 'name'; \n" +
 260             "        map.id            = 1234;\n" +
 261             "        map.basePath      = 'basePath'; \n" +
 262             "        map.extensionPath = 'extension';\n" +
 263             "        map.address       = 'address';\n" +
 264             "        map.name          = 'name'; \n" +
 265             "        map.id            = 1234;\n" +
 266             "        map.basePath      = 'basePath'; \n" +
 267             "        map.extensionPath = 'extension';\n" +
 268             "        map.address       = 'address'; \n" +
 269             "        map.name          = 'name'; \n" +
 270             "        map.id            = 1234;\n" +
 271             "        map.basePath      = 'basePath'; \n" +
 272             "        map.extensionPath = 'extension';\n" +
 273             "        map.address       = 'address'; \n" +
 274             "        map.name          = 'name'; \n" +
 275             "        map.id            = 1234;\n" +
 276             "        map.basePath      = 'basePath'; \n" +
 277             "        map.extensionPath = 'extension';\n" +
 278             "        map.address       = 'address';\n" +
 279             "        map.name          = 'name'; \n" +
 280             "        map.id            = 1234;\n" +
 281             "        map.basePath      = 'basePath'; \n" +
 282             "        map.extensionPath = 'extension';\n" +
 283             "        map.address       = 'address'; \n" +
 284             "        map.name          = 'name'; \n" +
 285             "        map.id            = 1234;\n" +
 286             "        map.basePath      = 'basePath'; \n" +
 287             "        map.extensionPath = 'extension';\n" +
 288             "        map.address       = 'address'; \n" +
 289             "        map.name          = 'name'; \n" +
 290             "        map.id            = 1234;\n" +
 291             "        map.basePath      = 'basePath'; \n" +
 292             "        map.extensionPath = 'extension';\n" +
 293             "        map.address       = 'address';\n" +
 294             "        map.name          = 'name'; \n" +
 295             "        map.id            = 1234;\n" +
 296             "        map.basePath      = 'basePath'; \n" +
 297             "        map.extensionPath = 'extension';\n" +
 298             "        map.address       = 'address'; \n" +
 299             "        map.name          = 'name'; \n" +
 300             "        map.id            = 1234;\n" +
 301             "        map.basePath      = 'basePath'; \n" +
 302             "        map.extensionPath = 'extension';\n" +
 303             "        map.address       = 'address'; \n" +
 304             "        map.name          = 'name'; \n" +
 305             "        map.id            = 1234;\n" +
 306             "        map.basePath      = 'basePath'; \n" +
 307             "        map.extensionPath = 'extension';\n" +
 308             "        map.address       = 'address';\n" +
 309             "        map.name          = 'name'; \n" +
 310             "        map.id            = 1234;\n" +
 311             "        map.basePath      = 'basePath'; \n" +
 312             "        map.extensionPath = 'extension';\n" +
 313             "        map.address       = 'address'; \n" +
 314             "        map.name          = 'name'; \n" +
 315             "        map.id            = 1234;\n" +
 316             "        map.basePath      = 'basePath'; \n" +
 317             "        map.extensionPath = 'extension';\n" +
 318             "        map.address       = 'address'; \n" +
 319             "        map.name          = 'name'; \n" +
 320             "        map.id            = 1234;\n" +
 321             "        map.basePath      = 'basePath'; \n" +
 322             "        map.extensionPath = 'extension';\n" +
 323             "        map.address       = 'address';\n" +
 324             "        map.name          = 'name'; \n" +
 325             "        map.id            = 1234;\n" +
 326             "        map.basePath      = 'basePath'; \n" +
 327             "        map.extensionPath = 'extension';\n" +
 328             "        map.address       = 'address'; \n" +
 329             "        map.name          = 'name'; \n" +
 330             "        map.id            = 1234;\n" +
 331             "        map.basePath      = 'basePath'; \n" +
 332             "        map.extensionPath = 'extension';\n" +
 333             "        map.address       = 'address'; \n" +
 334             "        map.name          = 'name'; \n" +
 335             "        map.id            = 1234;\n" +
 336             "        map.basePath      = 'basePath'; \n" +
 337             "        map.extensionPath = 'extension';\n" +
 338             "        map.address       = 'address';\n" +
 339             "        map.name          = 'name'; \n" +
 340             "        map.id            = 1234;\n" +
 341             "        map.basePath      = 'basePath'; \n" +
 342             "        map.extensionPath = 'extension';\n" +
 343             "        map.address       = 'address'; \n" +
 344             "        map.name          = 'name'; \n" +
 345             "        map.id            = 1234;\n" +
 346             "        map.basePath      = 'basePath'; \n" +
 347             "        map.extensionPath = 'extension';\n" +
 348             "        map.address       = 'address'; \n" +
 349             "        map.name          = 'name'; \n" +
 350             "        map.id            = 1234;\n" +
 351             "        map.basePath      = 'basePath'; \n" +
 352             "        map.extensionPath = 'extension';\n" +
 353             "        map.address       = 'address';\n" +
 354             "        map.name          = 'name'; \n" +
 355             "        map.id            = 1234;\n" +
 356             "        map.basePath      = 'basePath'; \n" +
 357             "        map.extensionPath = 'extension';\n" +
 358             "        map.address       = 'address'; \n" +
 359             "        map.name          = 'name'; \n" +
 360             "        map.id            = 1234;\n" +
 361             "        map.basePath      = 'basePath'; \n" +
 362             "        map.extensionPath = 'extension';\n" +
 363             "        map.address       = 'address'; \n" +
 364             "        map.name          = 'name'; \n" +
 365             "        map.id            = 1234;\n" +
 366             "        map.basePath      = 'basePath'; \n" +
 367             "        map.extensionPath = 'extension';\n" +
 368             "        map.address       = 'address';\n" +
 369             "        map.name          = 'name'; \n" +
 370             "        map.id            = 1234;\n" +
 371             "        map.basePath      = 'basePath'; \n" +
 372             "        map.extensionPath = 'extension';\n" +
 373             "        map.address       = 'address'; \n" +
 374             "        map.name          = 'name'; \n" +
 375             "        map.id            = 1234;\n" +
 376             "        map.basePath      = 'basePath'; \n" +
 377             "        map.extensionPath = 'extension';\n" +
 378             "        map.address       = 'address'; \n" +
 379             "        map.name          = 'name'; \n" +
 380             "        map.id            = 1234;\n" +
 381             "        map.basePath      = 'basePath'; \n" +
 382             "        map.extensionPath = 'extension';\n" +
 383             "        map.address       = 'address';\n" +
 384             "        map.name          = 'name'; \n" +
 385             "        map.id            = 1234;\n" +
 386             "        map.basePath      = 'basePath'; \n" +
 387             "        map.extensionPath = 'extension';\n" +
 388             "        map.address       = 'address'; \n" +
 389             "        object = {}; \n" +
 390             "        return object; \n" +
 391             "    })(); \n" +
 392             "}()); ";
 393     final static String codeCache = "build/nashorn_code_cache";
 394     final static String oldUserDir = System.getProperty("user.dir");
 395 
 396     private static final String[] ENGINE_OPTIONS_OPT   = new String[]{"--persistent-code-cache", "--optimistic-types=true"};
 397     private static final String[] ENGINE_OPTIONS_NOOPT = new String[]{"--persistent-code-cache", "--optimistic-types=false"};
 398 
 399     @Test
 400     public void pathHandlingTest() {
 401         System.setProperty("nashorn.persistent.code.cache", codeCache);
 402         final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
 403 
 404         fac.getScriptEngine(ENGINE_OPTIONS_NOOPT);
 405 
 406         final Path expectedCodeCachePath = FileSystems.getDefault().getPath(oldUserDir + File.separator + codeCache);
 407         final Path actualCodeCachePath = FileSystems.getDefault().getPath(System.getProperty(
 408                             "nashorn.persistent.code.cache")).toAbsolutePath();
 409         // Check that nashorn code cache is created in current working directory
 410         assertEquals(actualCodeCachePath, expectedCodeCachePath);
 411         // Check that code cache dir exists and it's not empty
 412         final File file = new File(actualCodeCachePath.toUri());
 413         assertTrue(file.exists(), "No code cache directory was created!");
 414         assertTrue(file.isDirectory(), "Code cache location is not a directory!");
 415         assertFalse(file.list().length == 0, "Code cache directory is empty!");
 416     }
 417 
 418     @Test
 419     public void changeUserDirTest() throws ScriptException, IOException {
 420         System.setProperty("nashorn.persistent.code.cache", codeCache);
 421         final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
 422         final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_NOOPT);
 423         final Path codeCachePath = getCodeCachePath(false);
 424         final String newUserDir = "build/newUserDir";
 425         // Now changing current working directory
 426         System.setProperty("user.dir", System.getProperty("user.dir") + File.separator + newUserDir);
 427         try {
 428             // Check that a new compiled script is stored in existing code cache
 429             e.eval(code1);
 430             final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath);
 431             checkCompiledScripts(stream, 1);
 432             // Setting to default current working dir
 433         } finally {
 434             System.setProperty("user.dir", oldUserDir);
 435         }
 436     }
 437 
 438     @Test
 439     public void codeCacheTest() throws ScriptException, IOException {
 440         System.setProperty("nashorn.persistent.code.cache", codeCache);
 441         final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
 442         final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_NOOPT);
 443         final Path codeCachePath = getCodeCachePath(false);
 444         e.eval(code1);
 445         e.eval(code2);
 446         e.eval(code3);// less than minimum size for storing
 447         // adding code1 and code2.
 448         final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath);
 449         checkCompiledScripts(stream, 2);
 450     }
 451 
 452     @Test
 453     public void codeCacheTestOpt() throws ScriptException, IOException {
 454         System.setProperty("nashorn.persistent.code.cache", codeCache);
 455         final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
 456         final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_OPT);
 457         final Path codeCachePath = getCodeCachePath(true);
 458         e.eval(code1);
 459         e.eval(code2);
 460         e.eval(code3);// less than minimum size for storing
 461         // adding code1 and code2.
 462         final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath);
 463         checkCompiledScripts(stream, 4);
 464     }
 465 
 466     @Test
 467     public void testNestedFunctionStore() throws ScriptException, IOException {
 468         System.setProperty("nashorn.persistent.code.cache", codeCache);
 469         final NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
 470         factory.getScriptEngine(ENGINE_OPTIONS_OPT).eval(nestedFunctions);
 471         factory.getScriptEngine(ENGINE_OPTIONS_OPT).eval(nestedFunctions);
 472         factory.getScriptEngine(ENGINE_OPTIONS_OPT).eval(nestedFunctions);
 473         factory.getScriptEngine(ENGINE_OPTIONS_OPT).eval(nestedFunctions);
 474     }
 475 
 476     @Test
 477     public void testSplitFunctionStore() throws ScriptException, IOException {
 478         System.setProperty("nashorn.persistent.code.cache", codeCache);
 479         System.setProperty("nashorn.compiler.splitter.threshold", "500");
 480         final NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
 481         factory.getScriptEngine(ENGINE_OPTIONS_OPT).eval(longNestedFunctions);
 482         factory.getScriptEngine(ENGINE_OPTIONS_OPT).eval(longNestedFunctions);
 483         factory.getScriptEngine(ENGINE_OPTIONS_OPT).eval(longNestedFunctions);
 484         factory.getScriptEngine(ENGINE_OPTIONS_OPT).eval(longNestedFunctions);
 485         System.getProperties().remove("nashorn.compiler.splitter.threshold");
 486     }
 487 
 488     private static Path getCodeCachePath(final boolean optimistic) {
 489         final String codeCache = System.getProperty("nashorn.persistent.code.cache");
 490         final Path codeCachePath = FileSystems.getDefault().getPath(codeCache).toAbsolutePath();
 491         final String[] files = codeCachePath.toFile().list();
 492         for (final String file : files) {
 493             if (file.endsWith("_opt") == optimistic) {
 494                 return codeCachePath.resolve(file);
 495             }
 496         }
 497         throw new AssertionError("Code cache path not found: " + codeCachePath.toString());
 498     }
 499 
 500     private static void checkCompiledScripts(final DirectoryStream<Path> stream, final int numberOfScripts) throws IOException {
 501         int n = numberOfScripts;
 502         for (@SuppressWarnings("unused") final Path file : stream) {
 503             n--;
 504         }
 505         stream.close();
 506         assertEquals(n, 0);
 507     }
 508 
 509 }