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