From b001eb2bcb618ad963c862bf203df3c0a302bd99 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Wed, 2 Apr 2003 12:42:50 +0000 Subject: [PATCH] Initial refactoring to address 14849 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274372 13f79535-47bb-0310-9956-ffa450edef68 --- .../taskdefs/optional/sitraka/CovBase.java | 98 +++++++++++++++++++ .../taskdefs/optional/sitraka/CovMerge.java | 35 ++----- .../taskdefs/optional/sitraka/CovReport.java | 26 ++--- .../taskdefs/optional/sitraka/Coverage.java | 42 ++------ 4 files changed, 124 insertions(+), 77 deletions(-) create mode 100644 src/main/org/apache/tools/ant/taskdefs/optional/sitraka/CovBase.java diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/sitraka/CovBase.java b/src/main/org/apache/tools/ant/taskdefs/optional/sitraka/CovBase.java new file mode 100644 index 000000000..5fa333ad6 --- /dev/null +++ b/src/main/org/apache/tools/ant/taskdefs/optional/sitraka/CovBase.java @@ -0,0 +1,98 @@ +/* + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "Ant" and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +package org.apache.tools.ant.taskdefs.optional.sitraka; + +import java.io.File; +import org.apache.tools.ant.Task; +import org.apache.tools.ant.util.FileUtils; + +/** + * Base class that deals with JProbe version incompatibilities. + * + * @since Ant 1.6 + * + * @author Stefan Bodewig + */ +public abstract class CovBase extends Task { + private File home; + private static FileUtils fu = FileUtils.newFileUtils(); + + /** + * The directory where JProbe is installed. + */ + public void setHome(File value) { + this.home = value; + } + + protected File getHome() { + return home; + } + + protected File findJar(String relativePath) { + return fu.resolveFile(home, relativePath); + } + + protected String findExecutable(String relativePath) { + return fu.resolveFile(home, relativePath).getAbsolutePath(); + } + + protected File createTempFile(String prefix) { + return fu.createTempFile(prefix, ".tmp", null); + } + + protected String getParamFileArgument() { + return "-jp_paramfile="; + } +} diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/sitraka/CovMerge.java b/src/main/org/apache/tools/ant/taskdefs/optional/sitraka/CovMerge.java index a67c90870..bc78b698a 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/sitraka/CovMerge.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/sitraka/CovMerge.java @@ -1,7 +1,7 @@ /* * The Apache Software License, Version 1.1 * - * Copyright (c) 2001-2002 The Apache Software Foundation. All rights + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -63,7 +63,6 @@ import java.util.Vector; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.Project; -import org.apache.tools.ant.Task; import org.apache.tools.ant.taskdefs.Execute; import org.apache.tools.ant.taskdefs.LogStreamHandler; import org.apache.tools.ant.types.Commandline; @@ -75,10 +74,7 @@ import org.apache.tools.ant.types.FileSet; * @author Stephane Bailliez * @ant.task name="jpcovmerge" category="metrics" */ -public class CovMerge extends Task { - - /** coverage home, it is mandatory */ - private File home = null; +public class CovMerge extends CovBase { /** the name of the output snapshot */ private File tofile = null; @@ -88,13 +84,6 @@ public class CovMerge extends Task { private boolean verbose; - /** - * The directory where JProbe is installed. - */ - public void setHome(File value) { - this.home = value; - } - /** * Set the output snapshot file. */ @@ -129,11 +118,12 @@ public class CovMerge extends Task { File paramfile = createParamFile(); try { Commandline cmdl = new Commandline(); - cmdl.setExecutable(new File(home, "jpcovmerge").getAbsolutePath()); + cmdl.setExecutable(findExecutable("jpcovmerge")); if (verbose) { cmdl.createArgument().setValue("-v"); } - cmdl.createArgument().setValue("-jp_paramfile=" + paramfile.getAbsolutePath()); + cmdl.createArgument().setValue(getParamFileArgument() + + paramfile.getAbsolutePath()); LogStreamHandler handler = new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN); Execute exec = new Execute(handler); @@ -161,13 +151,12 @@ public class CovMerge extends Task { } // check coverage home - if (home == null || !home.isDirectory()) { + if (getHome() == null || !getHome().isDirectory()) { throw new BuildException("Invalid home directory. Must point to JProbe home directory"); } - home = new File(home, "coverage"); - File jar = new File(home, "coverage.jar"); + File jar = findJar("coverage/coverage.jar"); if (!jar.exists()) { - throw new BuildException("Cannot find Coverage directory: " + home); + throw new BuildException("Cannot find Coverage directory: " + getHome()); } } @@ -200,7 +189,7 @@ public class CovMerge extends Task { */ protected File createParamFile() throws BuildException { File[] snapshots = getSnapshots(); - File file = createTmpFile(); + File file = createTempFile("jpcovm"); FileWriter fw = null; try { fw = new FileWriter(file); @@ -224,10 +213,4 @@ public class CovMerge extends Task { return file; } - /** create a temporary file in the current dir (For JDK1.1 support) */ - protected File createTmpFile() { - final long rand = (new Random(System.currentTimeMillis())).nextLong(); - File file = new File("jpcovmerge" + rand + ".tmp"); - return file; - } } diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/sitraka/CovReport.java b/src/main/org/apache/tools/ant/taskdefs/optional/sitraka/CovReport.java index a08426551..c144c9c9e 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/sitraka/CovReport.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/sitraka/CovReport.java @@ -1,7 +1,7 @@ /* * The Apache Software License, Version 1.1 * - * Copyright (c) 2001-2002 The Apache Software Foundation. All rights + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -66,7 +66,6 @@ import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; -import org.apache.tools.ant.Task; import org.apache.tools.ant.taskdefs.Execute; import org.apache.tools.ant.taskdefs.LogStreamHandler; import org.apache.tools.ant.types.Commandline; @@ -81,7 +80,7 @@ import org.w3c.dom.Document; * @author Stephane Bailliez * @ant.task name="jpcovreport" category="metrics" */ -public class CovReport extends Task { +public class CovReport extends CovBase { /* jpcoverport [options] -output=file -snapshot=snapshot.jpc jpcovreport [options] [-paramfile=file] -output= -snapshot= @@ -130,9 +129,6 @@ public class CovReport extends Task { /* - /** coverage home, mandatory */ - private File home = null; - /** format of generated report, optional */ private String format = null; @@ -163,13 +159,6 @@ public class CovReport extends Task { private Reference reference = null; - /** - * The directory where JProbe is installed. - */ - public void setHome(File value) { - this.home = value; - } - public static class ReportFormat extends EnumeratedAttribute { public String[] getValues() { return new String[]{"html", "text", "xml"}; @@ -279,13 +268,12 @@ public class CovReport extends Task { if (snapshot == null) { throw new BuildException("'snapshot' attribute must be set."); } - if (home == null) { + if (getHome() == null) { throw new BuildException("'home' attribute must be set to JProbe home directory"); } - home = new File(home, "coverage"); - File jar = new File(home, "coverage.jar"); + File jar = findJar("coverage/coverage.jar"); if (!jar.exists()) { - throw new BuildException("Cannot find Coverage directory: " + home); + throw new BuildException("Cannot find Coverage directory: " + getHome()); } if (reference != null && !"xml".equals(format)) { log("Ignored reference. It cannot be used in non XML report."); @@ -299,7 +287,7 @@ public class CovReport extends Task { try { Commandline cmdl = new Commandline(); // we need to run Coverage from his directory due to dll/jar issues - cmdl.setExecutable(new File(home, "jpcovreport").getAbsolutePath()); + cmdl.setExecutable(findExecutable("jpcovreport")); String[] params = getParameters(); for (int i = 0; i < params.length; i++) { cmdl.createArgument().setValue(params[i]); @@ -398,7 +386,7 @@ public class CovReport extends Task { log("Creating enhanced XML report", Project.MSG_VERBOSE); XMLReport report = new XMLReport(CovReport.this, tofile); report.setReportFilters(filters); - report.setJProbehome(new File(home.getParent())); + report.setJProbehome(new File(getHome().getParent())); Document doc = report.createDocument(paths); TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer transformer = tfactory.newTransformer(); diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/sitraka/Coverage.java b/src/main/org/apache/tools/ant/taskdefs/optional/sitraka/Coverage.java index c17ba512f..70e299172 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/sitraka/Coverage.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/sitraka/Coverage.java @@ -1,7 +1,7 @@ /* * The Apache Software License, Version 1.1 * - * Copyright (c) 2001-2002 The Apache Software Foundation. All rights + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -72,6 +72,7 @@ import org.apache.tools.ant.types.CommandlineJava; import org.apache.tools.ant.types.EnumeratedAttribute; import org.apache.tools.ant.types.FileSet; import org.apache.tools.ant.types.Path; +import org.apache.tools.ant.util.JavaEnvUtils; /** * Runs Sitraka JProbe Coverage. @@ -86,9 +87,7 @@ import org.apache.tools.ant.types.Path; * * @ant.task name="jpcoverage" category="metrics" */ -public class Coverage extends Task { - - protected File home; +public class Coverage extends CovBase { protected Commandline cmdl = new Commandline(); @@ -131,13 +130,6 @@ public class Coverage extends Task { //--------- setters used via reflection -- - /** - * The directory where JProbe is installed. - */ - public void setHome(File value) { - home = value; - } - /** seed name for snapshot file. Can be null, default to snap */ public void setSeedname(String value) { seedName = value; @@ -331,7 +323,7 @@ public class Coverage extends Task { } try { // we need to run Coverage from his directory due to dll/jar issues - cmdl.setExecutable(new File(home, "jplauncher").getAbsolutePath()); + cmdl.setExecutable(findExecutable("jplauncher")); cmdl.createArgument().setValue("-jp_input=" + paramfile.getAbsolutePath()); // use the custom handler for stdin issues @@ -356,13 +348,12 @@ public class Coverage extends Task { /** wheck what is necessary to check, Coverage will do the job for us */ protected void checkOptions() throws BuildException { // check coverage home - if (home == null || !home.isDirectory()) { + if (getHome() == null || !getHome().isDirectory()) { throw new BuildException("Invalid home directory. Must point to JProbe home directory"); } - home = new File(home, "coverage"); - File jar = new File(home, "coverage.jar"); + File jar = findJar("coverage/coverage.jar"); if (!jar.exists()) { - throw new BuildException("Cannot find Coverage directory: " + home); + throw new BuildException("Cannot find Coverage directory: " + getHome()); } // make sure snapshot dir exists and is resolved @@ -381,17 +372,11 @@ public class Coverage extends Task { // check for info, do your best to select the java executable. // JProbe 3.0 fails if there is no javaexe option. So if (javaExe == null && (vm == null || "java2".equals(vm))) { - String version = System.getProperty("java.version"); - // make we are using 1.2+, if it is, then do your best to - // get a javaexe - if (!version.startsWith("1.1")) { + if (!JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) { if (vm == null) { vm = "java2"; } - // if we are here obviously it is java2 - String home = System.getProperty("java.home"); - boolean isUnix = File.separatorChar == '/'; - javaExe = isUnix ? new File(home, "bin/java") : new File(home, "/bin/java.exe"); + javaExe = new File(JavaEnvUtils.getJreExecutable("java")); } } } @@ -464,7 +449,7 @@ public class Coverage extends Task { */ protected File createParamFile() throws BuildException { //@todo change this when switching to JDK 1.2 and use File.createTmpFile() - File file = createTmpFile(); + File file = createTempFile("jpcov"); log("Creating parameter file: " + file, Project.MSG_VERBOSE); // options need to be one per line in the parameter file @@ -497,13 +482,6 @@ public class Coverage extends Task { return file; } - /** create a temporary file in the current dir (For JDK1.1 support) */ - protected File createTmpFile() { - final long rand = (new Random(System.currentTimeMillis())).nextLong(); - File file = new File("jpcoverage" + rand + ".tmp"); - return file; - } - /** specific pumper to avoid those nasty stdin issues */ static class CoverageStreamHandler extends LogStreamHandler { CoverageStreamHandler(Task task) {