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