View Javadoc
1   package org.apache.maven.shared.dependency.analyzer;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.ByteArrayOutputStream;
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.io.FileOutputStream;
26  import java.io.IOException;
27  import java.util.HashSet;
28  import java.util.Set;
29  import java.util.jar.JarOutputStream;
30  import java.util.zip.ZipException;
31  
32  import org.codehaus.plexus.util.IOUtil;
33  
34  /**
35   * Tests <code>DefaultClassAnalyzer</code>.
36   *
37   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
38   * @version $Id$
39   * @see DefaultClassAnalyzer
40   */
41  public class DefaultClassAnalyzerTest
42      extends AbstractFileTest
43  {
44      // tests ------------------------------------------------------------------
45  
46      public void testAnalyzeWithJar()
47          throws IOException
48      {
49          File file = createJar();
50          JarOutputStream out = new JarOutputStream( new FileOutputStream( file ) );
51          writeEntry( out, "a/b/c.class", "class a.b.c" );
52          writeEntry( out, "x/y/z.class", "class x.y.z" );
53          out.close();
54  
55          Set<String> expectedClasses = new HashSet<String>();
56          expectedClasses.add( "a.b.c" );
57          expectedClasses.add( "x.y.z" );
58  
59          DefaultClassAnalyzer analyzer = new DefaultClassAnalyzer();
60          Set<String> actualClasses = analyzer.analyze( file.toURI().toURL() );
61  
62          assertEquals( expectedClasses, actualClasses );
63      }
64  
65      public void testAnalyzeBadJar()
66          throws IOException
67      {
68          //to reproduce MDEP-143
69          File file = createJar();
70          JarOutputStream out = new JarOutputStream( new FileOutputStream( file ) );
71          writeEntry( out, "a/b/c.class", "class a.b.c" );
72          writeEntry( out, "x/y/z.class", "class x.y.z" );
73          out.close();
74  
75          //corrupt the jar file by alter its contents
76          FileInputStream fis = new FileInputStream( file );
77          ByteArrayOutputStream baos = new ByteArrayOutputStream( 100 );
78          IOUtil.copy( fis, baos, 100 );
79          fis.close();
80          byte [] ba = baos.toByteArray();
81          ba[50] = 1;
82          FileOutputStream fos = new FileOutputStream( file );
83          IOUtil.copy( ba, fos );
84          fos.close();
85  
86          DefaultClassAnalyzer analyzer = new DefaultClassAnalyzer();
87  
88          try
89          {
90              analyzer.analyze( file.toURI().toURL() );
91              fail( "Exception expected" );
92          }
93          catch ( ZipException e )
94          {
95              assertTrue( e.getMessage().startsWith( "Cannot process Jar entry on URL:" ) );
96          }
97  
98      }
99  }