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.dependency;
034:
035: import junit.framework.*;
036:
037: public class TestMetricsGatherer extends TestCase {
038: private NodeFactory factory;
039:
040: private Node _package;
041: private Node test_class;
042: private Node test_main_method;
043: private Node test_test_method;
044:
045: private Node java_lang_package;
046: private Node java_lang_Object_class;
047: private Node java_lang_Object_Object_method;
048: private Node java_lang_String_class;
049:
050: private Node java_io_package;
051: private Node java_io_Writer_class;
052: private Node java_io_Writer_write_method;
053:
054: private Node java_util_package;
055: private Node java_util_Collections_class;
056: private Node java_util_Collections_singleton_method;
057: private Node java_util_Set_class;
058:
059: private MetricsGatherer metrics;
060:
061: protected void setUp() throws Exception {
062: factory = new NodeFactory();
063:
064: _package = factory.createPackage("", true);
065: test_class = factory.createClass("test", true);
066: test_main_method = factory.createFeature(
067: "test.main(java.lang.String[])", true);
068: test_test_method = factory.createFeature("test.test()", true);
069:
070: java_lang_package = factory.createPackage("java.lang");
071: java_lang_Object_class = factory
072: .createClass("java.lang.Object");
073: java_lang_Object_Object_method = factory
074: .createFeature("java.lang.Object.Object()");
075: java_lang_String_class = factory
076: .createClass("java.lang.String");
077:
078: java_io_package = factory.createPackage("java.io");
079: java_io_Writer_class = factory.createClass("java.io.Writer");
080: java_io_Writer_write_method = factory
081: .createFeature("java.io.Writer.write(int)");
082:
083: java_util_package = factory.createPackage("java.util");
084: java_util_Collections_class = factory
085: .createClass("java.util.Collections");
086: java_util_Collections_singleton_method = factory
087: .createFeature("java.util.Collections.singleton(java.lang.Object)");
088: java_util_Set_class = factory.createClass("java.util.Set");
089:
090: test_class.addDependency(java_lang_Object_class);
091: test_main_method.addDependency(java_lang_Object_class);
092: test_main_method.addDependency(java_lang_Object_Object_method);
093: test_main_method.addDependency(java_lang_String_class);
094: test_main_method
095: .addDependency(java_util_Collections_singleton_method);
096: test_main_method.addDependency(java_util_Set_class);
097: test_test_method.addDependency(java_lang_Object_Object_method);
098:
099: metrics = new MetricsGatherer();
100: }
101:
102: public void testEverything() {
103: metrics.traverseNodes(factory.getPackages().values());
104:
105: assertEquals("Number of packages", 4, metrics.getPackages()
106: .size());
107: assertEquals("Number of classes", 6, metrics.getClasses()
108: .size());
109: assertEquals("Number of features", 5, metrics.getFeatures()
110: .size());
111:
112: assertEquals("Number of inbounds", 7, metrics.getNbInbound());
113: assertEquals("Number of inbounds to packages", 0, metrics
114: .getNbInboundPackages());
115: assertEquals("Number of inbounds to classes", 4, metrics
116: .getNbInboundClasses());
117: assertEquals("Number of inbounds to features", 3, metrics
118: .getNbInboundFeatures());
119:
120: assertEquals("Number of outbounds", 7, metrics.getNbOutbound());
121: assertEquals("Number of outbounds from packages", 0, metrics
122: .getNbOutboundPackages());
123: assertEquals("Number of outbounds from classes", 1, metrics
124: .getNbOutboundClasses());
125: assertEquals("Number of outbounds from features", 6, metrics
126: .getNbOutboundFeatures());
127: }
128: }
|