PageRenderTime 33ms CodeModel.GetById 13ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 1ms

/ant-task/src/main/java/com/google/ant/TaskModel.java

http://testability-explorer.googlecode.com/
Java | 362 lines | 290 code | 72 blank | 0 comment | 37 complexity | ddacf8eca16cc3c162604637f5ddea8d MD5 | raw file
  1package com.google.ant;
  2
  3import org.apache.tools.ant.BuildException;
  4import org.apache.tools.ant.types.Path;
  5
  6import java.io.FileNotFoundException;
  7import java.io.FileOutputStream;
  8import java.io.OutputStream;
  9import java.io.PrintStream;
 10import java.util.List;
 11import java.util.Vector;
 12
 13public class TaskModel {
 14
 15  public static final String DEFAULT_RESULT_FILE = "System.out";
 16  public static final String DEFAULT_ERROR_FILE = "System.err";
 17  public static final String DEFAULT_FILTER = "";
 18  public static final String DEFAULT_WHITE_LIST = "IGNORE_ME_WHITELIST";
 19  public static final int DEFAULT_MAX_ACCEPTABLE_COST = 100;
 20  public static final int DEFAULT_MAX_EXCELLENT_COST = 50;
 21  public static final int DEFAULT_WORST_OFFENDER_COUNT = 20;
 22  public static final int DEFAULT_MIN_COST = 1;
 23  public static final int DEFAULT_PRINT_DEPTH = 2;
 24  public static final String DEFAULT_GROUPING = "cost"; // cost | package
 25  public static final String DEFAULT_PRINT = "summary"; // summary | detail | html
 26  public static final int DEFAULT_CYCLOMATIC = 1;
 27  public static final int DEFAULT_GLOBAL = 10;
 28
 29  public static final String ERROR_FILESET_NOT_SET = "fileset to jar and/or classfile directories must be set";
 30  public static final String ERROR_FILTER_NOT_SET = "filter must be set. default is " + DEFAULT_FILTER + " (all)";
 31  public static final String ERROR_WHITE_LIST_NOT_SET = "white list not set. using default" + DEFAULT_WHITE_LIST;
 32  public static final String ERROR_RESULT_FILE_NOT_SET = "resultfile must be set. either a filepath or System.(out|err). default is " + DEFAULT_RESULT_FILE;
 33  public static final String ERROR_PRINT_DEPTH_NOT_SET = "print depth not set. using default " + DEFAULT_PRINT_DEPTH;
 34  public static final String ERROR_MIN_COST_NOT_SET = "min cost not set. using default " + DEFAULT_MIN_COST;
 35  public static final String ERROR_MAX_EXCELLENT_COST_NOT_SET = "max excellent cost not set. using default " + DEFAULT_MAX_EXCELLENT_COST;
 36  public static final String ERROR_MAX_ACCEPTABLE_COST_NOT_SET = "max acceptable cost not set. using default " + DEFAULT_MAX_ACCEPTABLE_COST;
 37  public static final String ERROR_GROUPING_NOT_SET = "grouping not set. using default " + DEFAULT_GROUPING;
 38  public static final String ERROR_PRINT_NOT_SET = "print not set. using default " + DEFAULT_PRINT;
 39  public static final String ERROR_WORST_OFFENDER_COUNT_NOT_SET = "worst offender count not set. using default " + DEFAULT_WORST_OFFENDER_COUNT;
 40  public static final String ERROR_CYCLOMATIC_NOT_SET = "cyclomatic not set. using default " + DEFAULT_CYCLOMATIC;
 41  public static final String ERROR_GLOBAL_NOT_SET = "global not set. using default " + DEFAULT_GLOBAL;
 42  public static final String ERROR_ERROR_FILE_SET_TO_RESULT_FILE = "error file not set. using result file.";
 43  public static final String ERROR_RESULT_FILE_CREATION_FAILED = "resultfile could not be created";
 44  public static final String ERROR_ERROR_FILE_CREATION_FAILED = "errorfile could not be created";
 45
 46  private final Vector<Path> classPaths = new Vector<Path>();
 47  private String failProperty;
 48  private String resultFile = null;
 49  private String errorFile = null;
 50  private String filter = null;
 51  private int printDepth = -1;
 52  private int minCost = -1;
 53  private int maxExcellentCost = -1;
 54  private int maxAcceptableCost = -1;
 55  private int worstOffenderCount = -1;
 56  private String whiteList = null;
 57  private String print = null;
 58  private int cyclomatic = -1;
 59  private int global = -1;
 60  private int constructor = 1;
 61
 62  public int getCyclomatic() {
 63    return cyclomatic;
 64  }
 65
 66  public void setCyclomatic(int cyclomatic) {
 67    this.cyclomatic = cyclomatic;
 68  }
 69
 70  public void setMaxAcceptableCost(int cost) {
 71    maxAcceptableCost = cost;
 72  }
 73
 74  public int getMaxAcceptableCost() {
 75    return maxAcceptableCost;
 76  }
 77
 78  public void setMaxExcellentCost(int cost) {
 79    maxExcellentCost = cost;
 80  }
 81
 82  public int getMaxExcellentCost() {
 83    return maxExcellentCost;
 84  }
 85
 86  public void setMinCost(int cost) {
 87    minCost = cost;
 88  }
 89
 90  public int getMinCost() {
 91    return minCost;
 92  }
 93
 94  public void setPrint(String printVal) {
 95    print = printVal;
 96  }
 97
 98  public String getPrint() {
 99    return print;
100  }
101
102  public void setPrintDepth(int depth) {
103    printDepth = depth;
104  }
105
106  public int getPrintDepth() {
107    return printDepth;
108  }
109
110  public void setWhiteList(String whiteListVal) {
111    whiteList = whiteListVal;
112  }
113
114  public String getWhiteList() {
115    return whiteList;
116  }
117
118  public void setWorstOffenderCount(int count) {
119    worstOffenderCount = count;
120  }
121
122  public int getWorstOffenderCount() {
123    return worstOffenderCount;
124  }
125
126  public void setResultFile(String resultFile) {
127    this.resultFile = resultFile;
128  }
129
130  public String getResultFile() {
131    return resultFile;
132  }
133
134  public void setErrorFile(String resultFile) {
135    errorFile = resultFile;
136  }
137
138  public String getErrorFile() {
139    return errorFile;
140  }
141
142  public void setFilter(String filter) {
143    this.filter = filter;
144  }
145
146  public int getGlobal() {
147    return global;
148  }
149
150  public void setGlobal(int glob) {
151    global = glob;
152  }
153
154  public double getConstructor() {
155    return constructor;
156  }
157
158  public String getFilter() {
159    return filter;
160  }
161
162  public void setFailProperty(String property) {
163    failProperty = property;
164  }
165
166  public boolean isFailPropertySet() {
167    return failProperty != null && !failProperty.equals("");
168  }
169
170  public String getFailProperty() {
171    return failProperty;
172  }
173
174  public void addClasspath(Path path) {
175    classPaths.addElement(path);
176  }
177
178  public Vector<Path> getFileSets() {
179    return classPaths;
180  }
181
182  public String getClassPath() {
183    Path totalPath = null;
184    for (Path p : classPaths) {
185      if (totalPath == null) {
186        totalPath = p;
187        continue;
188      }
189      totalPath.add(p);
190    }
191
192    return totalPath != null ? totalPath.toString() : DEFAULT_FILTER;
193  }
194
195  public PrintStream getResultPrintStream() {
196    try {
197      OutputStream os = getOutputStream(resultFile);
198
199      return new PrintStream(os);
200    } catch (FileNotFoundException e) {
201      throw new BuildException(ERROR_RESULT_FILE_CREATION_FAILED);
202    }
203  }
204
205  public PrintStream getErrorPrintStream() {
206    try {
207      OutputStream os = getOutputStream(errorFile);
208
209      return new PrintStream(os);
210    } catch (FileNotFoundException e) {
211      throw new BuildException(ERROR_ERROR_FILE_CREATION_FAILED);
212    }
213  }
214
215  OutputStream getOutputStream(String target) throws FileNotFoundException {
216    OutputStream os = null;
217
218    if (target.equals(DEFAULT_RESULT_FILE)) {
219      os = System.out;
220    } else if (target.equals(DEFAULT_ERROR_FILE)) {
221      os = System.err;
222    } else {
223      os = new FileOutputStream(target);
224    }
225
226    return os;
227  }
228
229  public boolean validate(List<String> messages) {
230    boolean allOk = true;
231
232    if (! isPrintDepthSet()) {
233      printDepth = DEFAULT_PRINT_DEPTH;
234      messages.add(TaskModel.ERROR_PRINT_DEPTH_NOT_SET);
235    }
236
237    if (! isMinCostSet()) {
238      minCost = DEFAULT_MIN_COST;
239      messages.add(TaskModel.ERROR_MIN_COST_NOT_SET);
240    }
241
242    if (! isMaxExcellentCostSet()) {
243      maxExcellentCost = DEFAULT_MAX_EXCELLENT_COST;
244      messages.add(TaskModel.ERROR_MAX_EXCELLENT_COST_NOT_SET);
245    }
246
247    if (! isMaxAcceptableCostSet()) {
248      maxAcceptableCost = DEFAULT_MAX_ACCEPTABLE_COST;
249      messages.add(TaskModel.ERROR_MAX_ACCEPTABLE_COST_NOT_SET);
250    }
251
252    if (! isWorstOffenderCountSet()) {
253      worstOffenderCount = DEFAULT_WORST_OFFENDER_COUNT;
254      messages.add(TaskModel.ERROR_WORST_OFFENDER_COUNT_NOT_SET);
255    }
256
257    if (! isPrintSet()) {
258      print = DEFAULT_PRINT;
259      messages.add(TaskModel.ERROR_PRINT_NOT_SET);
260    }
261
262    if (! isCyclomaticSet()) {
263      cyclomatic = DEFAULT_CYCLOMATIC;
264      messages.add(TaskModel.ERROR_CYCLOMATIC_NOT_SET);
265    }
266
267    if (! isGlobalSet()) {
268      global = DEFAULT_GLOBAL;
269      messages.add(TaskModel.ERROR_GLOBAL_NOT_SET);
270    }
271
272    if (! isResultFileSet()) {
273      resultFile = DEFAULT_RESULT_FILE;
274      messages.add(TaskModel.ERROR_RESULT_FILE_NOT_SET);
275    }
276    if (! isErrorFileSet()) {
277      messages.add(TaskModel.ERROR_ERROR_FILE_SET_TO_RESULT_FILE);
278      errorFile = resultFile;
279    }
280    if (! isFilterSet()) {
281      filter = DEFAULT_FILTER;
282      messages.add(TaskModel.ERROR_FILTER_NOT_SET);
283    }
284    if (! isWhiteListSet()) {
285      whiteList = DEFAULT_WHITE_LIST;
286      messages.add(TaskModel.ERROR_WHITE_LIST_NOT_SET);
287    }
288
289    if (! isFileSetSet()) {
290      allOk = false;
291      messages.add(TaskModel.ERROR_FILESET_NOT_SET);
292    }
293
294    try {
295      getOutputStream(resultFile);
296    } catch (FileNotFoundException e) {
297      allOk = false;
298      messages.add(TaskModel.ERROR_RESULT_FILE_CREATION_FAILED);
299    }
300
301    try {
302      getOutputStream(errorFile);
303    } catch (FileNotFoundException e) {
304      allOk = false;
305      messages.add(TaskModel.ERROR_ERROR_FILE_CREATION_FAILED);
306    }
307
308    return allOk;
309  }
310
311  private boolean isWhiteListSet() {
312    return whiteList != null;
313  }
314
315  private boolean isPrintDepthSet() {
316    return printDepth != -1;
317  }
318
319  private boolean isMinCostSet() {
320    return minCost != -1;
321  }
322
323  private boolean isMaxExcellentCostSet() {
324    return maxExcellentCost != -1;
325  }
326
327  private boolean isMaxAcceptableCostSet() {
328    return maxAcceptableCost != -1;
329  }
330
331  private boolean isWorstOffenderCountSet() {
332    return worstOffenderCount != -1;
333  }
334
335  private boolean isPrintSet() {
336    return print != null;
337  }
338
339  private boolean isCyclomaticSet() {
340    return cyclomatic != -1;
341  }
342
343  private boolean isGlobalSet() {
344    return global != -1;
345  }
346
347  private boolean isFileSetSet() {
348    return classPaths.size() > 0;
349  }
350
351  private boolean isFilterSet() {
352    return filter != null;
353  }
354
355  private boolean isResultFileSet() {
356    return !(resultFile == null || resultFile.equals(""));
357  }
358
359  private boolean isErrorFileSet() {
360    return !(errorFile == null || errorFile.equals(""));
361  }
362}