KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > jar > JarCreationTest


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.util.jar;
35
36 import edu.rice.cs.drjava.DrJavaTestCase;
37
38 import java.io.*;
39 import java.util.Arrays JavaDoc;
40 import java.util.Set JavaDoc;
41 import java.util.TreeSet JavaDoc;
42 import java.util.jar.Attributes JavaDoc;
43 import java.util.jar.JarEntry JavaDoc;
44 import java.util.jar.JarInputStream JavaDoc;
45 import java.util.jar.Manifest JavaDoc;
46
47 public class JarCreationTest extends DrJavaTestCase {
48   /**
49    * Tests the creation of manifest files through the ManifestWriter class
50    */

51   public void testCreateManifest() {
52     ManifestWriter mw = new ManifestWriter();
53     Manifest JavaDoc manifest = mw.getManifest();
54     assertTrue("should have version attribute", manifest.getMainAttributes().containsKey(Attributes.Name.MANIFEST_VERSION));
55     assertEquals("should have version attribute", "1.0", manifest.getMainAttributes().get(Attributes.Name.MANIFEST_VERSION));
56     assertEquals("should only have manifest attribute", 1, manifest.getMainAttributes().size());
57
58     mw.setMainClass("edu.rice.cs.drjava.DrJava");
59     manifest = mw.getManifest();
60     assertTrue("should have version attribute", manifest.getMainAttributes().containsKey(Attributes.Name.MANIFEST_VERSION));
61     assertEquals("should have version attribute", "1.0", manifest.getMainAttributes().get(Attributes.Name.MANIFEST_VERSION));
62     assertTrue("should have main class attribute", manifest.getMainAttributes().containsKey(Attributes.Name.MAIN_CLASS));
63     assertEquals("should have main class attribute", "edu.rice.cs.drjava.DrJava", manifest.getMainAttributes().get(Attributes.Name.MAIN_CLASS));
64     assertEquals("should only have manifest attribute", 2, manifest.getMainAttributes().size());
65
66
67     mw = new ManifestWriter();
68     mw.addClassPath("koala.dynamicjava");
69     manifest = mw.getManifest();
70     assertTrue("should have version attribute", manifest.getMainAttributes().containsKey(Attributes.Name.MANIFEST_VERSION));
71     assertEquals("should have version attribute", "1.0", manifest.getMainAttributes().get(Attributes.Name.MANIFEST_VERSION));
72     assertTrue("should have classpath attribute", manifest.getMainAttributes().containsKey(Attributes.Name.CLASS_PATH));
73     assertEquals("should have correct classpath", "koala.dynamicjava", manifest.getMainAttributes().get(Attributes.Name.CLASS_PATH));
74     assertEquals("have version and classpath", 2, manifest.getMainAttributes().size());
75
76     mw.addClassPath("edu.rice.cs.util");
77     manifest = mw.getManifest();
78     assertTrue("should have version attribute", manifest.getMainAttributes().containsKey(Attributes.Name.MANIFEST_VERSION));
79     assertEquals("should have version attribute", "1.0", manifest.getMainAttributes().get(Attributes.Name.MANIFEST_VERSION));
80     assertTrue("should have classpath attribute", manifest.getMainAttributes().containsKey(Attributes.Name.CLASS_PATH));
81     assertEquals("should have correct classpath", "koala.dynamicjava edu.rice.cs.util", manifest.getMainAttributes().get(Attributes.Name.CLASS_PATH));
82     assertEquals("have version and classpath", 2, manifest.getMainAttributes().size());
83
84     mw.setMainClass("edu.rice.cs.drjava.DrJava");
85     manifest = mw.getManifest();
86     assertTrue("should have version attribute", manifest.getMainAttributes().containsKey(Attributes.Name.MANIFEST_VERSION));
87     assertEquals("should have version attribute", "1.0", manifest.getMainAttributes().get(Attributes.Name.MANIFEST_VERSION));
88     assertTrue("should have classpath attribute", manifest.getMainAttributes().containsKey(Attributes.Name.CLASS_PATH));
89     assertEquals("should have correct classpath", "koala.dynamicjava edu.rice.cs.util", manifest.getMainAttributes().get(Attributes.Name.CLASS_PATH));
90     assertTrue("should have main class attribute", manifest.getMainAttributes().containsKey(Attributes.Name.MAIN_CLASS));
91     assertEquals("should have main class attribute", "edu.rice.cs.drjava.DrJava", manifest.getMainAttributes().get(Attributes.Name.MAIN_CLASS));
92     assertEquals("have version and classpath", 3, manifest.getMainAttributes().size());
93   }
94
95   /**
96    * Test create addDirectoryRecursive
97    */

98   public void testCreateJarFromDirectoryRecursive() {
99     File dir = new File("temp_dir");
100     dir.mkdir();
101     File dir1 = new File(dir, "dir");
102     dir1.mkdir();
103     File[] files = new File[]{new File(dir, "test.java"),
104                               new File(dir, "test.class"),
105                               new File(dir, "p1.tmp"),
106                               new File(dir1, "test1.java"),
107                               new File(dir1, "out.class"),
108                               new File(dir1, "out.out.out.class"),
109                               new File(dir1, "that.java")};
110     dir.deleteOnExit();
111     dir1.deleteOnExit();
112     for(int i = 0; i < files.length; i++)
113       files[i].deleteOnExit();
114     try {
115
116       PrintWriter pw = null;
117       for (int i = 0; i < files.length; i++) {
118         pw = new PrintWriter(new FileOutputStream(files[i]));
119         pw.write(files[i].getName());
120         pw.close();
121       }
122
123       File f = new File("test~.jar");
124       f.deleteOnExit();
125       JarBuilder jb = new JarBuilder(f);
126       jb.addDirectoryRecursive(dir, "");
127       jb.close();
128
129       testArchive(f,
130               new TreeSet JavaDoc<String JavaDoc>(Arrays.asList(new String JavaDoc[]{files[0].getName(),
131                                                              files[1].getName(),
132                                                              files[2].getName(),
133                                                              "dir/" + files[3].getName(),
134                                                              "dir/" + files[4].getName(),
135                                                              "dir/" + files[5].getName(),
136                                                              "dir/" + files[6].getName()})));
137
138       jb = new JarBuilder(f);
139       jb.addDirectoryRecursive(dir, "", new FileFilter() {
140         public boolean accept(File pathname) {
141           return pathname.getName().endsWith(".class") || pathname.isDirectory();
142         }
143       });
144       jb.close();
145
146       testArchive(f,
147               new TreeSet JavaDoc<String JavaDoc>(Arrays.asList(new String JavaDoc[]{files[1].getName(),
148                                                              "dir/" + files[4].getName(),
149                                                              "dir/" + files[5].getName()})));
150
151       jb = new JarBuilder(f);
152       jb.addDirectoryRecursive(dir, "", new FileFilter() {
153         public boolean accept(File pathname) {
154           return pathname.getName().endsWith(".java") || pathname.isDirectory();
155         }
156       });
157       jb.close();
158
159       testArchive(f,
160               new TreeSet JavaDoc<String JavaDoc>(Arrays.asList(new String JavaDoc[]{files[0].getName(),
161                                                              "dir/" + files[3].getName(),
162                                                              "dir/" + files[6].getName()})));
163
164     }
165     catch (FileNotFoundException e) {
166       e.printStackTrace();
167     }
168     catch (IOException e) {
169       e.printStackTrace();
170     }
171
172   }
173
174   /**
175    * Test the manual creation of jar files
176    */

177   public void testCreateJar() {
178     File f = new File("test.jar");
179     f.deleteOnExit();
180     File add = null;
181     try {
182       String JavaDoc fileContents = "public class JarTest {" +
183               "\tpublic String getClassName() {" +
184               "\t\treturn \"JarTest\";" +
185               "\t}" +
186               "}";
187       byte[] b = new byte[fileContents.getBytes("UTF-8").length];
188
189       add = File.createTempFile("JarTest",".java").getCanonicalFile();
190       add.deleteOnExit();
191
192       PrintWriter pw = new PrintWriter(new FileOutputStream(add));
193       pw.write(fileContents);
194       pw.close();
195
196       JarBuilder jb = new JarBuilder(f);
197       jb.addFile(add, "", "JarTest.java");
198       jb.addFile(add, "dir", "JarTest.java");
199       jb.close();
200
201
202       testArchive(f,
203               new TreeSet JavaDoc<String JavaDoc>(Arrays.asList(new String JavaDoc[]{"JarTest.java",
204                                                              "dir/JarTest.java"})));
205
206       JarInputStream JavaDoc jarStream = new JarInputStream JavaDoc(new FileInputStream(f), true);
207
208       JarEntry JavaDoc ent = jarStream.getNextJarEntry();
209       assertTrue("should have JarTest", ent != null);
210       assertEquals("names should match", "JarTest.java", ent.getName());
211
212       ent = jarStream.getNextJarEntry();
213       assertTrue("should have JarTest", ent != null);
214       assertEquals("names should match", "dir/JarTest.java", ent.getName());
215     }
216     catch (IOException e) {
217       e.printStackTrace();
218       fail("failed test");
219     }
220   }
221
222   /**
223    * Check that all files in an a Set are in the jar file
224    * @param jar the jar file to check
225    * @param fileNames the set of the names of files
226    */

227   private void testArchive(File jar, Set JavaDoc<String JavaDoc> fileNames) {
228     JarInputStream JavaDoc jarStream = null;
229     try {
230       jarStream = new JarInputStream JavaDoc(new FileInputStream(jar), true);
231
232       JarEntry JavaDoc ent = null;
233       while( (ent = jarStream.getNextJarEntry()) != null ) {
234         assertTrue("found "+ent.getName()+" should be in list", fileNames.contains(ent.getName()));
235         fileNames.remove(ent.getName());
236       }
237     }
238     catch (IOException e) {
239       fail("couldn't open file");
240     } finally {
241       if( jarStream != null)
242         try {
243           jarStream.close();
244         }
245         catch (IOException e) {
246           e.printStackTrace();
247         }
248     }
249     assertEquals("all listed files should have been in archive", 0, fileNames.size());
250   }
251 }
Popular Tags