KickJava   Java API By Example, From Geeks To Geeks.

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


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 public class TestGraphCopier extends TestCase {
41     private RegularExpressionSelectionCriteria scopeCriteria;
42     private RegularExpressionSelectionCriteria filterCriteria;
43     private NodeFactory factory;
44     
45     private Node _package;
46     private Node test_class;
47     private Node test_main_method;
48     private Node test_Test_method;
49         
50     private Node java_lang_package;
51     private Node java_lang_Object_class;
52     private Node java_lang_Object_Object_method;
53     private Node java_lang_String_class;
54         
55     private Node java_util_package;
56     private Node java_util_Collections_class;
57     private Node java_util_Collections_singleton_method;
58     
59     private GraphCopier copier;
60
61     protected void setUp() throws Exception JavaDoc {
62         scopeCriteria = new RegularExpressionSelectionCriteria();
63         filterCriteria = new RegularExpressionSelectionCriteria();
64         factory = new NodeFactory();
65
66         _package = factory.createPackage("");
67         test_class = factory.createClass("test");
68         test_main_method = factory.createFeature("test.main(String[])");
69         test_Test_method = factory.createFeature("test.Test()");
70         
71         java_lang_package = factory.createPackage("java.lang");
72         java_lang_Object_class = factory.createClass("java.lang.Object");
73         java_lang_Object_Object_method = factory.createFeature("java.lang.Object.Object()");
74         java_lang_String_class = factory.createClass("java.lang.String");
75         
76         java_util_package = factory.createPackage("java.util");
77         java_util_Collections_class = factory.createClass("java.util.Collections");
78         java_util_Collections_singleton_method = factory.createFeature("java.util.Collections.singleton(java.lang.Object)");
79         
80         test_class.addDependency(java_lang_Object_class);
81         test_main_method.addDependency(java_lang_Object_class);
82         test_main_method.addDependency(java_lang_Object_Object_method);
83         test_main_method.addDependency(java_lang_String_class);
84         test_main_method.addDependency(java_util_Collections_singleton_method);
85         test_Test_method.addDependency(java_lang_Object_Object_method);
86
87         copier = new GraphCopier(new SelectiveTraversalStrategy(scopeCriteria, filterCriteria));
88     }
89
90     public void testCopyFullGraph() {
91         copier.traverseNodes(factory.getPackages().values());
92
93         assertEquals("Different number of packages",
94                      factory.getPackages().size(),
95                      copier.getScopeFactory().getPackages().size());
96         assertEquals("Different number of classes",
97                      factory.getClasses().size(),
98                      copier.getScopeFactory().getClasses().size());
99         assertEquals("Different number of features",
100                      factory.getFeatures().size(),
101                      copier.getScopeFactory().getFeatures().size());
102
103         Iterator i;
104
105         i = factory.getPackages().keySet().iterator();
106         while(i.hasNext()) {
107             Object JavaDoc key = i.next();
108             assertEquals(factory.getPackages().get(key), copier.getScopeFactory().getPackages().get(key));
109             assertTrue(factory.getPackages().get(key) != copier.getScopeFactory().getPackages().get(key));
110             assertEquals(((Node) factory.getPackages().get(key)).getInboundDependencies().size(),
111                          ((Node) copier.getScopeFactory().getPackages().get(key)).getInboundDependencies().size());
112             assertEquals(((Node) factory.getPackages().get(key)).getOutboundDependencies().size(),
113                          ((Node) copier.getScopeFactory().getPackages().get(key)).getOutboundDependencies().size());
114         }
115         
116         i = factory.getClasses().keySet().iterator();
117         while(i.hasNext()) {
118             Object JavaDoc key = i.next();
119             assertEquals(factory.getClasses().get(key), copier.getScopeFactory().getClasses().get(key));
120             assertTrue(factory.getClasses().get(key) != copier.getScopeFactory().getClasses().get(key));
121             assertEquals(((Node) factory.getClasses().get(key)).getInboundDependencies().size(),
122                          ((Node) copier.getScopeFactory().getClasses().get(key)).getInboundDependencies().size());
123             assertEquals(((Node) factory.getClasses().get(key)).getOutboundDependencies().size(),
124                          ((Node) copier.getScopeFactory().getClasses().get(key)).getOutboundDependencies().size());
125         }
126         
127         i = factory.getFeatures().keySet().iterator();
128         while(i.hasNext()) {
129             Object JavaDoc key = i.next();
130             assertEquals(factory.getFeatures().get(key), copier.getScopeFactory().getFeatures().get(key));
131             assertTrue(factory.getFeatures().get(key) != copier.getScopeFactory().getFeatures().get(key));
132             assertEquals(((Node) factory.getFeatures().get(key)).getInboundDependencies().size(),
133                          ((Node) copier.getScopeFactory().getFeatures().get(key)).getInboundDependencies().size());
134             assertEquals(((Node) factory.getFeatures().get(key)).getOutboundDependencies().size(),
135                          ((Node) copier.getScopeFactory().getFeatures().get(key)).getOutboundDependencies().size());
136         }
137     }
138
139     public void testCopyAllNodesOnly() {
140         filterCriteria.setMatchingPackages(false);
141         filterCriteria.setMatchingClasses(false);
142         filterCriteria.setMatchingFeatures(false);
143         
144         copier.traverseNodes(factory.getPackages().values());
145
146         assertEquals("Different number of packages",
147                      factory.getPackages().size(),
148                      copier.getScopeFactory().getPackages().size());
149         assertEquals("Different number of classes",
150                      factory.getClasses().size(),
151                      copier.getScopeFactory().getClasses().size());
152         assertEquals("Different number of features",
153                      factory.getFeatures().size(),
154                      copier.getScopeFactory().getFeatures().size());
155
156         Iterator i;
157
158         i = factory.getPackages().keySet().iterator();
159         while(i.hasNext()) {
160             Object JavaDoc key = i.next();
161             assertEquals(factory.getPackages().get(key), copier.getScopeFactory().getPackages().get(key));
162             assertTrue(factory.getPackages().get(key) != copier.getScopeFactory().getPackages().get(key));
163             assertTrue(((Node) copier.getScopeFactory().getPackages().get(key)).getInboundDependencies().isEmpty());
164             assertTrue(((Node) copier.getScopeFactory().getPackages().get(key)).getOutboundDependencies().isEmpty());
165         }
166         
167         i = factory.getClasses().keySet().iterator();
168         while(i.hasNext()) {
169             Object JavaDoc key = i.next();
170             assertEquals(factory.getClasses().get(key), copier.getScopeFactory().getClasses().get(key));
171             assertTrue(factory.getClasses().get(key) != copier.getScopeFactory().getClasses().get(key));
172             assertTrue(((Node) copier.getScopeFactory().getClasses().get(key)).getInboundDependencies().isEmpty());
173             assertTrue(((Node) copier.getScopeFactory().getClasses().get(key)).getOutboundDependencies().isEmpty());
174         }
175         
176         i = factory.getFeatures().keySet().iterator();
177         while(i.hasNext()) {
178             Object JavaDoc key = i.next();
179             assertEquals(factory.getFeatures().get(key), copier.getScopeFactory().getFeatures().get(key));
180             assertTrue(factory.getFeatures().get(key) != copier.getScopeFactory().getFeatures().get(key));
181             assertTrue(((Node) copier.getScopeFactory().getFeatures().get(key)).getInboundDependencies().isEmpty());
182             assertTrue(((Node) copier.getScopeFactory().getFeatures().get(key)).getOutboundDependencies().isEmpty());
183         }
184     }
185
186     public void testCopyPackageNodesOnly() {
187         scopeCriteria.setMatchingClasses(false);
188         scopeCriteria.setMatchingFeatures(false);
189         filterCriteria.setMatchingPackages(false);
190         filterCriteria.setMatchingClasses(false);
191         filterCriteria.setMatchingFeatures(false);
192         
193         copier.traverseNodes(factory.getPackages().values());
194
195         assertEquals("Different number of packages",
196                      factory.getPackages().size(),
197                      copier.getScopeFactory().getPackages().size());
198         assertTrue(copier.getScopeFactory().getClasses().isEmpty());
199         assertTrue(copier.getScopeFactory().getFeatures().isEmpty());
200
201         Iterator i;
202
203         i = factory.getPackages().keySet().iterator();
204         while(i.hasNext()) {
205             Object JavaDoc key = i.next();
206             assertEquals(factory.getPackages().get(key), copier.getScopeFactory().getPackages().get(key));
207             assertTrue(factory.getPackages().get(key) != copier.getScopeFactory().getPackages().get(key));
208             assertTrue(((Node) copier.getScopeFactory().getPackages().get(key)).getInboundDependencies().isEmpty());
209             assertTrue(((Node) copier.getScopeFactory().getPackages().get(key)).getOutboundDependencies().isEmpty());
210         }
211     }
212
213     public void testCopyClassNodesOnly() {
214         scopeCriteria.setMatchingPackages(false);
215         scopeCriteria.setMatchingFeatures(false);
216         filterCriteria.setMatchingPackages(false);
217         filterCriteria.setMatchingClasses(false);
218         filterCriteria.setMatchingFeatures(false);
219         
220         copier.traverseNodes(factory.getPackages().values());
221
222         assertEquals("Different number of packages",
223                      factory.getPackages().size(),
224                      copier.getScopeFactory().getPackages().size());
225         assertEquals("Different number of classes",
226                      factory.getClasses().size(),
227                      copier.getScopeFactory().getClasses().size());
228         assertTrue(copier.getScopeFactory().getFeatures().isEmpty());
229
230         Iterator i;
231
232         i = factory.getPackages().keySet().iterator();
233         while(i.hasNext()) {
234             Object JavaDoc key = i.next();
235             assertEquals(factory.getPackages().get(key), copier.getScopeFactory().getPackages().get(key));
236             assertTrue(factory.getPackages().get(key) != copier.getScopeFactory().getPackages().get(key));
237             assertTrue(((Node) copier.getScopeFactory().getPackages().get(key)).getInboundDependencies().isEmpty());
238             assertTrue(((Node) copier.getScopeFactory().getPackages().get(key)).getOutboundDependencies().isEmpty());
239         }
240         
241         i = factory.getClasses().keySet().iterator();
242         while(i.hasNext()) {
243             Object JavaDoc key = i.next();
244             assertEquals(factory.getClasses().get(key), copier.getScopeFactory().getClasses().get(key));
245             assertTrue(factory.getClasses().get(key) != copier.getScopeFactory().getClasses().get(key));
246             assertTrue(((Node) copier.getScopeFactory().getClasses().get(key)).getInboundDependencies().isEmpty());
247             assertTrue(((Node) copier.getScopeFactory().getClasses().get(key)).getOutboundDependencies().isEmpty());
248         }
249     }
250
251     public void testCopyFeatureNodesOnly() {
252         scopeCriteria.setMatchingPackages(false);
253         scopeCriteria.setMatchingClasses(false);
254         filterCriteria.setMatchingPackages(false);
255         filterCriteria.setMatchingClasses(false);
256         filterCriteria.setMatchingFeatures(false);
257         
258         copier.traverseNodes(factory.getPackages().values());
259
260         assertEquals("Different number of packages",
261                      factory.getPackages().size(),
262                      copier.getScopeFactory().getPackages().size());
263         assertEquals("Different number of classes",
264                      3,
265                      copier.getScopeFactory().getClasses().size());
266         assertEquals("Different number of features",
267                      factory.getFeatures().size(),
268                      copier.getScopeFactory().getFeatures().size());
269
270         Iterator i;
271
272         i = copier.getScopeFactory().getPackages().keySet().iterator();
273         while(i.hasNext()) {
274             Object JavaDoc key = i.next();
275             assertEquals(factory.getPackages().get(key), copier.getScopeFactory().getPackages().get(key));
276             assertTrue(factory.getPackages().get(key) != copier.getScopeFactory().getPackages().get(key));
277             assertTrue(((Node) copier.getScopeFactory().getPackages().get(key)).getInboundDependencies().isEmpty());
278             assertTrue(((Node) copier.getScopeFactory().getPackages().get(key)).getOutboundDependencies().isEmpty());
279         }
280         
281         i = copier.getScopeFactory().getClasses().keySet().iterator();
282         while(i.hasNext()) {
283             Object JavaDoc key = i.next();
284             assertEquals(factory.getClasses().get(key), copier.getScopeFactory().getClasses().get(key));
285             assertTrue(factory.getClasses().get(key) != copier.getScopeFactory().getClasses().get(key));
286             assertTrue(((Node) copier.getScopeFactory().getClasses().get(key)).getInboundDependencies().isEmpty());
287             assertTrue(((Node) copier.getScopeFactory().getClasses().get(key)).getOutboundDependencies().isEmpty());
288         }
289         
290         i = copier.getScopeFactory().getFeatures().keySet().iterator();
291         while(i.hasNext()) {
292             Object JavaDoc key = i.next();
293             assertEquals(factory.getFeatures().get(key), copier.getScopeFactory().getFeatures().get(key));
294             assertTrue(factory.getFeatures().get(key) != copier.getScopeFactory().getFeatures().get(key));
295             assertTrue(((Node) copier.getScopeFactory().getFeatures().get(key)).getInboundDependencies().isEmpty());
296             assertTrue(((Node) copier.getScopeFactory().getFeatures().get(key)).getOutboundDependencies().isEmpty());
297         }
298     }
299
300     public void testCopyNothing() {
301         scopeCriteria.setMatchingPackages(false);
302         scopeCriteria.setMatchingClasses(false);
303         scopeCriteria.setMatchingFeatures(false);
304         
305         copier.traverseNodes(factory.getPackages().values());
306
307         assertTrue(copier.getScopeFactory().getPackages().isEmpty());
308         assertTrue(copier.getScopeFactory().getClasses().isEmpty());
309         assertTrue(copier.getScopeFactory().getFeatures().isEmpty());
310     }
311
312     public void testC2CasP2CSamePackage() {
313         NodeFactory factory = new NodeFactory();
314
315         Node a = factory.createPackage("a");
316         Node a_A = factory.createClass("a.A");
317         Node a_B = factory.createClass("a.B");
318     
319         a_A.addDependency(a_B);
320
321         RegularExpressionSelectionCriteria scopeCriteria = new RegularExpressionSelectionCriteria();
322         scopeCriteria.setMatchingClasses(false);
323         scopeCriteria.setMatchingFeatures(false);
324
325         RegularExpressionSelectionCriteria filterCriteria = new RegularExpressionSelectionCriteria();
326         filterCriteria.setMatchingPackages(false);
327         filterCriteria.setMatchingFeatures(false);
328         
329         GraphCopier copier = new GraphCopier(new SelectiveTraversalStrategy(scopeCriteria, filterCriteria));
330
331         copier.traverseNodes(factory.getPackages().values());
332
333         assertTrue(copier.getScopeFactory().getPackages().keySet().toString(), copier.getScopeFactory().getPackages().keySet().contains("a"));
334         assertTrue(copier.getScopeFactory().getClasses().isEmpty());
335         assertTrue(copier.getScopeFactory().getFeatures().isEmpty());
336
337         assertEquals(0, copier.getScopeFactory().createPackage("a").getInboundDependencies().size());
338         assertEquals(0, copier.getScopeFactory().createPackage("a").getOutboundDependencies().size());
339         assertEquals(0, copier.getScopeFactory().createPackage("b").getInboundDependencies().size());
340         assertEquals(0, copier.getScopeFactory().createPackage("b").getOutboundDependencies().size());
341     }
342 }
343
Popular Tags