1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/limits/NumArgsTest.java Thu Jun 27 00:37:13 2013 -0400 1.3 @@ -0,0 +1,269 @@ 1.4 +/* 1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +import com.sun.tools.javac.util.*; 1.28 +import com.sun.tools.javac.api.*; 1.29 +import com.sun.tools.javac.file.*; 1.30 +import java.io.*; 1.31 +import java.util.*; 1.32 +import javax.tools.*; 1.33 + 1.34 +// More general parameter limit testing framework, and designed so 1.35 +// that it could be expanded into a general limits-testing framework 1.36 +// in the future. 1.37 +public class NumArgsTest { 1.38 + 1.39 + private static final NumArgsTest.NestingDef[] NO_NESTING = {}; 1.40 + 1.41 + // threshold is named as such because "threshold" args is expected 1.42 + // to pass, and "threshold" + 1 args is expected to fail. 1.43 + private final int threshold; 1.44 + private final boolean isStaticMethod; 1.45 + private final String result; 1.46 + private final String testName; 1.47 + private final String methodName; 1.48 + private final NestingDef[] nesting; 1.49 + private final File testdir; 1.50 + private final JavacTool tool = JavacTool.create(); 1.51 + private final JavacFileManager fm = 1.52 + tool.getStandardFileManager(null, null, null); 1.53 + private int errors = 0; 1.54 + 1.55 + public NumArgsTest(final int threshold, 1.56 + final boolean isStaticMethod, 1.57 + final String result, 1.58 + final String methodName, 1.59 + final String testName, 1.60 + final NestingDef[] nesting) { 1.61 + this.threshold = threshold; 1.62 + this.isStaticMethod = isStaticMethod; 1.63 + this.result = result; 1.64 + this.methodName = methodName; 1.65 + this.testName = testName; 1.66 + this.nesting = nesting; 1.67 + testdir = new File(testName); 1.68 + testdir.mkdir(); 1.69 + } 1.70 + 1.71 + public NumArgsTest(final int threshold, 1.72 + final boolean isStaticMethod, 1.73 + final String result, 1.74 + final String methodName, 1.75 + final String testName) { 1.76 + this(threshold, isStaticMethod, result, methodName, 1.77 + testName, NO_NESTING); 1.78 + } 1.79 + 1.80 + public NumArgsTest(final int threshold, 1.81 + final String result, 1.82 + final String methodName, 1.83 + final String testName, 1.84 + final NestingDef[] nesting) { 1.85 + this(threshold, false, result, methodName, testName, nesting); 1.86 + } 1.87 + 1.88 + public NumArgsTest(final int threshold, 1.89 + final String result, 1.90 + final String methodName, 1.91 + final String testName) { 1.92 + this(threshold, false, result, methodName, testName, NO_NESTING); 1.93 + } 1.94 + 1.95 + public NumArgsTest(final int threshold, 1.96 + final String testName, 1.97 + final NestingDef[] nesting) { 1.98 + this(threshold, null, null, testName, nesting); 1.99 + } 1.100 + 1.101 + public NumArgsTest(final int threshold, 1.102 + final String testName) { 1.103 + this(threshold, null, null, testName, NO_NESTING); 1.104 + } 1.105 + 1.106 + public NumArgsTest(final int threshold, 1.107 + final String testName, 1.108 + final String constructorName, 1.109 + final NestingDef[] nesting) { 1.110 + this(threshold, null, constructorName, testName, nesting); 1.111 + } 1.112 + 1.113 + protected void writeArgs(final int num, final PrintWriter stream) 1.114 + throws IOException { 1.115 + stream.print("int x1"); 1.116 + for(int i = 1; i < num; i++) 1.117 + stream.print(", int x" + (i + 1)); 1.118 + } 1.119 + 1.120 + protected void writeMethod(final int num, 1.121 + final String name, 1.122 + final PrintWriter stream) 1.123 + throws IOException { 1.124 + stream.write("public "); 1.125 + if (isStaticMethod) stream.write("static "); 1.126 + if (result == null) 1.127 + stream.write(""); 1.128 + else { 1.129 + stream.write(result); 1.130 + stream.write(" "); 1.131 + } 1.132 + stream.write(name); 1.133 + stream.write("("); 1.134 + writeArgs(num, stream); 1.135 + stream.write(") {}\n"); 1.136 + } 1.137 + 1.138 + protected void writeJavaFile(final int num, 1.139 + final boolean pass, 1.140 + final PrintWriter stream) 1.141 + throws IOException { 1.142 + final String fullName = testName + (pass ? "Pass" : "Fail"); 1.143 + stream.write("public class "); 1.144 + stream.write(fullName); 1.145 + stream.write(" {\n"); 1.146 + for(int i = 0; i < nesting.length; i++) 1.147 + nesting[i].writeBefore(stream); 1.148 + if (null == methodName) 1.149 + writeMethod(num, fullName, stream); 1.150 + else 1.151 + writeMethod(num, methodName, stream); 1.152 + for(int i = nesting.length - 1; i >= 0; i--) 1.153 + nesting[i].writeAfter(stream); 1.154 + stream.write("}\n"); 1.155 + } 1.156 + 1.157 + public void runTest() throws Exception { 1.158 + // Run the pass test 1.159 + final String passTestName = testName + "Pass.java"; 1.160 + final StringWriter passBody = new StringWriter(); 1.161 + final PrintWriter passStream = new PrintWriter(passBody); 1.162 + final File passFile = new File(testdir, passTestName); 1.163 + final FileWriter passWriter = new FileWriter(passFile); 1.164 + 1.165 + writeJavaFile(threshold, true, passStream); 1.166 + passStream.close(); 1.167 + passWriter.write(passBody.toString()); 1.168 + passWriter.close(); 1.169 + 1.170 + final StringWriter passSW = new StringWriter(); 1.171 + final String[] passArgs = { passFile.toString() }; 1.172 + final Iterable<? extends JavaFileObject> passFiles = 1.173 + fm.getJavaFileObjectsFromFiles(Arrays.asList(passFile)); 1.174 + final JavaCompiler.CompilationTask passTask = 1.175 + tool.getTask(passSW, fm, null, null, null, passFiles); 1.176 + 1.177 + if (!passTask.call()) { 1.178 + errors++; 1.179 + System.err.println("Compilation unexpectedly failed. Body:\n" + 1.180 + passBody); 1.181 + System.err.println("Output:\n" + passSW.toString()); 1.182 + } 1.183 + 1.184 + // Run the fail test 1.185 + final String failTestName = testName + "Fail.java"; 1.186 + final StringWriter failBody = new StringWriter(); 1.187 + final PrintWriter failStream = new PrintWriter(failBody); 1.188 + final File failFile = new File(testdir, failTestName); 1.189 + final FileWriter failWriter = new FileWriter(failFile); 1.190 + 1.191 + writeJavaFile(threshold + 1, false, failStream); 1.192 + failStream.close(); 1.193 + failWriter.write(failBody.toString()); 1.194 + failWriter.close(); 1.195 + 1.196 + final StringWriter failSW = new StringWriter(); 1.197 + final TestDiagnosticHandler failDiag = 1.198 + new TestDiagnosticHandler("compiler.err.limit.parameters"); 1.199 + final Iterable<? extends JavaFileObject> failFiles = 1.200 + fm.getJavaFileObjectsFromFiles(Arrays.asList(failFile)); 1.201 + final JavaCompiler.CompilationTask failTask = 1.202 + tool.getTask(failSW, 1.203 + tool.getStandardFileManager(null, null, null), 1.204 + failDiag, 1.205 + null, 1.206 + null, 1.207 + failFiles); 1.208 + 1.209 + if (failTask.call()) { 1.210 + errors++; 1.211 + System.err.println("Compilation unexpectedly succeeded."); 1.212 + System.err.println("Input:\n" + failBody); 1.213 + } 1.214 + 1.215 + if (!failDiag.sawError) { 1.216 + errors++; 1.217 + System.err.println("Did not see expected compile error."); 1.218 + } 1.219 + 1.220 + if (errors != 0) 1.221 + throw new RuntimeException("Test failed with " + 1.222 + errors + " errors"); 1.223 + } 1.224 + 1.225 + public static NestingDef classNesting(final String name) { 1.226 + return new NestedClassBuilder(name, false); 1.227 + } 1.228 + 1.229 + public static NestingDef classNesting(final String name, 1.230 + final boolean isStatic) { 1.231 + return new NestedClassBuilder(name, isStatic); 1.232 + } 1.233 + 1.234 + protected interface NestingDef { 1.235 + public abstract void writeBefore(final PrintWriter stream); 1.236 + public abstract void writeAfter(final PrintWriter stream); 1.237 + } 1.238 + 1.239 + private static class NestedClassBuilder implements NestingDef { 1.240 + private final String name; 1.241 + private final boolean isStatic; 1.242 + public NestedClassBuilder(final String name, final boolean isStatic) { 1.243 + this.name = name; 1.244 + this.isStatic = isStatic; 1.245 + } 1.246 + public void writeBefore(final PrintWriter stream) { 1.247 + stream.write("public "); 1.248 + if (isStatic) stream.write("static"); 1.249 + stream.write(" class "); 1.250 + stream.write(name); 1.251 + stream.write(" {\n"); 1.252 + } 1.253 + public void writeAfter(final PrintWriter stream) { 1.254 + stream.write("}\n"); 1.255 + } 1.256 + } 1.257 + 1.258 + public class TestDiagnosticHandler<T> implements DiagnosticListener<T> { 1.259 + public boolean sawError; 1.260 + public final String target; 1.261 + 1.262 + public TestDiagnosticHandler(final String target) { 1.263 + this.target = target; 1.264 + } 1.265 + 1.266 + public void report(final Diagnostic<? extends T> diag) { 1.267 + if (diag.getCode().equals(target)) 1.268 + sawError = true; 1.269 + } 1.270 + } 1.271 + 1.272 +}