SourceTest.java revision 1239:77609e069f9f
14Srgrimes/* 24Srgrimes * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved. 34Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 44Srgrimes * 54Srgrimes * This code is free software; you can redistribute it and/or modify it 64Srgrimes * under the terms of the GNU General Public License version 2 only, as 74Srgrimes * published by the Free Software Foundation. Oracle designates this 84Srgrimes * particular file as subject to the "Classpath" exception as provided 94Srgrimes * by Oracle in the LICENSE file that accompanied this code. 104Srgrimes * 114Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT 124Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 134Srgrimes * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 144Srgrimes * version 2 for more details (a copy is included in the LICENSE file that 154Srgrimes * accompanied this code). 164Srgrimes * 174Srgrimes * You should have received a copy of the GNU General Public License version 184Srgrimes * 2 along with this work; if not, write to the Free Software Foundation, 194Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 204Srgrimes * 214Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 224Srgrimes * or visit www.oracle.com if you need additional information or have any 234Srgrimes * questions. 244Srgrimes */ 254Srgrimes 264Srgrimespackage jdk.nashorn.internal.runtime.test; 274Srgrimes 284Srgrimesimport static jdk.nashorn.internal.runtime.Source.sourceFor; 294Srgrimesimport static org.testng.Assert.assertEquals; 304Srgrimesimport static org.testng.Assert.assertTrue; 314Srgrimesimport static org.testng.Assert.fail; 324Srgrimesimport java.io.File; 334Srgrimesimport java.io.IOException; 344Srgrimesimport java.io.InputStreamReader; 354Srgrimesimport java.io.Reader; 36619Srgrimesimport java.net.URL; 378876Srgrimesimport java.util.Arrays; 384Srgrimesimport jdk.nashorn.api.scripting.URLReader; 394Srgrimesimport jdk.nashorn.internal.runtime.Source; 403185Ssosimport org.testng.annotations.Test; 413185Ssos 423185Ssos/** 433185Ssos * Tests different Source representations. 443185Ssos */ 452913Sache@SuppressWarnings("javadoc") 462913Sachepublic class SourceTest { 474Srgrimes 484Srgrimes final private static String SOURCE_NAME = "source.js"; 494Srgrimes final private static String SOURCE_STRING = "var x = 1;"; 502056Swollman final private static char[] SOURCE_CHARS = SOURCE_STRING.toCharArray(); 512056Swollman final private static String RESOURCE_PATH = "resources/load_test.js"; 522056Swollman final private static File SOURCE_FILE = new File("build/test/classes/jdk/nashorn/internal/runtime/" + RESOURCE_PATH); 532056Swollman final private static URL SOURCE_URL = SourceTest.class.getResource(RESOURCE_PATH); 544180Sbde 552056Swollman 562056Swollman @Test 572056Swollman public void testStringSource() { 587090Sbde testSources(sourceFor(SOURCE_NAME, SOURCE_STRING), sourceFor(SOURCE_NAME, SOURCE_STRING)); 592056Swollman testSources(sourceFor(SOURCE_NAME, SOURCE_STRING), sourceFor(SOURCE_NAME, SOURCE_CHARS)); 602056Swollman } 614Srgrimes 622873Sbde @Test 632873Sbde public void testCharArraySource() { 642873Sbde testSources(sourceFor(SOURCE_NAME, SOURCE_CHARS), sourceFor(SOURCE_NAME, SOURCE_CHARS)); 652873Sbde testSources(sourceFor(SOURCE_NAME, SOURCE_CHARS), sourceFor(SOURCE_NAME, SOURCE_STRING)); 662873Sbde } 672913Sache 682873Sbde @Test 694Srgrimes public void testURLSource() { 704Srgrimes try { 714Srgrimes testSources(sourceFor(SOURCE_NAME, SOURCE_URL), sourceFor(SOURCE_NAME, SOURCE_URL)); 724Srgrimes testSources(sourceFor(SOURCE_NAME, SOURCE_URL), sourceFor(SOURCE_NAME, new URLReader(SOURCE_URL))); 731390Ssos 744Srgrimes } catch (final IOException e) { 754180Sbde fail(e.toString()); 764180Sbde } 774180Sbde } 784180Sbde 794180Sbde @Test 804180Sbde public void testURLReaderSource() { 814180Sbde try { 824180Sbde System.err.println(SourceTest.class.getResource("")); 834180Sbde testSources(sourceFor(SOURCE_NAME, new URLReader(SOURCE_URL)), sourceFor(SOURCE_NAME, new URLReader(SOURCE_URL))); 844180Sbde testSources(sourceFor(SOURCE_NAME, new URLReader(SOURCE_URL)), sourceFor(SOURCE_NAME, SOURCE_URL)); 854180Sbde } catch (final IOException e) { 864180Sbde fail(e.toString()); 874180Sbde } 884180Sbde } 894180Sbde 904180Sbde @Test 914180Sbde public void testReaderSource() { 924180Sbde try { 934180Sbde testSources(sourceFor(SOURCE_NAME, getReader(RESOURCE_PATH)), sourceFor(SOURCE_NAME, getReader(RESOURCE_PATH))); 943366Sache } catch (final IOException e) { 958448Sbde fail(e.toString()); 962017Swollman } 974180Sbde } 982017Swollman 995291Sbde @Test 1004180Sbde public void testFileSource() { 1014180Sbde try { 1024180Sbde testSources(sourceFor(SOURCE_NAME, SOURCE_FILE), sourceFor(SOURCE_NAME, SOURCE_FILE)); 1031390Ssos } catch (final IOException e) { 1044180Sbde fail(e.toString()); 1055291Sbde } 1064180Sbde } 1074180Sbde 1084180Sbde private static Reader getReader(final String path) { 1094180Sbde return new InputStreamReader(SourceTest.class.getResourceAsStream(path)); 1104180Sbde } 1114180Sbde 1124180Sbde private static void testSources(final Source source1, final Source source2) { 1134180Sbde final char[] chars1 = source1.getContent(); 1144180Sbde final char[] chars2 = source2.getContent(); 1154180Sbde final String str1 = source1.getString(); 1164180Sbde final String str2 = source2.getString(); 1174180Sbde assertTrue(Arrays.equals(chars1, chars2)); 1184180Sbde assertEquals(str1, str2); 1194180Sbde assertEquals(source1.hashCode(), source2.hashCode()); 1204180Sbde assertTrue(source1.equals(source2)); 1214180Sbde assertTrue(Arrays.equals(source1.getContent(), str1.toCharArray())); 1224180Sbde assertTrue(Arrays.equals(source1.getContent(), chars1)); 1233185Ssos assertTrue(Arrays.equals(source1.getContent(), source2.getContent())); 124798Swollman } 1253185Ssos} 1261390Ssos