/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

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