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