1 32 33 package com.jeantessier.metrics; 34 35 import junit.framework.*; 36 37 import java.io.*; 38 import java.util.*; 39 40 import org.xml.sax.*; 41 42 import com.jeantessier.classreader.*; 43 44 public class TestMetricsGathererEvents extends TestCase implements MetricsListener { 45 public static final String TEST_CLASS = "test"; 46 public static final String TEST_FILENAME = "classes" + File.separator + "test.class"; 47 public static final String TEST_DIRNAME = "classes" + File.separator + "testpackage"; 48 public static final String OTHER_DIRNAME = "classes" + File.separator + "otherpackage"; 49 50 private ClassfileLoader loader; 51 private MetricsGatherer gatherer; 52 53 private LinkedList beginSessionEvents; 54 private LinkedList beginClassEvents; 55 private LinkedList beginMethodEvents; 56 private LinkedList endMethodEvents; 57 private LinkedList endClassEvents; 58 private LinkedList endSessionEvents; 59 60 protected void setUp() throws Exception { 61 loader = new AggregatingClassfileLoader(); 62 63 MetricsFactory factory = new MetricsFactory("test", new MetricsConfigurationLoader(Boolean.getBoolean("DEPENDENCYFINDER_TESTS_VALIDATE")).load("etc" + File.separator + "MetricsConfig.xml")); 64 gatherer = new MetricsGatherer("test", factory); 65 gatherer.addMetricsListener(this); 66 67 beginSessionEvents = new LinkedList(); 68 beginClassEvents = new LinkedList(); 69 beginMethodEvents = new LinkedList(); 70 endMethodEvents = new LinkedList(); 71 endClassEvents = new LinkedList(); 72 endSessionEvents = new LinkedList(); 73 } 74 75 public void testEvents() throws IOException { 76 loader.load(Collections.singleton(TEST_FILENAME)); 77 78 gatherer.visitClassfiles(loader.getAllClassfiles()); 79 80 assertEquals("Begin Session", 1, beginSessionEvents.size()); 81 assertEquals("Begin Class", 1, beginClassEvents.size()); 82 assertEquals("Begin Method", 2, beginMethodEvents.size()); 83 assertEquals("End Method", 2, endMethodEvents.size()); 84 assertEquals("End Class", 1, endClassEvents.size()); 85 assertEquals("End Session", 1, endSessionEvents.size()); 86 87 assertEquals(loader.getClassfile(TEST_CLASS), ((MetricsEvent) beginClassEvents.getLast()).getClassfile()); 88 assertEquals(loader.getClassfile(TEST_CLASS), ((MetricsEvent) endClassEvents.getLast()).getClassfile()); 89 } 90 91 public void testMultipleEvents() throws IOException { 92 Collection dirs = new ArrayList(2); 93 dirs.add(TEST_DIRNAME); 94 dirs.add(OTHER_DIRNAME); 95 loader.load(dirs); 96 97 gatherer.visitClassfiles(loader.getAllClassfiles()); 98 99 assertEquals("Begin Session", 1, beginSessionEvents.size()); 100 assertEquals("Begin Class", 9, beginClassEvents.size()); 101 assertEquals("Begin Method", 16, beginMethodEvents.size()); 102 assertEquals("End Method", 16, endMethodEvents.size()); 103 assertEquals("End Class", 9, endClassEvents.size()); 104 assertEquals("End Session", 1, endSessionEvents.size()); 105 } 106 107 public void testEventsWithNothing() throws IOException { 108 loader.load(Collections.EMPTY_SET); 109 110 gatherer.visitClassfiles(loader.getAllClassfiles()); 111 112 assertEquals("Begin Session", 1, beginSessionEvents.size()); 113 assertEquals("Begin Class", 0, beginClassEvents.size()); 114 assertEquals("Begin Method", 0, beginMethodEvents.size()); 115 assertEquals("End Method", 0, endMethodEvents.size()); 116 assertEquals("End Class", 0, endClassEvents.size()); 117 assertEquals("End Session", 1, endSessionEvents.size()); 118 } 119 120 public void beginSession(MetricsEvent event) { 121 beginSessionEvents.add(event); 122 } 123 124 public void beginClass(MetricsEvent event) { 125 beginClassEvents.add(event); 126 } 127 128 public void beginMethod(MetricsEvent event) { 129 beginMethodEvents.add(event); 130 } 131 132 public void endMethod(MetricsEvent event) { 133 endMethodEvents.add(event); 134 } 135 136 public void endClass(MetricsEvent event) { 137 endClassEvents.add(event); 138 } 139 140 public void endSession(MetricsEvent event) { 141 endSessionEvents.add(event); 142 } 143 } 144 | Popular Tags |