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.classreader;
034:
035: import java.io.*;
036: import java.util.*;
037:
038: import junit.framework.*;
039:
040: public class TestPackageMapper extends TestCase {
041: private static final String TEST_CLASSPATH = "tests"
042: + File.separator + "JarJarDiff" + File.separator + "old";
043: private static final String TEST_PACKAGENAME1 = "ModifiedPackage";
044: private static final String TEST_FILENAME1 = TEST_CLASSPATH
045: + File.separator + TEST_PACKAGENAME1 + File.separator
046: + "ModifiedClass.class";
047: private static final String TEST_CLASSNAME1 = TEST_PACKAGENAME1
048: + ".ModifiedClass";
049: private static final String TEST_FILENAME2 = TEST_CLASSPATH
050: + File.separator + TEST_PACKAGENAME1 + File.separator
051: + "ModifiedInterface.class";
052: private static final String TEST_CLASSNAME2 = TEST_PACKAGENAME1
053: + ".ModifiedInterface";
054: private static final String TEST_PACKAGENAME3 = "UnmodifiedPackage";
055: private static final String TEST_FILENAME3 = TEST_CLASSPATH
056: + File.separator + TEST_PACKAGENAME3 + File.separator
057: + "UnmodifiedClass.class";
058: private static final String TEST_CLASSNAME3 = TEST_PACKAGENAME3
059: + ".UnmodifiedClass";
060:
061: private PackageMapper mapper;
062:
063: private ClassfileLoader loader;
064:
065: protected void setUp() throws Exception {
066: super .setUp();
067:
068: mapper = new PackageMapper();
069:
070: loader = new AggregatingClassfileLoader();
071: Collection<String> filenames = new ArrayList<String>();
072: filenames.add(TEST_FILENAME1);
073: filenames.add(TEST_FILENAME2);
074: filenames.add(TEST_FILENAME3);
075: loader.load(filenames);
076: }
077:
078: public void testBeginSession() {
079: mapper.beginSession(new LoadEvent(loader, null, null, null));
080:
081: assertEquals("Nb package names", 0, mapper.getPackageNames()
082: .size());
083: }
084:
085: public void testBeginGroup() {
086: mapper.beginGroup(new LoadEvent(loader, TEST_CLASSPATH, null,
087: null));
088:
089: assertEquals("Nb package names", 0, mapper.getPackageNames()
090: .size());
091: }
092:
093: public void testBeginFile() {
094: mapper.beginFile(new LoadEvent(loader, TEST_CLASSPATH,
095: TEST_FILENAME1, null));
096:
097: assertEquals("Nb package names", 0, mapper.getPackageNames()
098: .size());
099: }
100:
101: public void testBeginClassfile() {
102: mapper.beginClassfile(new LoadEvent(loader, TEST_CLASSPATH,
103: TEST_FILENAME1, null));
104:
105: assertEquals("Nb package names", 0, mapper.getPackageNames()
106: .size());
107: }
108:
109: public void testOneClassfile() {
110: Classfile classfile = loader.getClassfile(TEST_CLASSNAME1);
111:
112: mapper.endClassfile(new LoadEvent(loader, TEST_CLASSPATH,
113: TEST_FILENAME1, classfile));
114:
115: assertEquals("Nb package names", 1, mapper.getPackageNames()
116: .size());
117: assertTrue(TEST_PACKAGENAME1 + " not in "
118: + mapper.getPackageNames(), mapper.getPackageNames()
119: .contains(TEST_PACKAGENAME1));
120: assertEquals("Nb classes in " + TEST_PACKAGENAME1, 1, mapper
121: .getPackage(TEST_PACKAGENAME1).size());
122: assertSame(TEST_CLASSNAME1, classfile, mapper.getPackage(
123: TEST_PACKAGENAME1).get(TEST_CLASSNAME1));
124: }
125:
126: public void testTwoClassfilesInSamePackages() {
127: Classfile classfile1 = loader.getClassfile(TEST_CLASSNAME1);
128: Classfile classfile2 = loader.getClassfile(TEST_CLASSNAME2);
129:
130: mapper.endClassfile(new LoadEvent(loader, TEST_CLASSPATH,
131: TEST_FILENAME1, classfile1));
132: mapper.endClassfile(new LoadEvent(loader, TEST_CLASSPATH,
133: TEST_FILENAME2, classfile2));
134:
135: assertEquals("Nb package names", 1, mapper.getPackageNames()
136: .size());
137: assertTrue(TEST_PACKAGENAME1, mapper.getPackageNames()
138: .contains(TEST_PACKAGENAME1));
139: assertEquals("Nb classes in " + TEST_PACKAGENAME1, 2, mapper
140: .getPackage(TEST_PACKAGENAME1).size());
141: assertSame(TEST_CLASSNAME1, classfile1, mapper.getPackage(
142: TEST_PACKAGENAME1).get(TEST_CLASSNAME1));
143: assertSame(TEST_CLASSNAME2, classfile2, mapper.getPackage(
144: TEST_PACKAGENAME1).get(TEST_CLASSNAME2));
145: }
146:
147: public void testTwoClassfilesInDifferentPackages() {
148: Classfile classfile1 = loader.getClassfile(TEST_CLASSNAME1);
149: Classfile classfile3 = loader.getClassfile(TEST_CLASSNAME3);
150:
151: mapper.endClassfile(new LoadEvent(loader, TEST_CLASSPATH,
152: TEST_FILENAME1, classfile1));
153: mapper.endClassfile(new LoadEvent(loader, TEST_CLASSPATH,
154: TEST_FILENAME3, classfile3));
155:
156: assertEquals("Nb package names", 2, mapper.getPackageNames()
157: .size());
158: assertTrue(TEST_PACKAGENAME1, mapper.getPackageNames()
159: .contains(TEST_PACKAGENAME1));
160: assertTrue(TEST_PACKAGENAME3, mapper.getPackageNames()
161: .contains(TEST_PACKAGENAME3));
162: assertEquals("Nb classes in " + TEST_PACKAGENAME1, 1, mapper
163: .getPackage(TEST_PACKAGENAME1).size());
164: assertSame(TEST_CLASSNAME1, classfile1, mapper.getPackage(
165: TEST_PACKAGENAME1).get(TEST_CLASSNAME1));
166: assertEquals("Nb classes in " + TEST_PACKAGENAME3, 1, mapper
167: .getPackage(TEST_PACKAGENAME3).size());
168: assertSame(TEST_CLASSNAME3, classfile3, mapper.getPackage(
169: TEST_PACKAGENAME3).get(TEST_CLASSNAME3));
170: }
171:
172: public void testEndFile() {
173: mapper.endFile(new LoadEvent(loader, TEST_CLASSPATH,
174: TEST_FILENAME1, null));
175:
176: assertEquals("Nb package names", 0, mapper.getPackageNames()
177: .size());
178: }
179:
180: public void testEndGroup() {
181: mapper.endGroup(new LoadEvent(loader, TEST_CLASSPATH, null,
182: null));
183:
184: assertEquals("Nb package names", 0, mapper.getPackageNames()
185: .size());
186: }
187:
188: public void testSession() {
189: mapper.endSession(new LoadEvent(loader, null, null, null));
190:
191: assertEquals("Nb package names", 0, mapper.getPackageNames()
192: .size());
193: }
194: }
|