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 * Copyright (C) 2005 Joakim Erdfelt <joakim@erdfelt.net>
009 *
010 * Redistribution and use in source and binary forms, with or without
011 * modification, are permitted provided that the following conditions
012 * are met:
013 *
014 * 1. Redistributions of source code must retain the above copyright
015 * notice, this list of conditions and the following disclaimer.
016 *
017 * 2. Redistributions in binary form must reproduce the above copyright
018 * notice, this list of conditions and the following disclaimer in
019 * the documentation and/or other materials provided with the
020 * distribution.
021 *
022 * 3. The end-user documentation included with the redistribution, if
023 * any, must include the following acknowlegement:
024 * "This product includes software developed by the
025 * Apache Software Foundation (http://www.apache.org/)."
026 * Alternately, this acknowlegement may appear in the software itself,
027 * if and wherever such third-party acknowlegements normally appear.
028 *
029 * 4. The names "Ant" and "Apache Software
030 * Foundation" must not be used to endorse or promote products derived
031 * from this software without prior written permission. For written
032 * permission, please contact apache@apache.org.
033 *
034 * 5. Products derived from this software may not be called "Apache"
035 * nor may "Apache" appear in their names without prior written
036 * permission of the Apache Group.
037 *
038 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
039 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
040 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
041 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
042 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
043 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
044 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
045 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
046 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
047 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
048 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
049 * SUCH DAMAGE.
050 * ====================================================================
051 *
052 * This software consists of voluntary contributions made by many
053 * individuals on behalf of the Apache Software Foundation. For more
054 * information on the Apache Software Foundation, please see
055 * <http://www.apache.org/>.
056 */
057
058 package net.sourceforge.cobertura.ant;
059
060 import java.util.Arrays;
061 import java.util.HashSet;
062 import java.util.Iterator;
063 import java.util.Set;
064
065 import net.sourceforge.cobertura.util.Copyright;
066
067 import org.apache.tools.ant.BuildException;
068 import org.apache.tools.ant.Project;
069 import org.apache.tools.ant.types.FileSet;
070
071 public class InstrumentTask extends CommonMatchingTask
072 {
073
074 Ignore ignoreRegex = null;
075
076 public InstrumentTask()
077 {
078 super("net.sourceforge.cobertura.coverage.Main");
079 }
080
081 public Ignore createIgnore()
082 {
083 ignoreRegex = new Ignore();
084 return ignoreRegex;
085 }
086
087 void coverage(String[] filenames)
088 {
089 if (filenames.length == 0)
090 {
091 return;
092 }
093
094 for (int i = 0; i < filenames.length; i++)
095 {
096 getProject().log("Adding " + filenames[i] + " to list",
097 Project.MSG_VERBOSE);
098 addArg(filenames[i]);
099 }
100
101 saveArgs();
102
103 Copyright.print(System.out);
104 System.out.println("instrumenting " + filenames.length + " "
105 + (filenames.length == 1 ? "class" : "classes")
106 + (toDir != null ? " to " + toDir : ""));
107
108 /**
109 * TODO: Do something here so that we can set System.in and System.out on
110 * getJava() to the one we're using now. So that when instrumentation calls
111 * System.out, it will show up as "[instrument] doing stuff" instead of
112 * "[java] doing stuff" in the ant output.
113 */
114 if (getJava().executeJava() != 0)
115 {
116 throw new BuildException();
117 }
118 }
119
120 public void execute() throws BuildException
121 {
122 initArgs();
123 if (toDir != null)
124 {
125 addArg("-d");
126 addArg(toDir.toString());
127 }
128
129 // TODO: ignoreRegex.setRegex() is never called
130 if (ignoreRegex != null)
131 {
132 addArg("-ignore");
133 addArg(ignoreRegex.getRegex());
134 }
135
136 Set filenames = new HashSet();
137 Iterator i = fileSets.iterator();
138
139 while (i.hasNext())
140 {
141 FileSet fileSet = (FileSet)i.next();
142
143 addArg("-basedir");
144 addArg(baseDir(fileSet));
145
146 filenames.addAll(Arrays.asList(getFilenames(fileSet)));
147 }
148
149 coverage((String[])filenames.toArray(new String[filenames.size()]));
150
151 unInitArgs();
152 }
153 }