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 TestDirectoryClassfileLoader extends
038: TestClassfileLoaderBase {
039: public static final String TEST_CLASS = "test";
040: public static final String TEST_FILENAME = "classes"
041: + File.separator + "test.class";
042: public static final String BOGUS_TEST_FILENAME = "classes"
043: + File.separator + "bogus" + File.separator + "test.class";
044: public static final String TEST_DIRNAME = "classes"
045: + File.separator + "testpackage";
046: public static final String OTHER_DIRNAME = "classes"
047: + File.separator + "otherpackage";
048:
049: private ClassfileLoader loader;
050:
051: protected void setUp() throws Exception {
052: super .setUp();
053:
054: ClassfileLoader eventSource = new TransientClassfileLoader();
055: eventSource.addLoadListener(this );
056: loader = new DirectoryClassfileLoader(eventSource);
057: }
058:
059: public void testLoadClassFile() {
060: loader.load(TEST_FILENAME);
061:
062: assertEquals("Begin Session", 0, getBeginSessionEvents().size());
063: assertEquals("Begin Group", 1, getBeginGroupEvents().size());
064: assertEquals("Begin File", 1, getBeginFileEvents().size());
065: assertEquals("Begin Classfile", 1, getBeginClassfileEvents()
066: .size());
067: assertEquals("End Classfile", 1, getEndClassfileEvents().size());
068: assertEquals("End File", 1, getEndFileEvents().size());
069: assertEquals("End Group", 1, getEndGroupEvents().size());
070: assertEquals("End Session", 0, getEndSessionEvents().size());
071:
072: assertEquals(TEST_FILENAME, getEndClassfileEvents().getLast()
073: .getGroupName());
074: assertNotNull("Classfile", getEndClassfileEvents().getLast()
075: .getClassfile());
076: }
077:
078: public void testLoadClassInputStream() throws IOException {
079: loader.load(TEST_FILENAME, new FileInputStream(TEST_FILENAME));
080:
081: assertEquals("Begin Session", 0, getBeginSessionEvents().size());
082: assertEquals("Begin Group", 0, getBeginGroupEvents().size());
083: assertEquals("Begin File", 0, getBeginFileEvents().size());
084: assertEquals("Begin Classfile", 0, getBeginClassfileEvents()
085: .size());
086: assertEquals("End Classfile", 0, getEndClassfileEvents().size());
087: assertEquals("End File", 0, getEndFileEvents().size());
088: assertEquals("End Group", 0, getEndGroupEvents().size());
089: assertEquals("End Session", 0, getEndSessionEvents().size());
090: }
091:
092: public void testLoadBogusFile() {
093: loader.load(BOGUS_TEST_FILENAME);
094:
095: assertEquals("Begin Session", 0, getBeginSessionEvents().size());
096: assertEquals("Begin Group", 1, getBeginGroupEvents().size());
097: assertEquals("Begin File", 0, getBeginFileEvents().size());
098: assertEquals("Begin Classfile", 0, getBeginClassfileEvents()
099: .size());
100: assertEquals("End Classfile", 0, getEndClassfileEvents().size());
101: assertEquals("End File", 0, getEndFileEvents().size());
102: assertEquals("End Group", 1, getEndGroupEvents().size());
103: assertEquals("End Session", 0, getEndSessionEvents().size());
104: }
105:
106: public void testLoadBogusInputStream() throws IOException {
107: loader.load(BOGUS_TEST_FILENAME, new FileInputStream(
108: TEST_FILENAME));
109:
110: assertEquals("Begin Session", 0, getBeginSessionEvents().size());
111: assertEquals("Begin Group", 0, getBeginGroupEvents().size());
112: assertEquals("Begin File", 0, getBeginFileEvents().size());
113: assertEquals("Begin Classfile", 0, getBeginClassfileEvents()
114: .size());
115: assertEquals("End Classfile", 0, getEndClassfileEvents().size());
116: assertEquals("End File", 0, getEndFileEvents().size());
117: assertEquals("End Group", 0, getEndGroupEvents().size());
118: assertEquals("End Session", 0, getEndSessionEvents().size());
119: }
120:
121: public void testLoadDirectory() {
122: loader.load(TEST_DIRNAME);
123:
124: assertEquals("Begin Session", 0, getBeginSessionEvents().size());
125: assertEquals("Begin Group", 1, getBeginGroupEvents().size());
126: assertEquals("Begin File", 7, getBeginFileEvents().size());
127: assertEquals("Begin Classfile", 6, getBeginClassfileEvents()
128: .size());
129: assertEquals("End Classfile", 6, getEndClassfileEvents().size());
130: assertEquals("End File", 7, getEndFileEvents().size());
131: assertEquals("End Group", 1, getEndGroupEvents().size());
132: assertEquals("End Session", 0, getEndSessionEvents().size());
133: }
134:
135: public void testMultipleDirectories() {
136: loader.load(TEST_DIRNAME);
137: loader.load(OTHER_DIRNAME);
138:
139: assertEquals("Begin Session", 0, getBeginSessionEvents().size());
140: assertEquals("Begin Group", 2, getBeginGroupEvents().size());
141: assertEquals("Begin File", 11, getBeginFileEvents().size());
142: assertEquals("Begin Classfile", 9, getBeginClassfileEvents()
143: .size());
144: assertEquals("End Classfile", 9, getEndClassfileEvents().size());
145: assertEquals("End File", 11, getEndFileEvents().size());
146: assertEquals("End Group", 2, getEndGroupEvents().size());
147: assertEquals("End Session", 0, getEndSessionEvents().size());
148: }
149: }
|