1 package moduleDependencyFixture; 2 3 import fit.Parse; 4 import fit.exception.FitParseException; 5 import jdepend.framework.*; 6 import junit.framework.TestCase; 7 import moduleDependencyFixture.*; 8 9 import java.util.*; 10 11 public class ModuleDependenciesTest extends TestCase { 12 private ModuleDependencies md; 13 private final String FIXTURE_ROW = "<tr><td>fixturename</td></tr>"; 14 private final String SINGLE_MODULE_COLUMN_HEAD = "<tr><td></td><td>noSuchModule</td></tr>"; 15 private Parse table; 16 private final String moduleA = "<td>moduleDependencyFixture.test.a</td>"; 17 private final String moduleB = "<td>moduleDependencyFixture.test.b</td>"; 18 19 20 protected void tearDown() throws Exception { 21 ModuleDependencyPaths.paths.clear(); 22 } 23 24 protected void setUp() throws Exception { 25 md = new ModuleDependencies(); 26 ModuleDependencyPaths.paths.add("C:\\cygwin\\home\\Bob\\ModuleDependencyFixture\\classes"); 27 } 28 29 public void testAddComponent() throws Exception { 30 assertEquals("", md.getComponents()); 31 md.addComponent("bob"); 32 assertEquals("bob", md.getComponents()); 33 md.addComponent("bill"); 34 assertEquals("bob,bill", md.getComponents()); 35 md.addComponent("john"); 36 assertEquals("bob,bill,john", md.getComponents()); 37 } 38 39 private void assertTableFailsWith(String messagePart) { 40 try { 41 md.doRows(table.parts.more); 42 fail("did not throw: \"" + messagePart + "\""); 43 } catch (Exception e) { 44 assertContains(e.getMessage(), messagePart); 45 } 46 } 47 48 private void assertContains(final String message, final String str) { 49 assertTrue("\"" + str + "\"" + " was not found in \"" + message + "\"", message.indexOf(str) >= 0); 50 } 51 52 public void testEmptyFixture() throws Exception { 53 table = new Parse("<table>" + FIXTURE_ROW + "</table>"); 54 assertTableFailsWith("no rows"); 55 } 56 57 public void testTableWithNoModuleRows() throws Exception { 58 table = new Parse("<table>" + FIXTURE_ROW + SINGLE_MODULE_COLUMN_HEAD + "</table>"); 59 assertTableFailsWith("no module rows"); 60 } 61 62 public void testUnbalancedtable() throws Exception { 63 table = 64 new Parse("<table>" + FIXTURE_ROW + SINGLE_MODULE_COLUMN_HEAD + "<tr><td>notSame</td><td></td></tr></table>"); 65 assertTableFailsWith("unbalanced"); 66 } 67 68 public void testTableWithBlankFirstCell() throws Exception { 69 table = new Parse("<table>" + FIXTURE_ROW + "<tr><td>x</td></tr></table>"); 70 assertTableFailsWith("blank"); 71 } 72 73 public void testOneNonExistentModule() throws Exception { 74 table = new Parse("<table>" + FIXTURE_ROW + SINGLE_MODULE_COLUMN_HEAD + "<tr><td>noSuchModule</td></tr></table>"); 75 md.doRows(table.parts.more); 76 String moduleHeader = table.parts.more.parts.more.text(); 77 assertContains(moduleHeader, "no such module"); 78 } 79 80 public void testOneExistentModule() throws Exception { 81 String head = "<tr><td></td>" + moduleA + "</tr>"; 82 String moduleRow = "<tr>" + moduleA + "</tr>"; 83 table = new Parse("<table>" + FIXTURE_ROW + head + moduleRow + "</table>"); 84 md.doRows(table.parts.more); 85 String moduleHeader = table.parts.more.parts.more.tag; 86 assertContains(moduleHeader, "pass"); 87 } 88 89 public void testIllicitDependencyFound() throws Exception { 90 makeTableWithNoAllowedDependencies(); 91 Parse tableAfterFixture = table.parts.more; 92 md.doRows(tableAfterFixture); 93 Parse redCell = tableAfterFixture.at(1, 2); 94 assertContains(redCell.tag, "fail"); 95 } 96 97 public void testPrefix() throws Exception { 98 Parse table = new Parse("<table><tr><td>fixture</td></tr>"+ 99 "<tr><td></td><td>.a</td><td>b</td></tr>"+ 100 "<tr><td>.a</td><td></td><td></td></tr>"+ 101 "<tr><td>b</td><td></td><td></td></tr></table>"); 102 Parse tableAfterFixture = table.parts.more; 103 md.doRows(tableAfterFixture); 104 md.prefix = "x"; 105 assertEquals("x.a", md.getModuleName(1,0)); 106 assertEquals("b", md.getModuleName(2,0)); 107 } 108 109 private void makeTableWithNoAllowedDependencies() throws FitParseException { 110 String head = "<tr><td></td>" + moduleA + moduleB + "</tr>"; 111 String moduleRow1 = "<tr>" + moduleA + "<td></td><td></td></tr>"; 112 String moduleRow2 = "<tr>" + moduleB + "<td></td><td></td></tr>"; 113 table = new Parse("<table>" + FIXTURE_ROW + head + moduleRow1 + moduleRow2 + "</table>"); 114 } 115 116 public void testExpectedDependencyFound() throws Exception { 117 String head = "<tr><td></td>" + moduleA + moduleB + "</tr>"; 118 String moduleRow1 = "<tr>" + moduleA + "<td></td><td>!</td></tr>"; 119 String moduleRow2 = "<tr>" + moduleB + "<td></td><td></td></tr>"; 120 table = new Parse("<table>" + FIXTURE_ROW + head + moduleRow1 + moduleRow2 + "</table>"); 121 Parse tableAfterFixture = table.parts.more; 122 md.doRows(tableAfterFixture); 123 Parse greenCell = tableAfterFixture.at(1, 2); 124 assertContains(greenCell.tag, "pass"); 125 } 126 127 public void testDiagonal() throws Exception { 128 makeTableWithNoAllowedDependencies(); 129 Parse tableAfterFixture = table.parts.more; 130 md.doRows(tableAfterFixture); 131 Parse diagonal1 = tableAfterFixture.at(1, 1); 132 Parse diagonal2 = tableAfterFixture.at(2, 2); 133 assertContains(diagonal1.tag, "ignore"); 134 assertContains(diagonal2.tag, "ignore"); 135 } 136 137 public void testModuleLookup() throws Exception { 138 makeTableWithNoAllowedDependencies(); 139 Parse tableAfterFixture = table.parts.more; 140 md.doRows(tableAfterFixture); 141 assertEquals(0, md.getModuleNumber("moduleDependencyFixture.test.a")); 142 assertEquals(1, md.getModuleNumber("moduleDependencyFixture.test.b")); 143 try { 144 md.getModuleNumber("noSuchModule"); 145 fail("found 'noSuchModule'"); 146 } catch (Throwable e) { 147 } 148 } 149 150 private void makeTableWithCycle() throws FitParseException { 151 ModuleDependencyPaths.paths.add("C:\\cygwin\\home\\Bob\\ModuleDependencyFixture\\Cycle\\classes"); 153 String head = "<tr><td></td>" + "<td>a</td>" + "<td>b</td>" + "<td>c</td>"+"<td>d</td>"+"</tr>"; 154 String moduleRow1 = "<tr>" + "<td>a</td>" + "<td></td><td></td><td></td><td></td></tr>"; 155 String moduleRow2 = "<tr>" + "<td>b</td>" + "<td></td><td></td><td></td><td></td></tr>"; 156 String moduleRow3 = "<tr>" + "<td>c</td>" + "<td></td><td></td><td></td><td></td></tr>"; 157 String moduleRow4 = "<tr>" + "<td>d</td>" + "<td></td><td></td><td></td><td></td></tr>"; 158 table = new Parse("<table>" + FIXTURE_ROW + head + moduleRow1 + moduleRow2 + moduleRow3 + moduleRow4 + "</table>"); 159 } 160 161 public void testDependencyMap() throws Exception { 162 makeTableWithCycle(); 163 Parse tableAfterFixture = table.parts.more; 164 md.doRows(tableAfterFixture); 165 boolean dependencies[][] = md.getDependencyMap(); 166 assertEquals(4, dependencies.length); 167 assertEquals(4, dependencies[0].length); 168 assertTrue(dependencies[0][1]); 169 assertTrue(dependencies[1][0]); 170 assertTrue(dependencies[2][0]); 171 assertTrue(dependencies[1][3]); 172 assertEquals(4, countTrueCells(dependencies)); 173 } 174 175 private int countTrueCells(boolean[][] dependencies) { 176 int trueCells = 0; 177 for (int from=0; from<dependencies.length; from++) 178 for (int to=0; to<dependencies.length; to++) 179 if (dependencies[from][to]) 180 trueCells++; 181 return trueCells; 182 } 183 184 public void testRemoveCycles() throws Exception { 185 boolean[][] noCycle = {{false, true},{false, false}}; 186 boolean[][] noDependencies = new boolean[2][2]; 187 188 removeNonCyclicDependencies(noCycle); 189 assertDependenciesEquals(noDependencies, noCycle); 190 191 boolean[][] cycle = {{false, true},{true, false}}; 192 boolean[][] cycle2 = {{false, true},{true, false}}; 193 removeNonCyclicDependencies(cycle); 194 assertDependenciesEquals(cycle2, cycle); 195 } 196 197 private void removeNonCyclicDependencies(boolean[][] d) { 198 md.dependencies = d; 199 md.removeNonCyclicDependencies(); 200 } 201 202 public void testFindInstablePackage() throws Exception { 203 boolean[][] noInstable = {{false, false},{false,false}}; 204 assertEquals(-1, findInstablePackage(noInstable)); 205 206 boolean[][] cycle = {{false, true},{true, false}}; 207 assertEquals(-1, findInstablePackage(cycle)); 208 209 boolean[][] firstColInstable = {{false, true, false},{false, false, false},{false, false, false}}; 210 assertEquals(0, findInstablePackage(firstColInstable)); 211 212 boolean[][] secondColInstable = {{false, false, false},{false, false, true}, {false, false, false}}; 213 assertEquals(1, findInstablePackage(secondColInstable)); 214 } 215 216 private int findInstablePackage(boolean[][] d) { 217 md.dependencies = d; 218 return md.findNonCyclicPackage(); 219 } 220 221 private void assertDependenciesEquals(boolean[][] expected, boolean[][] actual) 222 { 223 assertTrue("arrays not same size", expected.length == actual.length); 224 for (int i=0; i<expected.length; i++) 225 for (int j=0; j<actual.length; j++) 226 assertTrue("expected["+i+"]["+j+"] does not match", expected[i][j] == actual[i][j]); 227 } 228 229 public void testCycle() throws Exception { 230 makeTableWithCycle(); 231 Parse tableAfterFixture = table.parts.more; 232 md.doRows(tableAfterFixture); 233 Parse redRow = tableAfterFixture.at(0, 0); 234 assertContains(redRow.tag, "fail"); 235 assertContains(redRow.text(), "cycles"); 236 Parse atob = tableAfterFixture.at(1, 2); 238 assertContains(atob.text(), "cycle"); 239 assertContains(atob.tag, "fail"); 240 241 Parse btoa = tableAfterFixture.at(2, 1); 243 assertContains(btoa.text(), "cycle"); 244 assertContains(btoa.tag, "fail"); 245 246 Parse ctoa = tableAfterFixture.at(3,1); 248 assertEquals("", ctoa.text()); 249 250 Parse btod = tableAfterFixture.at(2,4); 252 assertEquals("", btod.text()); 253 } 254 255 public void testFitNesse() throws Exception { 256 JDepend jd = new JDepend(); 257 jd.addDirectory("C:\\cygwin\\home\\Bob\\Fitnesse\\classes"); 258 jd.setComponents("fitnesse,eg,fit"); 259 jd.analyze(); 260 261 Collection packages = jd.getPackages(); 262 for (Iterator pi = packages.iterator(); pi.hasNext();) { 263 JavaPackage p = (JavaPackage) pi.next(); 264 System.out.println("p.getName() = " + p.getName()); 265 } 266 267 assertNotNull(jd.getPackage("fit")); 268 assertNotNull(jd.getPackage("fitnesse")); 269 assertNotNull(jd.getPackage("eg")); 270 } 271 } 272 | Popular Tags |