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:
037: public class TestZipClassfileLoader extends TestClassfileLoaderBase {
038: private ClassfileLoader loader;
039:
040: protected void setUp() throws Exception {
041: super .setUp();
042:
043: ClassfileLoader eventSource = new TransientClassfileLoader();
044: eventSource.addLoadListener(this );
045: loader = new ZipClassfileLoader(eventSource);
046: }
047:
048: public void testLoadFile() {
049: String filename = TEST_DIR + File.separator + "onelevel.zip";
050: assertTrue(filename + " missing", new File(filename).exists());
051:
052: loader.load(filename);
053:
054: assertEquals("Begin Session", 0, getBeginSessionEvents().size());
055: assertEquals("Begin Group", 1, getBeginGroupEvents().size());
056: assertEquals("Begin File", 31, getBeginFileEvents().size());
057: assertEquals("Begin Classfile", 14, getBeginClassfileEvents()
058: .size());
059: assertEquals("End Classfile", 14, getEndClassfileEvents()
060: .size());
061: assertEquals("End File", 31, getEndFileEvents().size());
062: assertEquals("End Group", 1, getEndGroupEvents().size());
063: assertEquals("End Session", 0, getEndSessionEvents().size());
064:
065: assertEquals("Group size", 31, getBeginGroupEvents().getFirst()
066: .getSize());
067: }
068:
069: public void testLoadFileWrongName() {
070: String filename = TEST_DIR + File.separator + "onelevel.mis";
071: assertTrue(filename + " missing", new File(filename).exists());
072:
073: loader.load(filename);
074:
075: assertEquals("Begin Session", 0, getBeginSessionEvents().size());
076: assertEquals("Begin Group", 1, getBeginGroupEvents().size());
077: assertEquals("Begin File", 31, getBeginFileEvents().size());
078: assertEquals("Begin Classfile", 14, getBeginClassfileEvents()
079: .size());
080: assertEquals("End Classfile", 14, getEndClassfileEvents()
081: .size());
082: assertEquals("End File", 31, getEndFileEvents().size());
083: assertEquals("End Group", 1, getEndGroupEvents().size());
084: assertEquals("End Session", 0, getEndSessionEvents().size());
085:
086: assertEquals("Group size", 31, getBeginGroupEvents().getFirst()
087: .getSize());
088: }
089:
090: public void testLoadWrongFile() {
091: String filename = TEST_DIR + File.separator + "old"
092: + File.separator + "ModifiedPackage" + File.separator
093: + "ModifiedClass.class";
094: assertTrue(filename + " missing", new File(filename).exists());
095:
096: loader.load(filename);
097:
098: assertEquals("Begin Session", 0, getBeginSessionEvents().size());
099: assertEquals("Begin Group", 0, getBeginGroupEvents().size());
100: assertEquals("Begin File", 0, getBeginFileEvents().size());
101: assertEquals("Begin Classfile", 0, getBeginClassfileEvents()
102: .size());
103: assertEquals("End Classfile", 0, getEndClassfileEvents().size());
104: assertEquals("End File", 0, getEndFileEvents().size());
105: assertEquals("End Group", 0, getEndGroupEvents().size());
106: assertEquals("End Session", 0, getEndSessionEvents().size());
107: }
108:
109: public void testLoadInputStream() throws IOException {
110: String filename = TEST_DIR + File.separator + "onelevel.zip";
111: assertTrue(filename + " missing", new File(filename).exists());
112:
113: loader.load(filename, new FileInputStream(filename));
114:
115: assertEquals("Begin Session", 0, getBeginSessionEvents().size());
116: assertEquals("Begin Group", 1, getBeginGroupEvents().size());
117: assertEquals("Begin File", 31, getBeginFileEvents().size());
118: assertEquals("Begin Classfile", 14, getBeginClassfileEvents()
119: .size());
120: assertEquals("End Classfile", 14, getEndClassfileEvents()
121: .size());
122: assertEquals("End File", 31, getEndFileEvents().size());
123: assertEquals("End Group", 1, getEndGroupEvents().size());
124: assertEquals("End Session", 0, getEndSessionEvents().size());
125:
126: assertEquals("Group size", -1, getBeginGroupEvents().getFirst()
127: .getSize());
128: }
129:
130: public void testLoadInputStreamWrongName() throws IOException {
131: String filename = TEST_DIR + File.separator + "onelevel.mis";
132: assertTrue(filename + " missing", new File(filename).exists());
133:
134: loader.load(filename, new FileInputStream(filename));
135:
136: assertEquals("Begin Session", 0, getBeginSessionEvents().size());
137: assertEquals("Begin Group", 1, getBeginGroupEvents().size());
138: assertEquals("Begin File", 31, getBeginFileEvents().size());
139: assertEquals("Begin Classfile", 14, getBeginClassfileEvents()
140: .size());
141: assertEquals("End Classfile", 14, getEndClassfileEvents()
142: .size());
143: assertEquals("End File", 31, getEndFileEvents().size());
144: assertEquals("End Group", 1, getEndGroupEvents().size());
145: assertEquals("End Session", 0, getEndSessionEvents().size());
146:
147: assertEquals("Group size", -1, getBeginGroupEvents().getFirst()
148: .getSize());
149: }
150:
151: public void testLoadWrongInputStream() throws IOException {
152: String filename = TEST_DIR + File.separator + "old"
153: + File.separator + "ModifiedPackage" + File.separator
154: + "ModifiedClass.class";
155: assertTrue(filename + " missing", new File(filename).exists());
156:
157: loader.load(filename, new FileInputStream(filename));
158:
159: assertEquals("Begin Session", 0, getBeginSessionEvents().size());
160: assertEquals("Begin Group", 1, getBeginGroupEvents().size());
161: assertEquals("Begin File", 0, getBeginFileEvents().size());
162: assertEquals("Begin Classfile", 0, getBeginClassfileEvents()
163: .size());
164: assertEquals("End Classfile", 0, getEndClassfileEvents().size());
165: assertEquals("End File", 0, getEndFileEvents().size());
166: assertEquals("End Group", 1, getEndGroupEvents().size());
167: assertEquals("End Session", 0, getEndSessionEvents().size());
168: }
169: }
|