1 /*
   2  * Copyright (c) 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  * @summary Unit tests for Raw String Literal language changes
  27  * @library /tools/lib
  28  * @modules jdk.compiler/com.sun.tools.javac.api
  29  *          jdk.compiler/com.sun.tools.javac.main
  30  * @build toolbox.ToolBox toolbox.JavacTask
  31  * @run main/othervm RawStringLiteralLangAPI
  32  */
  33 
  34 import toolbox.JavacTask;
  35 import toolbox.Task;
  36 import toolbox.ToolBox;
  37 
  38 public class RawStringLiteralLangAPI {
  39     private static ToolBox TOOLBOX = new ToolBox();
  40 
  41     public static void main(String... args) {
  42         test1();
  43         test2();
  44         test3();
  45     }
  46 
  47     /*
  48      * Check that correct syntax is properly detected
  49      */
  50     static void test1() {
  51         for (int i = 1; i < 16; i++) {
  52             for (int j = 0; j < i; j++) {
  53                 compPass("public class NoEndRawString {\n" +
  54                         "    public static void main(String... args) {\n" +
  55                         "        String xxx = " +
  56                         "`".repeat(i) + "abc" + "`".repeat(j) + "def" +  "`".repeat(i) +
  57                         ";\n" +
  58                         "    }\n" +
  59                         "}\n");
  60             }
  61         }
  62     }
  63 
  64     /*
  65      * Check that syntax errors are properly detected
  66      */
  67     static void test2() {
  68         for (int i = 1; i < 16; i++) {
  69             for (int j = 0; j < i; j++) {
  70                 compFail("public class NoEndRawString {\n" +
  71                         "    public static void main(String... args) {\n" +
  72                         "        String xxx = " +
  73                         "`".repeat(i) + "abc" + "`".repeat(j) + "def" +  "`".repeat(i - 1) +
  74                         ";\n" +
  75                         "    }\n" +
  76                         "}\n", "compiler.err.unclosed.str.lit");
  77             }
  78         }
  79     }
  80 
  81     /*
  82      * Check that misuse of \u0060 is properly detected
  83      */
  84     static void test3() {
  85         compFail("public class BadDelimiter {\n" +
  86                 "    public static void main(String... args) {\n" +
  87                 "        String xxx = \\u0060`abc`;\n" +
  88                 "    }\n" +
  89                 "}\n", "compiler.err.illegal.char");
  90     }
  91 
  92     /*
  93      * Test source for successful compile.
  94      */
  95     static void compPass(String source) {
  96         new JavacTask(TOOLBOX)
  97                 .sources(source)
  98                 .classpath(".")
  99                 .options("--enable-preview", "-source", "12")
 100                 .run();
 101     }
 102 
 103     /*
 104      * Test source for unsuccessful compile and specific error.
 105      */
 106     static void compFail(String source, String error)  {
 107         String errors = new JavacTask(TOOLBOX)
 108                 .sources(source)
 109                 .classpath(".")
 110                 .options("-XDrawDiagnostics", "--enable-preview", "-source", "12")
 111                 .run(Task.Expect.FAIL)
 112                 .writeAll()
 113                 .getOutput(Task.OutputKind.DIRECT);
 114 
 115         if (!errors.contains(error)) {
 116             throw new RuntimeException("Wrong error");
 117         }
 118     }
 119 }