CodeStoreAndPathTest.java revision 1243:df6c3e9c1a0b
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 * @ignore Fails with jtreg, but passes with ant test run. Ignore for now.
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")
48public 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        assertFalse(!file.isDirectory(), "No code cache directory was created!");
117        assertFalse(file.list().length == 0, "Code cache directory is empty!");
118    }
119
120    @Test
121    public void changeUserDirTest() throws ScriptException, IOException {
122        System.setProperty("nashorn.persistent.code.cache", codeCache);
123        final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
124        final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_NOOPT);
125        final Path codeCachePath = getCodeCachePath(false);
126        final String newUserDir = "build/newUserDir";
127        // Now changing current working directory
128        System.setProperty("user.dir", System.getProperty("user.dir") + File.separator + newUserDir);
129        try {
130            // Check that a new compiled script is stored in existing code cache
131            e.eval(code1);
132            final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath);
133            checkCompiledScripts(stream, 1);
134            // Setting to default current working dir
135        } finally {
136            System.setProperty("user.dir", oldUserDir);
137        }
138    }
139
140    @Test
141    public void codeCacheTest() throws ScriptException, IOException {
142        System.setProperty("nashorn.persistent.code.cache", codeCache);
143        final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
144        final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_NOOPT);
145        final Path codeCachePath = getCodeCachePath(false);
146        e.eval(code1);
147        e.eval(code2);
148        e.eval(code3);// less than minimum size for storing
149        // adding code1 and code2.
150        final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath);
151        checkCompiledScripts(stream, 2);
152    }
153
154    @Test
155    public void codeCacheTestOpt() throws ScriptException, IOException {
156        System.setProperty("nashorn.persistent.code.cache", codeCache);
157        final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
158        final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_OPT);
159        final Path codeCachePath = getCodeCachePath(true);
160        e.eval(code1);
161        e.eval(code2);
162        e.eval(code3);// less than minimum size for storing
163        // adding code1 and code2.
164        final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath);
165        checkCompiledScripts(stream, 2);
166    }
167
168    private static Path getCodeCachePath(final boolean optimistic) {
169        final String codeCache = System.getProperty("nashorn.persistent.code.cache");
170        final Path codeCachePath = FileSystems.getDefault().getPath(codeCache).toAbsolutePath();
171        final String[] files = codeCachePath.toFile().list();
172        for (final String file : files) {
173            if (file.endsWith("_opt") == optimistic) {
174                return codeCachePath.resolve(file);
175            }
176        }
177        throw new AssertionError("Code cache path not found");
178    }
179
180    private static void checkCompiledScripts(final DirectoryStream<Path> stream, final int numberOfScripts) throws IOException {
181        int n = numberOfScripts;
182        for (@SuppressWarnings("unused") final Path file : stream) {
183            n--;
184        }
185        stream.close();
186        assertEquals(n, 0);
187    }
188
189}
190