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: public class TestClassfileLoaderStrictDispatcher extends
039: TestClassfileLoaderBase {
040: public static final String TEST_FILENAME = "classes"
041: + File.separator + "test.class";
042:
043: private ClassfileLoader loader;
044:
045: protected void setUp() throws Exception {
046: super .setUp();
047:
048: loader = new TransientClassfileLoader(new StrictDispatcher());
049: loader.addLoadListener(this );
050: }
051:
052: public void testOneFile() {
053: String filename = TEST_FILENAME;
054: assertTrue(filename + " missing", new File(filename).exists());
055:
056: loader.load(Collections.singleton(filename));
057:
058: assertEquals("Begin Session", 1, getBeginSessionEvents().size());
059: assertEquals("Begin Group", 1, getBeginGroupEvents().size());
060: assertEquals("Begin File", 1, getBeginFileEvents().size());
061: assertEquals("Begin Classfile", 1, getBeginClassfileEvents()
062: .size());
063: assertEquals("End Classfile", 1, getEndClassfileEvents().size());
064: assertEquals("End File", 1, getEndFileEvents().size());
065: assertEquals("End Group", 1, getEndGroupEvents().size());
066: assertEquals("End Session", 1, getEndSessionEvents().size());
067:
068: assertEquals("Group size", 1, getBeginGroupEvents().getFirst()
069: .getSize());
070: }
071:
072: public void testOneLevelZip() {
073: String filename = TEST_DIR + File.separator + "onelevel.zip";
074: assertTrue(filename + " missing", new File(filename).exists());
075:
076: loader.load(Collections.singleton(filename));
077:
078: assertEquals("Begin Session", 1, getBeginSessionEvents().size());
079: assertEquals("Begin Group", 1, getBeginGroupEvents().size());
080: assertEquals("Begin File", 31, getBeginFileEvents().size());
081: assertEquals("Begin Classfile", 14, getBeginClassfileEvents()
082: .size());
083: assertEquals("End Classfile", 14, getEndClassfileEvents()
084: .size());
085: assertEquals("End File", 31, getEndFileEvents().size());
086: assertEquals("End Group", 1, getEndGroupEvents().size());
087: assertEquals("End Session", 1, getEndSessionEvents().size());
088:
089: assertEquals("Group size", 31, getBeginGroupEvents().getFirst()
090: .getSize());
091: }
092:
093: public void testOneLevelJar() {
094: String filename = TEST_DIR + File.separator + "onelevel.jar";
095: assertTrue(filename + " missing", new File(filename).exists());
096:
097: loader.load(Collections.singleton(filename));
098:
099: assertEquals("Begin Session", 1, getBeginSessionEvents().size());
100: assertEquals("Begin Group", 1, getBeginGroupEvents().size());
101: assertEquals("Begin File", 33, getBeginFileEvents().size());
102: assertEquals("Begin Classfile", 14, getBeginClassfileEvents()
103: .size());
104: assertEquals("End Classfile", 14, getEndClassfileEvents()
105: .size());
106: assertEquals("End File", 33, getEndFileEvents().size());
107: assertEquals("End Group", 1, getEndGroupEvents().size());
108: assertEquals("End Session", 1, getEndSessionEvents().size());
109:
110: assertEquals("Group size", 33, getBeginGroupEvents().getFirst()
111: .getSize());
112: }
113:
114: public void testOneLevelMiscellaneous() {
115: String filename = TEST_DIR + File.separator + "onelevel.mis";
116: assertTrue(filename + " missing", new File(filename).exists());
117:
118: loader.load(Collections.singleton(filename));
119:
120: assertEquals("Begin Session", 1, getBeginSessionEvents().size());
121: assertEquals("Begin Group", 1, getBeginGroupEvents().size());
122: assertEquals("Begin File", 1, getBeginFileEvents().size());
123: assertEquals("Begin Classfile", 0, getBeginClassfileEvents()
124: .size());
125: assertEquals("End Classfile", 0, getEndClassfileEvents().size());
126: assertEquals("End File", 1, getEndFileEvents().size());
127: assertEquals("End Group", 1, getEndGroupEvents().size());
128: assertEquals("End Session", 1, getEndSessionEvents().size());
129: }
130:
131: public void testTwoLevelZip() {
132: String filename = TEST_DIR + File.separator + "twolevel.zip";
133: assertTrue(filename + " missing", new File(filename).exists());
134:
135: loader.load(Collections.singleton(filename));
136:
137: assertEquals("Begin Session", 1, getBeginSessionEvents().size());
138: assertEquals("Begin Group", 2, getBeginGroupEvents().size());
139: assertEquals("Begin File", 32, getBeginFileEvents().size());
140: assertEquals("Begin Classfile", 14, getBeginClassfileEvents()
141: .size());
142: assertEquals("End Classfile", 14, getEndClassfileEvents()
143: .size());
144: assertEquals("End File", 32, getEndFileEvents().size());
145: assertEquals("End Group", 2, getEndGroupEvents().size());
146: assertEquals("End Session", 1, getEndSessionEvents().size());
147: }
148:
149: public void testTwoLevelJar() {
150: String filename = TEST_DIR + File.separator + "twolevel.jar";
151: assertTrue(filename + " missing", new File(filename).exists());
152:
153: loader.load(Collections.singleton(filename));
154:
155: assertEquals("Begin Session", 1, getBeginSessionEvents().size());
156: assertEquals("Begin Group", 2, getBeginGroupEvents().size());
157: assertEquals("Begin File", 34, getBeginFileEvents().size());
158: assertEquals("Begin Classfile", 14, getBeginClassfileEvents()
159: .size());
160: assertEquals("End Classfile", 14, getEndClassfileEvents()
161: .size());
162: assertEquals("End File", 34, getEndFileEvents().size());
163: assertEquals("End Group", 2, getEndGroupEvents().size());
164: assertEquals("End Session", 1, getEndSessionEvents().size());
165: }
166:
167: public void testTwoLevelMiscellaneous() {
168: String filename = TEST_DIR + File.separator + "twolevel.mis";
169: assertTrue(filename + " missing", new File(filename).exists());
170:
171: loader.load(Collections.singleton(filename));
172:
173: assertEquals("Begin Session", 1, getBeginSessionEvents().size());
174: assertEquals("Begin Group", 1, getBeginGroupEvents().size());
175: assertEquals("Begin File", 1, getBeginFileEvents().size());
176: assertEquals("Begin Classfile", 0, getBeginClassfileEvents()
177: .size());
178: assertEquals("End Classfile", 0, getEndClassfileEvents().size());
179: assertEquals("End File", 1, getEndFileEvents().size());
180: assertEquals("End Group", 1, getEndGroupEvents().size());
181: assertEquals("End Session", 1, getEndSessionEvents().size());
182: }
183: }
|