KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > csdl > jblanket > util > TestJarFactory


1 package csdl.jblanket.util;
2
3 import java.io.File JavaDoc;
4 import java.util.Enumeration JavaDoc;
5 import java.util.jar.JarEntry JavaDoc;
6 import java.util.jar.JarFile JavaDoc;
7
8 import junit.framework.TestCase;
9 import junit.framework.TestSuite;
10 import junit.textui.TestRunner;
11
12 import org.apache.tools.ant.DirectoryScanner;
13
14 /**
15  * Tests operations in the JarFactory class.
16  * <p>
17  * If using Ant to execute this test class a 'jblanket.testdir' system property needs to be set in
18  * the build.xml file. If running test class from command line, must provide '-Djblanket.testdir'
19  * to set the system property. This value is used by both the testJar and testUnjar methods.
20  *
21  * @author Joy M. Agustin
22  * @version $Id: TestJarFactory.java,v 1.1 2004/11/07 00:32:26 timshadel Exp $
23  */

24 public class TestJarFactory extends TestCase {
25   
26   /** Subdirectory holding JAR file for testing */
27   private String JavaDoc jarTestdataDir;
28   /** Subdirectory holding extracted JAR files for testing */
29   private String JavaDoc unjarTestdataDir;
30
31   /** Directory holding files created by testing */
32   private String JavaDoc testDir;
33
34   /** Name of directory holding data for testing */
35   private final String JavaDoc testdataDirName = "testdata";
36   /** Name of directory holding JAR file for testing */
37   private final String JavaDoc jarTestdataDirName = "jar";
38   /** Name of directory holding extracted JAR files for testing */
39   private final String JavaDoc unjarTestdataDirName = "unjar";
40   /** Name of JAR file to use for testing extraction */
41   private final String JavaDoc jarFileName = "stack.jar";
42
43   /** Directory seperator */
44   private final String JavaDoc SLASH = File.separator;
45
46   /** Grammar for CVS directories */
47   private final String JavaDoc cvsGrammar = "**" + SLASH + "CVS" + SLASH + "**";
48   /** Include condition 1 */
49   private final String JavaDoc include1 = "edu" + SLASH + "**";
50   /** Include condition 2 */
51   private final String JavaDoc include2 = "META-INF";
52
53   /**
54    * Required for JUnit.
55    *
56    * @param name Test case name.
57    */

58   public TestJarFactory(String JavaDoc name) {
59     super(name);
60   }
61
62   /**
63    * Sets up variables for testing.
64    */

65   public void setUp() {
66     
67     this.testDir = System.getProperty("jblanket.testdir") + SLASH + "testjar";
68     assertNotNull("Checking for presence of methodset_testdir.", this.testDir);
69
70     // initialize testdata sub/directory variables
71
String JavaDoc testdataDir = System.getProperty("jblanket.data.dir");
72     this.jarTestdataDir = testdataDir + SLASH + this.jarTestdataDirName;
73     this.unjarTestdataDir = testdataDir + SLASH + this.unjarTestdataDirName;
74   }
75
76   /**
77    * Tests both the <code>jar</code> and <code>unjar</code> methods in the JarFactory class.
78    *
79    * @throws Exception If problems occur.
80    */

81   public void testJarFactory() throws Exception JavaDoc {
82
83     // tests extraction of a JAR file by checking the content that is output.
84
JarFactory factory = new JarFactory(this.testDir);
85     factory.unJar(new File JavaDoc(this.jarTestdataDir, this.jarFileName));
86
87     // get known unpacked contents
88
DirectoryScanner knownScanner = new DirectoryScanner();
89     knownScanner.setIncludes(new String JavaDoc[] {include1, include2});
90     knownScanner.setExcludes(new String JavaDoc[] {cvsGrammar});
91     knownScanner.setBasedir(new File JavaDoc(this.unjarTestdataDir));
92     knownScanner.scan();
93     String JavaDoc[] knownDirectories = knownScanner.getIncludedDirectories();
94     String JavaDoc[] knownFiles = knownScanner.getIncludedFiles();
95
96     // get contents unpacked by factory
97
DirectoryScanner factoryScanner = new DirectoryScanner();
98     factoryScanner.setIncludes(new String JavaDoc[] {include1, include2});
99     factoryScanner.setBasedir(new File JavaDoc(this.testDir));
100     factoryScanner.scan();
101     String JavaDoc[] factoryDirectories = factoryScanner.getIncludedDirectories();
102     String JavaDoc[] factoryFiles = factoryScanner.getIncludedFiles();
103
104     // check known contents to factory contents
105
assertEquals("checking number of directories", knownDirectories.length,
106                  factoryDirectories.length);
107     assertEquals("checking number of files", knownFiles.length, factoryFiles.length);
108
109     assertTrue("Checking at least one file is not empty", factoryFiles[0].length() > 0);
110
111     // test creation of a JAR file by checking the content that is output.
112
factory = new JarFactory(this.testDir);
113     // if file exists, delete and create new one
114
File JavaDoc jarFile = new File JavaDoc(this.testDir, this.jarFileName);
115     if (jarFile.exists()) {
116       jarFile.delete();
117     }
118     factory.jar(jarFile);
119
120     // get known jar file entries
121
JarFile JavaDoc knownFile = new JarFile JavaDoc(new File JavaDoc(this.jarTestdataDir, this.jarFileName));
122     Enumeration JavaDoc knownEnum = knownFile.entries();
123
124     // get entries created by factory
125
JarFile JavaDoc factoryFile = new JarFile JavaDoc(new File JavaDoc(this.testDir, this.jarFileName));
126     Enumeration JavaDoc factoryEnum = factoryFile.entries();
127
128     // check known entries to factory entries
129
while (knownEnum.hasMoreElements() && factoryEnum.hasMoreElements()) {
130       assertEquals("checking JAR entries", ((JarEntry JavaDoc) knownEnum.nextElement()).getName(),
131                    ((JarEntry JavaDoc) factoryEnum.nextElement()).getName());
132     }
133     assertTrue("checking if all entries were included", !knownEnum.hasMoreElements());
134     assertTrue("checking if extra entries were included", !factoryEnum.hasMoreElements());
135
136     knownFile.close();
137     factoryFile.close();
138   }
139
140   /**
141    * Provide stand-alone execution of this test case during initial development.
142    *
143    * @param args The command line arguments
144    */

145   public static void main(String JavaDoc[] args) {
146     
147     System.out.println("JUnit testing JarFactory.");
148     //Runs all no-arg methods starting with "test".
149
TestRunner.run(new TestSuite(TestJarFactory.class));
150   }
151 }
152
Popular Tags