/ant-task/src/test/java/com/google/ant/TaskModelTest.java

http://testability-explorer.googlecode.com/ · Java · 171 lines · 134 code · 37 blank · 0 comment · 0 complexity · 3f3fe3de62b6e038e1139c1c6a104296 MD5 · raw file

  1. package com.google.ant;
  2. import java.io.File;
  3. import java.io.OutputStream;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8. import junit.framework.TestCase;
  9. import org.apache.tools.ant.Project;
  10. import org.apache.tools.ant.types.Path;
  11. public class TaskModelTest extends TestCase {
  12. private TaskModel model;
  13. public TaskModelTest(String name)
  14. {
  15. super(name);
  16. }
  17. @Override
  18. protected void setUp()
  19. {
  20. model = new TaskModel();
  21. }
  22. @Override
  23. protected void tearDown()
  24. {
  25. }
  26. public void testDefaultPrintDepth() throws Exception {
  27. assertEquals(model.getPrintDepth(), -1);
  28. model.validate(new ArrayList<String>());
  29. assertEquals(model.getPrintDepth(), TaskModel.DEFAULT_PRINT_DEPTH);
  30. }
  31. public void testDefaultMinCost() throws Exception {
  32. assertEquals(model.getMinCost(), -1);
  33. model.validate(new ArrayList<String>());
  34. assertEquals(model.getMinCost(), TaskModel.DEFAULT_MIN_COST);
  35. }
  36. public void testDefaultMaxExcellenceCost() throws Exception {
  37. assertEquals(model.getMaxExcellentCost(), -1);
  38. model.validate(new ArrayList<String>());
  39. assertEquals(model.getMaxExcellentCost(), TaskModel.DEFAULT_MAX_EXCELLENT_COST);
  40. }
  41. public void testDefaultMaxAcceptableCost() throws Exception {
  42. assertEquals(model.getMaxAcceptableCost(), -1);
  43. model.validate(new ArrayList<String>());
  44. assertEquals(model.getMaxAcceptableCost(), TaskModel.DEFAULT_MAX_ACCEPTABLE_COST);
  45. }
  46. public void testDeafaultWorstOffenderCount() throws Exception {
  47. assertEquals(model.getWorstOffenderCount(), -1);
  48. model.validate(new ArrayList<String>());
  49. assertEquals(model.getWorstOffenderCount(), TaskModel.DEFAULT_WORST_OFFENDER_COUNT);
  50. }
  51. public void testDefaultWhiteList() throws Exception {
  52. assertEquals(model.getWhiteList(), null);
  53. model.validate(new ArrayList<String>());
  54. assertEquals(model.getWhiteList(), TaskModel.DEFAULT_WHITE_LIST);
  55. }
  56. public void testDefaultPrint() throws Exception {
  57. assertEquals(model.getPrint(), null);
  58. model.validate(new ArrayList<String>());
  59. assertEquals(model.getPrint(), TaskModel.DEFAULT_PRINT);
  60. }
  61. public void testDefaultResultFile() throws Exception {
  62. assertEquals(null, model.getResultFile());
  63. model.validate(new ArrayList<String>());
  64. assertEquals(model.getResultFile(), TaskModel.DEFAULT_RESULT_FILE);
  65. }
  66. public void testDefaultErrorFile() throws Exception {
  67. assertEquals(null, model.getErrorFile());
  68. model.validate(new ArrayList<String>());
  69. assertEquals(model.getErrorFile(), TaskModel.DEFAULT_RESULT_FILE); // error becomes result if not set
  70. }
  71. public void testDefaultFilter() throws Exception {
  72. assertEquals(null, model.getFilter());
  73. model.validate(new ArrayList<String>());
  74. assertEquals(model.getFilter(), TaskModel.DEFAULT_FILTER);
  75. }
  76. public void testSystemOutOutputStream() throws Exception {
  77. OutputStream os = model.getOutputStream("System.out");
  78. assertEquals(os, System.out);
  79. }
  80. public void testSystemErrOutputStream() throws Exception {
  81. OutputStream os = model.getOutputStream("System.err");
  82. assertEquals(os, System.err);
  83. }
  84. public void testResultStreamError() throws Exception {
  85. List<String> messages = new ArrayList<String>();
  86. model.addClasspath(new Path(new Project()));
  87. model.setResultFile("/this/result/file/does/not/exist.txt");
  88. assertFalse(model.validate(messages));
  89. assertTrue(messages.contains(TaskModel.ERROR_RESULT_FILE_CREATION_FAILED));
  90. }
  91. public void testErrorStreamError() throws Exception {
  92. List<String> messages = new ArrayList<String>();
  93. model.addClasspath(new Path(new Project()));
  94. model.setErrorFile("/this/error/file/does/not/exist.txt");
  95. assertFalse(model.validate(messages));
  96. assertTrue(messages.contains(TaskModel.ERROR_ERROR_FILE_CREATION_FAILED));
  97. }
  98. public void testGetClassPath() throws Exception {
  99. Project proj = new Project();
  100. Path p = new Path(proj);
  101. proj.setBasedir(".");
  102. p.setLocation(new File("src-test/com/google/ant"));
  103. model.addClasspath(p);
  104. Matcher matcher = Pattern.compile("src-test.com.google.ant").matcher(model.getClassPath());
  105. assertTrue(matcher.find());
  106. }
  107. public void testResultFileNotSet() throws Exception {
  108. List<String> messages = new ArrayList<String>();
  109. model.addClasspath(new Path(new Project()));
  110. model.setResultFile(null);
  111. assertTrue(model.validate(messages));
  112. assertTrue(messages.contains(TaskModel.ERROR_RESULT_FILE_NOT_SET));
  113. }
  114. public void testFilterNotSet() throws Exception {
  115. List<String> messages = new ArrayList<String>();
  116. model.addClasspath(new Path(new Project()));
  117. model.setFilter(null);
  118. assertTrue(model.validate(messages));
  119. assertTrue(messages.contains(TaskModel.ERROR_FILTER_NOT_SET));
  120. }
  121. public void testErrorFileSameAsResultFile() throws Exception {
  122. List<String> messages = new ArrayList<String>();
  123. model.addClasspath(new Path(new Project()));
  124. model.setErrorFile(null);
  125. model.setResultFile(File.createTempFile("anyfile", ".temp").toString());
  126. assertTrue(model.validate(messages));
  127. assertEquals(model.getResultFile(), model.getErrorFile());
  128. }
  129. public void testFileSetNotSet() throws Exception {
  130. List<String> messages = new ArrayList<String>();
  131. assertFalse(model.validate(messages));
  132. assertTrue(messages.contains(TaskModel.ERROR_FILESET_NOT_SET));
  133. }
  134. }