KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > metrics > TestMetricsGathererEvents


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

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 JavaDoc TEST_CLASS = "test";
46     public static final String JavaDoc TEST_FILENAME = "classes" + File.separator + "test.class";
47     public static final String JavaDoc TEST_DIRNAME = "classes" + File.separator + "testpackage";
48     public static final String JavaDoc 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 JavaDoc {
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