001: package csdl.jblanket.util;
002:
003: import java.io.File;
004: import java.util.Enumeration;
005: import java.util.jar.JarEntry;
006: import java.util.jar.JarFile;
007:
008: import junit.framework.TestCase;
009: import junit.framework.TestSuite;
010: import junit.textui.TestRunner;
011:
012: import org.apache.tools.ant.DirectoryScanner;
013:
014: /**
015: * Tests operations in the JarFactory class.
016: * <p>
017: * If using Ant to execute this test class a 'jblanket.testdir' system property needs to be set in
018: * the build.xml file. If running test class from command line, must provide '-Djblanket.testdir'
019: * to set the system property. This value is used by both the testJar and testUnjar methods.
020: *
021: * @author Joy M. Agustin
022: * @version $Id: TestJarFactory.java,v 1.1 2004/11/07 00:32:26 timshadel Exp $
023: */
024: public class TestJarFactory extends TestCase {
025:
026: /** Subdirectory holding JAR file for testing */
027: private String jarTestdataDir;
028: /** Subdirectory holding extracted JAR files for testing */
029: private String unjarTestdataDir;
030:
031: /** Directory holding files created by testing */
032: private String testDir;
033:
034: /** Name of directory holding data for testing */
035: private final String testdataDirName = "testdata";
036: /** Name of directory holding JAR file for testing */
037: private final String jarTestdataDirName = "jar";
038: /** Name of directory holding extracted JAR files for testing */
039: private final String unjarTestdataDirName = "unjar";
040: /** Name of JAR file to use for testing extraction */
041: private final String jarFileName = "stack.jar";
042:
043: /** Directory seperator */
044: private final String SLASH = File.separator;
045:
046: /** Grammar for CVS directories */
047: private final String cvsGrammar = "**" + SLASH + "CVS" + SLASH
048: + "**";
049: /** Include condition 1 */
050: private final String include1 = "edu" + SLASH + "**";
051: /** Include condition 2 */
052: private final String include2 = "META-INF";
053:
054: /**
055: * Required for JUnit.
056: *
057: * @param name Test case name.
058: */
059: public TestJarFactory(String name) {
060: super (name);
061: }
062:
063: /**
064: * Sets up variables for testing.
065: */
066: public void setUp() {
067:
068: this .testDir = System.getProperty("jblanket.testdir") + SLASH
069: + "testjar";
070: assertNotNull("Checking for presence of methodset_testdir.",
071: this .testDir);
072:
073: // initialize testdata sub/directory variables
074: String testdataDir = System.getProperty("jblanket.data.dir");
075: this .jarTestdataDir = testdataDir + SLASH
076: + this .jarTestdataDirName;
077: this .unjarTestdataDir = testdataDir + SLASH
078: + this .unjarTestdataDirName;
079: }
080:
081: /**
082: * Tests both the <code>jar</code> and <code>unjar</code> methods in the JarFactory class.
083: *
084: * @throws Exception If problems occur.
085: */
086: public void testJarFactory() throws Exception {
087:
088: // tests extraction of a JAR file by checking the content that is output.
089: JarFactory factory = new JarFactory(this .testDir);
090: factory.unJar(new File(this .jarTestdataDir, this .jarFileName));
091:
092: // get known unpacked contents
093: DirectoryScanner knownScanner = new DirectoryScanner();
094: knownScanner.setIncludes(new String[] { include1, include2 });
095: knownScanner.setExcludes(new String[] { cvsGrammar });
096: knownScanner.setBasedir(new File(this .unjarTestdataDir));
097: knownScanner.scan();
098: String[] knownDirectories = knownScanner
099: .getIncludedDirectories();
100: String[] knownFiles = knownScanner.getIncludedFiles();
101:
102: // get contents unpacked by factory
103: DirectoryScanner factoryScanner = new DirectoryScanner();
104: factoryScanner.setIncludes(new String[] { include1, include2 });
105: factoryScanner.setBasedir(new File(this .testDir));
106: factoryScanner.scan();
107: String[] factoryDirectories = factoryScanner
108: .getIncludedDirectories();
109: String[] factoryFiles = factoryScanner.getIncludedFiles();
110:
111: // check known contents to factory contents
112: assertEquals("checking number of directories",
113: knownDirectories.length, factoryDirectories.length);
114: assertEquals("checking number of files", knownFiles.length,
115: factoryFiles.length);
116:
117: assertTrue("Checking at least one file is not empty",
118: factoryFiles[0].length() > 0);
119:
120: // test creation of a JAR file by checking the content that is output.
121: factory = new JarFactory(this .testDir);
122: // if file exists, delete and create new one
123: File jarFile = new File(this .testDir, this .jarFileName);
124: if (jarFile.exists()) {
125: jarFile.delete();
126: }
127: factory.jar(jarFile);
128:
129: // get known jar file entries
130: JarFile knownFile = new JarFile(new File(this .jarTestdataDir,
131: this .jarFileName));
132: Enumeration knownEnum = knownFile.entries();
133:
134: // get entries created by factory
135: JarFile factoryFile = new JarFile(new File(this .testDir,
136: this .jarFileName));
137: Enumeration factoryEnum = factoryFile.entries();
138:
139: // check known entries to factory entries
140: while (knownEnum.hasMoreElements()
141: && factoryEnum.hasMoreElements()) {
142: assertEquals("checking JAR entries", ((JarEntry) knownEnum
143: .nextElement()).getName(), ((JarEntry) factoryEnum
144: .nextElement()).getName());
145: }
146: assertTrue("checking if all entries were included", !knownEnum
147: .hasMoreElements());
148: assertTrue("checking if extra entries were included",
149: !factoryEnum.hasMoreElements());
150:
151: knownFile.close();
152: factoryFile.close();
153: }
154:
155: /**
156: * Provide stand-alone execution of this test case during initial development.
157: *
158: * @param args The command line arguments
159: */
160: public static void main(String[] args) {
161:
162: System.out.println("JUnit testing JarFactory.");
163: //Runs all no-arg methods starting with "test".
164: TestRunner.run(new TestSuite(TestJarFactory.class));
165: }
166: }
|