1 package org.apache.maven.shared.dependency.analyzer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
36
37
38
39
40
41 public class DefaultClassAnalyzerTest
42 extends AbstractFileTest
43 {
44
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
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
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 }