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