View Javadoc
1   package org.codehaus.plexus.archiver.jar;
2   
3   import java.io.ByteArrayOutputStream;
4   import java.io.IOException;
5   import java.io.InputStream;
6   import java.util.jar.Attributes;
7   import java.util.jar.Manifest;
8   import org.codehaus.plexus.PlexusTestCase;
9   import org.codehaus.plexus.archiver.util.Streams;
10  
11  /**
12   * @author Kristian Rosenvold
13   */
14  public class JdkManifestFactoryTest
15      extends PlexusTestCase
16  {
17  
18      public void testGetDefaultManifest()
19          throws Exception
20      {
21          ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
22          Manifest manifest = JdkManifestFactory.getDefaultManifest();
23          manifest.write( byteArrayOutputStream );
24          System.out.println( new String( byteArrayOutputStream.toByteArray() ) );
25      }
26  
27      public void testGetDefaultManifestString()
28          throws Exception
29      {
30          Manifest manifest = getManifest( "src/test/resources/manifests/manifestWithClassPath.mf" );
31          Manifest manifestWithout = getManifest( "src/test/resources/manifests/manifest1.mf" );
32          String value = manifest.getMainAttributes().getValue( ManifestConstants.ATTRIBUTE_CLASSPATH );
33          System.out.println( "value = " + value );
34          manifestWithout.getMainAttributes().putValue( ManifestConstants.ATTRIBUTE_CLASSPATH, value );
35          String value2 = manifestWithout.getMainAttributes().getValue( ManifestConstants.ATTRIBUTE_CLASSPATH );
36  
37          assertEquals( value, value2 );
38      }
39  
40      public void testIllegals()
41          throws ManifestException, IOException
42      {
43          Manifest manifest = getManifest( "src/test/resources/manifests/manifest6.mf" );
44          assertNotNull( manifest );
45  
46          try
47          {
48              getManifest( "src/test/resources/manifests/manifest5.mf" );
49              fail( "We expect to fail" );
50          }
51          catch ( IOException ignore )
52          {
53  
54          }
55      }
56  
57      public void testMerge()
58          throws ManifestException, IOException
59      {
60          Manifest manifest1 = getManifest( "src/test/resources/manifests/manifestMerge1.mf" );
61          Manifest manifest2 = getManifest( "src/test/resources/manifests/manifestMerge2.mf" );
62  
63          Manifest target = new Manifest();
64          JdkManifestFactory.merge( target, manifest1, false );
65  
66          assertEquals( "001", target.getMainAttributes().getValue( "Bar" ) );
67          Attributes fudz = target.getAttributes( "Fudz" );
68          assertNotNull( fudz );
69          assertEquals( "002", fudz.getValue( "Bar" ) );
70          Attributes redz = target.getAttributes( "Redz" );
71          assertNotNull( redz );
72          assertEquals( "002", redz.getValue( "Baz" ) );
73  
74          JdkManifestFactory.merge( target, manifest2, false );
75  
76          assertEquals( "001", target.getMainAttributes().getValue( "Bar" ) );
77          fudz = target.getAttributes( "Fudz" );
78          assertNotNull( fudz );
79          assertEquals( "003", fudz.getValue( "Bar" ) );
80          redz = target.getAttributes( "Redz" );
81          assertNotNull( redz );
82          assertEquals( "002", redz.getValue( "Baz" ) );
83      }
84  
85      public void testDualClassPath()
86          throws ManifestException, IOException
87      {
88          Manifest manifest = getManifest( "src/test/resources/manifests/manifestWithDualClassPath.mf" );
89          final Attributes mainAttributes = manifest.getMainAttributes();
90          final String attribute = mainAttributes.getValue( "Class-Path" );
91          // assertEquals( "../config/ classes12.jar baz", attribute );
92      }
93  
94      /**
95       * Reads a Manifest file.
96       */
97      private java.util.jar.Manifest getManifest( String filename )
98          throws IOException, ManifestException
99      {
100         InputStream r = Streams.fileInputStream( getTestFile( filename ) );
101 
102         try
103         {
104             return new Manifest( r );
105         }
106         finally
107         {
108             r.close();
109         }
110     }
111 
112 }