1 32 33 package com.jeantessier.classreader; 34 35 import java.io.*; 36 import java.util.*; 37 38 import org.apache.log4j.*; 39 40 import junit.framework.*; 41 42 public class TestMonitor extends TestCase { 43 public static final String TEST_CLASS = "test"; 44 public static final String TEST_FILENAME = "classes" + File.separator + "test.class"; 45 46 private MockVisitor addVisitor; 47 private MockRemoveVisitor removeVisitor; 48 49 private Monitor monitor; 50 51 private Classfile testClassfile; 52 53 protected void setUp() throws Exception { 54 super.setUp(); 55 56 addVisitor = new MockVisitor(); 57 removeVisitor = new MockRemoveVisitor(); 58 59 monitor = new Monitor(addVisitor, removeVisitor); 60 61 ClassfileLoader loader = new AggregatingClassfileLoader(); 62 loader.load(TEST_FILENAME); 63 testClassfile = loader.getClassfile(TEST_CLASS); 64 } 65 66 public void testFileTracking() { 67 monitor.beginSession(new LoadEvent(this, null, null, null)); 68 monitor.beginGroup(new LoadEvent(this, null, null, null)); 69 70 assertEquals("previous", 0, monitor.previousFiles.size()); 71 assertEquals("current", 0, monitor.currentFiles.size()); 72 73 monitor.beginFile(new LoadEvent(this, null, TEST_FILENAME, null)); 74 75 assertEquals("previous", 0, monitor.previousFiles.size()); 76 assertEquals("current", 1, monitor.currentFiles.size()); 77 assertTrue("TEST_FILENAME not in current", monitor.currentFiles.contains(TEST_FILENAME)); 78 79 monitor.endFile(new LoadEvent(this, null, TEST_FILENAME, null)); 80 81 assertEquals("previous", 0, monitor.previousFiles.size()); 82 assertEquals("current", 1, monitor.currentFiles.size()); 83 assertTrue("TEST_FILENAME not in current", monitor.currentFiles.contains(TEST_FILENAME)); 84 85 monitor.endGroup(new LoadEvent(this, null, null, null)); 86 monitor.endSession(new LoadEvent(this, null, null, null)); 87 88 assertEquals("previous", 1, monitor.previousFiles.size()); 89 assertTrue("TEST_FILENAME not in previous", monitor.previousFiles.contains(TEST_FILENAME)); 90 assertEquals("current", 0, monitor.currentFiles.size()); 91 } 92 93 public void testFileTrackingAcrossSessions() { 94 monitor.beginSession(new LoadEvent(this, null, null, null)); 95 monitor.beginGroup(new LoadEvent(this, null, null, null)); 96 monitor.beginFile(new LoadEvent(this, null, TEST_FILENAME, null)); 97 monitor.beginClassfile(new LoadEvent(this, null, TEST_FILENAME, null)); 98 monitor.endClassfile(new LoadEvent(this, null, TEST_FILENAME, testClassfile)); 99 monitor.endFile(new LoadEvent(this, null, TEST_FILENAME, null)); 100 monitor.endGroup(new LoadEvent(this, null, null, null)); 101 monitor.endSession(new LoadEvent(this, null, null, null)); 102 103 assertEquals("previous", 1, monitor.previousFiles.size()); 104 assertTrue("TEST_FILENAME not in previous", monitor.previousFiles.contains(TEST_FILENAME)); 105 assertEquals("current", 0, monitor.currentFiles.size()); 106 107 monitor.beginSession(new LoadEvent(this, null, null, null)); 108 monitor.beginGroup(new LoadEvent(this, null, null, null)); 109 monitor.beginFile(new LoadEvent(this, null, TEST_FILENAME, null)); 110 monitor.beginClassfile(new LoadEvent(this, null, TEST_FILENAME, null)); 111 112 assertEquals("previous", 1, monitor.previousFiles.size()); 113 assertTrue("TEST_FILENAME not in previous", monitor.previousFiles.contains(TEST_FILENAME)); 114 assertEquals("current", 1, monitor.currentFiles.size()); 115 assertTrue("TEST_FILENAME not in current", monitor.currentFiles.contains(TEST_FILENAME)); 116 117 monitor.endClassfile(new LoadEvent(this, null, TEST_FILENAME, testClassfile)); 118 119 assertEquals("previous", 1, monitor.previousFiles.size()); 120 assertTrue("TEST_FILENAME not in previous", monitor.previousFiles.contains(TEST_FILENAME)); 121 assertEquals("current", 1, monitor.currentFiles.size()); 122 assertTrue("TEST_FILENAME not in current", monitor.currentFiles.contains(TEST_FILENAME)); 123 124 monitor.endFile(new LoadEvent(this, null, TEST_FILENAME, null)); 125 126 assertEquals("previous", 0, monitor.previousFiles.size()); 127 assertEquals("current", 1, monitor.currentFiles.size()); 128 assertTrue("TEST_FILENAME not in current", monitor.currentFiles.contains(TEST_FILENAME)); 129 130 monitor.endGroup(new LoadEvent(this, null, null, null)); 131 monitor.endSession(new LoadEvent(this, null, null, null)); 132 133 assertEquals("previous", 1, monitor.previousFiles.size()); 134 assertTrue("TEST_FILENAME not in previous", monitor.previousFiles.contains(TEST_FILENAME)); 135 assertEquals("current", 0, monitor.currentFiles.size()); 136 } 137 138 public void testFileTrackingWithSkippedFile() { 139 monitor.beginSession(new LoadEvent(this, null, null, null)); 140 monitor.beginGroup(new LoadEvent(this, null, null, null)); 141 monitor.beginFile(new LoadEvent(this, null, TEST_FILENAME, null)); 142 monitor.beginClassfile(new LoadEvent(this, null, TEST_FILENAME, null)); 143 monitor.endClassfile(new LoadEvent(this, null, TEST_FILENAME, testClassfile)); 144 monitor.endFile(new LoadEvent(this, null, TEST_FILENAME, null)); 145 monitor.endGroup(new LoadEvent(this, null, null, null)); 146 monitor.endSession(new LoadEvent(this, null, null, null)); 147 148 assertEquals("previous", 1, monitor.previousFiles.size()); 149 assertTrue("TEST_FILENAME not in previous", monitor.previousFiles.contains(TEST_FILENAME)); 150 assertEquals("current", 0, monitor.currentFiles.size()); 151 152 monitor.beginSession(new LoadEvent(this, null, null, null)); 153 monitor.beginGroup(new LoadEvent(this, null, null, null)); 154 monitor.beginFile(new LoadEvent(this, null, TEST_FILENAME, null)); 155 156 assertEquals("previous", 1, monitor.previousFiles.size()); 157 assertTrue("TEST_FILENAME not in previous", monitor.previousFiles.contains(TEST_FILENAME)); 158 assertEquals("current", 1, monitor.currentFiles.size()); 159 assertTrue("TEST_FILENAME not in current", monitor.currentFiles.contains(TEST_FILENAME)); 160 161 monitor.endFile(new LoadEvent(this, null, TEST_FILENAME, null)); 162 163 assertEquals("previous", 0, monitor.previousFiles.size()); 164 assertEquals("current", 1, monitor.currentFiles.size()); 165 assertTrue("TEST_FILENAME not in current", monitor.currentFiles.contains(TEST_FILENAME)); 166 167 monitor.endGroup(new LoadEvent(this, null, null, null)); 168 monitor.endSession(new LoadEvent(this, null, null, null)); 169 170 assertEquals("previous", 1, monitor.previousFiles.size()); 171 assertTrue("TEST_FILENAME not in previous", monitor.previousFiles.contains(TEST_FILENAME)); 172 assertEquals("current", 0, monitor.currentFiles.size()); 173 } 174 175 public void testFileTrackingWithMissingFile() { 176 monitor.beginSession(new LoadEvent(this, null, null, null)); 177 monitor.beginGroup(new LoadEvent(this, null, null, null)); 178 monitor.beginFile(new LoadEvent(this, null, TEST_FILENAME, null)); 179 monitor.beginClassfile(new LoadEvent(this, null, TEST_FILENAME, null)); 180 monitor.endClassfile(new LoadEvent(this, null, TEST_FILENAME, testClassfile)); 181 monitor.endFile(new LoadEvent(this, null, TEST_FILENAME, null)); 182 monitor.endGroup(new LoadEvent(this, null, null, null)); 183 monitor.endSession(new LoadEvent(this, null, null, null)); 184 185 assertEquals("previous", 1, monitor.previousFiles.size()); 186 assertTrue("TEST_FILENAME not in previous", monitor.previousFiles.contains(TEST_FILENAME)); 187 assertEquals("current", 0, monitor.currentFiles.size()); 188 189 monitor.beginSession(new LoadEvent(this, null, null, null)); 190 monitor.beginGroup(new LoadEvent(this, null, null, null)); 191 192 assertEquals("previous", 1, monitor.previousFiles.size()); 193 assertTrue("TEST_FILENAME not in previous", monitor.previousFiles.contains(TEST_FILENAME)); 194 assertEquals("current", 0, monitor.currentFiles.size()); 195 196 monitor.endGroup(new LoadEvent(this, null, null, null)); 197 monitor.endSession(new LoadEvent(this, null, null, null)); 198 199 assertEquals("previous", 0, monitor.previousFiles.size()); 200 assertEquals("current", 0, monitor.currentFiles.size()); 201 } 202 203 public void testNewClassfile() { 204 monitor.beginSession(new LoadEvent(this, null, null, null)); 205 monitor.beginGroup(new LoadEvent(this, null, null, null)); 206 monitor.beginFile(new LoadEvent(this, null, TEST_FILENAME, null)); 207 monitor.beginClassfile(new LoadEvent(this, null, TEST_FILENAME, null)); 208 monitor.endClassfile(new LoadEvent(this, null, TEST_FILENAME, testClassfile)); 209 210 assertEquals("added", 1, addVisitor.getVisitedClasses().size()); 211 assertTrue("added missed " + TEST_CLASS, addVisitor.getVisitedClasses().contains(testClassfile)); 212 assertEquals("removed", 0, removeVisitor.getVisitedClasses().size()); 213 } 214 215 public void testRepeatInSession() { 216 monitor.beginSession(new LoadEvent(this, null, null, null)); 217 monitor.beginGroup(new LoadEvent(this, null, null, null)); 218 monitor.beginFile(new LoadEvent(this, null, TEST_FILENAME, null)); 219 monitor.beginClassfile(new LoadEvent(this, null, TEST_FILENAME, null)); 220 monitor.endClassfile(new LoadEvent(this, null, TEST_FILENAME, testClassfile)); 221 monitor.endFile(new LoadEvent(this, null, TEST_FILENAME, null)); 222 monitor.beginFile(new LoadEvent(this, null, TEST_FILENAME, null)); 223 monitor.beginClassfile(new LoadEvent(this, null, TEST_FILENAME, null)); 224 monitor.endClassfile(new LoadEvent(this, null, TEST_FILENAME, testClassfile)); 225 226 assertEquals("added", 2, addVisitor.getVisitedClasses().size()); 227 Iterator i = addVisitor.getVisitedClasses().iterator(); 228 assertEquals("added 1", testClassfile, i.next()); 229 assertEquals("added 2", testClassfile, i.next()); 230 assertEquals("removed", 0, removeVisitor.getVisitedClasses().size()); 231 } 232 233 public void testRepeatAcrossSessions() { 234 monitor.beginSession(new LoadEvent(this, null, null, null)); 235 monitor.beginGroup(new LoadEvent(this, null, null, null)); 236 monitor.beginFile(new LoadEvent(this, null, TEST_FILENAME, null)); 237 monitor.beginClassfile(new LoadEvent(this, null, TEST_FILENAME, null)); 238 monitor.endClassfile(new LoadEvent(this, null, TEST_FILENAME, testClassfile)); 239 monitor.endFile(new LoadEvent(this, null, TEST_FILENAME, null)); 240 monitor.endGroup(new LoadEvent(this, null, null, null)); 241 monitor.endSession(new LoadEvent(this, null, null, null)); 242 243 monitor.beginSession(new LoadEvent(this, null, null, null)); 244 monitor.beginGroup(new LoadEvent(this, null, null, null)); 245 monitor.beginFile(new LoadEvent(this, null, TEST_FILENAME, null)); 246 monitor.beginClassfile(new LoadEvent(this, null, TEST_FILENAME, null)); 247 monitor.endClassfile(new LoadEvent(this, null, TEST_FILENAME, testClassfile)); 248 249 assertEquals("added", 2, addVisitor.getVisitedClasses().size()); 250 Iterator i = addVisitor.getVisitedClasses().iterator(); 251 assertEquals("added 1", testClassfile, i.next()); 252 assertEquals("added 2", testClassfile, i.next()); 253 assertEquals("removed", 1, removeVisitor.getVisitedClasses().size()); 254 assertTrue("removed missed " + TEST_CLASS, removeVisitor.getVisitedClasses().contains(TEST_CLASS)); 255 } 256 257 public void testRemoval() { 258 monitor.beginSession(new LoadEvent(this, null, null, null)); 259 monitor.beginGroup(new LoadEvent(this, null, null, null)); 260 monitor.beginFile(new LoadEvent(this, null, TEST_FILENAME, null)); 261 monitor.beginClassfile(new LoadEvent(this, null, TEST_FILENAME, null)); 262 monitor.endClassfile(new LoadEvent(this, null, TEST_FILENAME, testClassfile)); 263 monitor.endFile(new LoadEvent(this, null, TEST_FILENAME, null)); 264 monitor.endGroup(new LoadEvent(this, null, null, null)); 265 monitor.endSession(new LoadEvent(this, null, null, null)); 266 267 monitor.beginSession(new LoadEvent(this, null, null, null)); 268 monitor.beginGroup(new LoadEvent(this, null, null, null)); 269 monitor.endGroup(new LoadEvent(this, null, null, null)); 270 monitor.endSession(new LoadEvent(this, null, null, null)); 271 272 assertEquals("added", 1, addVisitor.getVisitedClasses().size()); 273 assertTrue("added missed " + TEST_CLASS, addVisitor.getVisitedClasses().contains(testClassfile)); 274 assertEquals("removed", 1, removeVisitor.getVisitedClasses().size()); 275 assertTrue("removed missed " + TEST_CLASS, removeVisitor.getVisitedClasses().contains(TEST_CLASS)); 276 } 277 278 public void testSkip() { 279 monitor.beginSession(new LoadEvent(this, null, null, null)); 280 monitor.beginGroup(new LoadEvent(this, null, null, null)); 281 monitor.beginFile(new LoadEvent(this, null, TEST_FILENAME, null)); 282 monitor.beginClassfile(new LoadEvent(this, null, TEST_FILENAME, null)); 283 monitor.endClassfile(new LoadEvent(this, null, TEST_FILENAME, testClassfile)); 284 monitor.endFile(new LoadEvent(this, null, TEST_FILENAME, null)); 285 monitor.endGroup(new LoadEvent(this, null, null, null)); 286 monitor.endSession(new LoadEvent(this, null, null, null)); 287 288 monitor.beginSession(new LoadEvent(this, null, null, null)); 289 monitor.beginGroup(new LoadEvent(this, null, null, null)); 290 monitor.beginFile(new LoadEvent(this, null, TEST_FILENAME, null)); 291 monitor.endFile(new LoadEvent(this, null, TEST_FILENAME, null)); 292 monitor.endGroup(new LoadEvent(this, null, null, null)); 293 monitor.endSession(new LoadEvent(this, null, null, null)); 294 295 assertEquals("added", 1, addVisitor.getVisitedClasses().size()); 296 assertTrue("added missed " + TEST_CLASS, addVisitor.getVisitedClasses().contains(testClassfile)); 297 assertEquals("removed", 0, removeVisitor.getVisitedClasses().size()); 298 } 299 300 public void testOpenSession() { 301 monitor.setClosedSession(false); 302 303 monitor.beginSession(new LoadEvent(this, null, null, null)); 304 monitor.beginGroup(new LoadEvent(this, null, null, null)); 305 monitor.beginFile(new LoadEvent(this, null, TEST_FILENAME, null)); 306 monitor.beginClassfile(new LoadEvent(this, null, TEST_FILENAME, null)); 307 monitor.endClassfile(new LoadEvent(this, null, TEST_FILENAME, testClassfile)); 308 monitor.endFile(new LoadEvent(this, null, TEST_FILENAME, null)); 309 monitor.endGroup(new LoadEvent(this, null, null, null)); 310 monitor.endSession(new LoadEvent(this, null, null, null)); 311 312 monitor.beginSession(new LoadEvent(this, null, null, null)); 313 monitor.beginGroup(new LoadEvent(this, null, null, null)); 314 monitor.endGroup(new LoadEvent(this, null, null, null)); 315 monitor.endSession(new LoadEvent(this, null, null, null)); 316 317 assertEquals("added", 1, addVisitor.getVisitedClasses().size()); 318 assertTrue("added missed " + TEST_CLASS, addVisitor.getVisitedClasses().contains(testClassfile)); 319 assertEquals("removed", 0, removeVisitor.getVisitedClasses().size()); 320 } 321 322 public void testExtractAgainInOpenSession() { 323 monitor.setClosedSession(false); 324 325 monitor.beginSession(new LoadEvent(this, null, null, null)); 326 monitor.beginGroup(new LoadEvent(this, null, null, null)); 327 monitor.beginFile(new LoadEvent(this, null, TEST_FILENAME, null)); 328 monitor.beginClassfile(new LoadEvent(this, null, TEST_FILENAME, null)); 329 monitor.endClassfile(new LoadEvent(this, null, TEST_FILENAME, testClassfile)); 330 monitor.endFile(new LoadEvent(this, null, TEST_FILENAME, null)); 331 monitor.endGroup(new LoadEvent(this, null, null, null)); 332 monitor.endSession(new LoadEvent(this, null, null, null)); 333 334 addVisitor.reset(); 335 removeVisitor.reset(); 336 337 monitor.beginSession(new LoadEvent(this, null, null, null)); 338 monitor.beginGroup(new LoadEvent(this, null, null, null)); 339 monitor.beginFile(new LoadEvent(this, null, TEST_FILENAME, null)); 340 monitor.beginClassfile(new LoadEvent(this, null, TEST_FILENAME, null)); 341 monitor.endClassfile(new LoadEvent(this, null, TEST_FILENAME, testClassfile)); 342 monitor.endFile(new LoadEvent(this, null, TEST_FILENAME, null)); 343 monitor.endGroup(new LoadEvent(this, null, null, null)); 344 monitor.endSession(new LoadEvent(this, null, null, null)); 345 346 assertEquals("added", 1, addVisitor.getVisitedClasses().size()); 347 assertTrue("added missed " + TEST_CLASS, addVisitor.getVisitedClasses().contains(testClassfile)); 348 assertEquals("removed", 0, removeVisitor.getVisitedClasses().size()); 349 } 350 351 public void testReloadAfterClosingSessionSession() { 352 monitor.setClosedSession(false); 353 354 monitor.beginSession(new LoadEvent(this, null, null, null)); 355 monitor.beginGroup(new LoadEvent(this, null, null, null)); 356 monitor.beginFile(new LoadEvent(this, null, TEST_FILENAME, null)); 357 monitor.beginClassfile(new LoadEvent(this, null, TEST_FILENAME, null)); 358 monitor.endClassfile(new LoadEvent(this, null, TEST_FILENAME, testClassfile)); 359 monitor.endFile(new LoadEvent(this, null, TEST_FILENAME, null)); 360 monitor.endGroup(new LoadEvent(this, null, null, null)); 361 monitor.endSession(new LoadEvent(this, null, null, null)); 362 363 monitor.setClosedSession(true); 364 365 addVisitor.reset(); 366 removeVisitor.reset(); 367 368 monitor.beginSession(new LoadEvent(this, null, null, null)); 369 monitor.beginGroup(new LoadEvent(this, null, null, null)); 370 monitor.beginFile(new LoadEvent(this, null, TEST_FILENAME, null)); 371 monitor.beginClassfile(new LoadEvent(this, null, TEST_FILENAME, null)); 372 monitor.endClassfile(new LoadEvent(this, null, TEST_FILENAME, testClassfile)); 373 monitor.endFile(new LoadEvent(this, null, TEST_FILENAME, null)); 374 monitor.endGroup(new LoadEvent(this, null, null, null)); 375 monitor.endSession(new LoadEvent(this, null, null, null)); 376 377 assertEquals("added", 1, addVisitor.getVisitedClasses().size()); 378 assertTrue("added missed " + TEST_CLASS, addVisitor.getVisitedClasses().contains(testClassfile)); 379 assertEquals("removed", 1, removeVisitor.getVisitedClasses().size()); 380 assertTrue("removed missed " + TEST_CLASS, removeVisitor.getVisitedClasses().contains(TEST_CLASS)); 381 } 382 } 383 | Popular Tags |