cmlenz 2003/06/09 12:04:14
Modified: integration/ant/src/test/org/apache/cactus/integration/ant/deployment
TestJarArchive.java
integration/ant/src/java/org/apache/cactus/integration/ant/deployment
WarArchive.java JarArchive.java
Added: integration/ant/src/test/org/apache/cactus/integration/ant/deployment
TestWarArchive.java
Log:
New methods to detect whether a JAR or WAR file contains a specified class.
This will allow duplicate checking in the <cactifywar> task.
Revision Changes Path
1.3 +30 -8
jakarta-cactus/integration/ant/src/test/org/apache/cactus/integration/ant/deployment/TestJarArchive.java
Index: TestJarArchive.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/integration/ant/src/test/org/apache/cactus/integration/ant/deployment/TestJarArchive.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TestJarArchive.java 26 May 2003 13:27:19 -0000 1.2
+++ TestJarArchive.java 9 Jun 2003 19:04:13 -0000 1.3
@@ -87,7 +87,7 @@
{
try
{
- new JarArchive((File) null) { };
+ new JarArchive((File) null);
fail("NullPointerException expected");
}
catch (NullPointerException expected)
@@ -106,9 +106,7 @@
{
try
{
- new JarArchive((InputStream) null)
- {
- };
+ new JarArchive((InputStream) null);
fail("NullPointerException expected");
}
catch (NullPointerException expected)
@@ -125,13 +123,37 @@
public void testRandomAccess() throws Exception
{
JarArchive jar = new JarArchive(getTestInput(
- "org/apache/cactus/integration/ant/deployment/randomaccess.jar"))
- {
- };
+ "org/apache/cactus/integration/ant/deployment/randomaccess.jar"));
assertContains(jar.getResource("firstEntry.txt"), "firstEntry");
assertContains(jar.getResource("secondEntry.txt"), "secondEntry");
assertContains(jar.getResource("secondEntry.txt"), "secondEntry");
assertContains(jar.getResource("firstEntry.txt"), "firstEntry");
+ }
+
+ /**
+ * Verifies that the method <code>containsClass()</code> returns
+ * <code>true</code> if the JAR contains the requested class.
+ *
+ * @throws Exception If an unexpected error occurs
+ */
+ public void testContainsClass() throws Exception
+ {
+ JarArchive jar = new JarArchive(getTestInput(
+ "org/apache/cactus/integration/ant/deployment/containsclass.jar"));
+ assertTrue(jar.containsClass("test.Test"));
+ }
+
+ /**
+ * Verifies that the method <code>containsClass()</code> returns
+ * <code>false</code> if the JAR does not contain such a class.
+ *
+ * @throws Exception If an unexpected error occurs
+ */
+ public void testContainsClassEmpty() throws Exception
+ {
+ JarArchive jar = new JarArchive(getTestInput(
+ "org/apache/cactus/integration/ant/deployment/empty.jar"));
+ assertTrue(!jar.containsClass("test.Test"));
}
// Private Methods ---------------------------------------------------------
1.1
jakarta-cactus/integration/ant/src/test/org/apache/cactus/integration/ant/deployment/TestWarArchive.java
Index: TestWarArchive.java
===================================================================
/*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
* Foundation" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.cactus.integration.ant.deployment;
import java.io.File;
import junit.framework.TestCase;
/**
* Unit tests for [EMAIL PROTECTED] WarArchive}.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Christopher Lenz</a>
*
* @version $Id: TestWarArchive.java,v 1.1 2003/06/09 19:04:13 cmlenz Exp $
*/
public final class TestWarArchive extends TestCase
{
// Test Methods ------------------------------------------------------------
/**
* Verifies that the method <code>containsClass()</code> returns
* <code>true</code> if the WAR contains the requested class in
* <code>WEB-INF/classes</code>.
*
* @throws Exception If an unexpected error occurs
*/
public void testContainsClassInWebinfClasses() throws Exception
{
WarArchive war = new WarArchive(getTestInput(
"org/apache/cactus/integration/ant/deployment/containsclass.war"));
assertTrue(war.containsClass("test.Test"));
}
/**
* Verifies that the method <code>containsClass()</code> returns
* <code>true</code> if the WAR contains the requested class in a JAR in
* <code>WEB-INF/lib</code>.
*
* @throws Exception If an unexpected error occurs
*/
public void testContainsClassInWebinfLib() throws Exception
{
WarArchive war = new WarArchive(getTestInput(
"org/apache/cactus/integration/ant/deployment/"
+ "containsclasslib.war"));
assertTrue(war.containsClass("test.Test"));
}
/**
* Verifies that the method <code>containsClass()</code> returns
* <code>false</code> if the WAR does not contain such a class.
*
* @throws Exception If an unexpected error occurs
*/
public void testContainsClassEmpty() throws Exception
{
WarArchive war = new WarArchive(getTestInput(
"org/apache/cactus/integration/ant/deployment/empty.war"));
assertTrue(!war.containsClass("test.Test"));
}
// Private Methods ---------------------------------------------------------
/**
* Returns a file from the test inputs directory, which is determined by the
* system property <code>testinput.dir</code>.
*
* @param theFileName The name of the file relative to the test input
* directory
* @return The file from the test input directory
*/
private File getTestInput(String theFileName)
{
String testInputDirProperty = System.getProperty("testinput.dir");
assertTrue("The system property 'testinput.dir' must be set",
testInputDirProperty != null);
File testInputDir = new File(testInputDirProperty);
assertTrue("The system property 'testinput.dir' must point to an "
+ "existing directory", testInputDir.isDirectory());
File testInputFile = new File(testInputDir, theFileName);
assertTrue("The test input " + theFileName + " does not exist",
testInputFile.exists());
return testInputFile;
}
}
1.4 +37 -1
jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/deployment/WarArchive.java
Index: WarArchive.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/deployment/WarArchive.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- WarArchive.java 28 May 2003 09:32:14 -0000 1.3
+++ WarArchive.java 9 Jun 2003 19:04:14 -0000 1.4
@@ -59,6 +59,8 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
+import java.util.Iterator;
+import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
@@ -144,6 +146,40 @@
}
}
return this.webXml;
+ }
+
+ /**
+ * Returns whether a class of the specified name is contained in the web-app
+ * archive, either directly in WEB-INF/classes, or in one of the JARs in
+ * WEB-INF/lib.
+ *
+ * @param theClassName The name of the class to search for
+ * @return Whether the class was found in the archive
+ * @throws IOException If an I/O error occurred reading the archive
+ */
+ public boolean containsClass(String theClassName)
+ throws IOException
+ {
+ // Look in WEB-INF/classes first
+ String resourceName =
+ "WEB-INF/classes/" + theClassName.replace('.', '/') + ".class";
+ if (getResource(resourceName) != null)
+ {
+ return true;
+ }
+
+ // Next scan the JARs in WEB-INF/lib
+ List jars = getResources("WEB-INF/lib/");
+ for (Iterator i = jars.iterator(); i.hasNext();)
+ {
+ JarArchive jar = new JarArchive(getResource((String) i.next()));
+ if (jar.containsClass(theClassName))
+ {
+ return true;
+ }
+ }
+
+ return false;
}
}
1.4 +54 -2
jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/deployment/JarArchive.java
Index: JarArchive.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/deployment/JarArchive.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- JarArchive.java 16 May 2003 14:08:44 -0000 1.3
+++ JarArchive.java 9 Jun 2003 19:04:14 -0000 1.4
@@ -62,6 +62,8 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
import java.util.jar.JarInputStream;
import java.util.zip.ZipEntry;
@@ -74,7 +76,7 @@
* @since Cactus 1.5
* @version $Id$
*/
-public abstract class JarArchive
+public class JarArchive
{
// Instance Variables ------------------------------------------------------
@@ -131,6 +133,21 @@
// Public Methods ----------------------------------------------------------
/**
+ * Returns whether a class of the specified name is contained in the
+ * archive.
+ *
+ * @param theClassName The name of the class to search for
+ * @return Whether the class was found
+ * @throws IOException If an I/O error occurred reading the archive
+ */
+ public boolean containsClass(String theClassName)
+ throws IOException
+ {
+ String resourceName = theClassName.replace('.', '/') + ".class";
+ return (getResource(resourceName) != null);
+ }
+
+ /**
* Returns the full path of a named resource in the archive.
*
* @param theName The name of the resource
@@ -208,6 +225,41 @@
}
}
return null;
+ }
+
+ /**
+ * Returns the list of resources in the specified directory.
+ *
+ * @param thePath The directory
+ * @return The list of resources
+ * @throws IOException If an I/O error occurs
+ */
+ public List getResources(String thePath)
+ throws IOException
+ {
+ List resources = new ArrayList();
+ JarInputStream in = null;
+ try
+ {
+ in = getContentAsStream();
+ ZipEntry zipEntry = null;
+ while ((zipEntry = in.getNextEntry()) != null)
+ {
+ if ((zipEntry.getName().startsWith(thePath)
+ && !zipEntry.getName().equals(thePath)))
+ {
+ resources.add(zipEntry.getName());
+ }
+ }
+ }
+ finally
+ {
+ if (in != null)
+ {
+ in.close();
+ }
+ }
+ return resources;
}
// Protected Methods -------------------------------------------------------
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]