View Javadoc

1   /*
2    * $Id: JarUtilsTestCase.java 22377 2011-07-11 12:41:42Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.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   * Test suite for jar utilities.
29   */
30  public class JarUtilsTestCase extends AbstractMuleTestCase
31  {
32      /**
33       * <ol>
34       *  <li>Create jar file with all supported entries</li>
35       *  <li>Append jar file with additional entry</li>
36       *  <li>Read jar file and compare against previous entries written</li>
37       * </ol>
38       */
39      @Test
40      public void testCreateAppendReadJarFileEntries()
41      {
42          File jarFile = null;
43          File jarEntryFile = null;
44          
45          try
46          {
47              // Create jar file from scratch
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              // Append entry to jar file
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              // Read jar file and verify previously written values
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              // Iterate through original and read jar entries, which must be equal.
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 }