1
2
3
4
5
6
7
8
9
10
11 package org.mule.util;
12
13 import org.mule.tck.AbstractMuleTestCase;
14
15 import java.io.File;
16 import java.util.Arrays;
17 import java.util.Iterator;
18 import java.util.LinkedHashMap;
19
20
21
22
23 public class JarUtilsTestCase extends AbstractMuleTestCase
24 {
25
26
27
28
29
30
31
32 public void testCreateAppendReadJarFileEntries()
33 {
34 File jarFile = null;
35 File jarEntryFile = null;
36
37 try
38 {
39
40 String jarEntryString = "testString";
41 jarEntryFile = File.createTempFile("test", "file");
42 byte[] jarEntryBytes = jarEntryString.getBytes();
43
44 LinkedHashMap jarEntries = new LinkedHashMap();
45 jarEntries.put("META-INF/string", jarEntryString);
46 jarEntries.put("META-INF/file", jarEntryFile);
47 jarEntries.put("META-INF/byte", jarEntryBytes);
48
49 jarFile = File.createTempFile("test", ".jar");
50 assertTrue(jarFile.delete());
51
52 JarUtils.createJarFileEntries(jarFile, jarEntries);
53
54
55 LinkedHashMap additionalJarEntries = new LinkedHashMap();
56 additionalJarEntries.put("META-INF/append/string", jarEntryString);
57 jarEntries.put("META-INF/append/string", jarEntryString);
58 JarUtils.appendJarFileEntries(jarFile, additionalJarEntries);
59
60
61 LinkedHashMap readJarEntries = JarUtils.readJarFileEntries(jarFile);
62
63 assertEquals(jarEntries.size(), readJarEntries.size());
64
65 Iterator jarEntryIter = jarEntries.keySet().iterator();
66 Iterator readJarEntryIter = readJarEntries.keySet().iterator();
67
68
69 while (jarEntryIter.hasNext())
70 {
71 String jarEntryPath = (String) jarEntryIter.next();
72 String readJarEntryPath = (String) readJarEntryIter.next();
73
74 assertNotNull(jarEntryPath);
75 assertNotNull(readJarEntryPath);
76 assertEquals(jarEntryPath, readJarEntryPath);
77
78 Object jarEntry = jarEntries.get(jarEntryPath);
79 Object readJarEntry = jarEntries.get(readJarEntryPath);
80
81 if (jarEntry instanceof String || jarEntry instanceof File)
82 {
83 assertEquals(jarEntry, readJarEntry);
84 }
85 else if (jarEntry instanceof byte[])
86 {
87 assertTrue(Arrays.equals((byte[]) jarEntry, (byte[]) readJarEntry));
88 }
89 else
90 {
91 fail("Unsupported jar entry read for " + jarEntryPath);
92 }
93 }
94 }
95 catch (Exception e)
96 {
97 fail(e.getMessage());
98 }
99 finally
100 {
101 if (jarFile != null)
102 {
103 jarFile.delete();
104 }
105 if (jarEntryFile != null)
106 {
107 jarEntryFile.delete();
108 }
109 }
110 }
111 }