< prev index next >

test/langtools/jdk/jshell/GetResourceTest.java

Print this page
rev 50604 : imported patch jep181-rev1
   1 /*
   2  * Copyright (c) 2015, 2017, 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
  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" +


  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, false, 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.*;");


   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" +


  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.*;");


< prev index next >