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: import junit.framework.*;
039:
040: public class TestMonitor extends TestCase {
041: public static final String TEST_CLASS = "test";
042: public static final String TEST_FILENAME = "classes"
043: + File.separator + "test.class";
044:
045: private MockVisitor addVisitor;
046: private MockRemoveVisitor removeVisitor;
047:
048: private Monitor monitor;
049:
050: private Classfile testClassfile;
051:
052: protected void setUp() throws Exception {
053: super .setUp();
054:
055: addVisitor = new MockVisitor();
056: removeVisitor = new MockRemoveVisitor();
057:
058: monitor = new Monitor(addVisitor, removeVisitor);
059:
060: ClassfileLoader loader = new AggregatingClassfileLoader();
061: loader.load(TEST_FILENAME);
062: testClassfile = loader.getClassfile(TEST_CLASS);
063: }
064:
065: public void testFileTracking() {
066: monitor.beginSession(new LoadEvent(this , null, null, null));
067: monitor.beginGroup(new LoadEvent(this , null, null, null));
068:
069: assertEquals("previous", 0, monitor.previousFiles.size());
070: assertEquals("current", 0, monitor.currentFiles.size());
071:
072: monitor
073: .beginFile(new LoadEvent(this , null, TEST_FILENAME,
074: null));
075:
076: assertEquals("previous", 0, monitor.previousFiles.size());
077: assertEquals("current", 1, monitor.currentFiles.size());
078: assertTrue("TEST_FILENAME not in current", monitor.currentFiles
079: .contains(TEST_FILENAME));
080:
081: monitor.endFile(new LoadEvent(this , null, TEST_FILENAME, null));
082:
083: assertEquals("previous", 0, monitor.previousFiles.size());
084: assertEquals("current", 1, monitor.currentFiles.size());
085: assertTrue("TEST_FILENAME not in current", monitor.currentFiles
086: .contains(TEST_FILENAME));
087:
088: monitor.endGroup(new LoadEvent(this , null, null, null));
089: monitor.endSession(new LoadEvent(this , null, null, null));
090:
091: assertEquals("previous", 1, monitor.previousFiles.size());
092: assertTrue("TEST_FILENAME not in previous",
093: monitor.previousFiles.contains(TEST_FILENAME));
094: assertEquals("current", 0, monitor.currentFiles.size());
095: }
096:
097: public void testFileTrackingAcrossSessions() {
098: monitor.beginSession(new LoadEvent(this , null, null, null));
099: monitor.beginGroup(new LoadEvent(this , null, null, null));
100: monitor
101: .beginFile(new LoadEvent(this , null, TEST_FILENAME,
102: null));
103: monitor.beginClassfile(new LoadEvent(this , null, TEST_FILENAME,
104: null));
105: monitor.endClassfile(new LoadEvent(this , null, TEST_FILENAME,
106: testClassfile));
107: monitor.endFile(new LoadEvent(this , null, TEST_FILENAME, null));
108: monitor.endGroup(new LoadEvent(this , null, null, null));
109: monitor.endSession(new LoadEvent(this , null, null, null));
110:
111: assertEquals("previous", 1, monitor.previousFiles.size());
112: assertTrue("TEST_FILENAME not in previous",
113: monitor.previousFiles.contains(TEST_FILENAME));
114: assertEquals("current", 0, monitor.currentFiles.size());
115:
116: monitor.beginSession(new LoadEvent(this , null, null, null));
117: monitor.beginGroup(new LoadEvent(this , null, null, null));
118: monitor
119: .beginFile(new LoadEvent(this , null, TEST_FILENAME,
120: null));
121: monitor.beginClassfile(new LoadEvent(this , null, TEST_FILENAME,
122: null));
123:
124: assertEquals("previous", 1, monitor.previousFiles.size());
125: assertTrue("TEST_FILENAME not in previous",
126: monitor.previousFiles.contains(TEST_FILENAME));
127: assertEquals("current", 1, monitor.currentFiles.size());
128: assertTrue("TEST_FILENAME not in current", monitor.currentFiles
129: .contains(TEST_FILENAME));
130:
131: monitor.endClassfile(new LoadEvent(this , null, TEST_FILENAME,
132: testClassfile));
133:
134: assertEquals("previous", 1, monitor.previousFiles.size());
135: assertTrue("TEST_FILENAME not in previous",
136: monitor.previousFiles.contains(TEST_FILENAME));
137: assertEquals("current", 1, monitor.currentFiles.size());
138: assertTrue("TEST_FILENAME not in current", monitor.currentFiles
139: .contains(TEST_FILENAME));
140:
141: monitor.endFile(new LoadEvent(this , null, TEST_FILENAME, null));
142:
143: assertEquals("previous", 0, monitor.previousFiles.size());
144: assertEquals("current", 1, monitor.currentFiles.size());
145: assertTrue("TEST_FILENAME not in current", monitor.currentFiles
146: .contains(TEST_FILENAME));
147:
148: monitor.endGroup(new LoadEvent(this , null, null, null));
149: monitor.endSession(new LoadEvent(this , null, null, null));
150:
151: assertEquals("previous", 1, monitor.previousFiles.size());
152: assertTrue("TEST_FILENAME not in previous",
153: monitor.previousFiles.contains(TEST_FILENAME));
154: assertEquals("current", 0, monitor.currentFiles.size());
155: }
156:
157: public void testFileTrackingWithSkippedFile() {
158: monitor.beginSession(new LoadEvent(this , null, null, null));
159: monitor.beginGroup(new LoadEvent(this , null, null, null));
160: monitor
161: .beginFile(new LoadEvent(this , null, TEST_FILENAME,
162: null));
163: monitor.beginClassfile(new LoadEvent(this , null, TEST_FILENAME,
164: null));
165: monitor.endClassfile(new LoadEvent(this , null, TEST_FILENAME,
166: testClassfile));
167: monitor.endFile(new LoadEvent(this , null, TEST_FILENAME, null));
168: monitor.endGroup(new LoadEvent(this , null, null, null));
169: monitor.endSession(new LoadEvent(this , null, null, null));
170:
171: assertEquals("previous", 1, monitor.previousFiles.size());
172: assertTrue("TEST_FILENAME not in previous",
173: monitor.previousFiles.contains(TEST_FILENAME));
174: assertEquals("current", 0, monitor.currentFiles.size());
175:
176: monitor.beginSession(new LoadEvent(this , null, null, null));
177: monitor.beginGroup(new LoadEvent(this , null, null, null));
178: monitor
179: .beginFile(new LoadEvent(this , null, TEST_FILENAME,
180: null));
181:
182: assertEquals("previous", 1, monitor.previousFiles.size());
183: assertTrue("TEST_FILENAME not in previous",
184: monitor.previousFiles.contains(TEST_FILENAME));
185: assertEquals("current", 1, monitor.currentFiles.size());
186: assertTrue("TEST_FILENAME not in current", monitor.currentFiles
187: .contains(TEST_FILENAME));
188:
189: monitor.endFile(new LoadEvent(this , null, TEST_FILENAME, null));
190:
191: assertEquals("previous", 0, monitor.previousFiles.size());
192: assertEquals("current", 1, monitor.currentFiles.size());
193: assertTrue("TEST_FILENAME not in current", monitor.currentFiles
194: .contains(TEST_FILENAME));
195:
196: monitor.endGroup(new LoadEvent(this , null, null, null));
197: monitor.endSession(new LoadEvent(this , null, null, null));
198:
199: assertEquals("previous", 1, monitor.previousFiles.size());
200: assertTrue("TEST_FILENAME not in previous",
201: monitor.previousFiles.contains(TEST_FILENAME));
202: assertEquals("current", 0, monitor.currentFiles.size());
203: }
204:
205: public void testFileTrackingWithMissingFile() {
206: monitor.beginSession(new LoadEvent(this , null, null, null));
207: monitor.beginGroup(new LoadEvent(this , null, null, null));
208: monitor
209: .beginFile(new LoadEvent(this , null, TEST_FILENAME,
210: null));
211: monitor.beginClassfile(new LoadEvent(this , null, TEST_FILENAME,
212: null));
213: monitor.endClassfile(new LoadEvent(this , null, TEST_FILENAME,
214: testClassfile));
215: monitor.endFile(new LoadEvent(this , null, TEST_FILENAME, null));
216: monitor.endGroup(new LoadEvent(this , null, null, null));
217: monitor.endSession(new LoadEvent(this , null, null, null));
218:
219: assertEquals("previous", 1, monitor.previousFiles.size());
220: assertTrue("TEST_FILENAME not in previous",
221: monitor.previousFiles.contains(TEST_FILENAME));
222: assertEquals("current", 0, monitor.currentFiles.size());
223:
224: monitor.beginSession(new LoadEvent(this , null, null, null));
225: monitor.beginGroup(new LoadEvent(this , null, null, null));
226:
227: assertEquals("previous", 1, monitor.previousFiles.size());
228: assertTrue("TEST_FILENAME not in previous",
229: monitor.previousFiles.contains(TEST_FILENAME));
230: assertEquals("current", 0, monitor.currentFiles.size());
231:
232: monitor.endGroup(new LoadEvent(this , null, null, null));
233: monitor.endSession(new LoadEvent(this , null, null, null));
234:
235: assertEquals("previous", 0, monitor.previousFiles.size());
236: assertEquals("current", 0, monitor.currentFiles.size());
237: }
238:
239: public void testNewClassfile() {
240: monitor.beginSession(new LoadEvent(this , null, null, null));
241: monitor.beginGroup(new LoadEvent(this , null, null, null));
242: monitor
243: .beginFile(new LoadEvent(this , null, TEST_FILENAME,
244: null));
245: monitor.beginClassfile(new LoadEvent(this , null, TEST_FILENAME,
246: null));
247: monitor.endClassfile(new LoadEvent(this , null, TEST_FILENAME,
248: testClassfile));
249:
250: assertEquals("added", 1, addVisitor.getVisitedClassfiles()
251: .size());
252: assertTrue("added missed " + TEST_CLASS, addVisitor
253: .getVisitedClassfiles().contains(testClassfile));
254: assertEquals("removed", 0, removeVisitor.getVisitedClasses()
255: .size());
256: }
257:
258: public void testRepeatInSession() {
259: monitor.beginSession(new LoadEvent(this , null, null, null));
260: monitor.beginGroup(new LoadEvent(this , null, null, null));
261: monitor
262: .beginFile(new LoadEvent(this , null, TEST_FILENAME,
263: null));
264: monitor.beginClassfile(new LoadEvent(this , null, TEST_FILENAME,
265: null));
266: monitor.endClassfile(new LoadEvent(this , null, TEST_FILENAME,
267: testClassfile));
268: monitor.endFile(new LoadEvent(this , null, TEST_FILENAME, null));
269: monitor
270: .beginFile(new LoadEvent(this , null, TEST_FILENAME,
271: null));
272: monitor.beginClassfile(new LoadEvent(this , null, TEST_FILENAME,
273: null));
274: monitor.endClassfile(new LoadEvent(this , null, TEST_FILENAME,
275: testClassfile));
276:
277: assertEquals("added", 2, addVisitor.getVisitedClassfiles()
278: .size());
279: Iterator i = addVisitor.getVisitedClassfiles().iterator();
280: assertEquals("added 1", testClassfile, i.next());
281: assertEquals("added 2", testClassfile, i.next());
282: assertEquals("removed", 0, removeVisitor.getVisitedClasses()
283: .size());
284: }
285:
286: public void testRepeatAcrossSessions() {
287: monitor.beginSession(new LoadEvent(this , null, null, null));
288: monitor.beginGroup(new LoadEvent(this , null, null, null));
289: monitor
290: .beginFile(new LoadEvent(this , null, TEST_FILENAME,
291: null));
292: monitor.beginClassfile(new LoadEvent(this , null, TEST_FILENAME,
293: null));
294: monitor.endClassfile(new LoadEvent(this , null, TEST_FILENAME,
295: testClassfile));
296: monitor.endFile(new LoadEvent(this , null, TEST_FILENAME, null));
297: monitor.endGroup(new LoadEvent(this , null, null, null));
298: monitor.endSession(new LoadEvent(this , null, null, null));
299:
300: monitor.beginSession(new LoadEvent(this , null, null, null));
301: monitor.beginGroup(new LoadEvent(this , null, null, null));
302: monitor
303: .beginFile(new LoadEvent(this , null, TEST_FILENAME,
304: null));
305: monitor.beginClassfile(new LoadEvent(this , null, TEST_FILENAME,
306: null));
307: monitor.endClassfile(new LoadEvent(this , null, TEST_FILENAME,
308: testClassfile));
309:
310: assertEquals("added", 2, addVisitor.getVisitedClassfiles()
311: .size());
312: Iterator i = addVisitor.getVisitedClassfiles().iterator();
313: assertEquals("added 1", testClassfile, i.next());
314: assertEquals("added 2", testClassfile, i.next());
315: assertEquals("removed", 1, removeVisitor.getVisitedClasses()
316: .size());
317: assertTrue("removed missed " + TEST_CLASS, removeVisitor
318: .getVisitedClasses().contains(TEST_CLASS));
319: }
320:
321: public void testRemoval() {
322: monitor.beginSession(new LoadEvent(this , null, null, null));
323: monitor.beginGroup(new LoadEvent(this , null, null, null));
324: monitor
325: .beginFile(new LoadEvent(this , null, TEST_FILENAME,
326: null));
327: monitor.beginClassfile(new LoadEvent(this , null, TEST_FILENAME,
328: null));
329: monitor.endClassfile(new LoadEvent(this , null, TEST_FILENAME,
330: testClassfile));
331: monitor.endFile(new LoadEvent(this , null, TEST_FILENAME, null));
332: monitor.endGroup(new LoadEvent(this , null, null, null));
333: monitor.endSession(new LoadEvent(this , null, null, null));
334:
335: monitor.beginSession(new LoadEvent(this , null, null, null));
336: monitor.beginGroup(new LoadEvent(this , null, null, null));
337: monitor.endGroup(new LoadEvent(this , null, null, null));
338: monitor.endSession(new LoadEvent(this , null, null, null));
339:
340: assertEquals("added", 1, addVisitor.getVisitedClassfiles()
341: .size());
342: assertTrue("added missed " + TEST_CLASS, addVisitor
343: .getVisitedClassfiles().contains(testClassfile));
344: assertEquals("removed", 1, removeVisitor.getVisitedClasses()
345: .size());
346: assertTrue("removed missed " + TEST_CLASS, removeVisitor
347: .getVisitedClasses().contains(TEST_CLASS));
348: }
349:
350: public void testSkip() {
351: monitor.beginSession(new LoadEvent(this , null, null, null));
352: monitor.beginGroup(new LoadEvent(this , null, null, null));
353: monitor
354: .beginFile(new LoadEvent(this , null, TEST_FILENAME,
355: null));
356: monitor.beginClassfile(new LoadEvent(this , null, TEST_FILENAME,
357: null));
358: monitor.endClassfile(new LoadEvent(this , null, TEST_FILENAME,
359: testClassfile));
360: monitor.endFile(new LoadEvent(this , null, TEST_FILENAME, null));
361: monitor.endGroup(new LoadEvent(this , null, null, null));
362: monitor.endSession(new LoadEvent(this , null, null, null));
363:
364: monitor.beginSession(new LoadEvent(this , null, null, null));
365: monitor.beginGroup(new LoadEvent(this , null, null, null));
366: monitor
367: .beginFile(new LoadEvent(this , null, TEST_FILENAME,
368: null));
369: monitor.endFile(new LoadEvent(this , null, TEST_FILENAME, null));
370: monitor.endGroup(new LoadEvent(this , null, null, null));
371: monitor.endSession(new LoadEvent(this , null, null, null));
372:
373: assertEquals("added", 1, addVisitor.getVisitedClassfiles()
374: .size());
375: assertTrue("added missed " + TEST_CLASS, addVisitor
376: .getVisitedClassfiles().contains(testClassfile));
377: assertEquals("removed", 0, removeVisitor.getVisitedClasses()
378: .size());
379: }
380:
381: public void testOpenSession() {
382: monitor.setClosedSession(false);
383:
384: monitor.beginSession(new LoadEvent(this , null, null, null));
385: monitor.beginGroup(new LoadEvent(this , null, null, null));
386: monitor
387: .beginFile(new LoadEvent(this , null, TEST_FILENAME,
388: null));
389: monitor.beginClassfile(new LoadEvent(this , null, TEST_FILENAME,
390: null));
391: monitor.endClassfile(new LoadEvent(this , null, TEST_FILENAME,
392: testClassfile));
393: monitor.endFile(new LoadEvent(this , null, TEST_FILENAME, null));
394: monitor.endGroup(new LoadEvent(this , null, null, null));
395: monitor.endSession(new LoadEvent(this , null, null, null));
396:
397: monitor.beginSession(new LoadEvent(this , null, null, null));
398: monitor.beginGroup(new LoadEvent(this , null, null, null));
399: monitor.endGroup(new LoadEvent(this , null, null, null));
400: monitor.endSession(new LoadEvent(this , null, null, null));
401:
402: assertEquals("added", 1, addVisitor.getVisitedClassfiles()
403: .size());
404: assertTrue("added missed " + TEST_CLASS, addVisitor
405: .getVisitedClassfiles().contains(testClassfile));
406: assertEquals("removed", 0, removeVisitor.getVisitedClasses()
407: .size());
408: }
409:
410: public void testExtractAgainInOpenSession() {
411: monitor.setClosedSession(false);
412:
413: monitor.beginSession(new LoadEvent(this , null, null, null));
414: monitor.beginGroup(new LoadEvent(this , null, null, null));
415: monitor
416: .beginFile(new LoadEvent(this , null, TEST_FILENAME,
417: null));
418: monitor.beginClassfile(new LoadEvent(this , null, TEST_FILENAME,
419: null));
420: monitor.endClassfile(new LoadEvent(this , null, TEST_FILENAME,
421: testClassfile));
422: monitor.endFile(new LoadEvent(this , null, TEST_FILENAME, null));
423: monitor.endGroup(new LoadEvent(this , null, null, null));
424: monitor.endSession(new LoadEvent(this , null, null, null));
425:
426: addVisitor.reset();
427: removeVisitor.reset();
428:
429: monitor.beginSession(new LoadEvent(this , null, null, null));
430: monitor.beginGroup(new LoadEvent(this , null, null, null));
431: monitor
432: .beginFile(new LoadEvent(this , null, TEST_FILENAME,
433: null));
434: monitor.beginClassfile(new LoadEvent(this , null, TEST_FILENAME,
435: null));
436: monitor.endClassfile(new LoadEvent(this , null, TEST_FILENAME,
437: testClassfile));
438: monitor.endFile(new LoadEvent(this , null, TEST_FILENAME, null));
439: monitor.endGroup(new LoadEvent(this , null, null, null));
440: monitor.endSession(new LoadEvent(this , null, null, null));
441:
442: assertEquals("added", 1, addVisitor.getVisitedClassfiles()
443: .size());
444: assertTrue("added missed " + TEST_CLASS, addVisitor
445: .getVisitedClassfiles().contains(testClassfile));
446: assertEquals("removed", 0, removeVisitor.getVisitedClasses()
447: .size());
448: }
449:
450: public void testReloadAfterClosingSessionSession() {
451: monitor.setClosedSession(false);
452:
453: monitor.beginSession(new LoadEvent(this , null, null, null));
454: monitor.beginGroup(new LoadEvent(this , null, null, null));
455: monitor
456: .beginFile(new LoadEvent(this , null, TEST_FILENAME,
457: null));
458: monitor.beginClassfile(new LoadEvent(this , null, TEST_FILENAME,
459: null));
460: monitor.endClassfile(new LoadEvent(this , null, TEST_FILENAME,
461: testClassfile));
462: monitor.endFile(new LoadEvent(this , null, TEST_FILENAME, null));
463: monitor.endGroup(new LoadEvent(this , null, null, null));
464: monitor.endSession(new LoadEvent(this , null, null, null));
465:
466: monitor.setClosedSession(true);
467:
468: addVisitor.reset();
469: removeVisitor.reset();
470:
471: monitor.beginSession(new LoadEvent(this , null, null, null));
472: monitor.beginGroup(new LoadEvent(this , null, null, null));
473: monitor
474: .beginFile(new LoadEvent(this , null, TEST_FILENAME,
475: null));
476: monitor.beginClassfile(new LoadEvent(this , null, TEST_FILENAME,
477: null));
478: monitor.endClassfile(new LoadEvent(this , null, TEST_FILENAME,
479: testClassfile));
480: monitor.endFile(new LoadEvent(this , null, TEST_FILENAME, null));
481: monitor.endGroup(new LoadEvent(this , null, null, null));
482: monitor.endSession(new LoadEvent(this , null, null, null));
483:
484: assertEquals("added", 1, addVisitor.getVisitedClassfiles()
485: .size());
486: assertTrue("added missed " + TEST_CLASS, addVisitor
487: .getVisitedClassfiles().contains(testClassfile));
488: assertEquals("removed", 1, removeVisitor.getVisitedClasses()
489: .size());
490: assertTrue("removed missed " + TEST_CLASS, removeVisitor
491: .getVisitedClasses().contains(TEST_CLASS));
492: }
493: }
|