1 32 33 package com.jeantessier.dependency; 34 35 import java.io.*; 36 import java.util.*; 37 38 import junit.framework.*; 39 40 import org.apache.oro.text.perl.*; 41 42 public class TestTransitiveClosureWithTestClass extends TestCase { 43 private NodeFactory factory; 44 45 private Node _package; 46 private Node test_class; 47 private Node test_main_method; 48 private Node test_Test_method; 49 50 private Node java_lang_package; 51 private Node java_lang_Object_class; 52 private Node java_lang_Object_Object_method; 53 private Node java_lang_String_class; 54 55 private Node java_util_package; 56 private Node java_util_Collections_class; 57 private Node java_util_Collections_singleton_method; 58 59 private List scopeIncludes; 60 61 private RegularExpressionSelectionCriteria startCriteria; 62 private RegularExpressionSelectionCriteria stopCriteria; 63 64 private NodeFactory resultFactory; 65 66 protected void setUp() throws Exception { 67 factory = new NodeFactory(); 68 69 _package = factory.createPackage(""); 70 test_class = factory.createClass("test"); 71 test_main_method = factory.createFeature("test.main(String[])"); 72 test_Test_method = factory.createFeature("test.test()"); 73 74 java_lang_package = factory.createPackage("java.lang"); 75 java_lang_Object_class = factory.createClass("java.lang.Object"); 76 java_lang_Object_Object_method = factory.createFeature("java.lang.Object.Object()"); 77 java_lang_String_class = factory.createClass("java.lang.String"); 78 79 java_util_package = factory.createPackage("java.util"); 80 java_util_Collections_class = factory.createClass("java.util.Collections"); 81 java_util_Collections_singleton_method = factory.createFeature("java.util.Collections.singleton(java.lang.Object)"); 82 83 test_class.addDependency(java_lang_Object_class); 84 test_main_method.addDependency(java_lang_Object_class); 85 test_main_method.addDependency(java_lang_Object_Object_method); 86 test_main_method.addDependency(java_lang_String_class); 87 test_main_method.addDependency(java_util_Collections_singleton_method); 88 test_Test_method.addDependency(java_lang_Object_Object_method); 89 90 scopeIncludes = new ArrayList(1); 91 scopeIncludes.add("/test/"); 92 93 startCriteria = new RegularExpressionSelectionCriteria(); 94 stopCriteria = new RegularExpressionSelectionCriteria(); 95 } 96 97 public void testCompleteClosure() { 98 startCriteria.setGlobalIncludes(scopeIncludes); 99 stopCriteria.setGlobalIncludes(Collections.EMPTY_LIST); 100 101 compute(factory.getPackages().values()); 102 103 assertEquals("Different number of packages", 104 factory.getPackages().size(), 105 resultFactory.getPackages().size()); 106 assertEquals("Different number of classes", 107 factory.getClasses().size(), 108 resultFactory.getClasses().size()); 109 assertEquals("Different number of features", 110 factory.getFeatures().size(), 111 resultFactory.getFeatures().size()); 112 113 Iterator i; 114 115 i = resultFactory.getPackages().keySet().iterator(); 116 while(i.hasNext()) { 117 Object key = i.next(); 118 assertEquals(factory.getPackages().get(key), resultFactory.getPackages().get(key)); 119 assertTrue(factory.getPackages().get(key) != resultFactory.getPackages().get(key)); 120 assertEquals("Package " + key + " has different inbound count", 121 ((Node) factory.getPackages().get(key)).getInboundDependencies().size(), 122 ((Node) resultFactory.getPackages().get(key)).getInboundDependencies().size()); 123 assertEquals("Package " + key + " has different outbound count", 124 ((Node) factory.getPackages().get(key)).getOutboundDependencies().size(), 125 ((Node) resultFactory.getPackages().get(key)).getOutboundDependencies().size()); 126 } 127 128 i = resultFactory.getClasses().keySet().iterator(); 129 while(i.hasNext()) { 130 Object key = i.next(); 131 assertEquals(factory.getClasses().get(key), resultFactory.getClasses().get(key)); 132 assertTrue(factory.getClasses().get(key) != resultFactory.getClasses().get(key)); 133 assertEquals("Class " + key + " has different inbound count", 134 ((Node) factory.getClasses().get(key)).getInboundDependencies().size(), 135 ((Node) resultFactory.getClasses().get(key)).getInboundDependencies().size()); 136 assertEquals("Class " + key + " has different outbound count", 137 ((Node) factory.getClasses().get(key)).getOutboundDependencies().size(), 138 ((Node) resultFactory.getClasses().get(key)).getOutboundDependencies().size()); 139 } 140 141 i = resultFactory.getFeatures().keySet().iterator(); 142 while(i.hasNext()) { 143 Object key = i.next(); 144 assertEquals(factory.getFeatures().get(key), resultFactory.getFeatures().get(key)); 145 assertTrue(factory.getFeatures().get(key) != resultFactory.getFeatures().get(key)); 146 assertEquals("Feature " + key + " has different inbound count", 147 ((Node) factory.getFeatures().get(key)).getInboundDependencies().size(), 148 ((Node) resultFactory.getFeatures().get(key)).getInboundDependencies().size()); 149 assertEquals("Feature " + key + " has different outbound count", 150 ((Node) factory.getFeatures().get(key)).getOutboundDependencies().size(), 151 ((Node) resultFactory.getFeatures().get(key)).getOutboundDependencies().size()); 152 } 153 } 154 155 public void testCopyAllNodesOnly() { 156 startCriteria.setGlobalIncludes(scopeIncludes); 157 stopCriteria.setMatchingPackages(false); 158 stopCriteria.setMatchingClasses(false); 159 stopCriteria.setMatchingFeatures(false); 160 161 compute(factory.getPackages().values()); 162 163 assertEquals("Different number of packages", 164 1, 165 resultFactory.getPackages().size()); 166 assertEquals("Different number of classes", 167 1, 168 resultFactory.getClasses().size()); 169 assertEquals("Different number of features", 170 2, 171 resultFactory.getFeatures().size()); 172 173 Iterator i; 174 175 i = resultFactory.getPackages().keySet().iterator(); 176 while(i.hasNext()) { 177 Object key = i.next(); 178 assertEquals(factory.getPackages().get(key), resultFactory.getPackages().get(key)); 179 assertTrue(factory.getPackages().get(key) != resultFactory.getPackages().get(key)); 180 assertTrue(((Node) resultFactory.getPackages().get(key)).getInboundDependencies().isEmpty()); 181 assertTrue(((Node) resultFactory.getPackages().get(key)).getOutboundDependencies().isEmpty()); 182 } 183 184 i = resultFactory.getClasses().keySet().iterator(); 185 while(i.hasNext()) { 186 Object key = i.next(); 187 assertEquals(factory.getClasses().get(key), resultFactory.getClasses().get(key)); 188 assertTrue(factory.getClasses().get(key) != resultFactory.getClasses().get(key)); 189 assertTrue(((Node) resultFactory.getClasses().get(key)).getInboundDependencies().isEmpty()); 190 assertTrue(((Node) resultFactory.getClasses().get(key)).getOutboundDependencies().isEmpty()); 191 } 192 193 i = resultFactory.getFeatures().keySet().iterator(); 194 while(i.hasNext()) { 195 Object key = i.next(); 196 assertEquals(factory.getFeatures().get(key), resultFactory.getFeatures().get(key)); 197 assertTrue(factory.getFeatures().get(key) != resultFactory.getFeatures().get(key)); 198 assertTrue(((Node) resultFactory.getFeatures().get(key)).getInboundDependencies().isEmpty()); 199 assertTrue(((Node) resultFactory.getFeatures().get(key)).getOutboundDependencies().isEmpty()); 200 } 201 } 202 203 public void testCopyPackageNodesOnly() { 204 startCriteria.setMatchingClasses(false); 205 startCriteria.setMatchingFeatures(false); 206 startCriteria.setGlobalIncludes(scopeIncludes); 207 stopCriteria.setMatchingPackages(false); 208 stopCriteria.setMatchingClasses(false); 209 stopCriteria.setMatchingFeatures(false); 210 211 compute(factory.getPackages().values()); 212 213 assertEquals("Different number of packages", 214 1, 215 resultFactory.getPackages().size()); 216 assertTrue(resultFactory.getClasses().isEmpty()); 217 assertTrue(resultFactory.getFeatures().isEmpty()); 218 } 219 220 public void testCopyClassNodesOnly() { 221 startCriteria.setMatchingPackages(false); 222 startCriteria.setMatchingFeatures(false); 223 startCriteria.setGlobalIncludes(scopeIncludes); 224 stopCriteria.setMatchingPackages(false); 225 stopCriteria.setMatchingClasses(false); 226 stopCriteria.setMatchingFeatures(false); 227 228 compute(factory.getPackages().values()); 229 230 assertEquals("Different number of packages", 231 1, 232 resultFactory.getPackages().size()); 233 assertEquals("Different number of classes", 234 1, 235 resultFactory.getClasses().size()); 236 assertTrue(resultFactory.getFeatures().isEmpty()); 237 238 Iterator i; 239 240 i = resultFactory.getPackages().keySet().iterator(); 241 while(i.hasNext()) { 242 Object key = i.next(); 243 assertEquals(factory.getPackages().get(key), resultFactory.getPackages().get(key)); 244 assertTrue(factory.getPackages().get(key) != resultFactory.getPackages().get(key)); 245 assertTrue(((Node) resultFactory.getPackages().get(key)).getInboundDependencies().isEmpty()); 246 assertTrue(((Node) resultFactory.getPackages().get(key)).getOutboundDependencies().isEmpty()); 247 } 248 249 i = resultFactory.getClasses().keySet().iterator(); 250 while(i.hasNext()) { 251 Object key = i.next(); 252 assertEquals(factory.getClasses().get(key), resultFactory.getClasses().get(key)); 253 assertTrue(factory.getClasses().get(key) != resultFactory.getClasses().get(key)); 254 assertTrue(((Node) resultFactory.getClasses().get(key)).getInboundDependencies().isEmpty()); 255 assertTrue(((Node) resultFactory.getClasses().get(key)).getOutboundDependencies().isEmpty()); 256 } 257 } 258 259 public void testCopyFeatureNodesOnly() { 260 startCriteria.setMatchingPackages(false); 261 startCriteria.setMatchingClasses(false); 262 startCriteria.setGlobalIncludes(scopeIncludes); 263 stopCriteria.setMatchingPackages(false); 264 stopCriteria.setMatchingClasses(false); 265 stopCriteria.setMatchingFeatures(false); 266 267 compute(factory.getPackages().values()); 268 269 assertEquals("Different number of packages", 270 1, 271 resultFactory.getPackages().size()); 272 assertEquals("Different number of classes", 273 1, 274 resultFactory.getClasses().size()); 275 assertEquals("Different number of features", 276 2, 277 resultFactory.getFeatures().size()); 278 279 Iterator i; 280 281 i = resultFactory.getPackages().keySet().iterator(); 282 while(i.hasNext()) { 283 Object key = i.next(); 284 assertEquals(factory.getPackages().get(key), resultFactory.getPackages().get(key)); 285 assertTrue(factory.getPackages().get(key) != resultFactory.getPackages().get(key)); 286 assertTrue(((Node) resultFactory.getPackages().get(key)).getInboundDependencies().isEmpty()); 287 assertTrue(((Node) resultFactory.getPackages().get(key)).getOutboundDependencies().isEmpty()); 288 } 289 290 i = resultFactory.getClasses().keySet().iterator(); 291 while(i.hasNext()) { 292 Object key = i.next(); 293 assertEquals(factory.getClasses().get(key), resultFactory.getClasses().get(key)); 294 assertTrue(factory.getClasses().get(key) != resultFactory.getClasses().get(key)); 295 assertTrue(((Node) resultFactory.getClasses().get(key)).getInboundDependencies().isEmpty()); 296 assertTrue(((Node) resultFactory.getClasses().get(key)).getOutboundDependencies().isEmpty()); 297 } 298 299 i = resultFactory.getFeatures().keySet().iterator(); 300 while(i.hasNext()) { 301 Object key = i.next(); 302 assertEquals(factory.getFeatures().get(key), resultFactory.getFeatures().get(key)); 303 assertTrue(factory.getFeatures().get(key) != resultFactory.getFeatures().get(key)); 304 assertTrue(((Node) resultFactory.getFeatures().get(key)).getInboundDependencies().isEmpty()); 305 assertTrue(((Node) resultFactory.getFeatures().get(key)).getOutboundDependencies().isEmpty()); 306 } 307 } 308 309 public void testCopyNothing() { 310 startCriteria.setMatchingPackages(false); 311 startCriteria.setMatchingClasses(false); 312 startCriteria.setMatchingFeatures(false); 313 314 compute(factory.getPackages().values()); 315 316 assertTrue(resultFactory.getPackages().isEmpty()); 317 assertTrue(resultFactory.getClasses().isEmpty()); 318 assertTrue(resultFactory.getFeatures().isEmpty()); 319 } 320 321 private void compute(Collection nodes) { 322 RegularExpressionSelectionCriteria localStartCriteria = new RegularExpressionSelectionCriteria(); 323 localStartCriteria.setGlobalIncludes(startCriteria.getGlobalIncludes()); 324 RegularExpressionSelectionCriteria localStopCriteria = new RegularExpressionSelectionCriteria(); 325 localStopCriteria.setGlobalIncludes(stopCriteria.getGlobalIncludes()); 326 327 TransitiveClosure closure = new TransitiveClosure(localStartCriteria, localStopCriteria); 328 closure.traverseNodes(nodes); 329 330 RegularExpressionSelectionCriteria localScopeCriteria = new RegularExpressionSelectionCriteria(); 331 localScopeCriteria.setMatchingPackages(startCriteria.isMatchingPackages()); 332 localScopeCriteria.setMatchingClasses(startCriteria.isMatchingClasses()); 333 localScopeCriteria.setMatchingFeatures(startCriteria.isMatchingFeatures()); 334 RegularExpressionSelectionCriteria localFilterCriteria = new RegularExpressionSelectionCriteria(); 335 localFilterCriteria.setMatchingPackages(stopCriteria.isMatchingPackages()); 336 localFilterCriteria.setMatchingClasses(stopCriteria.isMatchingClasses()); 337 localFilterCriteria.setMatchingFeatures(stopCriteria.isMatchingFeatures()); 338 339 GraphSummarizer summarizer = new GraphSummarizer(localScopeCriteria, localFilterCriteria); 340 summarizer.traverseNodes(closure.getFactory().getPackages().values()); 341 342 resultFactory = summarizer.getScopeFactory(); 343 } 344 } 345 | Popular Tags |