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 junit.framework.*;
036:
037: import java.io.*;
038: import java.util.*;
039:
040: import org.apache.log4j.*;
041:
042: import com.jeantessier.classreader.*;
043:
044: public class TestMetricsGathererSLOC extends TestCase {
045: public static final String TEST_DIRNAME = "classes"
046: + File.separator + "sloc";
047:
048: private MetricsFactory factory;
049:
050: protected void setUp() throws Exception {
051: Logger.getLogger(getClass()).debug(
052: "Starting " + getName() + " ...");
053:
054: factory = new MetricsFactory("test",
055: new MetricsConfigurationLoader(Boolean
056: .getBoolean("DEPENDENCYFINDER_TESTS_VALIDATE"))
057: .load("etc" + File.separator
058: + "MetricsConfig.xml"));
059:
060: ClassfileLoader loader = new AggregatingClassfileLoader();
061: loader.load(Collections.singleton(TEST_DIRNAME));
062:
063: MetricsGatherer gatherer = new MetricsGatherer("test", factory);
064:
065: for (Classfile classfile : loader.getAllClassfiles()) {
066: classfile.accept(gatherer);
067: }
068: }
069:
070: protected void tearDown() throws Exception {
071: Logger.getLogger(getClass()).debug(
072: "Done with " + getName() + " ...");
073: }
074:
075: public void test_sloc_TestInterface() {
076: assertEquals(Metrics.SLOC, 3, factory.createClassMetrics(
077: "sloc.TestInterface").getMeasurement(Metrics.SLOC)
078: .intValue());
079: assertEquals("M", 2, factory.createClassMetrics(
080: "sloc.TestInterface").getMeasurement("M").intValue());
081: assertEquals("AM", 2, factory.createClassMetrics(
082: "sloc.TestInterface").getMeasurement("AM").intValue());
083: assertEquals("SynthM", 0, factory.createClassMetrics(
084: "sloc.TestInterface").getMeasurement("SynthM")
085: .intValue());
086: }
087:
088: public void test_sloc_TestInterface_method1() {
089: assertEquals(Metrics.SLOC, 1, factory.createMethodMetrics(
090: "sloc.TestInterface.method1()").getMeasurement(
091: Metrics.SLOC).intValue());
092: }
093:
094: public void test_sloc_TestInterface_method2() {
095: assertEquals(Metrics.SLOC, 1, factory.createMethodMetrics(
096: "sloc.TestInterface.method2()").getMeasurement(
097: Metrics.SLOC).intValue());
098: }
099:
100: public void test_sloc_TestAbstractClass() {
101: assertEquals(Metrics.SLOC, 15, factory.createClassMetrics(
102: "sloc.TestAbstractClass").getMeasurement(Metrics.SLOC)
103: .intValue());
104: assertEquals("M", 2, factory.createClassMetrics(
105: "sloc.TestAbstractClass").getMeasurement("M")
106: .intValue());
107: assertEquals("AM", 0, factory.createClassMetrics(
108: "sloc.TestAbstractClass").getMeasurement("AM")
109: .intValue());
110: assertEquals("SynthM", 0, factory.createClassMetrics(
111: "sloc.TestAbstractClass").getMeasurement("SynthM")
112: .intValue());
113: }
114:
115: public void test_sloc_TestAbstractClass_method1() {
116: assertEquals(Metrics.SLOC, 13, factory.createMethodMetrics(
117: "sloc.TestAbstractClass.method1()").getMeasurement(
118: Metrics.SLOC).intValue());
119: }
120:
121: public void test_sloc_TestAbstractClass_method2() {
122: assertEquals(Metrics.SLOC, 0, factory.createMethodMetrics(
123: "sloc.TestAbstractClass.method2()").getMeasurement(
124: Metrics.SLOC).intValue());
125: }
126:
127: public void test_sloc_TestSuperClass() {
128: assertEquals(Metrics.SLOC, 2, factory.createClassMetrics(
129: "sloc.TestSuperClass").getMeasurement(Metrics.SLOC)
130: .intValue());
131: assertEquals("M", 1, factory.createClassMetrics(
132: "sloc.TestSuperClass").getMeasurement("M").intValue());
133: assertEquals("ABSM", 0, factory.createClassMetrics(
134: "sloc.TestSuperClass").getMeasurement("ABSM")
135: .intValue());
136: assertEquals("SYNTHM", 0, factory.createClassMetrics(
137: "sloc.TestSuperClass").getMeasurement("SYNTHM")
138: .intValue());
139: }
140:
141: public void test_sloc_TestClass() {
142: assertEquals(Metrics.SLOC, 4, factory.createClassMetrics(
143: "sloc.TestClass").getMeasurement(Metrics.SLOC)
144: .intValue());
145: assertEquals("M", 2, factory.createClassMetrics(
146: "sloc.TestClass").getMeasurement("M").intValue());
147: assertEquals("ABSM", 0, factory.createClassMetrics(
148: "sloc.TestClass").getMeasurement("ABSM").intValue());
149: assertEquals("SYNTHM", 0, factory.createClassMetrics(
150: "sloc.TestClass").getMeasurement("SYNTHM").intValue());
151: }
152:
153: public void test_sloc_TestClass_method1() {
154: assertEquals(Metrics.SLOC, 0, factory.createMethodMetrics(
155: "sloc.TestClass.method1()")
156: .getMeasurement(Metrics.SLOC).intValue());
157: }
158:
159: public void test_sloc_TestClass_method2() {
160: assertEquals(Metrics.SLOC, 2, factory.createMethodMetrics(
161: "sloc.TestClass.method2()")
162: .getMeasurement(Metrics.SLOC).intValue());
163: }
164:
165: public void test_sloc() {
166: assertEquals(Metrics.SLOC, 24, factory.createGroupMetrics(
167: "sloc").getMeasurement(Metrics.SLOC).intValue());
168: }
169:
170: public void testProject() {
171: assertEquals(Metrics.SLOC, 24, factory.createProjectMetrics(
172: "test").getMeasurement(Metrics.SLOC).intValue());
173: }
174: }
|