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.File;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.net.URL;
26 import java.util.jar.JarOutputStream;
27
28 import org.codehaus.plexus.util.FileUtils;
29 import org.jmock.Mock;
30
31
32
33
34
35
36
37
38 public class ClassFileVisitorUtilsTest
39 extends AbstractFileTest
40 {
41
42
43 public void testAcceptJar()
44 throws IOException
45 {
46 File file = createJar();
47 try ( JarOutputStream out = new JarOutputStream( new FileOutputStream( file ) ) )
48 {
49 writeEntry( out, "a/b/c.class", "class a.b.c" );
50 writeEntry( out, "x/y/z.class", "class x.y.z" );
51 }
52
53 Mock mock = mock( ClassFileVisitor.class );
54 expectVisitClass( mock, "a.b.c", "class a.b.c" );
55 expectVisitClass( mock, "x.y.z", "class x.y.z" );
56
57 ClassFileVisitorUtils.accept( file.toURI().toURL(), (ClassFileVisitor) mock.proxy() );
58
59 mock.verify();
60 }
61
62 public void testAcceptJarWithNonClassEntry()
63 throws IOException
64 {
65 File file = createJar();
66 try ( JarOutputStream out = new JarOutputStream( new FileOutputStream( file ) ) )
67 {
68 writeEntry( out, "a/b/c.jpg", "jpeg a.b.c" );
69 }
70
71 Mock mock = mock( ClassFileVisitor.class );
72
73 ClassFileVisitorUtils.accept( file.toURI().toURL(), (ClassFileVisitor) mock.proxy() );
74
75 mock.verify();
76 }
77
78 public void testAcceptDir()
79 throws IOException
80 {
81 File dir = createDir();
82
83 File abDir = mkdirs( dir, "a/b" );
84 createFile( abDir, "c.class", "class a.b.c" );
85
86 File xyDir = mkdirs( dir, "x/y" );
87 createFile( xyDir, "z.class", "class x.y.z" );
88
89 Mock mock = mock( ClassFileVisitor.class );
90 expectVisitClass( mock, "a.b.c", "class a.b.c" );
91 expectVisitClass( mock, "x.y.z", "class x.y.z" );
92
93 ClassFileVisitorUtils.accept( dir.toURI().toURL(), (ClassFileVisitor) mock.proxy() );
94
95 FileUtils.deleteDirectory( dir );
96
97 mock.verify();
98 }
99
100 public void testAcceptDirWithNonClassFile()
101 throws IOException
102 {
103 File dir = createDir();
104
105 File abDir = mkdirs( dir, "a/b" );
106 createFile( abDir, "c.jpg", "jpeg a.b.c" );
107
108 Mock mock = mock( ClassFileVisitor.class );
109
110 ClassFileVisitorUtils.accept( dir.toURI().toURL(), (ClassFileVisitor) mock.proxy() );
111
112 FileUtils.deleteDirectory( dir );
113
114 mock.verify();
115 }
116
117 public void testAcceptWithFile()
118 throws IOException
119 {
120 File file = File.createTempFile( "test", ".class" );
121 file.deleteOnExit();
122
123 Mock mock = mock( ClassFileVisitor.class );
124
125 URL url = file.toURI().toURL();
126
127 try
128 {
129 ClassFileVisitorUtils.accept( url, (ClassFileVisitor) mock.proxy() );
130 fail("expected IllegalArgumntException");
131 }
132 catch ( IllegalArgumentException exception )
133 {
134 assertEquals( "Cannot accept visitor on URL: " + url, exception.getMessage() );
135 }
136 }
137
138 public void testAcceptWithUnsupportedScheme()
139 throws IOException
140 {
141 Mock mock = mock( ClassFileVisitor.class );
142
143 URL url = new URL( "http://localhost/" );
144
145 try
146 {
147 ClassFileVisitorUtils.accept( url, (ClassFileVisitor) mock.proxy() );
148 fail("expected IllegalArgumntException");
149 }
150 catch ( IllegalArgumentException exception )
151 {
152 assertEquals( "Cannot accept visitor on URL: " + url, exception.getMessage() );
153 }
154 }
155
156
157
158 private void expectVisitClass( Mock mock, String className, String data )
159 {
160 mock.expects( atLeastOnce() ).method( "visitClass" ).with( eq( className ), in( data ) );
161 }
162
163 private InputStreamConstraint in( String expected )
164 {
165 return new InputStreamConstraint( expected );
166 }
167 }