1 /*
   2  * Copyright (c) 2015, 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 8137167
  27  * @summary Tests directive json parser
  28  * @library /testlibrary /../../test/lib ../share /
  29  * @run driver compiler.compilercontrol.parser.DirectiveParser
  30  */
  31 
  32 package compiler.compilercontrol.parser;
  33 
  34 import compiler.compilercontrol.share.JSONFile;
  35 import jdk.test.lib.Asserts;
  36 import jdk.test.lib.OutputAnalyzer;
  37 import jdk.test.lib.ProcessTools;
  38 import jdk.test.lib.Utils;
  39 
  40 public class DirectiveParser {
  41     private static final String ERROR_MSG = "VM should exit with error "
  42             + "on incorrect JSON file: ";
  43     private static final String EXPECTED_ERROR_STRING = "Parsing of compiler"
  44             + " directives failed";
  45 
  46     public static void main(String[] args) {
  47         simpleTest();
  48         nonMatchingBrackets();
  49         arrayTest();
  50         emptyObjectTest();
  51         emptyFile();
  52         noFile();
  53         directory();
  54     }
  55 
  56     private static void simpleTest() {
  57         String fileName = "simple.json";
  58         try (JSONFile file = new JSONFile(fileName)) {
  59             file.write(JSONFile.Element.ARRAY)
  60                     .write(JSONFile.Element.OBJECT)
  61                         .write(JSONFile.Element.PAIR, "match")
  62                         .write(JSONFile.Element.VALUE, "\"java/lang/String.*\"")
  63                         .write(JSONFile.Element.PAIR, "c2")
  64                         .write(JSONFile.Element.OBJECT)
  65                             .write(JSONFile.Element.PAIR, "inline")
  66                             .write(JSONFile.Element.ARRAY)
  67                                 .write(JSONFile.Element.VALUE, "\"+*.indexOf\"")
  68                                 .write(JSONFile.Element.VALUE, "\"-a.b\"")
  69                             .end()
  70                         .end()
  71                     .end() // end object
  72                     .write(JSONFile.Element.OBJECT)
  73                         .write(JSONFile.Element.PAIR, "match")
  74                         .write(JSONFile.Element.VALUE, "\"*.indexOf\"")
  75                         .write(JSONFile.Element.PAIR, "c1")
  76                         .write(JSONFile.Element.OBJECT)
  77                             .write(JSONFile.Element.PAIR, "enable")
  78                             .write(JSONFile.Element.VALUE, "false")
  79                         .end()
  80                     .end(); // end object
  81             file.end();
  82         }
  83         OutputAnalyzer output = execute(fileName);
  84         output.shouldHaveExitValue(0);
  85         output.shouldNotContain(EXPECTED_ERROR_STRING);
  86     }
  87 
  88     private static void nonMatchingBrackets() {
  89         String fileName = "non-matching.json";
  90         try (JSONFile file = new JSONFile(fileName)) {
  91             file.write(JSONFile.Element.ARRAY)
  92                     .write(JSONFile.Element.OBJECT)
  93                         .write(JSONFile.Element.PAIR, "match")
  94                         .write(JSONFile.Element.VALUE, "\"java/lang/String.*\"")
  95                     .end();
  96             // don't write matching }
  97         }
  98         OutputAnalyzer output = execute(fileName);
  99         Asserts.assertNE(output.getExitValue(), 0, ERROR_MSG + "non matching "
 100                 + "brackets");
 101         output.shouldContain(EXPECTED_ERROR_STRING);
 102     }
 103 
 104     private static void arrayTest() {
 105         String fileName = "array.json";
 106         try (JSONFile file = new JSONFile(fileName)) {
 107             file.write(JSONFile.Element.ARRAY);
 108             file.end();
 109         }
 110         OutputAnalyzer output = execute(fileName);
 111         Asserts.assertNE(output.getExitValue(), 0, ERROR_MSG + "empty array");
 112     }
 113 
 114     private static void emptyObjectTest() {
 115         String fileName = "emptyObject.json";
 116         try (JSONFile file = new JSONFile(fileName)) {
 117             file.write(JSONFile.Element.OBJECT);
 118             file.end();
 119         }
 120         OutputAnalyzer output = execute(fileName);
 121         Asserts.assertNE(output.getExitValue(), 0, ERROR_MSG + "empty object "
 122                 + "without any match");
 123         output.shouldContain(EXPECTED_ERROR_STRING);
 124     }
 125 
 126     private static void emptyFile() {
 127         String fileName = "empty.json";
 128         try (JSONFile file = new JSONFile(fileName)) {
 129             // empty
 130         }
 131         OutputAnalyzer output = execute(fileName);
 132         Asserts.assertNE(output.getExitValue(), 0, ERROR_MSG + "empty file");
 133         output.shouldContain(EXPECTED_ERROR_STRING);
 134     }
 135 
 136     private static void noFile() {
 137         OutputAnalyzer output = execute("nonexistent.json");
 138         Asserts.assertNE(output.getExitValue(), 0, ERROR_MSG + "non existing "
 139                 + "file");
 140     }
 141 
 142     private static void directory() {
 143         OutputAnalyzer output = execute(Utils.TEST_SRC);
 144         Asserts.assertNE(output.getExitValue(), 0, ERROR_MSG + "directory as "
 145                 + "a name");
 146     }
 147 
 148     private static OutputAnalyzer execute(String fileName) {
 149         OutputAnalyzer output;
 150         try {
 151             output = ProcessTools.executeTestJvm(
 152                     "-XX:+UnlockDiagnosticVMOptions",
 153                     "-XX:CompilerDirectivesFile=" + fileName,
 154                     "-version");
 155         } catch (Throwable thr) {
 156             throw new Error("Execution failed", thr);
 157         }
 158         return output;
 159     }
 160 }