KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > dependency > TestDependencyExtractor


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.dependency;
34
35 import java.io.*;
36 import java.util.*;
37
38 import junit.framework.*;
39
40 import org.apache.log4j.*;
41 import org.apache.oro.text.perl.*;
42
43 import com.jeantessier.classreader.*;
44
45 public class TestDependencyExtractor extends TestCase {
46     public static final String JavaDoc TEST_CLASS = "test";
47     public static final String JavaDoc TEST_FILENAME = "classes" + File.separator + "test.class";
48     
49     private NodeFactory factory;
50     
51     private Node _package;
52     private Node test_class;
53     private Node test_main_feature;
54     private Node test_test_feature;
55         
56     private Node java_io_package;
57     private Node java_io_PrintStream_class;
58     private Node java_io_PrintStream_println_feature;
59     
60     private Node java_lang_package;
61     private Node java_lang_NullPointerException_class;
62     private Node java_lang_Object_class;
63     private Node java_lang_Object_Object_feature;
64     private Node java_lang_String_class;
65     private Node java_lang_System_class;
66     private Node java_lang_System_out_feature;
67         
68     private Node java_util_package;
69     private Node java_util_Collections_class;
70     private Node java_util_Collections_singleton_feature;
71     private Node java_util_Set_class;
72
73     private ClassfileLoader loader;
74     private NodeFactory testFactory;
75
76     protected void setUp() throws Exception JavaDoc {
77         Logger.getLogger(getClass()).info("Starting test: " + getName());
78
79         factory = new NodeFactory();
80
81         _package = factory.createPackage("");
82         test_class = factory.createClass("test");
83         test_main_feature = factory.createFeature("test.main(java.lang.String[])");
84         test_test_feature = factory.createFeature("test.test()");
85         
86         java_io_package = factory.createPackage("java.io");
87         java_io_PrintStream_class = factory.createClass("java.io.PrintStream");
88         java_io_PrintStream_println_feature = factory.createFeature("java.io.PrintStream.println(java.lang.Object)");
89
90         java_lang_package = factory.createPackage("java.lang");
91         java_lang_NullPointerException_class = factory.createClass("java.lang.NullPointerException");
92         java_lang_Object_class = factory.createClass("java.lang.Object");
93         java_lang_Object_Object_feature = factory.createFeature("java.lang.Object.Object()");
94         java_lang_String_class = factory.createClass("java.lang.String");
95         java_lang_System_class = factory.createClass("java.lang.System");
96         java_lang_System_out_feature = factory.createFeature("java.lang.System.out");
97         
98         java_util_package = factory.createPackage("java.util");
99         java_util_Collections_class = factory.createClass("java.util.Collections");
100         java_util_Collections_singleton_feature = factory.createFeature("java.util.Collections.singleton(java.lang.Object)");
101         java_util_Set_class = factory.createClass("java.util.Set");
102         
103         test_class.addDependency(java_lang_Object_class);
104         test_main_feature.addDependency(java_io_PrintStream_class);
105         test_main_feature.addDependency(java_io_PrintStream_println_feature);
106         test_main_feature.addDependency(java_lang_NullPointerException_class);
107         test_main_feature.addDependency(java_lang_Object_class);
108         test_main_feature.addDependency(java_lang_Object_Object_feature);
109         test_main_feature.addDependency(java_lang_String_class);
110         test_main_feature.addDependency(java_lang_System_out_feature);
111         test_main_feature.addDependency(java_util_Collections_singleton_feature);
112         test_main_feature.addDependency(java_util_Set_class);
113         test_test_feature.addDependency(java_lang_Object_Object_feature);
114
115         loader = new AggregatingClassfileLoader();
116         loader.load(Collections.singleton(TEST_FILENAME));
117
118         testFactory = new NodeFactory();
119         loader.getClassfile(TEST_CLASS).accept(new CodeDependencyCollector(testFactory));
120     }
121
122     protected void tearDown() throws Exception JavaDoc {
123         Logger.getLogger(getClass()).info("End of " + getName());
124     }
125     
126     public void testPackageList() {
127         assertEquals("Different list of packages",
128                      factory.getPackages().keySet(),
129                      testFactory.getPackages().keySet());
130     }
131     
132     public void testClassList() {
133         assertEquals("Different list of classes",
134                      factory.getClasses().keySet(),
135                      testFactory.getClasses().keySet());
136     }
137     
138     public void testFeatureList() {
139         assertEquals("Different list of features",
140                      factory.getFeatures().keySet(),
141                      testFactory.getFeatures().keySet());
142     }
143     
144     public void testPackages() {
145         Iterator i = factory.getPackages().keySet().iterator();
146         while (i.hasNext()) {
147             Object JavaDoc key = i.next();
148             assertEquals(factory.getPackages().get(key), testFactory.getPackages().get(key));
149             assertTrue(key + " is same", factory.getPackages().get(key) != testFactory.getPackages().get(key));
150             assertEquals(key + " inbounds",
151                          ((Node) factory.getPackages().get(key)).getInboundDependencies().size(),
152                          ((Node) testFactory.getPackages().get(key)).getInboundDependencies().size());
153             assertEquals(key + " outbounds",
154                          ((Node) factory.getPackages().get(key)).getOutboundDependencies().size(),
155                          ((Node) testFactory.getPackages().get(key)).getOutboundDependencies().size());
156         }
157     }
158     
159     public void testClasses() {
160         Iterator i = factory.getClasses().keySet().iterator();
161         while (i.hasNext()) {
162             Object JavaDoc key = i.next();
163             assertEquals(factory.getClasses().get(key), testFactory.getClasses().get(key));
164             assertTrue(key + " is same", factory.getClasses().get(key) != testFactory.getClasses().get(key));
165             assertEquals(key + " inbounds",
166                          ((Node) factory.getClasses().get(key)).getInboundDependencies().size(),
167                          ((Node) testFactory.getClasses().get(key)).getInboundDependencies().size());
168             assertEquals(key + " outbounds",
169                          ((Node) factory.getClasses().get(key)).getOutboundDependencies().size(),
170                          ((Node) testFactory.getClasses().get(key)).getOutboundDependencies().size());
171         }
172     }
173     
174     public void testFeatures() {
175         Iterator i = factory.getFeatures().keySet().iterator();
176         while (i.hasNext()) {
177             Object JavaDoc key = i.next();
178             assertEquals(factory.getFeatures().get(key), testFactory.getFeatures().get(key));
179             assertTrue(key + " is same", factory.getFeatures().get(key) != testFactory.getFeatures().get(key));
180             assertEquals(key + " inbounds",
181                          ((Node) factory.getFeatures().get(key)).getInboundDependencies().size(),
182                          ((Node) testFactory.getFeatures().get(key)).getInboundDependencies().size());
183             assertEquals(key + " outbounds",
184                          ((Node) factory.getFeatures().get(key)).getOutboundDependencies().size(),
185                          ((Node) testFactory.getFeatures().get(key)).getOutboundDependencies().size());
186         }
187     }
188
189     public void testStaticInitializer() throws IOException {
190         ClassfileLoader loader = new AggregatingClassfileLoader();
191         NodeFactory factory = new NodeFactory();
192         
193         loader.load(Collections.singleton("classes" + File.separator + "StaticInitializerTest.class"));
194
195         Classfile classfile = loader.getClassfile("StaticInitializerTest");
196         classfile.accept(new CodeDependencyCollector(factory));
197
198         Collection featureNames = factory.getFeatures().keySet();
199         
200         Iterator i = classfile.getAllMethods().iterator();
201         while (i.hasNext()) {
202             Method_info method = (Method_info) i.next();
203             assertTrue("Missing method " + method.getFullSignature(), featureNames.contains(method.getFullSignature()));
204         }
205     }
206 }
207
Popular Tags