KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > integration > ant > deployment > TestJarArchive


1 /*
2  * ========================================================================
3  *
4  * Copyright 2003 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.integration.ant.deployment;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27 import java.io.StringReader JavaDoc;
28
29 import junit.framework.TestCase;
30
31 /**
32  * Unit tests for {@link JarArchive}.
33  *
34  * @version $Id: TestJarArchive.java,v 1.6 2004/02/29 16:32:16 vmassol Exp $
35  */

36 public final class TestJarArchive extends TestCase
37 {
38
39     // Test Methods ------------------------------------------------------------
40

41     /**
42      * Verifies that a <code>NullPointerException</code> is thrown when the
43      * constructor is passed a <code>null</code> argument as file.
44      *
45      * @throws Exception If an unexpected error occurs
46      */

47     public void testConstructorWithNullFile() throws Exception JavaDoc
48     {
49         try
50         {
51             new DefaultJarArchive((File JavaDoc) null);
52             fail("NullPointerException expected");
53         }
54         catch (NullPointerException JavaDoc expected)
55         {
56             // expected
57
}
58     }
59
60     /**
61      * Verifies that a <code>NullPointerException</code> is thrown when the
62      * constructor is passed a <code>null</code> argument as input stream.
63      *
64      * @throws Exception If an unexpected error occurs
65      */

66     public void testConstructorWithNullInputStream() throws Exception JavaDoc
67     {
68         try
69         {
70             new DefaultJarArchive((InputStream JavaDoc) null);
71             fail("NullPointerException expected");
72         }
73         catch (NullPointerException JavaDoc expected)
74         {
75             // expected
76
}
77     }
78
79     /**
80      * Verifies that random access to resources in the JAR is provided.
81      *
82      * @throws Exception If an unexpected error occurs
83      */

84     public void testRandomAccess() throws Exception JavaDoc
85     {
86         JarArchive jar = new DefaultJarArchive(getTestInput(
87             "org/apache/cactus/integration/ant/deployment/randomaccess.jar"));
88         assertContains(jar.getResource("firstEntry.txt"), "firstEntry");
89         assertContains(jar.getResource("secondEntry.txt"), "secondEntry");
90         assertContains(jar.getResource("secondEntry.txt"), "secondEntry");
91         assertContains(jar.getResource("firstEntry.txt"), "firstEntry");
92     }
93
94     /**
95      * Verifies that the method <code>containsClass()</code> returns
96      * <code>true</code> if the JAR contains the requested class.
97      *
98      * @throws Exception If an unexpected error occurs
99      */

100     public void testContainsClass() throws Exception JavaDoc
101     {
102         JarArchive jar = new DefaultJarArchive(getTestInput(
103             "org/apache/cactus/integration/ant/deployment/containsclass.jar"));
104         assertTrue(jar.containsClass("test.Test"));
105     }
106
107     /**
108      * Verifies that the method <code>containsClass()</code> returns
109      * <code>false</code> if the JAR does not contain such a class.
110      *
111      * @throws Exception If an unexpected error occurs
112      */

113     public void testContainsClassEmpty() throws Exception JavaDoc
114     {
115         JarArchive jar = new DefaultJarArchive(getTestInput(
116             "org/apache/cactus/integration/ant/deployment/empty.jar"));
117         assertTrue(!jar.containsClass("test.Test"));
118     }
119
120     // Private Methods ---------------------------------------------------------
121

122     /**
123      * Asserts whether the content of the specified input stream matches the
124      * specified string line per line.
125      *
126      * @param theInput The input stream to check
127      * @param theExpectedString The expected string
128      * @throws IOException If an I/O error occurs reading from the input stream
129      */

130     private void assertContains(InputStream JavaDoc theInput, String JavaDoc theExpectedString)
131         throws IOException JavaDoc
132     {
133         try
134         {
135             BufferedReader JavaDoc inReader =
136                 new BufferedReader JavaDoc(new InputStreamReader JavaDoc(theInput));
137             BufferedReader JavaDoc stringReader =
138                 new BufferedReader JavaDoc(new StringReader JavaDoc(theExpectedString));
139             String JavaDoc line = null;
140             while ((line = inReader.readLine()) != null)
141             {
142                 assertEquals(stringReader.readLine(), line);
143             }
144         }
145         finally
146         {
147             if (theInput != null)
148             {
149                 theInput.close();
150             }
151         }
152     }
153
154     /**
155      * Returns a file from the test inputs directory, which is determined by the
156      * system property <code>testinput.dir</code>.
157      *
158      * @param theFileName The name of the file relative to the test input
159      * directory
160      * @return The file from the test input directory
161      */

162     private File JavaDoc getTestInput(String JavaDoc theFileName)
163     {
164         String JavaDoc testInputDirProperty = System.getProperty("testinput.dir");
165         assertTrue("The system property 'testinput.dir' must be set",
166             testInputDirProperty != null);
167         File JavaDoc testInputDir = new File JavaDoc(testInputDirProperty);
168         assertTrue("The system property 'testinput.dir' must point to an "
169             + "existing directory", testInputDir.isDirectory());
170         File JavaDoc testInputFile = new File JavaDoc(testInputDir, theFileName);
171         assertTrue("The test input " + theFileName + " does not exist",
172             testInputFile.exists());
173         return testInputFile;
174     }
175
176 }
177
Popular Tags