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.log4j.*; 41 import org.apache.oro.text.perl.*; 42 43 import com.jeantessier.classreader.*; 44 45 public class TestDependencyExtractor extends TestCase { 46 public static final String TEST_CLASS = "test"; 47 public static final String TEST_FILENAME = "classes" + File.separator + "test.class"; 48 49 private NodeFactory factory; 50 51 private Node _package; 52 private Node test_class; 53 private Node test_main_feature; 54 private Node test_test_feature; 55 56 private Node java_io_package; 57 private Node java_io_PrintStream_class; 58 private Node java_io_PrintStream_println_feature; 59 60 private Node java_lang_package; 61 private Node java_lang_NullPointerException_class; 62 private Node java_lang_Object_class; 63 private Node java_lang_Object_Object_feature; 64 private Node java_lang_String_class; 65 private Node java_lang_System_class; 66 private Node java_lang_System_out_feature; 67 68 private Node java_util_package; 69 private Node java_util_Collections_class; 70 private Node java_util_Collections_singleton_feature; 71 private Node java_util_Set_class; 72 73 private ClassfileLoader loader; 74 private NodeFactory testFactory; 75 76 protected void setUp() throws Exception { 77 Logger.getLogger(getClass()).info("Starting test: " + getName()); 78 79 factory = new NodeFactory(); 80 81 _package = factory.createPackage(""); 82 test_class = factory.createClass("test"); 83 test_main_feature = factory.createFeature("test.main(java.lang.String[])"); 84 test_test_feature = factory.createFeature("test.test()"); 85 86 java_io_package = factory.createPackage("java.io"); 87 java_io_PrintStream_class = factory.createClass("java.io.PrintStream"); 88 java_io_PrintStream_println_feature = factory.createFeature("java.io.PrintStream.println(java.lang.Object)"); 89 90 java_lang_package = factory.createPackage("java.lang"); 91 java_lang_NullPointerException_class = factory.createClass("java.lang.NullPointerException"); 92 java_lang_Object_class = factory.createClass("java.lang.Object"); 93 java_lang_Object_Object_feature = factory.createFeature("java.lang.Object.Object()"); 94 java_lang_String_class = factory.createClass("java.lang.String"); 95 java_lang_System_class = factory.createClass("java.lang.System"); 96 java_lang_System_out_feature = factory.createFeature("java.lang.System.out"); 97 98 java_util_package = factory.createPackage("java.util"); 99 java_util_Collections_class = factory.createClass("java.util.Collections"); 100 java_util_Collections_singleton_feature = factory.createFeature("java.util.Collections.singleton(java.lang.Object)"); 101 java_util_Set_class = factory.createClass("java.util.Set"); 102 103 test_class.addDependency(java_lang_Object_class); 104 test_main_feature.addDependency(java_io_PrintStream_class); 105 test_main_feature.addDependency(java_io_PrintStream_println_feature); 106 test_main_feature.addDependency(java_lang_NullPointerException_class); 107 test_main_feature.addDependency(java_lang_Object_class); 108 test_main_feature.addDependency(java_lang_Object_Object_feature); 109 test_main_feature.addDependency(java_lang_String_class); 110 test_main_feature.addDependency(java_lang_System_out_feature); 111 test_main_feature.addDependency(java_util_Collections_singleton_feature); 112 test_main_feature.addDependency(java_util_Set_class); 113 test_test_feature.addDependency(java_lang_Object_Object_feature); 114 115 loader = new AggregatingClassfileLoader(); 116 loader.load(Collections.singleton(TEST_FILENAME)); 117 118 testFactory = new NodeFactory(); 119 loader.getClassfile(TEST_CLASS).accept(new CodeDependencyCollector(testFactory)); 120 } 121 122 protected void tearDown() throws Exception { 123 Logger.getLogger(getClass()).info("End of " + getName()); 124 } 125 126 public void testPackageList() { 127 assertEquals("Different list of packages", 128 factory.getPackages().keySet(), 129 testFactory.getPackages().keySet()); 130 } 131 132 public void testClassList() { 133 assertEquals("Different list of classes", 134 factory.getClasses().keySet(), 135 testFactory.getClasses().keySet()); 136 } 137 138 public void testFeatureList() { 139 assertEquals("Different list of features", 140 factory.getFeatures().keySet(), 141 testFactory.getFeatures().keySet()); 142 } 143 144 public void testPackages() { 145 Iterator i = factory.getPackages().keySet().iterator(); 146 while (i.hasNext()) { 147 Object key = i.next(); 148 assertEquals(factory.getPackages().get(key), testFactory.getPackages().get(key)); 149 assertTrue(key + " is same", factory.getPackages().get(key) != testFactory.getPackages().get(key)); 150 assertEquals(key + " inbounds", 151 ((Node) factory.getPackages().get(key)).getInboundDependencies().size(), 152 ((Node) testFactory.getPackages().get(key)).getInboundDependencies().size()); 153 assertEquals(key + " outbounds", 154 ((Node) factory.getPackages().get(key)).getOutboundDependencies().size(), 155 ((Node) testFactory.getPackages().get(key)).getOutboundDependencies().size()); 156 } 157 } 158 159 public void testClasses() { 160 Iterator i = factory.getClasses().keySet().iterator(); 161 while (i.hasNext()) { 162 Object key = i.next(); 163 assertEquals(factory.getClasses().get(key), testFactory.getClasses().get(key)); 164 assertTrue(key + " is same", factory.getClasses().get(key) != testFactory.getClasses().get(key)); 165 assertEquals(key + " inbounds", 166 ((Node) factory.getClasses().get(key)).getInboundDependencies().size(), 167 ((Node) testFactory.getClasses().get(key)).getInboundDependencies().size()); 168 assertEquals(key + " outbounds", 169 ((Node) factory.getClasses().get(key)).getOutboundDependencies().size(), 170 ((Node) testFactory.getClasses().get(key)).getOutboundDependencies().size()); 171 } 172 } 173 174 public void testFeatures() { 175 Iterator i = factory.getFeatures().keySet().iterator(); 176 while (i.hasNext()) { 177 Object key = i.next(); 178 assertEquals(factory.getFeatures().get(key), testFactory.getFeatures().get(key)); 179 assertTrue(key + " is same", factory.getFeatures().get(key) != testFactory.getFeatures().get(key)); 180 assertEquals(key + " inbounds", 181 ((Node) factory.getFeatures().get(key)).getInboundDependencies().size(), 182 ((Node) testFactory.getFeatures().get(key)).getInboundDependencies().size()); 183 assertEquals(key + " outbounds", 184 ((Node) factory.getFeatures().get(key)).getOutboundDependencies().size(), 185 ((Node) testFactory.getFeatures().get(key)).getOutboundDependencies().size()); 186 } 187 } 188 189 public void testStaticInitializer() throws IOException { 190 ClassfileLoader loader = new AggregatingClassfileLoader(); 191 NodeFactory factory = new NodeFactory(); 192 193 loader.load(Collections.singleton("classes" + File.separator + "StaticInitializerTest.class")); 194 195 Classfile classfile = loader.getClassfile("StaticInitializerTest"); 196 classfile.accept(new CodeDependencyCollector(factory)); 197 198 Collection featureNames = factory.getFeatures().keySet(); 199 200 Iterator i = classfile.getAllMethods().iterator(); 201 while (i.hasNext()) { 202 Method_info method = (Method_info) i.next(); 203 assertTrue("Missing method " + method.getFullSignature(), featureNames.contains(method.getFullSignature())); 204 } 205 } 206 } 207 | Popular Tags |