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: import junit.framework.*;
038:
039: public class TestModifiedOnlyDispatcher extends TestCase {
040: private MockDispatcher mockDispatcher;
041: private ClassfileLoaderDispatcher dispatcher;
042:
043: private String testDirname;
044: private String testFilename;
045:
046: protected void setUp() throws Exception {
047: super .setUp();
048:
049: mockDispatcher = new MockDispatcher();
050: dispatcher = new ModifiedOnlyDispatcher(mockDispatcher);
051:
052: testDirname = "classes";
053: testFilename = testDirname + File.separator
054: + getClass().getName() + "." + getName() + ".txt";
055: }
056:
057: protected void tearDown() throws Exception {
058: File file = new File(testFilename);
059: file.delete();
060:
061: super .tearDown();
062: }
063:
064: public void testDispatchNonExistingFile() {
065: assertEquals("dispatch action", mockDispatcher
066: .getReturnedAction(), dispatcher.dispatch(testFilename));
067: assertEquals("delegated calls", 1, mockDispatcher
068: .getDispatchCount(testFilename));
069: }
070:
071: public void testDispatchNewClassFile() throws IOException {
072: createFile();
073: mockDispatcher
074: .setReturnedAction(ClassfileLoaderDispatcher.Action.CLASS);
075:
076: assertEquals("dispatch action", mockDispatcher
077: .getReturnedAction(), dispatcher.dispatch(testFilename));
078: assertEquals("delegated calls", 1, mockDispatcher
079: .getDispatchCount(testFilename));
080: }
081:
082: public void testDispatchIdenticalClassFile() throws IOException {
083: createFile();
084: mockDispatcher
085: .setReturnedAction(ClassfileLoaderDispatcher.Action.CLASS);
086:
087: assertEquals("first dispatch action", mockDispatcher
088: .getReturnedAction(), dispatcher.dispatch(testFilename));
089: assertEquals("first delegated calls", 1, mockDispatcher
090: .getDispatchCount(testFilename));
091:
092: assertEquals("repeat dispatch action",
093: ClassfileLoaderDispatcher.Action.IGNORE, dispatcher
094: .dispatch(testFilename));
095: assertEquals("repeat delegated calls", 2, mockDispatcher
096: .getDispatchCount(testFilename));
097: }
098:
099: public void testDispatchModifiedClassFile() throws IOException {
100: createFile();
101: mockDispatcher
102: .setReturnedAction(ClassfileLoaderDispatcher.Action.CLASS);
103:
104: assertEquals("first dispatch action", mockDispatcher
105: .getReturnedAction(), dispatcher.dispatch(testFilename));
106: assertEquals("first delegated calls", 1, mockDispatcher
107: .getDispatchCount(testFilename));
108:
109: try {
110: Thread.sleep(1000);
111: } catch (InterruptedException ex) {
112: // Ignore
113: }
114: createFile();
115:
116: assertEquals("repeat dispatch action", mockDispatcher
117: .getReturnedAction(), dispatcher.dispatch(testFilename));
118: assertEquals("repeat delegated calls", 2, mockDispatcher
119: .getDispatchCount(testFilename));
120: }
121:
122: public void testDispatchDirectory() {
123: mockDispatcher
124: .setReturnedAction(ClassfileLoaderDispatcher.Action.DIRECTORY);
125:
126: assertEquals("first dispatch action", mockDispatcher
127: .getReturnedAction(), dispatcher.dispatch(testDirname));
128: assertEquals("first delegated calls", 1, mockDispatcher
129: .getDispatchCount(testDirname));
130:
131: assertEquals("repeat dispatch action", mockDispatcher
132: .getReturnedAction(), dispatcher.dispatch(testDirname));
133: assertEquals("repeat delegated calls", 2, mockDispatcher
134: .getDispatchCount(testDirname));
135: }
136:
137: public void testDispatchIdenticalZipFile() throws IOException {
138: createFile();
139: mockDispatcher
140: .setReturnedAction(ClassfileLoaderDispatcher.Action.ZIP);
141:
142: assertEquals("first dispatch action", mockDispatcher
143: .getReturnedAction(), dispatcher.dispatch(testFilename));
144: assertEquals("first delegated calls", 1, mockDispatcher
145: .getDispatchCount(testFilename));
146:
147: assertEquals("repeat dispatch action", mockDispatcher
148: .getReturnedAction(), dispatcher.dispatch(testFilename));
149: assertEquals("repeat delegated calls", 2, mockDispatcher
150: .getDispatchCount(testFilename));
151: }
152:
153: public void testDispatchIdenticalJarFile() throws IOException {
154: createFile();
155: mockDispatcher
156: .setReturnedAction(ClassfileLoaderDispatcher.Action.JAR);
157:
158: assertEquals("first dispatch action", mockDispatcher
159: .getReturnedAction(), dispatcher.dispatch(testFilename));
160: assertEquals("first delegated calls", 1, mockDispatcher
161: .getDispatchCount(testFilename));
162:
163: assertEquals("repeat dispatch action", mockDispatcher
164: .getReturnedAction(), dispatcher.dispatch(testFilename));
165: assertEquals("repeat delegated calls", 2, mockDispatcher
166: .getDispatchCount(testFilename));
167: }
168:
169: private void createFile() throws IOException {
170: PrintWriter out = new PrintWriter(new FileWriter(testFilename));
171: out.println("foobar");
172: out.close();
173: }
174: }
|