View Javadoc
1   /*
2    * Copyright  2001,2004 The Apache Software Foundation
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *
16   */
17  package org.codehaus.plexus.archiver.jar;
18  
19  import java.io.FileInputStream;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.StringWriter;
23  import java.util.jar.Attributes;
24  import org.codehaus.plexus.PlexusTestCase;
25  
26  /**
27   * @author Emmanuel Venisse
28   */
29  public class ManifestTest
30      extends PlexusTestCase
31  {
32  
33      public void testManifest1()
34          throws Exception
35      {
36          Manifest manifest = getManifest( "src/test/resources/manifests/manifest1.mf" );
37          String version = manifest.getManifestVersion();
38          assertEquals( "Manifest was not created with correct version - ", "1.0", version );
39      }
40  
41      public void testManifest2()
42          throws Exception
43      {
44          try
45          {
46              getManifest( "src/test/resources/manifests/manifest2.mf" );
47              fail( "Manifest isn't well formed. It must be generate an exception." );
48          }
49          catch ( IOException ignore )
50          {
51          }
52      }
53  
54      public void testManifest3()
55          throws Exception
56      {
57          try
58          {
59              getManifest( "src/test/resources/manifests/manifest3.mf" );
60              fail( "Manifest isn't well formed. It must be generate an exception." );
61          }
62          catch ( IOException ignore )
63          {
64          }
65      }
66  
67      public void testManifest5()
68          throws Exception
69      {
70          try
71          {
72              getManifest( "src/test/resources/manifests/manifest5.mf" );
73              fail();
74          }
75          catch ( IOException ignore )
76          {
77          }
78      }
79  
80      public void testAddConfiguredSection()
81          throws ManifestException
82      {
83          Manifest manifest = new Manifest();
84          Manifest.Section section = new Manifest.Section();
85          section.setName( "fud" );
86          section.addConfiguredAttribute( new Manifest.Attribute( "bar", "baz" ) );
87          manifest.addConfiguredSection( section );
88          assertEquals( "baz", manifest.getAttributes( "fud" ).getValue( "bar" ) );
89      }
90  
91      public void testAttributeLongLineWrite()
92          throws Exception
93      {
94          StringWriter writer = new StringWriter();
95          Manifest.Attribute attr = new Manifest.Attribute();
96          String longLineOfChars =
97              "123456789 123456789 123456789 123456789 123456789 123456789 123456789 "
98                  + "123456789 123456789 123456789 ";
99          attr.setName( "test" );
100         attr.setValue( longLineOfChars );
101         attr.write( writer );
102         writer.flush();
103         assertEquals( "should be multiline",
104                       "test: 123456789 123456789 123456789 123456789 123456789 123456789 1234"
105                           + Manifest.EOL + " 56789 123456789 123456789 123456789 " + Manifest.EOL,
106                       writer.toString() );
107 
108     }
109 
110     public void testAttributeLongLineWriteNonAscii()
111         throws Exception
112     {
113         StringWriter writer = new StringWriter();
114         Manifest.Attribute attr = new Manifest.Attribute();
115         String longLineOfChars =
116             "Ед докэндё форынчйбюж зкрипторэм векж, льабятюр ыкжпэтэндяз мэль ут, квюо ут модо "
117                 + "либриз такематыш. Ыюм йн лаборамюз компльыктётюр, векж ыпикурэи дежпютатионй ед,"
118                 + " ыам ты хабымуч мальюизчыт. Но вим алёэнюм вюльпутаты, ад нощтыр трётанё льаборэж"
119                 + " вэл, кевёбюж атоморюм кончюлату векж экз. Ку щольыат вёртюты ёнэрмйщ ыюм.";
120 
121         attr.setName( "test" );
122         attr.setValue( longLineOfChars );
123         attr.write( writer );
124         writer.flush();
125         assertEquals( "should be multiline",
126                       "test: Ед докэндё форынчйбюж зкрипторэм в"
127                           + Manifest.EOL + " екж, льабятюр ыкжпэтэндяз мэль ут, квю"
128                           + Manifest.EOL + " о ут модо либриз такематыш. Ыюм йн лаб"
129                           + Manifest.EOL + " орамюз компльыктётюр, векж ыпикурэи д"
130                           + Manifest.EOL + " ежпютатионй ед, ыам ты хабымуч мальюи"
131                           + Manifest.EOL + " зчыт. Но вим алёэнюм вюльпутаты, ад но"
132                           + Manifest.EOL + " щтыр трётанё льаборэж вэл, кевёбюж ат"
133                           + Manifest.EOL + " оморюм кончюлату векж экз. Ку щольыат "
134                           + Manifest.EOL + " вёртюты ёнэрмйщ ыюм."
135                           + Manifest.EOL,
136                       writer.toString() );
137 
138     }
139 
140     public void testDualClassPath()
141         throws ManifestException, IOException
142     {
143         Manifest manifest = getManifest( "src/test/resources/manifests/manifestWithDualClassPath.mf" );
144         final String attribute = manifest.getMainSection().getAttributeValue( "Class-Path" );
145         // According to discussions, we drop support for duplicate class-path attribute
146         assertEquals( "baz", attribute );
147     }
148 
149     public void testAttributeMultiLineValue()
150         throws Exception
151     {
152         checkMultiLineAttribute( "123456789" + Manifest.EOL + "123456789",
153                                  "123456789" + Manifest.EOL + " 123456789" + Manifest.EOL );
154 
155     }
156 
157     public void testAttributeDifferentLineEndings()
158         throws Exception
159     {
160         checkMultiLineAttribute( "\tA\rB\n\t C\r\n \tD\n\r", "\tA" + Manifest.EOL + " B" + Manifest.EOL + " \t C"
161                                                                  + Manifest.EOL + "  \tD" + Manifest.EOL );
162 
163     }
164 
165     public void testAddAttributes()
166         throws ManifestException, IOException
167     {
168         Manifest manifest = getManifest( "src/test/resources/manifests/manifestMerge1.mf" );
169         Manifest.ExistingSection fudz = manifest.getSection( "Fudz" );
170         fudz.addConfiguredAttribute( new Manifest.Attribute( "boz", "bzz" ) );
171         assertEquals( "bzz", fudz.getAttribute( "boz" ).getValue() );
172         assertEquals( "bzz", manifest.getSection( "Fudz" ).getAttributeValue( "boz" ) );
173     }
174 
175     public void testRemoveAttributes()
176         throws ManifestException, IOException
177     {
178         Manifest manifest = getManifest( "src/test/resources/manifests/manifestMerge1.mf" );
179         Manifest.ExistingSection fudz = manifest.getSection( "Fudz" );
180         fudz.addConfiguredAttribute( new Manifest.Attribute( "boz", "bzz" ) );
181         assertEquals( "bzz", fudz.getAttributeValue( "boz" ) );
182         fudz.removeAttribute( "boz" );
183         assertNull( fudz.getAttributeValue( "boz" ) );
184     }
185 
186     public void testAttributeSerialization()
187         throws IOException, ManifestException
188     {
189         Manifest manifest = new Manifest();
190         manifest.getMainAttributes().putValue( "mfa1", "fud1" );
191         manifest.getMainSection().addAttributeAndCheck( new Manifest.Attribute( "mfa2", "fud2" ) );
192         Attributes attributes = new Attributes();
193         attributes.putValue( "attA", "baz" );
194         manifest.getEntries().put( "sub", attributes );
195         manifest.getSection( "sub" ).addAttributeAndCheck( new Manifest.Attribute( "attB", "caB" ) );
196         StringWriter writer = new StringWriter();
197         manifest.write( writer );
198         String s = writer.toString();
199         assertTrue( s.contains( "mfa1: fud1" ) );
200         assertTrue( s.contains( "mfa2: fud2" ) );
201         assertTrue( s.contains( "attA: baz" ) );
202         assertTrue( s.contains( "attB: caB" ) );
203     }
204 
205     public void testDefaultBehaviour()
206     {
207         Manifest manifest = new Manifest();
208         Manifest.ExistingSection mainSection = manifest.getMainSection();
209         assertNotNull( mainSection );
210         String bar = mainSection.getAttributeValue( "Bar" );
211         assertNull( bar );
212         assertNull( manifest.getSection( "Fud" ) );
213     }
214 
215     public void testGetDefaultManifest()
216         throws Exception
217     {
218         java.util.jar.Manifest mf = Manifest.getDefaultManifest();
219         java.util.jar.Attributes mainAttributes = mf.getMainAttributes();
220         assertEquals( 2, mainAttributes.size() );
221         assertTrue( mainAttributes.containsKey( new java.util.jar.Attributes.Name( "Manifest-Version" ) ) );
222         assertTrue( mainAttributes.containsKey( new java.util.jar.Attributes.Name( "Created-By" ) ) );
223     }
224 
225     public void checkMultiLineAttribute( String in, String expected )
226         throws Exception
227     {
228         StringWriter writer = new StringWriter();
229         Manifest.Attribute attr = new Manifest.Attribute();
230         attr.setName( "test" );
231         attr.setValue( in );
232         attr.write( writer );
233         writer.flush();
234 
235         // Print the string with whitespace replaced with special codes
236         // so in case of failure you can see what went wrong.
237         System.err.println( "String: " + dumpString( writer.toString() ) );
238 
239         assertEquals( "should be indented multiline", "test: " + expected, writer.toString() );
240     }
241 
242     private static String dumpString( String in )
243     {
244         String out = "";
245 
246         char[] chars = in.toCharArray();
247 
248         for ( char aChar : chars )
249         {
250             switch ( aChar )
251             {
252                 case '\t':
253                     out += "\\t";
254                     break;
255                 case '\r':
256                     out += "\\r";
257                     break;
258                 case '\n':
259                     out += "\\n";
260                     break;
261                 case ' ':
262                     out += "\\s";
263                     break;
264                 default:
265                     out += aChar;
266                     break;
267             }
268         }
269 
270         return out;
271     }
272 
273     public void testAddAttributesPlexusManifest()
274         throws ManifestException, IOException
275     {
276         Manifest manifest = getManifest( "src/test/resources/manifests/manifestMerge1.mf" );
277         Manifest.ExistingSection fudz = manifest.getSection( "Fudz" );
278         fudz.addConfiguredAttribute( new Manifest.Attribute( "boz", "bzz" ) );
279         assertEquals( "bzz", manifest.getSection( "Fudz" ).getAttributeValue( "boz" ) );
280     }
281 
282     public void testRemoveAttributesPlexusManifest()
283         throws ManifestException, IOException
284     {
285         Manifest manifest = getManifest( "src/test/resources/manifests/manifestMerge1.mf" );
286         Manifest.ExistingSection fudz = manifest.getSection( "Fudz" );
287         fudz.addConfiguredAttribute( new Manifest.Attribute( "boz", "bzz" ) );
288         assertEquals( "bzz", fudz.getAttributeValue( "boz" ) );
289         fudz.removeAttribute( "boz" );
290         assertNull( fudz.getAttributeValue( "boz" ) );
291     }
292 
293     public void testAttributeSerializationPlexusManifest()
294         throws IOException, ManifestException
295     {
296         Manifest manifest = new Manifest();
297         manifest.getMainSection().addConfiguredAttribute( new Manifest.Attribute( "mfa1", "fud1" ) );
298         manifest.getMainSection().addConfiguredAttribute( new Manifest.Attribute( "mfa2", "fud2" ) );
299         Manifest.Section attributes = new Manifest.Section();
300         attributes.setName( "TestSection" );
301         attributes.addConfiguredAttribute( new Manifest.Attribute( "attA", "baz" ) );
302         attributes.addConfiguredAttribute( new Manifest.Attribute( "attB", "caB" ) );
303         manifest.addConfiguredSection( attributes );
304         StringWriter writer = new StringWriter();
305         manifest.write( writer );
306         String s = writer.toString();
307         assertTrue( s.contains( "mfa1: fud1" ) );
308         assertTrue( s.contains( "mfa2: fud2" ) );
309         assertTrue( s.contains( "attA: baz" ) );
310         assertTrue( s.contains( "attB: caB" ) );
311     }
312 
313     public void testClassPathPlexusManifest()
314         throws ManifestException
315     {
316         Manifest manifest = new Manifest();
317         manifest.addConfiguredAttribute( new Manifest.Attribute( ManifestConstants.ATTRIBUTE_CLASSPATH, "fud" ) );
318         manifest.addConfiguredAttribute( new Manifest.Attribute( ManifestConstants.ATTRIBUTE_CLASSPATH, "duf" ) );
319         assertEquals( "fud duf", manifest.getMainSection().getAttributeValue( ManifestConstants.ATTRIBUTE_CLASSPATH ) );
320     }
321 
322     public void testAddConfiguredSectionPlexusManifest()
323         throws ManifestException
324     {
325         Manifest manifest = new Manifest();
326         Manifest.Section section = new Manifest.Section();
327         section.setName( "fud" );
328         section.addConfiguredAttribute( new Manifest.Attribute( "bar", "baz" ) );
329         manifest.addConfiguredSection( section );
330         assertEquals( "baz", manifest.getSection( "fud" ).getAttributeValue( "bar" ) );
331     }
332 
333     /**
334      * Reads a Manifest file.
335      *
336      * @param filename the file
337      *
338      * @return a manifest
339      *
340      * @throws java.io.IOException .
341      * @throws ManifestException .
342      */
343     private Manifest getManifest( String filename )
344         throws IOException, ManifestException
345     {
346         InputStream is = new FileInputStream( getTestFile( filename ) );
347 
348         try
349         {
350             return new Manifest( is );
351         }
352         finally
353         {
354             is.close();
355         }
356     }
357 
358 }