1 32 33 package com.jeantessier.classreader; 34 35 import java.io.*; 36 import java.util.*; 37 38 public class TestDirectoryClassfileLoader extends TestClassfileLoader { 39 public static final String TEST_CLASS = "test"; 40 public static final String TEST_FILENAME = "classes" + File.separator + "test.class"; 41 public static final String BOGUS_TEST_FILENAME = "classes" + File.separator + "bogus" + File.separator + "test.class"; 42 public static final String TEST_DIRNAME = "classes" + File.separator + "testpackage"; 43 public static final String OTHER_DIRNAME = "classes" + File.separator + "otherpackage"; 44 45 private ClassfileLoader loader; 46 47 protected void setUp() throws Exception { 48 super.setUp(); 49 50 ClassfileLoader eventSource = new TransientClassfileLoader(); 51 eventSource.addLoadListener(this); 52 loader = new DirectoryClassfileLoader(eventSource); 53 } 54 55 public void testLoadClassFile() throws IOException { 56 loader.load(TEST_FILENAME); 57 58 assertEquals("Begin Session", 0, getBeginSessionEvents().size()); 59 assertEquals("Begin Group", 1, getBeginGroupEvents().size()); 60 assertEquals("Begin File", 1, getBeginFileEvents().size()); 61 assertEquals("Begin Classfile", 1, getBeginClassfileEvents().size()); 62 assertEquals("End Classfile", 1, getEndClassfileEvents().size()); 63 assertEquals("End File", 1, getEndFileEvents().size()); 64 assertEquals("End Group", 1, getEndGroupEvents().size()); 65 assertEquals("End Session", 0, getEndSessionEvents().size()); 66 67 assertEquals(TEST_FILENAME, ((LoadEvent) getEndClassfileEvents().getLast()).getGroupName()); 68 assertNotNull("Classfile", ((LoadEvent) getEndClassfileEvents().getLast()).getClassfile()); 69 } 70 71 public void testLoadClassInputStream() throws IOException { 72 loader.load(TEST_FILENAME, new FileInputStream(TEST_FILENAME)); 73 74 assertEquals("Begin Session", 0, getBeginSessionEvents().size()); 75 assertEquals("Begin Group", 0, getBeginGroupEvents().size()); 76 assertEquals("Begin File", 0, getBeginFileEvents().size()); 77 assertEquals("Begin Classfile", 0, getBeginClassfileEvents().size()); 78 assertEquals("End Classfile", 0, getEndClassfileEvents().size()); 79 assertEquals("End File", 0, getEndFileEvents().size()); 80 assertEquals("End Group", 0, getEndGroupEvents().size()); 81 assertEquals("End Session", 0, getEndSessionEvents().size()); 82 } 83 84 public void testLoadBogusFile() throws IOException { 85 loader.load(BOGUS_TEST_FILENAME); 86 87 assertEquals("Begin Session", 0, getBeginSessionEvents().size()); 88 assertEquals("Begin Group", 1, getBeginGroupEvents().size()); 89 assertEquals("Begin File", 0, getBeginFileEvents().size()); 90 assertEquals("Begin Classfile", 0, getBeginClassfileEvents().size()); 91 assertEquals("End Classfile", 0, getEndClassfileEvents().size()); 92 assertEquals("End File", 0, getEndFileEvents().size()); 93 assertEquals("End Group", 1, getEndGroupEvents().size()); 94 assertEquals("End Session", 0, getEndSessionEvents().size()); 95 } 96 97 public void testLoadBogusInputStream() throws IOException { 98 loader.load(BOGUS_TEST_FILENAME, new FileInputStream(TEST_FILENAME)); 99 100 assertEquals("Begin Session", 0, getBeginSessionEvents().size()); 101 assertEquals("Begin Group", 0, getBeginGroupEvents().size()); 102 assertEquals("Begin File", 0, getBeginFileEvents().size()); 103 assertEquals("Begin Classfile", 0, getBeginClassfileEvents().size()); 104 assertEquals("End Classfile", 0, getEndClassfileEvents().size()); 105 assertEquals("End File", 0, getEndFileEvents().size()); 106 assertEquals("End Group", 0, getEndGroupEvents().size()); 107 assertEquals("End Session", 0, getEndSessionEvents().size()); 108 } 109 110 public void testLoadDirectory() throws IOException { 111 loader.load(TEST_DIRNAME); 112 113 assertEquals("Begin Session", 0, getBeginSessionEvents().size()); 114 assertEquals("Begin Group", 1, getBeginGroupEvents().size()); 115 assertEquals("Begin File", 7, getBeginFileEvents().size()); 116 assertEquals("Begin Classfile", 6, getBeginClassfileEvents().size()); 117 assertEquals("End Classfile", 6, getEndClassfileEvents().size()); 118 assertEquals("End File", 7, getEndFileEvents().size()); 119 assertEquals("End Group", 1, getEndGroupEvents().size()); 120 assertEquals("End Session", 0, getEndSessionEvents().size()); 121 } 122 123 public void testMultipleDirectories() throws IOException { 124 loader.load(TEST_DIRNAME); 125 loader.load(OTHER_DIRNAME); 126 127 assertEquals("Begin Session", 0, getBeginSessionEvents().size()); 128 assertEquals("Begin Group", 2, getBeginGroupEvents().size()); 129 assertEquals("Begin File", 11, getBeginFileEvents().size()); 130 assertEquals("Begin Classfile", 9, getBeginClassfileEvents().size()); 131 assertEquals("End Classfile", 9, getEndClassfileEvents().size()); 132 assertEquals("End File", 11, getEndFileEvents().size()); 133 assertEquals("End Group", 2, getEndGroupEvents().size()); 134 assertEquals("End Session", 0, getEndSessionEvents().size()); 135 } 136 } 137 | Popular Tags |