1 /* |
|
2 * Copyright (c) 2010, 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 |
|
26 package jdk.nashorn.internal.runtime; |
|
27 |
|
28 import static jdk.nashorn.internal.runtime.Source.sourceFor; |
|
29 import static org.testng.Assert.assertEquals; |
|
30 import static org.testng.Assert.assertTrue; |
|
31 import static org.testng.Assert.fail; |
|
32 import java.io.File; |
|
33 import java.io.IOException; |
|
34 import java.io.InputStreamReader; |
|
35 import java.io.Reader; |
|
36 import java.net.URL; |
|
37 import java.util.Arrays; |
|
38 import jdk.nashorn.api.scripting.URLReader; |
|
39 import org.testng.annotations.Test; |
|
40 |
|
41 /** |
|
42 * Tests different Source representations. |
|
43 */ |
|
44 @SuppressWarnings("javadoc") |
|
45 public class SourceTest { |
|
46 |
|
47 final private static String SOURCE_NAME = "source.js"; |
|
48 final private static String SOURCE_STRING = "var x = 1;"; |
|
49 final private static char[] SOURCE_CHARS = SOURCE_STRING.toCharArray(); |
|
50 final private static String RESOURCE_PATH = "resources/load_test.js"; |
|
51 final private static File SOURCE_FILE = new File("build/test/classes/jdk/nashorn/internal/runtime/" + RESOURCE_PATH); |
|
52 final private static URL SOURCE_URL = SourceTest.class.getResource(RESOURCE_PATH); |
|
53 |
|
54 |
|
55 @Test |
|
56 public void testStringSource() { |
|
57 testSources(sourceFor(SOURCE_NAME, SOURCE_STRING), sourceFor(SOURCE_NAME, SOURCE_STRING)); |
|
58 testSources(sourceFor(SOURCE_NAME, SOURCE_STRING), sourceFor(SOURCE_NAME, SOURCE_CHARS)); |
|
59 } |
|
60 |
|
61 @Test |
|
62 public void testCharArraySource() { |
|
63 testSources(sourceFor(SOURCE_NAME, SOURCE_CHARS), sourceFor(SOURCE_NAME, SOURCE_CHARS)); |
|
64 testSources(sourceFor(SOURCE_NAME, SOURCE_CHARS), sourceFor(SOURCE_NAME, SOURCE_STRING)); |
|
65 } |
|
66 |
|
67 @Test |
|
68 public void testURLSource() { |
|
69 try { |
|
70 testSources(sourceFor(SOURCE_NAME, SOURCE_URL), sourceFor(SOURCE_NAME, SOURCE_URL)); |
|
71 testSources(sourceFor(SOURCE_NAME, SOURCE_URL), sourceFor(SOURCE_NAME, new URLReader(SOURCE_URL))); |
|
72 |
|
73 } catch (final IOException e) { |
|
74 fail(e.toString()); |
|
75 } |
|
76 } |
|
77 |
|
78 @Test |
|
79 public void testURLReaderSource() { |
|
80 try { |
|
81 System.err.println(SourceTest.class.getResource("")); |
|
82 testSources(sourceFor(SOURCE_NAME, new URLReader(SOURCE_URL)), sourceFor(SOURCE_NAME, new URLReader(SOURCE_URL))); |
|
83 testSources(sourceFor(SOURCE_NAME, new URLReader(SOURCE_URL)), sourceFor(SOURCE_NAME, SOURCE_URL)); |
|
84 } catch (final IOException e) { |
|
85 fail(e.toString()); |
|
86 } |
|
87 } |
|
88 |
|
89 @Test |
|
90 public void testReaderSource() { |
|
91 try { |
|
92 testSources(sourceFor(SOURCE_NAME, getReader(RESOURCE_PATH)), sourceFor(SOURCE_NAME, getReader(RESOURCE_PATH))); |
|
93 } catch (final IOException e) { |
|
94 fail(e.toString()); |
|
95 } |
|
96 } |
|
97 |
|
98 @Test |
|
99 public void testFileSource() { |
|
100 try { |
|
101 testSources(sourceFor(SOURCE_NAME, SOURCE_FILE), sourceFor(SOURCE_NAME, SOURCE_FILE)); |
|
102 } catch (final IOException e) { |
|
103 fail(e.toString()); |
|
104 } |
|
105 } |
|
106 |
|
107 private static Reader getReader(final String path) { |
|
108 return new InputStreamReader(SourceTest.class.getResourceAsStream(path)); |
|
109 } |
|
110 |
|
111 private static void testSources(final Source source1, final Source source2) { |
|
112 final char[] chars1 = source1.getContent(); |
|
113 final char[] chars2 = source2.getContent(); |
|
114 final String str1 = source1.getString(); |
|
115 final String str2 = source2.getString(); |
|
116 assertTrue(Arrays.equals(chars1, chars2)); |
|
117 assertEquals(str1, str2); |
|
118 assertEquals(source1.hashCode(), source2.hashCode()); |
|
119 assertTrue(source1.equals(source2)); |
|
120 assertTrue(Arrays.equals(source1.getContent(), str1.toCharArray())); |
|
121 assertTrue(Arrays.equals(source1.getContent(), chars1)); |
|
122 assertTrue(Arrays.equals(source1.getContent(), source2.getContent())); |
|
123 } |
|
124 } |
|