1 /*
   2  * Copyright (c) 2015, 2018, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8179531 8010319
  27  * @summary Check that ClassLoader.getResource works as expected in the JShell agent.
  28  * @modules jdk.jshell
  29  * @build KullaTesting TestingInputStream
  30  * @run testng GetResourceTest
  31  */
  32 
  33 import jdk.jshell.Snippet;
  34 import static jdk.jshell.Snippet.Status.OVERWRITTEN;
  35 import static jdk.jshell.Snippet.Status.VALID;
  36 import org.testng.annotations.Test;
  37 
  38 
  39 @Test
  40 public class GetResourceTest extends KullaTesting {
  41 
  42     public void checkGetResource() {
  43         assertEval("import java.util.Arrays;");
  44         assertEval("boolean match(byte[] data, byte[] snippet) {\n" +
  45                    "    for (int i = 0; i < data.length - snippet.length; i++) {\n" +
  46                    "        if (Arrays.equals(Arrays.copyOfRange(data, i, i + snippet.length), snippet)) {\n" +
  47                    "            return true;\n" +
  48                    "        }\n" +
  49                    "    }\n" +
  50                    "    return false;\n" +
  51                    "}");
  52         assertEval("boolean test() throws Exception {\n" +
  53                    "    Class c = new Object() {}.getClass().getEnclosingClass();\n" +
  54                    "    byte[] data = c.getClassLoader().getResource(c.getName().replace('.', '/') + \".class\").openStream().readAllBytes();\n" +
  55                    "    return match(data, \"check text\".getBytes(\"UTF-8\"));\n" +
  56                    "}");
  57         assertEval("test()", "true");
  58     }
  59 
  60     public void checkRedefine() {
  61         assertEval("import java.util.Arrays;");
  62         assertEval("boolean match(byte[] data, byte[] snippet) {\n" +
  63                    "    for (int i = 0; i < data.length - snippet.length; i++) {\n" +
  64                    "        if (Arrays.equals(Arrays.copyOfRange(data, i, i + snippet.length), snippet)) {\n" +
  65                    "            return true;\n" +
  66                    "        }\n" +
  67                    "    }\n" +
  68                    "    return false;\n" +
  69                    "}");
  70         Snippet testMethod =
  71                 methodKey(assertEval("boolean test() throws Exception {\n" +
  72                                      "    return false;\n" +
  73                                      "}"));
  74         assertEval("boolean test() throws Exception {\n" +
  75                    "    Class c = new Object() {}.getClass().getEnclosingClass();\n" +
  76                    "    byte[] data = c.getClassLoader().getResource(c.getName().replace('.', '/') + \".class\").openStream().readAllBytes();\n" +
  77                    "    return match(data, \"updated variant\".getBytes(\"UTF-8\"));\n" +
  78                    "}",
  79                    IGNORE_VALUE,
  80                    null,
  81                    DiagCheck.DIAG_OK,
  82                    DiagCheck.DIAG_OK,
  83                    ste(MAIN_SNIPPET, VALID, VALID, true, null),
  84                    ste(testMethod, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
  85         assertEval("test()", "true");
  86     }
  87 
  88     public void checkResourceSize() {
  89         assertEval("import java.net.*;");
  90         assertEval("boolean test() throws Exception {\n" +
  91                    "    Class c = new Object() {}.getClass().getEnclosingClass();" +
  92                    "    URL url = c.getClassLoader().getResource(c.getName().replace('.', '/') + \".class\");\n" +
  93                    "    URLConnection connection = url.openConnection();\n" +
  94                    "    connection.connect();\n" +
  95                    "    return connection.getContentLength() == connection.getInputStream().readAllBytes().length;\n" +
  96                    "}");
  97         assertEval("test()", "true");
  98     }
  99 
 100     public void checkTimestampCheck() {
 101         assertEval("import java.net.*;");
 102         assertEval("import java.time.*;");
 103         assertEval("import java.time.format.*;");
 104         assertEval("long[] times(Class c) throws Exception {\n" +
 105                    "    URL url = c.getClassLoader().getResource(c.getName().replace('.', '/') + \".class\");\n" +
 106                    "    URLConnection connection = url.openConnection();\n" +
 107                    "    connection.connect();\n" +
 108                    "    return new long[] {connection.getDate(),\n" +
 109                    "                       connection.getLastModified()," +
 110                    "                       Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(connection.getHeaderField(\"last-modified\"))).toEpochMilli()};\n" +
 111                    "}");
 112         Snippet testMethod =
 113                 methodKey(assertEval("long[] test() throws Exception {\n" +
 114                                      "    int i = 0;\n" +
 115                                      "    return times(new Object() {}.getClass().getEnclosingClass());\n" +
 116                                      "}"));
 117         assertEval("long[] orig = test();");
 118         long s = System.currentTimeMillis();
 119         while ((System.currentTimeMillis() - s) < 1000) { //ensure time change:
 120             try {
 121                 Thread.sleep(1000);
 122             } catch (InterruptedException ex) {}
 123         }
 124         assertEval("long[] test() throws Exception {\n" +
 125                    "    int i = 1;\n" +
 126                    "    return times(new Object() {}.getClass().getEnclosingClass());\n" +
 127                    "}",
 128                    IGNORE_VALUE,
 129                    null,
 130                    DiagCheck.DIAG_OK,
 131                    DiagCheck.DIAG_OK,
 132                    ste(MAIN_SNIPPET, VALID, VALID, false, null),
 133                    ste(testMethod, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
 134         assertEval("long[] nue = test();");
 135         assertEval("orig[0] < nue[0]", "true");
 136         assertEval("orig[1] < nue[1]", "true");
 137         assertEval("orig[0] == orig[2]", "true");
 138         assertEval("nue[0] == nue[2]", "true");
 139     }
 140 
 141     public void checkFieldAccess() {
 142         assertEval("import java.net.*;");
 143         assertEval("Class c = new Object() {}.getClass().getEnclosingClass();");
 144         assertEval("URL url = c.getClassLoader().getResource(c.getName().replace('.', '/') + \".class\");");
 145         assertEval("URLConnection connection = url.openConnection();");
 146         assertEval("connection.connect();");
 147         assertEval("connection.getHeaderFieldKey(0)", "\"content-length\"");
 148         assertEval("connection.getHeaderFieldKey(1)", "\"date\"");
 149         assertEval("connection.getHeaderFieldKey(2)", "\"last-modified\"");
 150         assertEval("connection.getHeaderFieldKey(3)", "null");
 151         assertEval("connection.getHeaderField(0) != null", "true");
 152         assertEval("connection.getHeaderField(1) != null", "true");
 153         assertEval("connection.getHeaderField(2) != null", "true");
 154         assertEval("connection.getHeaderField(3) == null", "true");
 155     }
 156 
 157     public void checkGetResources() {
 158         assertEval("import java.net.*;");
 159         assertEval("Class c = new Object() {}.getClass().getEnclosingClass();");
 160         assertEval("c.getClassLoader().getResources(c.getName().replace('.', '/') + \".class\").hasMoreElements()", "true");
 161     }
 162 
 163 }
 164