PrepTestNG.java revision 2768102f5fd52a5988c245a1032c777ed3c0e31b
0N/A/*
0N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms of the
0N/A * Common Development and Distribution License, Version 1.0 only
0N/A * (the "License"). You may not use this file except in compliance
0N/A * with the License.
0N/A *
0N/A * You can obtain a copy of the license at
0N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE
0N/A * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
0N/A * See the License for the specific language governing permissions
0N/A * and limitations under the License.
0N/A *
0N/A * When distributing Covered Code, include this CDDL HEADER in each
0N/A * file and include the License file at
0N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
0N/A * add the following below this CDDL HEADER, with the fields enclosed
0N/A * by brackets "[]" replaced with your own identifying information:
0N/A * Portions Copyright [yyyy] [name of copyright owner]
0N/A *
0N/A * CDDL HEADER END
0N/A *
0N/A *
0N/A * Portions Copyright 2006-2007 Sun Microsystems, Inc.
0N/A */
0N/A
0N/Apackage org.opends.build.tools;
0N/A
0N/Aimport java.io.FileOutputStream;
0N/Aimport java.io.PrintStream;
0N/Aimport java.io.BufferedReader;
0N/Aimport java.io.FileReader;
0N/Aimport java.util.Arrays;
0N/A
0N/Aimport org.apache.tools.ant.Task;
0N/Aimport org.apache.tools.ant.BuildException;
0N/A
0N/Apublic class PrepTestNG extends Task
0N/A{
0N/A
0N/A /** Template for inserting children elements of default test tag */
0N/A static private final String DEFAULT_TAGS_TEMPLATE =
0N/A "<!-- DO NOT REMOVE! - GENERATED DEFAULT TAGS (see PrepTestNG class) -->";
0N/A
0N/A /** Template for inserting global children elements of run tags */
0N/A static private final String GLOBAL_RUN_TAGS_TEMPLATE =
0N/A "<!-- DO NOT REMOVE! - GENERATED GLOBAL RUN TAGS (see PrepTestNG class) -->";
0N/A
0N/A /** Indentation used in testng.xml */
0N/A static private final int INDENT = 4;
0N/A
0N/A private String file;
0N/A private String toFile;
0N/A private String groupList;
0N/A private String packageList;
0N/A private String classList;
0N/A private String methodList;
0N/A
0N/A public void setFile(String file)
0N/A {
0N/A this.file = file;
0N/A }
0N/A
0N/A public void setToFile(String toFile)
0N/A {
0N/A this.toFile = toFile;
0N/A }
0N/A
0N/A public void setGroupList(String groupList)
0N/A {
0N/A this.groupList = groupList;
0N/A }
0N/A
0N/A public void setPackageList(String packageList)
0N/A {
0N/A this.packageList = packageList;
0N/A }
0N/A
0N/A public void setClassList(String classList)
0N/A {
0N/A this.classList = classList;
0N/A }
0N/A
0N/A public void setMethodList(String methodList)
0N/A {
0N/A this.methodList = methodList;
0N/A }
0N/A
0N/A public void execute() throws BuildException
0N/A {
0N/A if(file == null)
0N/A {
0N/A throw new BuildException("Attribute file must be set to the orginal " +
0N/A "TestNG XML file");
0N/A }
0N/A
0N/A if(toFile == null)
0N/A {
0N/A throw new BuildException("Attribute toFile must be set to the modified " +
0N/A "TestNG XML file");
0N/A }
0N/A
0N/A BufferedReader reader;
0N/A FileOutputStream outFile;
0N/A PrintStream writer;
0N/A String line;
0N/A String[] groups;
0N/A String[] packages;
0N/A String[] classes;
0N/A String[] methods;
0N/A String[] groupLine;
0N/A String[] methodLine;
0N/A String methodClass;
0N/A String methodName;
0N/A int methodNameStartIdx;
0N/A int groupCount = 0;
0N/A int packageCount = 0;
0N/A int classCount = 0;
0N/A int methodCount = 0;
0N/A
0N/A try
0N/A {
0N/A reader = new BufferedReader(new FileReader(file));
0N/A outFile = new FileOutputStream(toFile);
0N/A
0N/A writer = new PrintStream(outFile);
0N/A
0N/A line = reader.readLine();
0N/A
0N/A if(groupList != null && !groupList.trim().equals("") &&
0N/A !groupList.startsWith("${"))
0N/A {
0N/A groups = groupList.split(",");
0N/A }
0N/A else
0N/A {
0N/A groups = new String[0];
0N/A }
0N/A if(packageList != null && !packageList.trim().equals("") &&
0N/A !packageList.startsWith("${"))
0N/A {
packages = packageList.split(",");
}
else
{
packages = new String[0];
}
if(classList != null && !classList.trim().equals("") &&
!classList.startsWith("${"))
{
classes = classList.split(",");
}
else
{
classes = new String[0];
}
if(methodList != null && !methodList.trim().equals("") &&
!methodList.startsWith("${"))
{
methods = methodList.split(";");
}
else
{
methods = new String[0];
}
while(line != null)
{
if(line.indexOf(DEFAULT_TAGS_TEMPLATE) >= 0)
{
int level = 2;
if(groups.length > 0)
{
boolean windowsClause = false;
println(writer, level, "<groups>");
println(writer, ++level, "<run>");
level++;
for(String group : groups)
{
groupLine = group.split("=");
if(groupLine.length == 2)
{
String inc_exc = groupLine[0].trim();
if (inc_exc == null ||
!("include".equals(inc_exc.toLowerCase()) ||
"exclude".equals(inc_exc.toLowerCase()))) {
System.out.println("Error: illegal group clause " + group);
} else {
String gr = groupLine[1].trim();
println(writer, level, "<" +inc_exc +" "+
"name=\""+gr+ "\" />");
windowsClause |= "windows".equals(gr);
groupCount++;
}
}
}
// Exclude windows specific tests if the user has not provided
// an explicit windows clause and we're not on windows.
if (!windowsClause && !isWindows()) {
println(writer, level, "<exclude name=\"windows\"/>");
groupCount++;
}
println(writer, --level, "</run>");
println(writer, --level, "</groups>");
} else {
// No explicit groups have been specified so see if we need
// to exclude the windows tests.
if (!isWindows()) {
println(writer, level, "<groups>");
println(writer, ++level, "<run>");
println(writer, ++level, "<exclude name=\"windows\"/>");
println(writer, --level, "</run>");
println(writer, --level, "</groups>");
groupCount++;
}
}
if(packages.length > 0)
{
println(writer, level, "<packages>");
level++;
for(String pkg : packages)
{
println(writer, level, "<package name=\"" + pkg.trim() + "\" />");
packageCount++;
}
println(writer, --level, "</packages>");
}
if(classes.length > 0 || methods.length > 0)
{
println(writer, level, "<classes>");
if(classes.length > 0)
{
level++;
for(String cls : classes)
{
println(writer, level, "<class name=\"" + cls.trim() + "\" />");
classCount++;
}
}
if(methods.length > 0)
{
level++;
for(String mhd : methods)
{
methodLine = mhd.split(",");
if(methodLine.length > 0)
{
// Allow class.method or class#method
methodNameStartIdx = methodLine[0].lastIndexOf("#");
if (methodNameStartIdx == -1)
{
methodNameStartIdx = methodLine[0].lastIndexOf(".");
}
methodClass = methodLine[0].substring(0,
methodNameStartIdx);
methodName = methodLine[0].substring(methodNameStartIdx + 1,
methodLine[0].length());
println(writer, level, "<class name=\"" +
methodClass.trim() + "\" >");
println(writer, ++level, "<methods>");
println(writer, ++level, "<include name=\"" +
methodName.trim() + "\" />");
methodCount++;
classCount++;
for(int i = 1; i < methodLine.length; i ++)
{
println(writer, level, "<include name=\"" +
methodLine[i].trim() + "\" />");
methodCount++;
}
println(writer, --level, "</methods>");
println(writer, --level, "</class>");
}
}
}
println(writer, --level, "</classes>");
}
}
else if (line.indexOf(GLOBAL_RUN_TAGS_TEMPLATE) != -1)
{
if (!isWindows()) {
int index = line.indexOf(GLOBAL_RUN_TAGS_TEMPLATE);
println(writer, levelForIndex(index),
"<exclude name=\"windows\"/>");
}
}
else
{
println(writer, 0, line);
}
line = reader.readLine();
}
System.out.println("Adding " + groupCount + " group tags, " +
packageCount + " package tags, " + classCount + " class tags, " +
methodCount + " method tags to " + toFile);
}
catch(Exception e)
{
throw new BuildException("File Error: " + e.toString());
}
}
static private boolean isWindows() {
String os = System.getProperty("os.name");
return (os != null && os.toLowerCase().indexOf("windows") != -1);
}
static private String indent(int indent) {
char[] blankArray = new char[indent];
Arrays.fill(blankArray, ' ');
return new String(blankArray);
}
static private void println(PrintStream writer, int level, String txt) {
writer.print(indent(INDENT * level));
writer.print(txt);
writer.print(System.getProperty("line.separator"));
}
static private int levelForIndex(int index) {
return index / INDENT;
}
}