KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > dependencyfinder > cli > TestClassMatcher


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.dependencyfinder.cli;
34
35 import java.io.*;
36 import java.util.*;
37
38 import junit.framework.*;
39
40 import com.jeantessier.classreader.*;
41
42 public class TestClassMatcher extends TestCase {
43     public static final String JavaDoc TEST_CLASS = "test";
44     public static final String JavaDoc TEST_FILENAME = "classes" + File.separator + "test.class";
45     public static final String JavaDoc TEST_DIR = "tests" + File.separator + "JarJarDiff";
46     
47     public void testMatchNone() {
48         ClassMatcher matcher = new ClassMatcher(Collections.EMPTY_LIST, Collections.EMPTY_LIST);
49         
50         ClassfileLoader loader = new TransientClassfileLoader();
51         loader.addLoadListener(matcher);
52         loader.load(Collections.singleton(TEST_FILENAME));
53
54         assertEquals("Number of results", 0, matcher.getResults().size());
55     }
56     
57     public void testMatchClassfile() {
58         ClassMatcher matcher = new ClassMatcher(Collections.singletonList("//"), Collections.EMPTY_LIST);
59         
60         ClassfileLoader loader = new TransientClassfileLoader();
61         loader.addLoadListener(matcher);
62         loader.load(Collections.singleton(TEST_FILENAME));
63
64         assertEquals("Number of results", 1, matcher.getResults().size());
65         assertEquals("key", TEST_CLASS, matcher.getResults().keySet().iterator().next());
66         Iterator i = matcher.getResults().values().iterator();
67         while (i.hasNext()) {
68             List results = (List) i.next();
69             assertEquals("number results", 1, results.size());
70             assertEquals("value", TEST_FILENAME, results.get(0));
71         }
72     }
73
74     public void testOneLevelJar() {
75         String JavaDoc filename = TEST_DIR + File.separator + "onelevel.jar";
76         assertTrue(filename + " missing", new File(filename).exists());
77         
78         ClassMatcher matcher = new ClassMatcher(Collections.singletonList("//"), Collections.EMPTY_LIST);
79
80         ClassfileLoader loader = new TransientClassfileLoader();
81         loader.addLoadListener(matcher);
82         loader.load(Collections.singleton(filename));
83
84         assertEquals("Number of results", 17, matcher.getResults().size());
85         Iterator i = matcher.getResults().values().iterator();
86         while (i.hasNext()) {
87             List results = (List) i.next();
88             assertEquals("number results", 1, results.size());
89             assertEquals("value", filename, results.get(0));
90         }
91     }
92
93     public void testTwoLevelJar() {
94         String JavaDoc filename = TEST_DIR + File.separator + "twolevel.jar";
95         assertTrue(filename + " missing", new File(filename).exists());
96         
97         ClassMatcher matcher = new ClassMatcher(Collections.singletonList("//"), Collections.EMPTY_LIST);
98
99         ClassfileLoader loader = new TransientClassfileLoader();
100         loader.addLoadListener(matcher);
101         loader.load(Collections.singleton(filename));
102
103         assertEquals("Number of results", 17, matcher.getResults().size());
104         Iterator i = matcher.getResults().values().iterator();
105         while (i.hasNext()) {
106             List results = (List) i.next();
107             assertEquals("number results", 1, results.size());
108             assertEquals("value", "onelevel.zip", results.get(0));
109         }
110     }
111     
112     public void testMatchDirectory() {
113         String JavaDoc dirname = TEST_DIR + File.separator + "new";
114         assertTrue(dirname + " missing", new File(dirname).exists());
115
116         ClassMatcher matcher = new ClassMatcher(Collections.singletonList("//"), Collections.EMPTY_LIST);
117         
118         ClassfileLoader loader = new TransientClassfileLoader();
119         loader.addLoadListener(matcher);
120         loader.load(Collections.singleton(dirname));
121
122         assertEquals("Number of results", 17, matcher.getResults().size());
123         Iterator i = matcher.getResults().values().iterator();
124         while (i.hasNext()) {
125             List results = (List) i.next();
126             assertEquals("number results", 1, results.size());
127             assertEquals("value", dirname, results.get(0));
128         }
129     }
130     
131     public void testIncludes() {
132         String JavaDoc dirname = TEST_DIR + File.separator + "new";
133         assertTrue(dirname + " missing", new File(dirname).exists());
134
135         ClassMatcher matcher = new ClassMatcher(Collections.singletonList("/modified/i"), Collections.EMPTY_LIST);
136         
137         ClassfileLoader loader = new TransientClassfileLoader();
138         loader.addLoadListener(matcher);
139         loader.load(Collections.singleton(dirname));
140
141         assertEquals("Number of results", 12, matcher.getResults().size());
142         Iterator i = matcher.getResults().values().iterator();
143         while (i.hasNext()) {
144             List results = (List) i.next();
145             assertEquals("number results", 1, results.size());
146             assertEquals("value", dirname, results.get(0));
147         }
148     }
149     
150     public void testExcludes() {
151         String JavaDoc dirname = TEST_DIR + File.separator + "new";
152         assertTrue(dirname + " missing", new File(dirname).exists());
153
154         ClassMatcher matcher = new ClassMatcher(Collections.singletonList("//"), Collections.singletonList("/modified/i"));
155         
156         ClassfileLoader loader = new TransientClassfileLoader();
157         loader.addLoadListener(matcher);
158         loader.load(Collections.singleton(dirname));
159
160         assertEquals("Number of results", 5, matcher.getResults().size());
161         Iterator i = matcher.getResults().values().iterator();
162         while (i.hasNext()) {
163             List results = (List) i.next();
164             assertEquals("number results", 1, results.size());
165             assertEquals("value", dirname, results.get(0));
166         }
167     }
168
169     public void testMultiples() {
170         String JavaDoc filename1 = TEST_DIR + File.separator + "onelevel.zip";
171         String JavaDoc filename2 = TEST_DIR + File.separator + "onelevel.jar";
172         assertTrue(filename1 + " missing", new File(filename1).exists());
173         assertTrue(filename2 + " missing", new File(filename2).exists());
174
175         Collection filenames = new ArrayList();
176         filenames.add(filename1);
177         filenames.add(filename2);
178         
179         ClassMatcher matcher = new ClassMatcher(Collections.singletonList("//"), Collections.EMPTY_LIST);
180
181         ClassfileLoader loader = new TransientClassfileLoader();
182         loader.addLoadListener(matcher);
183         loader.load(filenames);
184
185         assertEquals("Number of results", 17, matcher.getResults().size());
186         Iterator i = matcher.getResults().values().iterator();
187         while (i.hasNext()) {
188             List results = (List) i.next();
189             assertEquals("number results", 2, results.size());
190             assertEquals("value", filename1, results.get(0));
191             assertEquals("value", filename2, results.get(1));
192         }
193     }
194 }
195
Popular Tags