1 /* 2 * Copyright (c) 2019, 2023, 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 package tools.javac.combo; 24 25 import java.io.File; 26 import java.io.IOException; 27 28 import java.util.Arrays; 29 import java.util.function.Consumer; 30 import java.util.stream.IntStream; 31 32 import javax.tools.Diagnostic; 33 34 /** 35 * Base class for negative and positive compilation tests. 36 */ 37 public class CompilationTestCase extends JavacTemplateTestBase { 38 private String[] compileOptions = new String[]{}; 39 private String defaultFileName = "Source.java"; 40 private String programShell = "#"; 41 42 protected void setProgramShell(String shell) { 43 programShell = shell; 44 } 45 46 protected void setCompileOptions(String... options) { 47 compileOptions = options.clone(); 48 } 49 50 protected String[] getCompileOptions() { 51 return compileOptions.clone(); 52 } 53 54 protected void appendCompileOptions(String... additionalOptions) { 55 String[] moreOptions = additionalOptions.clone(); 56 String[] newCompileOptions = Arrays.copyOf(compileOptions, compileOptions.length + additionalOptions.length); 57 IntStream.range(0, additionalOptions.length).forEach(i -> { 58 newCompileOptions[newCompileOptions.length - additionalOptions.length + i] = additionalOptions[i]; 59 }); 60 compileOptions = newCompileOptions; 61 } 62 63 protected void removeLastCompileOptions(int i) { 64 if (i < 0) { 65 throw new AssertionError("unexpected negative value " + i); 66 } 67 if (i >= compileOptions.length) { 68 compileOptions = new String[]{}; 69 } else { 70 compileOptions = Arrays.copyOf(compileOptions, compileOptions.length - i); 71 } 72 } 73 74 protected void setDefaultFilename(String name) { 75 defaultFileName = name; 76 } 77 78 protected String expandMarkers(String... constructs) { 79 String s = programShell; 80 for (String c : constructs) 81 s = s.replaceFirst("#", c); 82 return s; 83 } 84 85 private File assertCompile(String program, Runnable postTest, boolean generate) { 86 reset(); 87 addCompileOptions(compileOptions); 88 addSourceFile(defaultFileName, program); 89 File dir = null; 90 try { 91 dir = compile(generate); 92 } catch (IOException e) { 93 throw new RuntimeException(e); 94 } 95 postTest.run(); 96 return dir; 97 } 98 99 protected void assertOK(String... constructs) { 100 assertCompile(expandMarkers(constructs), this::assertCompileSucceeded, false); 101 } 102 103 protected File assertOK(boolean generate, String... constructs) { 104 return assertCompile(expandMarkers(constructs), this::assertCompileSucceeded, generate); 105 } 106 107 protected File assertOK(Consumer<Diagnostic<?>> diagConsumer, String... constructs) { 108 return assertCompile(expandMarkers(constructs), () -> assertCompileSucceeded(diagConsumer), false); 109 } 110 111 protected void assertOKWithWarning(String warning, String... constructs) { 112 assertCompile(expandMarkers(constructs), () -> assertCompileSucceededWithWarning(warning), false); 113 } 114 115 protected void assertOKWithWarning(String warning, int numberOfTimes, String... constructs) { 116 assertCompile(expandMarkers(constructs), () -> assertCompileSucceededWithWarning(warning, numberOfTimes), false); 117 } 118 119 protected void assertFail(String expectedDiag, String... constructs) { 120 assertCompile(expandMarkers(constructs), () -> assertCompileFailed(expectedDiag), false); 121 } 122 123 protected void assertFail(String expectedDiag, Consumer<Diagnostic<?>> diagConsumer, String... constructs) { 124 assertCompile(expandMarkers(constructs), () -> assertCompileFailed(expectedDiag, diagConsumer), false); 125 } 126 }