1 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 ; 40 import java.util.Set ; 41 import java.util.TreeSet ; 42 import java.util.jar.Attributes ; 43 import java.util.jar.JarEntry ; 44 import java.util.jar.JarInputStream ; 45 import java.util.jar.Manifest ; 46 47 public class JarCreationTest extends DrJavaTestCase { 48 51 public void testCreateManifest() { 52 ManifestWriter mw = new ManifestWriter(); 53 Manifest 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 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 <String >(Arrays.asList(new String []{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 <String >(Arrays.asList(new String []{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 <String >(Arrays.asList(new String []{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 177 public void testCreateJar() { 178 File f = new File("test.jar"); 179 f.deleteOnExit(); 180 File add = null; 181 try { 182 String 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 <String >(Arrays.asList(new String []{"JarTest.java", 204 "dir/JarTest.java"}))); 205 206 JarInputStream jarStream = new JarInputStream (new FileInputStream(f), true); 207 208 JarEntry 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 227 private void testArchive(File jar, Set <String > fileNames) { 228 JarInputStream jarStream = null; 229 try { 230 jarStream = new JarInputStream (new FileInputStream(jar), true); 231 232 JarEntry 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 |