View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.jar;
20  
21  import java.io.File;
22  import java.io.UnsupportedEncodingException;
23  import java.net.URLDecoder;
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.List;
27  import java.util.regex.Matcher;
28  import java.util.regex.Pattern;
29  
30  import org.opentest4j.AssertionFailedError;
31  
32  import static java.nio.charset.StandardCharsets.UTF_8;
33  
34  /**
35   * Abstract JarAnalyzer TestCase
36   */
37  public abstract class AbstractJarAnalyzerTestCase {
38  
39      protected File getSampleJar(String filename) throws UnsupportedEncodingException {
40          String path = getClass().getResource("/jars/" + filename).getPath();
41          // URLDecoder.decode necessary for JDK 1.5+, where spaces are escaped to %20
42          return new File(URLDecoder.decode(path, UTF_8.name()));
43      }
44  
45      protected void assertNotContainsRegex(String msg, String regex, Collection<String> coll) {
46          List<String> failures = new ArrayList<>();
47          Pattern pat = Pattern.compile(regex);
48          for (String value : coll) {
49              Matcher mat = pat.matcher(value);
50              if (mat.find()) {
51                  failures.add(value);
52              }
53          }
54  
55          if (!failures.isEmpty()) {
56              StringBuilder sb = new StringBuilder();
57              sb.append(msg)
58                      .append(" collection has illegal regex \"")
59                      .append(regex)
60                      .append("\"");
61              for (String failure : failures) {
62                  sb.append("\n   - \"").append(failure).append("\"");
63              }
64              throw new AssertionFailedError(sb.toString());
65          }
66      }
67  }