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 java.io.*;
036: import java.util.*;
037:
038: import junit.framework.*;
039:
040: import org.apache.log4j.*;
041: import org.apache.oro.text.perl.*;
042:
043: import com.jeantessier.classreader.*;
044:
045: public class TestDependencyExtractor extends TestCase {
046: public static final String TEST_CLASS = "test";
047: public static final String TEST_FILENAME = "classes"
048: + File.separator + "test.class";
049:
050: private NodeFactory factory;
051:
052: private Node _package;
053: private Node test_class;
054: private Node test_main_feature;
055: private Node test_test_feature;
056:
057: private Node java_io_package;
058: private Node java_io_PrintStream_class;
059: private Node java_io_PrintStream_println_feature;
060:
061: private Node java_lang_package;
062: private Node java_lang_NullPointerException_class;
063: private Node java_lang_Object_class;
064: private Node java_lang_Object_Object_feature;
065: private Node java_lang_String_class;
066: private Node java_lang_System_class;
067: private Node java_lang_System_out_feature;
068:
069: private Node java_util_package;
070: private Node java_util_Collections_class;
071: private Node java_util_Collections_singleton_feature;
072: private Node java_util_Set_class;
073:
074: private ClassfileLoader loader;
075: private NodeFactory testFactory;
076:
077: protected void setUp() throws Exception {
078: Logger.getLogger(getClass())
079: .info("Starting test: " + getName());
080:
081: factory = new NodeFactory();
082:
083: _package = factory.createPackage("");
084: test_class = factory.createClass("test");
085: test_main_feature = factory
086: .createFeature("test.main(java.lang.String[])");
087: test_test_feature = factory.createFeature("test.test()");
088:
089: java_io_package = factory.createPackage("java.io");
090: java_io_PrintStream_class = factory
091: .createClass("java.io.PrintStream");
092: java_io_PrintStream_println_feature = factory
093: .createFeature("java.io.PrintStream.println(java.lang.Object)");
094:
095: java_lang_package = factory.createPackage("java.lang");
096: java_lang_NullPointerException_class = factory
097: .createClass("java.lang.NullPointerException");
098: java_lang_Object_class = factory
099: .createClass("java.lang.Object");
100: java_lang_Object_Object_feature = factory
101: .createFeature("java.lang.Object.Object()");
102: java_lang_String_class = factory
103: .createClass("java.lang.String");
104: java_lang_System_class = factory
105: .createClass("java.lang.System");
106: java_lang_System_out_feature = factory
107: .createFeature("java.lang.System.out");
108:
109: java_util_package = factory.createPackage("java.util");
110: java_util_Collections_class = factory
111: .createClass("java.util.Collections");
112: java_util_Collections_singleton_feature = factory
113: .createFeature("java.util.Collections.singleton(java.lang.Object)");
114: java_util_Set_class = factory.createClass("java.util.Set");
115:
116: test_class.addDependency(java_lang_Object_class);
117: test_main_feature.addDependency(java_io_PrintStream_class);
118: test_main_feature
119: .addDependency(java_io_PrintStream_println_feature);
120: test_main_feature
121: .addDependency(java_lang_NullPointerException_class);
122: test_main_feature.addDependency(java_lang_Object_class);
123: test_main_feature
124: .addDependency(java_lang_Object_Object_feature);
125: test_main_feature.addDependency(java_lang_String_class);
126: test_main_feature.addDependency(java_lang_System_out_feature);
127: test_main_feature
128: .addDependency(java_util_Collections_singleton_feature);
129: test_main_feature.addDependency(java_util_Set_class);
130: test_test_feature
131: .addDependency(java_lang_Object_Object_feature);
132:
133: loader = new AggregatingClassfileLoader();
134: loader.load(Collections.singleton(TEST_FILENAME));
135:
136: testFactory = new NodeFactory();
137: loader.getClassfile(TEST_CLASS).accept(
138: new CodeDependencyCollector(testFactory));
139: }
140:
141: protected void tearDown() throws Exception {
142: Logger.getLogger(getClass()).info("End of " + getName());
143: }
144:
145: public void testPackageList() {
146: assertEquals("Different list of packages", factory
147: .getPackages().keySet(), testFactory.getPackages()
148: .keySet());
149: }
150:
151: public void testClassList() {
152: assertEquals("Different list of classes", factory.getClasses()
153: .keySet(), testFactory.getClasses().keySet());
154: }
155:
156: public void testFeatureList() {
157: assertEquals("Different list of features", factory
158: .getFeatures().keySet(), testFactory.getFeatures()
159: .keySet());
160: }
161:
162: public void testPackages() {
163: Iterator i = factory.getPackages().keySet().iterator();
164: while (i.hasNext()) {
165: Object key = i.next();
166: assertEquals(factory.getPackages().get(key), testFactory
167: .getPackages().get(key));
168: assertTrue(key + " is same",
169: factory.getPackages().get(key) != testFactory
170: .getPackages().get(key));
171: assertEquals(key + " inbounds", ((Node) factory
172: .getPackages().get(key)).getInboundDependencies()
173: .size(),
174: ((Node) testFactory.getPackages().get(key))
175: .getInboundDependencies().size());
176: assertEquals(key + " outbounds", ((Node) factory
177: .getPackages().get(key)).getOutboundDependencies()
178: .size(),
179: ((Node) testFactory.getPackages().get(key))
180: .getOutboundDependencies().size());
181: }
182: }
183:
184: public void testClasses() {
185: Iterator i = factory.getClasses().keySet().iterator();
186: while (i.hasNext()) {
187: Object key = i.next();
188: assertEquals(factory.getClasses().get(key), testFactory
189: .getClasses().get(key));
190: assertTrue(key + " is same",
191: factory.getClasses().get(key) != testFactory
192: .getClasses().get(key));
193: assertEquals(key + " inbounds", ((Node) factory
194: .getClasses().get(key)).getInboundDependencies()
195: .size(), ((Node) testFactory.getClasses().get(key))
196: .getInboundDependencies().size());
197: assertEquals(key + " outbounds", ((Node) factory
198: .getClasses().get(key)).getOutboundDependencies()
199: .size(), ((Node) testFactory.getClasses().get(key))
200: .getOutboundDependencies().size());
201: }
202: }
203:
204: public void testFeatures() {
205: Iterator i = factory.getFeatures().keySet().iterator();
206: while (i.hasNext()) {
207: Object key = i.next();
208: assertEquals(factory.getFeatures().get(key), testFactory
209: .getFeatures().get(key));
210: assertTrue(key + " is same",
211: factory.getFeatures().get(key) != testFactory
212: .getFeatures().get(key));
213: assertEquals(key + " inbounds", ((Node) factory
214: .getFeatures().get(key)).getInboundDependencies()
215: .size(),
216: ((Node) testFactory.getFeatures().get(key))
217: .getInboundDependencies().size());
218: assertEquals(key + " outbounds", ((Node) factory
219: .getFeatures().get(key)).getOutboundDependencies()
220: .size(),
221: ((Node) testFactory.getFeatures().get(key))
222: .getOutboundDependencies().size());
223: }
224: }
225:
226: public void testStaticInitializer() {
227: ClassfileLoader loader = new AggregatingClassfileLoader();
228: NodeFactory factory = new NodeFactory();
229:
230: loader.load(Collections.singleton("classes" + File.separator
231: + "StaticInitializerTest.class"));
232:
233: Classfile classfile = loader
234: .getClassfile("StaticInitializerTest");
235: classfile.accept(new CodeDependencyCollector(factory));
236:
237: Collection featureNames = factory.getFeatures().keySet();
238:
239: Iterator i = classfile.getAllMethods().iterator();
240: while (i.hasNext()) {
241: Method_info method = (Method_info) i.next();
242: assertTrue("Missing method " + method.getFullSignature(),
243: featureNames.contains(method.getFullSignature()));
244: }
245: }
246: }
|