View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.util;
8   
9   import org.mule.tck.junit4.AbstractMuleTestCase;
10  
11  import java.io.File;
12  import java.util.Arrays;
13  import java.util.Iterator;
14  import java.util.LinkedHashMap;
15  
16  import org.junit.Test;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertNotNull;
20  import static org.junit.Assert.assertTrue;
21  import static org.junit.Assert.fail;
22  
23  /**
24   * Test suite for jar utilities.
25   */
26  public class JarUtilsTestCase extends AbstractMuleTestCase
27  {
28      /**
29       * <ol>
30       *  <li>Create jar file with all supported entries</li>
31       *  <li>Append jar file with additional entry</li>
32       *  <li>Read jar file and compare against previous entries written</li>
33       * </ol>
34       */
35      @Test
36      public void testCreateAppendReadJarFileEntries()
37      {
38          File jarFile = null;
39          File jarEntryFile = null;
40          
41          try
42          {
43              // Create jar file from scratch
44              String jarEntryString = "testString";
45              jarEntryFile = File.createTempFile("test", "file");
46              byte[] jarEntryBytes = jarEntryString.getBytes();
47              
48              LinkedHashMap jarEntries = new LinkedHashMap();
49              jarEntries.put("META-INF/string", jarEntryString);
50              jarEntries.put("META-INF/file", jarEntryFile);
51              jarEntries.put("META-INF/byte", jarEntryBytes);
52              
53              jarFile = File.createTempFile("test", ".jar");
54              assertTrue(jarFile.delete());
55              
56              JarUtils.createJarFileEntries(jarFile, jarEntries);
57              
58              // Append entry to jar file
59              LinkedHashMap additionalJarEntries = new LinkedHashMap();
60              additionalJarEntries.put("META-INF/append/string", jarEntryString);
61              jarEntries.put("META-INF/append/string", jarEntryString);
62              JarUtils.appendJarFileEntries(jarFile, additionalJarEntries);
63              
64              // Read jar file and verify previously written values
65              LinkedHashMap readJarEntries = JarUtils.readJarFileEntries(jarFile);
66              
67              assertEquals(jarEntries.size(), readJarEntries.size());
68              
69              Iterator jarEntryIter = jarEntries.keySet().iterator();
70              Iterator readJarEntryIter = readJarEntries.keySet().iterator();
71              
72              // Iterate through original and read jar entries, which must be equal.
73              while (jarEntryIter.hasNext())
74              {
75                  String jarEntryPath = (String) jarEntryIter.next();
76                  String readJarEntryPath = (String) readJarEntryIter.next();
77                  
78                  assertNotNull(jarEntryPath);
79                  assertNotNull(readJarEntryPath);
80                  assertEquals(jarEntryPath, readJarEntryPath);
81                  
82                  Object jarEntry = jarEntries.get(jarEntryPath);
83                  Object readJarEntry = jarEntries.get(readJarEntryPath);
84                  
85                  if (jarEntry instanceof String || jarEntry instanceof File)
86                  {
87                      assertEquals(jarEntry, readJarEntry);   
88                  }
89                  else if (jarEntry instanceof byte[])
90                  {
91                      assertTrue(Arrays.equals((byte[]) jarEntry, (byte[]) readJarEntry));   
92                  }
93                  else
94                  {
95                      fail("Unsupported jar entry read for " +  jarEntryPath);
96                  }
97              }
98          }
99          catch (Exception e)
100         {
101             fail(e.getMessage());
102         }
103         finally
104         {
105             if (jarFile != null)
106             {
107                 jarFile.delete();
108             }
109             if (jarEntryFile != null)
110             {
111                 jarEntryFile.delete();
112             }
113         }
114     }
115 }