| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
package net.sourceforge.cobertura.ant; |
| 63 | |
|
| 64 | |
import java.io.File; |
| 65 | |
import java.io.IOException; |
| 66 | |
import java.util.ArrayList; |
| 67 | |
import java.util.HashMap; |
| 68 | |
import java.util.List; |
| 69 | |
|
| 70 | |
import net.sourceforge.cobertura.util.CommandLineBuilder; |
| 71 | |
|
| 72 | |
import org.apache.tools.ant.BuildException; |
| 73 | |
import org.apache.tools.ant.Project; |
| 74 | |
import org.apache.tools.ant.types.FileSet; |
| 75 | |
import org.apache.tools.ant.types.Path; |
| 76 | |
|
| 77 | |
public class InstrumentTask extends CommonMatchingTask |
| 78 | |
{ |
| 79 | |
|
| 80 | 4 | private String dataFile = null; |
| 81 | |
|
| 82 | 4 | private File toDir = null; |
| 83 | |
|
| 84 | 4 | List ignoreRegexs = new ArrayList(); |
| 85 | |
|
| 86 | 4 | List ignoreBranchesRegexs = new ArrayList(); |
| 87 | |
|
| 88 | 4 | List includeClassesRegexs = new ArrayList(); |
| 89 | |
|
| 90 | 4 | List excludeClassesRegexs = new ArrayList(); |
| 91 | |
|
| 92 | |
private Integer forkedJVMDebugPort; |
| 93 | |
|
| 94 | 4 | private Path instrumentationClasspath = null; |
| 95 | |
|
| 96 | 4 | private HashMap fileSetMap = new HashMap(); |
| 97 | |
|
| 98 | |
public InstrumentTask() |
| 99 | |
{ |
| 100 | 4 | super("net.sourceforge.cobertura.instrument.Main"); |
| 101 | 4 | } |
| 102 | |
|
| 103 | |
public Ignore createIgnore() |
| 104 | |
{ |
| 105 | 1 | Ignore ignoreRegex = new Ignore(); |
| 106 | 1 | ignoreRegexs.add(ignoreRegex); |
| 107 | 1 | return ignoreRegex; |
| 108 | |
} |
| 109 | |
|
| 110 | |
public IgnoreBranches createIgnoreBranches() |
| 111 | |
{ |
| 112 | 0 | IgnoreBranches ignoreBranchesRegex = new IgnoreBranches(); |
| 113 | 0 | ignoreBranchesRegexs.add(ignoreBranchesRegex); |
| 114 | 0 | return ignoreBranchesRegex; |
| 115 | |
} |
| 116 | |
|
| 117 | |
public IncludeClasses createIncludeClasses() |
| 118 | |
{ |
| 119 | 2 | IncludeClasses includeClassesRegex = new IncludeClasses(); |
| 120 | 2 | includeClassesRegexs.add(includeClassesRegex); |
| 121 | 2 | return includeClassesRegex; |
| 122 | |
} |
| 123 | |
|
| 124 | |
public ExcludeClasses createExcludeClasses() |
| 125 | |
{ |
| 126 | 4 | ExcludeClasses excludeClassesRegex = new ExcludeClasses(); |
| 127 | 4 | excludeClassesRegexs.add(excludeClassesRegex); |
| 128 | 4 | return excludeClassesRegex; |
| 129 | |
} |
| 130 | |
|
| 131 | |
public Path createInstrumentationClasspath() |
| 132 | |
{ |
| 133 | 1 | if (instrumentationClasspath == null) { |
| 134 | 1 | instrumentationClasspath = new Path(getProject()); |
| 135 | |
} |
| 136 | 1 | return instrumentationClasspath.createPath(); |
| 137 | |
} |
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
public void execute() throws BuildException |
| 151 | |
{ |
| 152 | 4 | CommandLineBuilder builder = null; |
| 153 | |
try { |
| 154 | 4 | builder = new CommandLineBuilder(); |
| 155 | 4 | if (dataFile != null) |
| 156 | 4 | builder.addArg("--datafile", dataFile); |
| 157 | 4 | if (toDir != null) |
| 158 | 4 | builder.addArg("--destination", toDir.getAbsolutePath()); |
| 159 | |
|
| 160 | 5 | for (int i = 0; i < ignoreRegexs.size(); i++) { |
| 161 | 1 | Ignore ignoreRegex = (Ignore)ignoreRegexs.get(i); |
| 162 | 1 | builder.addArg("--ignore", ignoreRegex.getRegex()); |
| 163 | |
} |
| 164 | |
|
| 165 | 4 | for (int i = 0; i < ignoreBranchesRegexs.size(); i++) { |
| 166 | 0 | IgnoreBranches ignoreBranchesRegex = (IgnoreBranches)ignoreBranchesRegexs.get(i); |
| 167 | 0 | builder.addArg("--ignoreBranches", ignoreBranchesRegex.getRegex()); |
| 168 | |
} |
| 169 | |
|
| 170 | 6 | for (int i = 0; i < includeClassesRegexs.size(); i++) { |
| 171 | 2 | IncludeClasses includeClassesRegex = (IncludeClasses)includeClassesRegexs.get(i); |
| 172 | 2 | builder.addArg("--includeClasses", includeClassesRegex.getRegex()); |
| 173 | |
} |
| 174 | |
|
| 175 | 8 | for (int i = 0; i < excludeClassesRegexs.size(); i++) { |
| 176 | 4 | ExcludeClasses excludeClassesRegex = (ExcludeClasses)excludeClassesRegexs.get(i); |
| 177 | 4 | builder.addArg("--excludeClasses", excludeClassesRegex.getRegex()); |
| 178 | |
} |
| 179 | |
|
| 180 | 4 | if (instrumentationClasspath != null) { |
| 181 | 1 | processInstrumentationClasspath(); |
| 182 | |
} |
| 183 | 4 | createArgumentsForFilesets(builder); |
| 184 | |
|
| 185 | 4 | builder.saveArgs(); |
| 186 | 0 | } catch (IOException ioe) { |
| 187 | 0 | getProject().log("Error creating commands file.", Project.MSG_ERR); |
| 188 | 0 | throw new BuildException("Unable to create the commands file.", ioe); |
| 189 | 4 | } |
| 190 | |
|
| 191 | |
|
| 192 | 4 | getJava().createArg().setValue("--commandsfile"); |
| 193 | 4 | getJava().createArg().setValue(builder.getCommandLineFile()); |
| 194 | 4 | if (forkedJVMDebugPort != null && forkedJVMDebugPort.intValue() > 0) { |
| 195 | 0 | getJava().createJvmarg().setValue("-Xdebug"); |
| 196 | 0 | getJava().createJvmarg().setValue("-Xrunjdwp:transport=dt_socket,address=" + forkedJVMDebugPort + ",server=y,suspend=y"); |
| 197 | |
} |
| 198 | 4 | AntUtil.transferCoberturaDataFileProperty(getJava()); |
| 199 | 4 | if (getJava().executeJava() != 0) { |
| 200 | 0 | throw new BuildException( |
| 201 | |
"Error instrumenting classes. See messages above."); |
| 202 | |
} |
| 203 | |
|
| 204 | 4 | builder.dispose(); |
| 205 | 4 | } |
| 206 | |
|
| 207 | |
private void processInstrumentationClasspath() |
| 208 | |
{ |
| 209 | 1 | if (includeClassesRegexs.size() == 0) |
| 210 | |
{ |
| 211 | 0 | throw new BuildException("'includeClasses' is required when 'instrumentationClasspath' is used"); |
| 212 | |
} |
| 213 | |
|
| 214 | 1 | String[] sources = instrumentationClasspath.list(); |
| 215 | 2 | for (int i = 0; i < sources.length; i++) { |
| 216 | 1 | File fileOrDir = new File(sources[i]); |
| 217 | 1 | if (fileOrDir.exists()) |
| 218 | |
{ |
| 219 | 1 | if (fileOrDir.isDirectory()) { |
| 220 | 1 | createFilesetForDirectory(fileOrDir); |
| 221 | 1 | } else { |
| 222 | 0 | addFileToFilesets(fileOrDir); |
| 223 | |
} |
| 224 | |
} |
| 225 | |
} |
| 226 | 1 | } |
| 227 | |
|
| 228 | |
private void addFileToFilesets(File file) |
| 229 | |
{ |
| 230 | 0 | File dir = file.getParentFile(); |
| 231 | 0 | String filename = file.getName(); |
| 232 | 0 | FileSet fileSet = getFileSet(dir); |
| 233 | 0 | fileSet.createInclude().setName(filename); |
| 234 | 0 | } |
| 235 | |
|
| 236 | |
private FileSet getFileSet(File dir) |
| 237 | |
{ |
| 238 | 1 | String key = dir.getAbsolutePath(); |
| 239 | 1 | FileSet fileSet = (FileSet)fileSetMap.get(key); |
| 240 | 1 | if (fileSet == null) |
| 241 | |
{ |
| 242 | 1 | fileSet = new FileSet(); |
| 243 | 1 | fileSet.setProject(getProject()); |
| 244 | 1 | fileSet.setDir(dir); |
| 245 | |
|
| 246 | |
|
| 247 | 1 | fileSetMap.put(key, fileSet); |
| 248 | 1 | addFileset(fileSet); |
| 249 | |
} |
| 250 | 1 | return fileSet; |
| 251 | |
} |
| 252 | |
|
| 253 | |
private void createFilesetForDirectory(File dir) |
| 254 | |
{ |
| 255 | 1 | FileSet fileSet = getFileSet(dir); |
| 256 | 1 | fileSet.createInclude().setName("**/*.class"); |
| 257 | 1 | } |
| 258 | |
|
| 259 | |
public void setDataFile(String dataFile) |
| 260 | |
{ |
| 261 | 4 | this.dataFile = dataFile; |
| 262 | 4 | } |
| 263 | |
|
| 264 | |
public void setToDir(File toDir) |
| 265 | |
{ |
| 266 | 4 | this.toDir = toDir; |
| 267 | 4 | } |
| 268 | |
|
| 269 | |
public void setForkedJVMDebugPort(Integer forkedJVMDebugPort) |
| 270 | |
{ |
| 271 | 0 | this.forkedJVMDebugPort = forkedJVMDebugPort; |
| 272 | 0 | } |
| 273 | |
|
| 274 | |
} |