1 2 import fit.Parse; 3 import fit.exception.FitParseException; 4 import jdepend.framework.*; 5 import junit.framework.TestCase; 6 import moduleDependencyFixture.*; 7 8 import java.util.*; 9 10 public class ModuleDependenciesTest extends TestCase { 11 private ModuleDependencies md; 12 private final String FIXTURE_ROW = "<tr><td>fixturename</td></tr>"; 13 private final String SINGLE_MODULE_COLUMN_HEAD = "<tr><td></td><td>noSuchModule</td></tr>"; 14 private Parse table; 15 private final String moduleA = "<td>moduleDependencyFixture.test.a</td>"; 16 private final String moduleB = "<td>moduleDependencyFixture.test.b</td>"; 17 18 19 protected void tearDown() throws Exception { 20 ModuleDependencyPaths.paths.clear(); 21 } 22 23 protected void setUp() throws Exception { 24 md = new ModuleDependencies(); 25 ModuleDependencyPaths.paths.add("C:\\cygwin\\home\\Bob\\ModuleDependencyFixture\\classes"); 26 } 27 28 public void testAddComponent() throws Exception { 29 assertEquals("", md.getComponents()); 30 md.addComponent("bob"); 31 assertEquals("bob", md.getComponents()); 32 md.addComponent("bill"); 33 assertEquals("bob,bill", md.getComponents()); 34 md.addComponent("john"); 35 assertEquals("bob,bill,john", md.getComponents()); 36 } 37 38 private void assertTableFailsWith(String messagePart) { 39 try { 40 md.doRows(table.parts.more); 41 fail("did not throw: \"" + messagePart + "\""); 42 } catch (Exception e) { 43 assertContains(e.getMessage(), messagePart); 44 } 45 } 46 47 private void assertContains(final String message, final String str) { 48 assertTrue("\"" + str + "\"" + " was not found in \"" + message + "\"", message.indexOf(str) >= 0); 49 } 50 51 public void testEmptyFixture() throws Exception { 52 table = new Parse("<table>" + FIXTURE_ROW + "</table>"); 53 assertTableFailsWith("no rows"); 54 } 55 56 public void testTableWithNoModuleRows() throws Exception { 57 table = new Parse("<table>" + FIXTURE_ROW + SINGLE_MODULE_COLUMN_HEAD + "</table>"); 58 assertTableFailsWith("no module rows"); 59 } 60 61 public void testUnbalancedtable() throws Exception { 62 table = 63 new Parse("<table>" + FIXTURE_ROW + SINGLE_MODULE_COLUMN_HEAD + "<tr><td>notSame</td><td></td></tr></table>"); 64 assertTableFailsWith("unbalanced"); 65 } 66 67 public void testTableWithBlankFirstCell() throws Exception { 68 table = new Parse("<table>" + FIXTURE_ROW + "<tr><td>x</td></tr></table>"); 69 assertTableFailsWith("blank"); 70 } 71 72 public void testOneNonExistentModule() throws Exception { 73 table = new Parse("<table>" + FIXTURE_ROW + SINGLE_MODULE_COLUMN_HEAD + "<tr><td>noSuchModule</td></tr></table>"); 74 md.doRows(table.parts.more); 75 String moduleHeader = table.parts.more.parts.more.text(); 76 assertContains(moduleHeader, "no such module"); 77 } 78 79 public void testOneExistentModule() throws Exception { 80 String head = "<tr><td></td>" + moduleA + "</tr>"; 81 String moduleRow = "<tr>" + moduleA + "</tr>"; 82 table = new Parse("<table>" + FIXTURE_ROW + head + moduleRow + "</table>"); 83 md.doRows(table.parts.more); 84 String moduleHeader = table.parts.more.parts.more.tag; 85 assertContains(moduleHeader, "pass"); 86 } 87 88 public void testIllicitDependencyFound() throws Exception { 89 makeTableWithNoAllowedDependencies(); 90 Parse tableAfterFixture = table.parts.more; 91 md.doRows(tableAfterFixture); 92 Parse redCell = tableAfterFixture.at(1, 2); 93 assertContains(redCell.tag, "fail"); 94 } 95 96 private void makeTableWithNoAllowedDependencies() throws FitParseException { 97 String head = "<tr><td></td>" + moduleA + moduleB + "</tr>"; 98 String moduleRow1 = "<tr>" + moduleA + "<td></td><td></td></tr>"; 99 String moduleRow2 = "<tr>" + moduleB + "<td></td><td></td></tr>"; 100 table = new Parse("<table>" + FIXTURE_ROW + head + moduleRow1 + moduleRow2 + "</table>"); 101 } 102 103 public void testExpectedDependencyFound() throws Exception { 104 String head = "<tr><td></td>" + moduleA + moduleB + "</tr>"; 105 String moduleRow1 = "<tr>" + moduleA + "<td></td><td>!</td></tr>"; 106 String moduleRow2 = "<tr>" + moduleB + "<td></td><td></td></tr>"; 107 table = new Parse("<table>" + FIXTURE_ROW + head + moduleRow1 + moduleRow2 + "</table>"); 108 Parse tableAfterFixture = table.parts.more; 109 md.doRows(tableAfterFixture); 110 Parse greenCell = tableAfterFixture.at(1, 2); 111 assertContains(greenCell.tag, "pass"); 112 } 113 114 public void testDiagonal() throws Exception { 115 makeTableWithNoAllowedDependencies(); 116 Parse tableAfterFixture = table.parts.more; 117 md.doRows(tableAfterFixture); 118 Parse diagonal1 = tableAfterFixture.at(1, 1); 119 Parse diagonal2 = tableAfterFixture.at(2, 2); 120 assertContains(diagonal1.tag, "ignore"); 121 assertContains(diagonal2.tag, "ignore"); 122 } 123 124 public void testModuleLookup() throws Exception { 125 makeTableWithNoAllowedDependencies(); 126 Parse tableAfterFixture = table.parts.more; 127 md.doRows(tableAfterFixture); 128 assertEquals(0, md.getModuleNumber("moduleDependencyFixture.test.a")); 129 assertEquals(1, md.getModuleNumber("moduleDependencyFixture.test.b")); 130 try { 131 md.getModuleNumber("noSuchModule"); 132 fail("found 'noSuchModule'"); 133 } catch (Throwable e) { 134 } 135 } 136 137 private void makeTableWithCycle() throws FitParseException { 138 ModuleDependencyPaths.paths.add("C:\\cygwin\\home\\Bob\\ModuleDependencyFixture\\Cycle\\classes"); 139 String head = "<tr><td></td>" + "<td>a</td>" + "<td>b</td>" + "</tr>"; 140 String moduleRow1 = "<tr>" + "<td>a</td>" + "<td></td><td></td></tr>"; 141 String moduleRow2 = "<tr>" + "<td>b</td>" + "<td></td><td></td></tr>"; 142 table = new Parse("<table>" + FIXTURE_ROW + head + moduleRow1 + moduleRow2 + "</table>"); 143 } 144 145 public void testCycle() throws Exception { 146 makeTableWithCycle(); 147 Parse tableAfterFixture = table.parts.more; 148 md.doRows(tableAfterFixture); 149 Parse redRow = tableAfterFixture.at(0, 0); 150 assertContains(redRow.tag, "fail"); 151 assertContains(redRow.text(), "cycles"); 152 Parse atob = tableAfterFixture.at(1, 2); 153 assertContains(atob.text(), "cycle"); 154 assertContains(atob.tag, "fail"); 155 Parse btoa = tableAfterFixture.at(2, 1); 156 assertContains(btoa.text(), "cycle"); 157 assertContains(btoa.tag, "fail"); 158 } 159 160 public void testFitNesse() throws Exception { 161 JDepend jd = new JDepend(); 162 jd.addDirectory("C:\\cygwin\\home\\Bob\\Fitnesse\\classes"); 163 jd.addDirectory("C:\\cygwin\\home\\Bob\\Fitnesse\\classes\\fitnesse"); 164 jd.setComponents("fitnesse,eg,fit"); 165 jd.analyze(); 166 167 Collection packages = jd.getPackages(); 168 for (Iterator pi = packages.iterator(); pi.hasNext();) { 169 JavaPackage p = (JavaPackage) pi.next(); 170 System.out.println("p.getName() = " + p.getName()); 171 } 172 173 assertNotNull(jd.getPackage("fit")); 174 assertNotNull(jd.getPackage("fitnesse")); 175 assertNotNull(jd.getPackage("eg")); 176 } 177 } 178
| Popular Tags
|