001: /*
002: * ========================================================================
003: *
004: * Copyright 2003 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.integration.ant.deployment;
021:
022: import java.io.BufferedReader;
023: import java.io.File;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.InputStreamReader;
027: import java.io.StringReader;
028:
029: import junit.framework.TestCase;
030:
031: /**
032: * Unit tests for {@link JarArchive}.
033: *
034: * @version $Id: TestJarArchive.java 238816 2004-02-29 16:36:46Z vmassol $
035: */
036: public final class TestJarArchive extends TestCase {
037:
038: // Test Methods ------------------------------------------------------------
039:
040: /**
041: * Verifies that a <code>NullPointerException</code> is thrown when the
042: * constructor is passed a <code>null</code> argument as file.
043: *
044: * @throws Exception If an unexpected error occurs
045: */
046: public void testConstructorWithNullFile() throws Exception {
047: try {
048: new DefaultJarArchive((File) null);
049: fail("NullPointerException expected");
050: } catch (NullPointerException expected) {
051: // expected
052: }
053: }
054:
055: /**
056: * Verifies that a <code>NullPointerException</code> is thrown when the
057: * constructor is passed a <code>null</code> argument as input stream.
058: *
059: * @throws Exception If an unexpected error occurs
060: */
061: public void testConstructorWithNullInputStream() throws Exception {
062: try {
063: new DefaultJarArchive((InputStream) null);
064: fail("NullPointerException expected");
065: } catch (NullPointerException expected) {
066: // expected
067: }
068: }
069:
070: /**
071: * Verifies that random access to resources in the JAR is provided.
072: *
073: * @throws Exception If an unexpected error occurs
074: */
075: public void testRandomAccess() throws Exception {
076: JarArchive jar = new DefaultJarArchive(
077: getTestInput("org/apache/cactus/integration/ant/deployment/randomaccess.jar"));
078: assertContains(jar.getResource("firstEntry.txt"), "firstEntry");
079: assertContains(jar.getResource("secondEntry.txt"),
080: "secondEntry");
081: assertContains(jar.getResource("secondEntry.txt"),
082: "secondEntry");
083: assertContains(jar.getResource("firstEntry.txt"), "firstEntry");
084: }
085:
086: /**
087: * Verifies that the method <code>containsClass()</code> returns
088: * <code>true</code> if the JAR contains the requested class.
089: *
090: * @throws Exception If an unexpected error occurs
091: */
092: public void testContainsClass() throws Exception {
093: JarArchive jar = new DefaultJarArchive(
094: getTestInput("org/apache/cactus/integration/ant/deployment/containsclass.jar"));
095: assertTrue(jar.containsClass("test.Test"));
096: }
097:
098: /**
099: * Verifies that the method <code>containsClass()</code> returns
100: * <code>false</code> if the JAR does not contain such a class.
101: *
102: * @throws Exception If an unexpected error occurs
103: */
104: public void testContainsClassEmpty() throws Exception {
105: JarArchive jar = new DefaultJarArchive(
106: getTestInput("org/apache/cactus/integration/ant/deployment/empty.jar"));
107: assertTrue(!jar.containsClass("test.Test"));
108: }
109:
110: // Private Methods ---------------------------------------------------------
111:
112: /**
113: * Asserts whether the content of the specified input stream matches the
114: * specified string line per line.
115: *
116: * @param theInput The input stream to check
117: * @param theExpectedString The expected string
118: * @throws IOException If an I/O error occurs reading from the input stream
119: */
120: private void assertContains(InputStream theInput,
121: String theExpectedString) throws IOException {
122: try {
123: BufferedReader inReader = new BufferedReader(
124: new InputStreamReader(theInput));
125: BufferedReader stringReader = new BufferedReader(
126: new StringReader(theExpectedString));
127: String line = null;
128: while ((line = inReader.readLine()) != null) {
129: assertEquals(stringReader.readLine(), line);
130: }
131: } finally {
132: if (theInput != null) {
133: theInput.close();
134: }
135: }
136: }
137:
138: /**
139: * Returns a file from the test inputs directory, which is determined by the
140: * system property <code>testinput.dir</code>.
141: *
142: * @param theFileName The name of the file relative to the test input
143: * directory
144: * @return The file from the test input directory
145: */
146: private File getTestInput(String theFileName) {
147: String testInputDirProperty = System
148: .getProperty("testinput.dir");
149: assertTrue("The system property 'testinput.dir' must be set",
150: testInputDirProperty != null);
151: File testInputDir = new File(testInputDirProperty);
152: assertTrue(
153: "The system property 'testinput.dir' must point to an "
154: + "existing directory", testInputDir
155: .isDirectory());
156: File testInputFile = new File(testInputDir, theFileName);
157: assertTrue("The test input " + theFileName + " does not exist",
158: testInputFile.exists());
159: return testInputFile;
160: }
161:
162: }
|