1   /*
2    * $Id: JarUtilsTestCase.java 8501 2007-09-20 03:15:10Z aguenther $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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   * Test suite for jar utilities.
22   */
23  public class JarUtilsTestCase extends AbstractMuleTestCase
24  {
25      /**
26       * <ol>
27       *  <li>Create jar file with all supported entries</li>
28       *  <li>Append jar file with additional entry</li>
29       *  <li>Read jar file and compare against previous entries written</li>
30       * </ol>
31       */
32      public void testCreateAppendReadJarFileEntries()
33      {
34          File jarFile = null;
35          File jarEntryFile = null;
36          
37          try
38          {
39              // Create jar file from scratch
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              // Append entry to jar file
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              // Read jar file and verify previously written values
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              // Iterate through original and read jar entries, which must be equal.
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 }