Path: blob/master/test/langtools/jdk/jshell/GetResourceTest.java
40931 views
/*1* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8179531 801031926* @summary Check that ClassLoader.getResource works as expected in the JShell agent.27* @modules jdk.jshell28* @build KullaTesting TestingInputStream29* @run testng GetResourceTest30*/3132import jdk.jshell.Snippet;33import static jdk.jshell.Snippet.Status.OVERWRITTEN;34import static jdk.jshell.Snippet.Status.VALID;35import org.testng.annotations.Test;363738@Test39public class GetResourceTest extends KullaTesting {4041public void checkGetResource() {42assertEval("import java.util.Arrays;");43assertEval("boolean match(byte[] data, byte[] snippet) {\n" +44" for (int i = 0; i < data.length - snippet.length; i++) {\n" +45" if (Arrays.equals(Arrays.copyOfRange(data, i, i + snippet.length), snippet)) {\n" +46" return true;\n" +47" }\n" +48" }\n" +49" return false;\n" +50"}");51assertEval("boolean test() throws Exception {\n" +52" Class c = new Object() {}.getClass().getEnclosingClass();\n" +53" byte[] data = c.getClassLoader().getResource(c.getName().replace('.', '/') + \".class\").openStream().readAllBytes();\n" +54" return match(data, \"check text\".getBytes(\"UTF-8\"));\n" +55"}");56assertEval("test()", "true");57}5859public void checkRedefine() {60assertEval("import java.util.Arrays;");61assertEval("boolean match(byte[] data, byte[] snippet) {\n" +62" for (int i = 0; i < data.length - snippet.length; i++) {\n" +63" if (Arrays.equals(Arrays.copyOfRange(data, i, i + snippet.length), snippet)) {\n" +64" return true;\n" +65" }\n" +66" }\n" +67" return false;\n" +68"}");69Snippet testMethod =70methodKey(assertEval("boolean test() throws Exception {\n" +71" return false;\n" +72"}"));73assertEval("boolean test() throws Exception {\n" +74" Class c = new Object() {}.getClass().getEnclosingClass();\n" +75" byte[] data = c.getClassLoader().getResource(c.getName().replace('.', '/') + \".class\").openStream().readAllBytes();\n" +76" return match(data, \"updated variant\".getBytes(\"UTF-8\"));\n" +77"}",78IGNORE_VALUE,79null,80DiagCheck.DIAG_OK,81DiagCheck.DIAG_OK,82ste(MAIN_SNIPPET, VALID, VALID, true, null),83ste(testMethod, VALID, OVERWRITTEN, false, MAIN_SNIPPET));84assertEval("test()", "true");85}8687public void checkResourceSize() {88assertEval("import java.net.*;");89assertEval("boolean test() throws Exception {\n" +90" Class c = new Object() {}.getClass().getEnclosingClass();" +91" URL url = c.getClassLoader().getResource(c.getName().replace('.', '/') + \".class\");\n" +92" URLConnection connection = url.openConnection();\n" +93" connection.connect();\n" +94" return connection.getContentLength() == connection.getInputStream().readAllBytes().length;\n" +95"}");96assertEval("test()", "true");97}9899public void checkTimestampCheck() {100assertEval("import java.net.*;");101assertEval("import java.time.*;");102assertEval("import java.time.format.*;");103assertEval("long[] times(Class c) throws Exception {\n" +104" URL url = c.getClassLoader().getResource(c.getName().replace('.', '/') + \".class\");\n" +105" URLConnection connection = url.openConnection();\n" +106" connection.connect();\n" +107" return new long[] {connection.getDate(),\n" +108" connection.getLastModified()," +109" Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(connection.getHeaderField(\"last-modified\"))).toEpochMilli()};\n" +110"}");111Snippet testMethod =112methodKey(assertEval("long[] test() throws Exception {\n" +113" int i = 0;\n" +114" return times(new Object() {}.getClass().getEnclosingClass());\n" +115"}"));116assertEval("long[] orig = test();");117long s = System.currentTimeMillis();118while ((System.currentTimeMillis() - s) < 1000) { //ensure time change:119try {120Thread.sleep(1000);121} catch (InterruptedException ex) {}122}123assertEval("long[] test() throws Exception {\n" +124" int i = 1;\n" +125" return times(new Object() {}.getClass().getEnclosingClass());\n" +126"}",127IGNORE_VALUE,128null,129DiagCheck.DIAG_OK,130DiagCheck.DIAG_OK,131ste(MAIN_SNIPPET, VALID, VALID, false, null),132ste(testMethod, VALID, OVERWRITTEN, false, MAIN_SNIPPET));133assertEval("long[] nue = test();");134assertEval("orig[0] < nue[0]", "true");135assertEval("orig[1] < nue[1]", "true");136assertEval("orig[0] == orig[2]", "true");137assertEval("nue[0] == nue[2]", "true");138}139140public void checkFieldAccess() {141assertEval("import java.net.*;");142assertEval("Class c = new Object() {}.getClass().getEnclosingClass();");143assertEval("URL url = c.getClassLoader().getResource(c.getName().replace('.', '/') + \".class\");");144assertEval("URLConnection connection = url.openConnection();");145assertEval("connection.connect();");146assertEval("connection.getHeaderFieldKey(0)", "\"content-length\"");147assertEval("connection.getHeaderFieldKey(1)", "\"date\"");148assertEval("connection.getHeaderFieldKey(2)", "\"last-modified\"");149assertEval("connection.getHeaderFieldKey(3)", "null");150assertEval("connection.getHeaderField(0) != null", "true");151assertEval("connection.getHeaderField(1) != null", "true");152assertEval("connection.getHeaderField(2) != null", "true");153assertEval("connection.getHeaderField(3) == null", "true");154}155156public void checkGetResources() {157assertEval("import java.net.*;");158assertEval("Class c = new Object() {}.getClass().getEnclosingClass();");159assertEval("c.getClassLoader().getResources(c.getName().replace('.', '/') + \".class\").hasMoreElements()", "true");160}161162}163164165166