001    /*
002     * The Apache Software License, Version 1.1
003     *
004     * Copyright (C) 2000-2002 The Apache Software Foundation.  All rights
005     * reserved.
006     * Copyright (C) 2003 jcoverage ltd.
007     * Copyright (C) 2005 Mark Doliner <thekingant@users.sourceforge.net>
008     *
009     * Redistribution and use in source and binary forms, with or without
010     * modification, are permitted provided that the following conditions
011     * are met:
012     *
013     * 1. Redistributions of source code must retain the above copyright
014     *    notice, this list of conditions and the following disclaimer.
015     *
016     * 2. Redistributions in binary form must reproduce the above copyright
017     *    notice, this list of conditions and the following disclaimer in
018     *    the documentation and/or other materials provided with the
019     *    distribution.
020     *
021     * 3. The end-user documentation included with the redistribution, if
022     *    any, must include the following acknowlegement:
023     *       "This product includes software developed by the
024     *        Apache Software Foundation (http://www.apache.org/)."
025     *    Alternately, this acknowlegement may appear in the software itself,
026     *    if and wherever such third-party acknowlegements normally appear.
027     *
028     * 4. The names "Ant" and "Apache Software
029     *    Foundation" must not be used to endorse or promote products derived
030     *    from this software without prior written permission. For written
031     *    permission, please contact apache@apache.org.
032     *
033     * 5. Products derived from this software may not be called "Apache"
034     *    nor may "Apache" appear in their names without prior written
035     *    permission of the Apache Group.
036     *
037     * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
038     * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
039     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
040     * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
041     * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
042     * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
043     * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
044     * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
045     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
046     * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
047     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
048     * SUCH DAMAGE.
049     * ====================================================================
050     *
051     * This software consists of voluntary contributions made by many
052     * individuals on behalf of the Apache Software Foundation.  For more
053     * information on the Apache Software Foundation, please see
054     * <http://www.apache.org/>.
055     */
056    
057    package net.sourceforge.cobertura.ant;
058    
059    import java.io.File;
060    import java.net.URL;
061    import java.net.URLClassLoader;
062    
063    import net.sourceforge.cobertura.util.Copyright;
064    
065    import org.apache.tools.ant.AntClassLoader;
066    import org.apache.tools.ant.BuildException;
067    import org.apache.tools.ant.taskdefs.Java;
068    import org.apache.tools.ant.taskdefs.MatchingTask;
069    import org.apache.tools.ant.types.Path;
070    import org.apache.tools.ant.types.Reference;
071    
072    /**
073     * Generate a coverage report based on coverage data generated 
074     * by instrumented classes.
075     */
076    public class CoverageReportTask extends MatchingTask
077    {
078    
079            private String coverageDataFileName = "cobertura.ser";
080            private String format = "html";
081            private Path src;
082            private File destDir;
083    
084            private Java java = null;
085    
086            public void setCoverageDataFileName(String coverageDataFileName)
087            {
088                    this.coverageDataFileName = coverageDataFileName;
089            }
090    
091            public void setFormat(String format)
092            {
093                    if (!format.equalsIgnoreCase("html")
094                                    && !format.equalsIgnoreCase("xml"))
095                    {
096                            throw new BuildException(
097                                            "Format must be either \"html\" or \"xml\"");
098                    }
099                    this.format = format;
100            }
101    
102            public void setSrcDir(Path srcDir)
103            {
104                    if (src == null)
105                    {
106                            src = srcDir;
107                    }
108                    else
109                    {
110                            src.append(srcDir);
111                    }
112            }
113    
114            public void setDestDir(File destDir)
115            {
116                    this.destDir = destDir;
117            }
118    
119            public void execute() throws BuildException
120            {
121                    Copyright.print(System.out);
122    
123                    getJava().createArg().setValue("-i");
124                    getJava().createArg().setValue(coverageDataFileName);
125    
126                    getJava().createArg().setValue("-s");
127                    getJava().createArg().setValue(src.toString());
128    
129                    getJava().createArg().setValue("-o");
130                    getJava().createArg().setValue(destDir.toString());
131    
132                    getJava().createArg().setValue("-f");
133                    getJava().createArg().setValue(format);
134    
135                    if (getJava().executeJava() != 0)
136                    {
137                            throw new BuildException();
138                    }
139            }
140    
141            protected Java getJava()
142            {
143                    if (java == null)
144                    {
145                            java = (Java)getProject().createTask("java");
146                            java.setTaskName(getTaskName());
147                            java.setClassname("net.sourceforge.cobertura.reporting.Main");
148                            java.setFork(true);
149                            java.setDir(getProject().getBaseDir());
150    
151                            if (getClass().getClassLoader() instanceof AntClassLoader)
152                            {
153                                    String classpath = ((AntClassLoader)getClass()
154                                                    .getClassLoader()).getClasspath();
155                                    createClasspath().setPath(classpath.replaceAll("%20", " "));
156                            }
157                            else if (getClass().getClassLoader() instanceof URLClassLoader)
158                            {
159                                    URL[] earls = ((URLClassLoader)getClass().getClassLoader())
160                                                    .getURLs();
161                                    for (int i = 0; i < earls.length; i++)
162                                    {
163                                            String classpath = earls[i].getFile();
164                                            createClasspath().setPath(
165                                                            classpath.replaceAll("%20", " "));
166                                    }
167                            }
168                    }
169                    return java;
170            }
171    
172            public Path createClasspath()
173            {
174                    return getJava().createClasspath().createPath();
175            }
176    
177            public void setClasspath(Path classpath)
178            {
179                    createClasspath().append(classpath);
180            }
181    
182            public void setClasspathRef(Reference r)
183            {
184                    createClasspath().setRefid(r);
185            }
186    
187    }