KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > classreader > TestDirectoryClassfileLoader


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

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 JavaDoc TEST_CLASS = "test";
40     public static final String JavaDoc TEST_FILENAME = "classes" + File.separator + "test.class";
41     public static final String JavaDoc BOGUS_TEST_FILENAME = "classes" + File.separator + "bogus" + File.separator + "test.class";
42     public static final String JavaDoc TEST_DIRNAME = "classes" + File.separator + "testpackage";
43     public static final String JavaDoc OTHER_DIRNAME = "classes" + File.separator + "otherpackage";
44     
45     private ClassfileLoader loader;
46
47     protected void setUp() throws Exception JavaDoc {
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