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 TestJarClassfileLoader 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 JarClassfileLoader(eventSource);
046: }
047:
048: public void testLoadFile() {
049: String filename = TEST_DIR + File.separator + "onelevel.jar";
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", 33, getBeginFileEvents().size());
057: assertEquals("Begin Classfile", 14, getBeginClassfileEvents()
058: .size());
059: assertEquals("End Classfile", 14, getEndClassfileEvents()
060: .size());
061: assertEquals("End File", 33, getEndFileEvents().size());
062: assertEquals("End Group", 1, getEndGroupEvents().size());
063: assertEquals("End Session", 0, getEndSessionEvents().size());
064:
065: assertEquals("Group size", 33, getBeginGroupEvents().getFirst()
066: .getSize());
067: }
068:
069: public void testLoadWrongFile() {
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 testLoadInputStream() throws IOException {
091: String filename = TEST_DIR + File.separator + "onelevel.jar";
092: assertTrue(filename + " missing", new File(filename).exists());
093:
094: loader.load(filename, new FileInputStream(filename));
095:
096: assertEquals("Begin Session", 0, getBeginSessionEvents().size());
097: assertEquals("Begin Group", 1, getBeginGroupEvents().size());
098: assertEquals("Begin File", 31, getBeginFileEvents().size());
099: assertEquals("Begin Classfile", 14, getBeginClassfileEvents()
100: .size());
101: assertEquals("End Classfile", 14, getEndClassfileEvents()
102: .size());
103: assertEquals("End File", 31, getEndFileEvents().size());
104: assertEquals("End Group", 1, getEndGroupEvents().size());
105: assertEquals("End Session", 0, getEndSessionEvents().size());
106:
107: assertEquals("Group size", -1, getBeginGroupEvents().getFirst()
108: .getSize());
109: }
110:
111: public void testLoadWrongInputStream() throws IOException {
112: String filename = TEST_DIR + File.separator + "onelevel.mis";
113: assertTrue(filename + " missing", new File(filename).exists());
114:
115: loader.load(filename, new FileInputStream(filename));
116:
117: assertEquals("Begin Session", 0, getBeginSessionEvents().size());
118: assertEquals("Begin Group", 1, getBeginGroupEvents().size());
119: assertEquals("Begin File", 31, getBeginFileEvents().size());
120: assertEquals("Begin Classfile", 14, getBeginClassfileEvents()
121: .size());
122: assertEquals("End Classfile", 14, getEndClassfileEvents()
123: .size());
124: assertEquals("End File", 31, getEndFileEvents().size());
125: assertEquals("End Group", 1, getEndGroupEvents().size());
126: assertEquals("End Session", 0, getEndSessionEvents().size());
127:
128: assertEquals("Group size", -1, getBeginGroupEvents().getFirst()
129: .getSize());
130: }
131: }
|