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