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.dependencyfinder.cli;
034:
035: import java.io.*;
036: import java.util.*;
037:
038: import junit.framework.*;
039:
040: import com.jeantessier.classreader.*;
041:
042: public class TestClassMatcher extends TestCase {
043: public static final String TEST_CLASS = "test";
044: public static final String TEST_FILENAME = "classes"
045: + File.separator + "test.class";
046: public static final String TEST_DIR = "tests" + File.separator
047: + "JarJarDiff";
048:
049: public void testMatchNone() {
050: ClassMatcher matcher = new ClassMatcher(Collections.EMPTY_LIST,
051: Collections.EMPTY_LIST);
052:
053: ClassfileLoader loader = new TransientClassfileLoader();
054: loader.addLoadListener(matcher);
055: loader.load(Collections.singleton(TEST_FILENAME));
056:
057: assertEquals("Number of results", 0, matcher.getResults()
058: .size());
059: }
060:
061: public void testMatchClassfile() {
062: ClassMatcher matcher = new ClassMatcher(Collections
063: .singletonList("//"), Collections.EMPTY_LIST);
064:
065: ClassfileLoader loader = new TransientClassfileLoader();
066: loader.addLoadListener(matcher);
067: loader.load(Collections.singleton(TEST_FILENAME));
068:
069: assertEquals("Number of results", 1, matcher.getResults()
070: .size());
071: assertEquals("key", TEST_CLASS, matcher.getResults().keySet()
072: .iterator().next());
073: Iterator i = matcher.getResults().values().iterator();
074: while (i.hasNext()) {
075: List results = (List) i.next();
076: assertEquals("number results", 1, results.size());
077: assertEquals("value", TEST_FILENAME, results.get(0));
078: }
079: }
080:
081: public void testOneLevelJar() {
082: String filename = TEST_DIR + File.separator + "onelevel.jar";
083: assertTrue(filename + " missing", new File(filename).exists());
084:
085: ClassMatcher matcher = new ClassMatcher(Collections
086: .singletonList("//"), Collections.EMPTY_LIST);
087:
088: ClassfileLoader loader = new TransientClassfileLoader();
089: loader.addLoadListener(matcher);
090: loader.load(Collections.singleton(filename));
091:
092: assertEquals("Number of results", 14, matcher.getResults()
093: .size());
094: Iterator i = matcher.getResults().values().iterator();
095: while (i.hasNext()) {
096: List results = (List) i.next();
097: assertEquals("number results", 1, results.size());
098: assertEquals("value", filename, results.get(0));
099: }
100: }
101:
102: public void testTwoLevelJar() {
103: String filename = TEST_DIR + File.separator + "twolevel.jar";
104: assertTrue(filename + " missing", new File(filename).exists());
105:
106: ClassMatcher matcher = new ClassMatcher(Collections
107: .singletonList("//"), Collections.EMPTY_LIST);
108:
109: ClassfileLoader loader = new TransientClassfileLoader();
110: loader.addLoadListener(matcher);
111: loader.load(Collections.singleton(filename));
112:
113: assertEquals("Number of results", 14, matcher.getResults()
114: .size());
115: Iterator i = matcher.getResults().values().iterator();
116: while (i.hasNext()) {
117: List results = (List) i.next();
118: assertEquals("number results", 1, results.size());
119: assertEquals("value", "onelevel.zip", results.get(0));
120: }
121: }
122:
123: public void testMatchDirectory() {
124: String dirname = TEST_DIR + File.separator + "new";
125: assertTrue(dirname + " missing", new File(dirname).exists());
126:
127: ClassMatcher matcher = new ClassMatcher(Collections
128: .singletonList("//"), Collections.EMPTY_LIST);
129:
130: ClassfileLoader loader = new TransientClassfileLoader();
131: loader.addLoadListener(matcher);
132: loader.load(Collections.singleton(dirname));
133:
134: assertEquals("Number of results", 14, matcher.getResults()
135: .size());
136: Iterator i = matcher.getResults().values().iterator();
137: while (i.hasNext()) {
138: List results = (List) i.next();
139: assertEquals("number results", 1, results.size());
140: assertEquals("value", dirname, results.get(0));
141: }
142: }
143:
144: public void testIncludes() {
145: String dirname = TEST_DIR + File.separator + "new";
146: assertTrue(dirname + " missing", new File(dirname).exists());
147:
148: ClassMatcher matcher = new ClassMatcher(Collections
149: .singletonList("/modified/i"), Collections.EMPTY_LIST);
150:
151: ClassfileLoader loader = new TransientClassfileLoader();
152: loader.addLoadListener(matcher);
153: loader.load(Collections.singleton(dirname));
154:
155: assertEquals("Number of results", 13, matcher.getResults()
156: .size());
157: Iterator i = matcher.getResults().values().iterator();
158: while (i.hasNext()) {
159: List results = (List) i.next();
160: assertEquals("number results", 1, results.size());
161: assertEquals("value", dirname, results.get(0));
162: }
163: }
164:
165: public void testExcludes() {
166: String dirname = TEST_DIR + File.separator + "new";
167: assertTrue(dirname + " missing", new File(dirname).exists());
168:
169: ClassMatcher matcher = new ClassMatcher(Collections
170: .singletonList("//"), Collections
171: .singletonList("/modified/i"));
172:
173: ClassfileLoader loader = new TransientClassfileLoader();
174: loader.addLoadListener(matcher);
175: loader.load(Collections.singleton(dirname));
176:
177: assertEquals("Number of results", 1, matcher.getResults()
178: .size());
179: Iterator i = matcher.getResults().values().iterator();
180: while (i.hasNext()) {
181: List results = (List) i.next();
182: assertEquals("number results", 1, results.size());
183: assertEquals("value", dirname, results.get(0));
184: }
185: }
186:
187: public void testMultiples() {
188: String filename1 = TEST_DIR + File.separator + "onelevel.zip";
189: String filename2 = TEST_DIR + File.separator + "onelevel.jar";
190: assertTrue(filename1 + " missing", new File(filename1).exists());
191: assertTrue(filename2 + " missing", new File(filename2).exists());
192:
193: Collection<String> filenames = new ArrayList<String>();
194: filenames.add(filename1);
195: filenames.add(filename2);
196:
197: ClassMatcher matcher = new ClassMatcher(Collections
198: .singletonList("//"), Collections.EMPTY_LIST);
199:
200: ClassfileLoader loader = new TransientClassfileLoader();
201: loader.addLoadListener(matcher);
202: loader.load(filenames);
203:
204: assertEquals("Number of results", 14, matcher.getResults()
205: .size());
206: Iterator i = matcher.getResults().values().iterator();
207: while (i.hasNext()) {
208: List results = (List) i.next();
209: assertEquals("number results", 2, results.size());
210: assertEquals("value", filename1, results.get(0));
211: assertEquals("value", filename2, results.get(1));
212: }
213: }
214: }
|