View Javadoc
1   package org.apache.maven.shared.jar;
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 junit.framework.AssertionFailedError;
23  import org.codehaus.plexus.PlexusTestCase;
24  
25  import java.io.File;
26  import java.io.UnsupportedEncodingException;
27  import java.net.MalformedURLException;
28  import java.net.URLDecoder;
29  import java.util.ArrayList;
30  import java.util.Collection;
31  import java.util.Iterator;
32  import java.util.List;
33  import java.util.regex.Matcher;
34  import java.util.regex.Pattern;
35  
36  /**
37   * Abstract JarAnalyzer TestCase
38   */
39  public abstract class AbstractJarAnalyzerTestCase
40      extends PlexusTestCase
41  {
42      public File getSampleJar( String filename )
43          throws MalformedURLException, UnsupportedEncodingException
44      {
45          String path = getClass().getResource( "/jars/" + filename ).getPath();
46          // URLDecoder.decode necessary for JDK 1.5+, where spaces are escaped to %20
47          return new File( URLDecoder.decode( path, "UTF-8" ) );
48      }
49  
50      public void assertNotContainsRegex( String msg, String regex, Collection coll )
51      {
52          List failures = new ArrayList();
53          Pattern pat = Pattern.compile( regex );
54          Iterator it = coll.iterator();
55          while ( it.hasNext() )
56          {
57              String value = (String) it.next();
58              Matcher mat = pat.matcher( value );
59              if ( mat.find() )
60              {
61                  failures.add( value );
62              }
63          }
64  
65          if ( !failures.isEmpty() )
66          {
67              StringBuffer sb = new StringBuffer();
68              sb.append( msg ).append( " collection has illegal regex \"" ).append( regex ).append( "\"" );
69              it = failures.iterator();
70              while ( it.hasNext() )
71              {
72                  sb.append( "\n   - \"" ).append( it.next() ).append( "\"" );
73              }
74              throw new AssertionFailedError( sb.toString() );
75          }
76      }
77  }