001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.util.jar;
038:
039: import edu.rice.cs.drjava.DrJavaTestCase;
040:
041: import java.io.*;
042: import java.util.Arrays;
043: import java.util.Set;
044: import java.util.TreeSet;
045: import java.util.jar.Attributes;
046: import java.util.jar.JarEntry;
047: import java.util.jar.JarInputStream;
048: import java.util.jar.Manifest;
049:
050: public class JarCreationTest extends DrJavaTestCase {
051: /**
052: * Tests the creation of manifest files through the ManifestWriter class
053: */
054: public void testCreateManifest() {
055: ManifestWriter mw = new ManifestWriter();
056: Manifest manifest = mw.getManifest();
057: assertTrue("should have version attribute", manifest
058: .getMainAttributes().containsKey(
059: Attributes.Name.MANIFEST_VERSION));
060: assertEquals("should have version attribute", "1.0", manifest
061: .getMainAttributes().get(
062: Attributes.Name.MANIFEST_VERSION));
063: assertEquals("should only have manifest attribute", 1, manifest
064: .getMainAttributes().size());
065:
066: mw.setMainClass("edu.rice.cs.drjava.DrJava");
067: manifest = mw.getManifest();
068: assertTrue("should have version attribute", manifest
069: .getMainAttributes().containsKey(
070: Attributes.Name.MANIFEST_VERSION));
071: assertEquals("should have version attribute", "1.0", manifest
072: .getMainAttributes().get(
073: Attributes.Name.MANIFEST_VERSION));
074: assertTrue("should have main class attribute", manifest
075: .getMainAttributes().containsKey(
076: Attributes.Name.MAIN_CLASS));
077: assertEquals("should have main class attribute",
078: "edu.rice.cs.drjava.DrJava", manifest
079: .getMainAttributes().get(
080: Attributes.Name.MAIN_CLASS));
081: assertEquals("should only have manifest attribute", 2, manifest
082: .getMainAttributes().size());
083:
084: mw = new ManifestWriter();
085: mw.addClassPath("koala.dynamicjava");
086: manifest = mw.getManifest();
087: assertTrue("should have version attribute", manifest
088: .getMainAttributes().containsKey(
089: Attributes.Name.MANIFEST_VERSION));
090: assertEquals("should have version attribute", "1.0", manifest
091: .getMainAttributes().get(
092: Attributes.Name.MANIFEST_VERSION));
093: assertTrue("should have classpath attribute", manifest
094: .getMainAttributes().containsKey(
095: Attributes.Name.CLASS_PATH));
096: assertEquals("should have correct classpath",
097: "koala.dynamicjava", manifest.getMainAttributes().get(
098: Attributes.Name.CLASS_PATH));
099: assertEquals("have version and classpath", 2, manifest
100: .getMainAttributes().size());
101:
102: mw.addClassPath("edu.rice.cs.util");
103: manifest = mw.getManifest();
104: assertTrue("should have version attribute", manifest
105: .getMainAttributes().containsKey(
106: Attributes.Name.MANIFEST_VERSION));
107: assertEquals("should have version attribute", "1.0", manifest
108: .getMainAttributes().get(
109: Attributes.Name.MANIFEST_VERSION));
110: assertTrue("should have classpath attribute", manifest
111: .getMainAttributes().containsKey(
112: Attributes.Name.CLASS_PATH));
113: assertEquals("should have correct classpath",
114: "koala.dynamicjava edu.rice.cs.util", manifest
115: .getMainAttributes().get(
116: Attributes.Name.CLASS_PATH));
117: assertEquals("have version and classpath", 2, manifest
118: .getMainAttributes().size());
119:
120: mw.setMainClass("edu.rice.cs.drjava.DrJava");
121: manifest = mw.getManifest();
122: assertTrue("should have version attribute", manifest
123: .getMainAttributes().containsKey(
124: Attributes.Name.MANIFEST_VERSION));
125: assertEquals("should have version attribute", "1.0", manifest
126: .getMainAttributes().get(
127: Attributes.Name.MANIFEST_VERSION));
128: assertTrue("should have classpath attribute", manifest
129: .getMainAttributes().containsKey(
130: Attributes.Name.CLASS_PATH));
131: assertEquals("should have correct classpath",
132: "koala.dynamicjava edu.rice.cs.util", manifest
133: .getMainAttributes().get(
134: Attributes.Name.CLASS_PATH));
135: assertTrue("should have main class attribute", manifest
136: .getMainAttributes().containsKey(
137: Attributes.Name.MAIN_CLASS));
138: assertEquals("should have main class attribute",
139: "edu.rice.cs.drjava.DrJava", manifest
140: .getMainAttributes().get(
141: Attributes.Name.MAIN_CLASS));
142: assertEquals("have version and classpath", 3, manifest
143: .getMainAttributes().size());
144: }
145:
146: /**
147: * Test create addDirectoryRecursive
148: */
149: public void testCreateJarFromDirectoryRecursive() {
150: File dir = new File("temp_dir");
151: dir.mkdir();
152: File dir1 = new File(dir, "dir");
153: dir1.mkdir();
154: File[] files = new File[] { new File(dir, "test.java"),
155: new File(dir, "test.class"), new File(dir, "p1.tmp"),
156: new File(dir1, "test1.java"),
157: new File(dir1, "out.class"),
158: new File(dir1, "out.out.out.class"),
159: new File(dir1, "that.java") };
160: dir.deleteOnExit();
161: dir1.deleteOnExit();
162: for (int i = 0; i < files.length; i++)
163: files[i].deleteOnExit();
164: try {
165:
166: PrintWriter pw = null;
167: for (int i = 0; i < files.length; i++) {
168: pw = new PrintWriter(new FileOutputStream(files[i]));
169: pw.write(files[i].getName());
170: pw.close();
171: }
172:
173: File f = new File("test~.jar");
174: f.deleteOnExit();
175: JarBuilder jb = new JarBuilder(f);
176: jb.addDirectoryRecursive(dir, "");
177: jb.close();
178:
179: testArchive(f, new TreeSet<String>(Arrays
180: .asList(new String[] { files[0].getName(),
181: files[1].getName(), files[2].getName(),
182: "dir/" + files[3].getName(),
183: "dir/" + files[4].getName(),
184: "dir/" + files[5].getName(),
185: "dir/" + files[6].getName() })));
186:
187: jb = new JarBuilder(f);
188: jb.addDirectoryRecursive(dir, "", new FileFilter() {
189: public boolean accept(File pathname) {
190: return pathname.getName().endsWith(".class")
191: || pathname.isDirectory();
192: }
193: });
194: jb.close();
195:
196: testArchive(f, new TreeSet<String>(Arrays
197: .asList(new String[] { files[1].getName(),
198: "dir/" + files[4].getName(),
199: "dir/" + files[5].getName() })));
200:
201: jb = new JarBuilder(f);
202: jb.addDirectoryRecursive(dir, "", new FileFilter() {
203: public boolean accept(File pathname) {
204: return pathname.getName().endsWith(".java")
205: || pathname.isDirectory();
206: }
207: });
208: jb.close();
209:
210: testArchive(f, new TreeSet<String>(Arrays
211: .asList(new String[] { files[0].getName(),
212: "dir/" + files[3].getName(),
213: "dir/" + files[6].getName() })));
214:
215: } catch (FileNotFoundException e) {
216: e.printStackTrace();
217: } catch (IOException e) {
218: e.printStackTrace();
219: }
220:
221: }
222:
223: /**
224: * Test the manual creation of jar files
225: */
226: public void testCreateJar() {
227: File f = new File("test.jar");
228: f.deleteOnExit();
229: File add = null;
230: try {
231: String fileContents = "public class JarTest {"
232: + "\tpublic String getClassName() {"
233: + "\t\treturn \"JarTest\";" + "\t}" + "}";
234: byte[] b = new byte[fileContents.getBytes("UTF-8").length];
235:
236: add = File.createTempFile("JarTest", ".java")
237: .getCanonicalFile();
238: add.deleteOnExit();
239:
240: PrintWriter pw = new PrintWriter(new FileOutputStream(add));
241: pw.write(fileContents);
242: pw.close();
243:
244: JarBuilder jb = new JarBuilder(f);
245: jb.addFile(add, "", "JarTest.java");
246: jb.addFile(add, "dir", "JarTest.java");
247: jb.close();
248:
249: testArchive(f, new TreeSet<String>(Arrays
250: .asList(new String[] { "JarTest.java",
251: "dir/JarTest.java" })));
252:
253: JarInputStream jarStream = new JarInputStream(
254: new FileInputStream(f), true);
255:
256: JarEntry ent = jarStream.getNextJarEntry();
257: assertTrue("should have JarTest", ent != null);
258: assertEquals("names should match", "JarTest.java", ent
259: .getName());
260:
261: ent = jarStream.getNextJarEntry();
262: assertTrue("should have JarTest", ent != null);
263: assertEquals("names should match", "dir/JarTest.java", ent
264: .getName());
265: } catch (IOException e) {
266: e.printStackTrace();
267: fail("failed test");
268: }
269: }
270:
271: /**
272: * Check that all files in an a Set are in the jar file
273: * @param jar the jar file to check
274: * @param fileNames the set of the names of files
275: */
276: private void testArchive(File jar, Set<String> fileNames) {
277: JarInputStream jarStream = null;
278: try {
279: jarStream = new JarInputStream(new FileInputStream(jar),
280: true);
281:
282: JarEntry ent = null;
283: while ((ent = jarStream.getNextJarEntry()) != null) {
284: assertTrue("found " + ent.getName()
285: + " should be in list", fileNames.contains(ent
286: .getName()));
287: fileNames.remove(ent.getName());
288: }
289: } catch (IOException e) {
290: fail("couldn't open file");
291: } finally {
292: if (jarStream != null)
293: try {
294: jarStream.close();
295: } catch (IOException e) {
296: e.printStackTrace();
297: }
298: }
299: assertEquals("all listed files should have been in archive", 0,
300: fileNames.size());
301: }
302: }
|