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 private File file;
45
46 public void setUp() throws IOException {
47 file = createJar();
48 try ( JarOutputStream out = new JarOutputStream( new FileOutputStream( file ) ) )
49 {
50 writeEntry( out, "a/b/c.class", "class a.b.c" );
51 writeEntry( out, "x/y/z.class", "class x.y.z" );
52 }
53 }
54
55
56 public void testAnalyzeWithJar()
57 throws IOException
58 {
59 Set<String> expectedClasses = new HashSet<String>();
60 expectedClasses.add( "a.b.c" );
61 expectedClasses.add( "x.y.z" );
62
63 DefaultClassAnalyzer analyzer = new DefaultClassAnalyzer();
64 Set<String> actualClasses = analyzer.analyze( file.toURI().toURL() );
65
66 assertEquals( expectedClasses, actualClasses );
67 }
68
69 public void testAnalyzeBadJar()
70 throws IOException
71 {
72
73
74 FileInputStream fis = new FileInputStream( file );
75 ByteArrayOutputStream baos = new ByteArrayOutputStream( 100 );
76 IOUtil.copy( fis, baos, 100 );
77 fis.close();
78 byte [] ba = baos.toByteArray();
79 ba[50] = 1;
80 FileOutputStream fos = new FileOutputStream( file );
81 IOUtil.copy( ba, fos );
82 fos.close();
83
84 DefaultClassAnalyzer analyzer = new DefaultClassAnalyzer();
85
86 try
87 {
88 analyzer.analyze( file.toURI().toURL() );
89 fail( "Exception expected" );
90 }
91 catch ( ZipException e )
92 {
93 assertTrue( e.getMessage().startsWith( "Cannot process Jar entry on URL:" ) );
94 }
95
96 }
97 }