/* * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
// More general parameter limit testing framework, and designed so // that it could be expanded into a general limits-testing framework // in the future. publicclass NumArgsTest {
// threshold is named as such because "threshold" args is expected // to pass, and "threshold" + 1 args is expected to fail. privatefinalint threshold; privatefinalboolean isStaticMethod; privatefinal String result; privatefinal String testName; privatefinal String methodName; privatefinal NestingDef[] nesting; privatefinal File testdir; privatefinal JavacTool tool = JavacTool.create(); privatefinal JavacFileManager fm =
tool.getStandardFileManager(null, null, null); privateint errors = 0;
public NumArgsTest(finalint threshold, finalboolean isStaticMethod, final String result, final String methodName, final String testName, final NestingDef[] nesting) { this.threshold = threshold; this.isStaticMethod = isStaticMethod; this.result = result; this.methodName = methodName; this.testName = testName; this.nesting = nesting;
testdir = new File(testName);
testdir.mkdir();
}
public NumArgsTest(finalint threshold, finalboolean isStaticMethod, final String result, final String methodName, final String testName) { this(threshold, isStaticMethod, result, methodName,
testName, NO_NESTING);
}
public NumArgsTest(finalint threshold, final String result, final String methodName, final String testName, final NestingDef[] nesting) { this(threshold, false, result, methodName, testName, nesting);
}
public NumArgsTest(finalint threshold, final String result, final String methodName, final String testName) { this(threshold, false, result, methodName, testName, NO_NESTING);
}
public NumArgsTest(finalint threshold, final String testName, final NestingDef[] nesting) { this(threshold, null, null, testName, nesting);
}
public NumArgsTest(finalint threshold, final String testName) { this(threshold, null, null, testName, NO_NESTING);
}
public NumArgsTest(finalint threshold, final String testName, final String constructorName, final NestingDef[] nesting) { this(threshold, null, constructorName, testName, nesting);
}
protectedvoid writeArgs(finalint num, final PrintWriter stream) throws IOException {
stream.print("int x1"); for(int i = 1; i < num; i++)
stream.print(", int x" + (i + 1));
}
protectedvoid writeMethod(finalint num, final String name, final PrintWriter stream) throws IOException {
stream.write("public "); if (isStaticMethod) stream.write("static "); if (result == null)
stream.write(""); else {
stream.write(result);
stream.write(" ");
}
stream.write(name);
stream.write("(");
writeArgs(num, stream);
stream.write(") {}\n");
}
protectedvoid writeJavaFile(finalint num, finalboolean pass, final PrintWriter stream) throws IOException { final String fullName = testName + (pass ? "Pass" : "Fail");
stream.write("public class ");
stream.write(fullName);
stream.write(" {\n"); for(int i = 0; i < nesting.length; i++)
nesting[i].writeBefore(stream); if (null == methodName)
writeMethod(num, fullName, stream); else
writeMethod(num, methodName, stream); for(int i = nesting.length - 1; i >= 0; i--)
nesting[i].writeAfter(stream);
stream.write("}\n");
}
publicvoid runTest() throws Exception { // Run the pass test final String passTestName = testName + "Pass.java"; final StringWriter passBody = new StringWriter(); final PrintWriter passStream = new PrintWriter(passBody); final File passFile = new File(testdir, passTestName); final FileWriter passWriter = new FileWriter(passFile);
// Run the fail test final String failTestName = testName + "Fail.java"; final StringWriter failBody = new StringWriter(); final PrintWriter failStream = new PrintWriter(failBody); final File failFile = new File(testdir, failTestName); final FileWriter failWriter = new FileWriter(failFile);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.