Browse Source

fix timeout attribute of jdepend

add unit tests
add includeruntime to allow unittests to run


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@275605 13f79535-47bb-0310-9956-ffa450edef68
master
Peter Reilly 21 years ago
parent
commit
1e7d5dd17b
4 changed files with 319 additions and 24 deletions
  1. +6
    -0
      docs/manual/OptionalTasks/jdepend.html
  2. +53
    -0
      src/etc/testcases/taskdefs/optional/jdepend/jdepend.xml
  3. +105
    -24
      src/main/org/apache/tools/ant/taskdefs/optional/jdepend/JDependTask.java
  4. +155
    -0
      src/testcases/org/apache/tools/ant/taskdefs/optional/jdepend/JDependTest.java

+ 6
- 0
docs/manual/OptionalTasks/jdepend.html View File

@@ -71,6 +71,12 @@ href="#nested">nested elements</a>.</p>
<td VALIGN=TOP>The directory to invoke the VM in. (Ignored if fork is disabled)</td>
<td ALIGN=CENTER VALIGN=TOP>No</td>
</tr>
<tr>
<td VALIGN=TOP>includeruntime</td>
<td VALIGN=TOP>Implicitly add the classes required to run jdepend
in forked mode. (Ignored if fork is disabled). Since ant 1.6.</td>
<td ALIGN=CENTER VALIGN=TOP>No, default is "no".</td>
</tr>
<tr>
<td VALIGN=TOP>classpathref</td>
<td VALIGN=TOP>the classpath to use, given as reference to a PATH defined elsewhere.</td>


+ 53
- 0
src/etc/testcases/taskdefs/optional/jdepend/jdepend.xml View File

@@ -0,0 +1,53 @@
<project>
<property name="testclasses" location="../../../../../../build/testcases"/>
<path id="all-test-classes.id">
<pathelement location="../../../../build/testcases" />
<pathelement path="${java.class.path}" />
</path>

<path id="example-classes.id">
<pathelement location="${testclasses}/org/apache/tools/ant/util/facade" />
</path>

<path id="test-classes.id">
<pathelement location="${testclasses}" />
</path>

<target name="simple">
<jdepend>
<classespath refid="example-classes.id"/>
</jdepend>
</target>

<target name="xml">
<jdepend format="xml">
<classespath refid="example-classes.id"/>
</jdepend>
</target>

<target name="fork">
<jdepend fork="yes" includeruntime="yes">
<classespath refid="example-classes.id"/>
</jdepend>
</target>

<target name="fork-xml">
<jdepend fork="yes" format="xml" includeruntime="yes">
<classespath refid="example-classes.id"/>
</jdepend>
</target>

<target name="fork-timeout">
<jdepend fork="yes" timeout="10" includeruntime="yes">
<classespath refid="test-classes.id"/>
</jdepend>
</target>

<target name="fork-timeout-not">
<jdepend fork="yes" timeout="100000" includeruntime="yes">
<classespath refid="example-classes.id"/>
</jdepend>
</target>

</project>

+ 105
- 24
src/main/org/apache/tools/ant/taskdefs/optional/jdepend/JDependTask.java View File

@@ -61,6 +61,7 @@ import java.io.PrintWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Vector;
import java.util.Enumeration;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
@@ -73,6 +74,7 @@ import org.apache.tools.ant.types.EnumeratedAttribute;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.PatternSet;
import org.apache.tools.ant.types.Reference;
import org.apache.tools.ant.util.LoaderUtils;

/**
* Runs JDepend tests.
@@ -99,7 +101,7 @@ public class JDependTask extends Task {
private Path compileClasspath;
private boolean haltonerror = false;
private boolean fork = false;
//private Integer _timeout = null;
private Long timeout = null;

private String jvm = null;
private String format = "text";
@@ -108,6 +110,9 @@ public class JDependTask extends Task {
private static Constructor packageFilterC;
private static Method setFilter;

private boolean includeRuntime = false;
private Path runtimeClasses = null;

static {
try {
Class packageFilter =
@@ -124,15 +129,36 @@ public class JDependTask extends Task {
}
}

/*
public void setTimeout(Integer value) {
_timeout = value;
}
/**
* If true,
* include jdepend.jar in the forked VM.
*
* @param b include ant run time yes or no
* @since Ant 1.6
*/
public void setIncluderuntime(boolean b) {
includeRuntime = b;
}

/**
* Set the timeout value (in milliseconds).
*
* <p>If the operation is running for more than this value, the jdepend
* will be canceled. (works only when in 'fork' mode).</p>
* @param value the maximum time (in milliseconds) allowed before
* declaring the test as 'timed-out'
* @see #setFork(boolean)
*/
public void setTimeout(Long value) {
timeout = value;
}

public Integer getTimeout() {
return _timeout;
}
*/
/**
* @return the timeout value
*/
public Long getTimeout() {
return timeout;
}

/**
* The output file name.
@@ -350,6 +376,43 @@ public class JDependTask extends Task {
*/
private static final int ERRORS = 1;

/**
* Search for the given resource and add the directory or archive
* that contains it to the classpath.
*
* <p>Doesn't work for archives in JDK 1.1 as the URL returned by
* getResource doesn't contain the name of the archive.</p>
*
* @param resource resource that one wants to lookup
* @since Ant 1.6
*/
private void addClasspathEntry(String resource) {
/*
* pre Ant 1.6 this method used to call getClass().getResource
* while Ant 1.6 will call ClassLoader.getResource().
*
* The difference is that Class.getResource expects a leading
* slash for "absolute" resources and will strip it before
* delegating to ClassLoader.getResource - so we now have to
* emulate Class's behavior.
*/
if (resource.startsWith("/")) {
resource = resource.substring(1);
} else {
resource = "org/apache/tools/ant/taskdefs/optional/jdepend/"
+ resource;
}

File f = LoaderUtils.getResourceSource(getClass().getClassLoader(),
resource);
if (f != null) {
log("Found " + f.getAbsolutePath(), Project.MSG_DEBUG);
runtimeClasses.createPath().setLocation(f);
} else {
log("Couldn\'t find " + resource, Project.MSG_DEBUG);
}
}

/**
* execute the task
*
@@ -380,7 +443,7 @@ public class JDependTask extends Task {

// execute the test and get the return code
int exitValue = JDependTask.ERRORS;
//boolean wasKilled = false;
boolean wasKilled = false;
if (!getFork()) {
exitValue = executeInVM(commandline);
} else {
@@ -388,21 +451,22 @@ public class JDependTask extends Task {
exitValue = executeAsForked(commandline, watchdog);
// null watchdog means no timeout, you'd better not check with null
if (watchdog != null) {
//info will be used in later version do nothing for now
//wasKilled = watchdog.killedProcess();
wasKilled = watchdog.killedProcess();
}
}

// if there is an error/failure and that it should halt, stop
// everything otherwise just log a statement
boolean errorOccurred = exitValue == JDependTask.ERRORS;
boolean errorOccurred = exitValue == JDependTask.ERRORS || wasKilled;

if (errorOccurred) {
String errorMessage = "JDepend FAILED"
+ (wasKilled ? " - Timed out" : "");

if (getHaltonerror()) {
throw new BuildException("JDepend failed",
getLocation());
throw new BuildException(errorMessage, getLocation());
} else {
log("JDepend FAILED", Project.MSG_ERR);
log(errorMessage, Project.MSG_ERR);
}
}
}
@@ -540,6 +604,9 @@ public class JDependTask extends Task {
// JL: comment extracted from JUnitTask (and slightly modified)
public int executeAsForked(CommandlineJava commandline,
ExecuteWatchdog watchdog) throws BuildException {
runtimeClasses = new Path(getProject());
addClasspathEntry("/jdepend/textui/JDepend.class");

// if not set, auto-create the ClassPath from the project
createClasspath();

@@ -550,6 +617,24 @@ public class JDependTask extends Task {
createJvmarg(commandline).setValue(getClasspath().toString());
}

if (includeRuntime) {
Vector v = Execute.getProcEnvironment();
Enumeration e = v.elements();
while (e.hasMoreElements()) {
String s = (String) e.nextElement();
if (s.startsWith("CLASSPATH=")) {
commandline.createClasspath(getProject()).createPath()
.append(new Path(getProject(),
s.substring("CLASSPATH=".length()
)));
}
}
log("Implicitly adding " + runtimeClasses + " to CLASSPATH",
Project.MSG_VERBOSE);
commandline.createClasspath(getProject()).createPath()
.append(runtimeClasses);
}

if (getOutputFile() != null) {
// having a space between the file and its path causes commandline
// to add quotes around the argument thus making JDepend not taking
@@ -620,13 +705,9 @@ public class JDependTask extends Task {
* @throws BuildException in case of error
*/
protected ExecuteWatchdog createWatchdog() throws BuildException {

return null;
/*
if (getTimeout() == null) {
return null;
}
return new ExecuteWatchdog(getTimeout().intValue());
*/
if (getTimeout() == null) {
return null;
}
return new ExecuteWatchdog(getTimeout().longValue());
}
}

+ 155
- 0
src/testcases/org/apache/tools/ant/taskdefs/optional/jdepend/JDependTest.java View File

@@ -0,0 +1,155 @@
/*
* 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
* <http://www.apache.org/>.
*/

package org.apache.tools.ant.taskdefs.optional.jdepend;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;
import java.util.Vector;
import java.util.Enumeration;
import java.util.Hashtable;
import org.apache.tools.ant.BuildFileTest;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.DirectoryScanner;

/**
* Testcase for the JDepend optional task.
*
* @author Peter Reilly
*/
public class JDependTest extends BuildFileTest {
public static final String RESULT_FILESET = "result";

public JDependTest(String name) {
super(name);
}

public void setUp() {
configureProject(
"src/etc/testcases/taskdefs/optional/jdepend/jdepend.xml");
}

/**
* Test simple
*/
public void testSimple() {
expectOutputContaining(
"simple", "Package: org.apache.tools.ant.util.facade");
}
/**
* Test xml
*/
public void testXml() {
expectOutputContaining(
"xml", "<Package>java.lang</Package>");
}

/**
* Test fork
* - forked output goes to log
*/
public void testFork() {
expectLogContaining(
"fork", "Package: org.apache.tools.ant.util.facade");
}
/**
* Test fork xml
*/
public void testForkXml() {
expectLogContaining(
"fork-xml", "<Package>java.lang</Package>");
}

/**
* Test timeout
*/
public void testTimeout() {
expectLogContaining(
"fork-timeout", "JDepend FAILED - Timed out");
}

/**
* Test timeout without timing out
*/
public void testTimeoutNot() {
expectLogContaining(
"fork-timeout-not", "Package: org.apache.tools.ant.util.facade");
}

/**
* Assert that the given substring is in the output messages
*/

protected void assertOutputContaining(String substring) {
String realOutput = getOutput();
assertTrue("expecting output to contain \"" + substring + "\" output was \""
+ realOutput + "\"",
realOutput.indexOf(substring) >= 0);
}
/**
* Assert that the given message has been outputted
*/
protected void expectOutputContaining(String target, String substring) {
executeTarget(target);
assertOutputContaining(substring);
}

}

Loading…
Cancel
Save