001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.metrics;
034:
035: import java.io.*;
036: import java.util.*;
037:
038: import junit.framework.*;
039:
040: import com.jeantessier.classreader.*;
041:
042: public class TestMetricsGathererEvents extends TestCase implements
043: MetricsListener {
044: public static final String TEST_CLASS = "test";
045: public static final String TEST_FILENAME = "classes"
046: + File.separator + "test.class";
047: public static final String TEST_DIRNAME = "classes"
048: + File.separator + "testpackage";
049: public static final String OTHER_DIRNAME = "classes"
050: + File.separator + "otherpackage";
051:
052: private ClassfileLoader loader;
053: private MetricsGatherer gatherer;
054:
055: private LinkedList<MetricsEvent> beginSessionEvents;
056: private LinkedList<MetricsEvent> beginClassEvents;
057: private LinkedList<MetricsEvent> beginMethodEvents;
058: private LinkedList<MetricsEvent> endMethodEvents;
059: private LinkedList<MetricsEvent> endClassEvents;
060: private LinkedList<MetricsEvent> endSessionEvents;
061:
062: protected void setUp() throws Exception {
063: loader = new AggregatingClassfileLoader();
064:
065: MetricsFactory factory = new MetricsFactory("test",
066: new MetricsConfigurationLoader(Boolean
067: .getBoolean("DEPENDENCYFINDER_TESTS_VALIDATE"))
068: .load("etc" + File.separator
069: + "MetricsConfig.xml"));
070: gatherer = new MetricsGatherer("test", factory);
071: gatherer.addMetricsListener(this );
072:
073: beginSessionEvents = new LinkedList<MetricsEvent>();
074: beginClassEvents = new LinkedList<MetricsEvent>();
075: beginMethodEvents = new LinkedList<MetricsEvent>();
076: endMethodEvents = new LinkedList<MetricsEvent>();
077: endClassEvents = new LinkedList<MetricsEvent>();
078: endSessionEvents = new LinkedList<MetricsEvent>();
079: }
080:
081: public void testEvents() {
082: loader.load(Collections.singleton(TEST_FILENAME));
083:
084: gatherer.visitClassfiles(loader.getAllClassfiles());
085:
086: assertEquals("Begin Session", 1, beginSessionEvents.size());
087: assertEquals("Begin Class", 1, beginClassEvents.size());
088: assertEquals("Begin Method", 2, beginMethodEvents.size());
089: assertEquals("End Method", 2, endMethodEvents.size());
090: assertEquals("End Class", 1, endClassEvents.size());
091: assertEquals("End Session", 1, endSessionEvents.size());
092:
093: assertEquals(loader.getClassfile(TEST_CLASS), beginClassEvents
094: .getLast().getClassfile());
095: assertEquals(loader.getClassfile(TEST_CLASS), endClassEvents
096: .getLast().getClassfile());
097: }
098:
099: public void testMultipleEvents() {
100: Collection<String> dirs = new ArrayList<String>();
101: dirs.add(TEST_DIRNAME);
102: dirs.add(OTHER_DIRNAME);
103: loader.load(dirs);
104:
105: gatherer.visitClassfiles(loader.getAllClassfiles());
106:
107: assertEquals("Begin Session", 1, beginSessionEvents.size());
108: assertEquals("Begin Class", 9, beginClassEvents.size());
109: assertEquals("Begin Method", 16, beginMethodEvents.size());
110: assertEquals("End Method", 16, endMethodEvents.size());
111: assertEquals("End Class", 9, endClassEvents.size());
112: assertEquals("End Session", 1, endSessionEvents.size());
113: }
114:
115: public void testEventsWithNothing() {
116: loader.load(Collections.EMPTY_SET);
117:
118: gatherer.visitClassfiles(loader.getAllClassfiles());
119:
120: assertEquals("Begin Session", 1, beginSessionEvents.size());
121: assertEquals("Begin Class", 0, beginClassEvents.size());
122: assertEquals("Begin Method", 0, beginMethodEvents.size());
123: assertEquals("End Method", 0, endMethodEvents.size());
124: assertEquals("End Class", 0, endClassEvents.size());
125: assertEquals("End Session", 1, endSessionEvents.size());
126: }
127:
128: public void beginSession(MetricsEvent event) {
129: beginSessionEvents.add(event);
130: }
131:
132: public void beginClass(MetricsEvent event) {
133: beginClassEvents.add(event);
134: }
135:
136: public void beginMethod(MetricsEvent event) {
137: beginMethodEvents.add(event);
138: }
139:
140: public void endMethod(MetricsEvent event) {
141: endMethodEvents.add(event);
142: }
143:
144: public void endClass(MetricsEvent event) {
145: endClassEvents.add(event);
146: }
147:
148: public void endSession(MetricsEvent event) {
149: endSessionEvents.add(event);
150: }
151: }
|