View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  package org.basepom.mojo.duplicatefinder.classpath;
15  
16  import java.util.List;
17  import java.util.Optional;
18  
19  import org.junit.Test;
20  
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertTrue;
23  
24  public class TestClasspathDescriptor {
25  
26      @Test
27      public void testValidIdentifierNames() {
28          String[] validNames = {
29                  "_.class",
30                  "hello/world.class",
31                  "test.class",
32                  "__auto_generated/from/some/tool.class",
33                  "some/inner$thing.class",
34                  "some/package/package-info.class", // package info in sub package
35                  "package-info.class", // package info in root package
36                  "some/package/module-info.class", // module info in sub package
37                  "module-info.class" // module info in root package
38          };
39  
40          for (String test : validNames) {
41              Optional<List<String>> result = ClasspathDescriptor.validateClassName(test);
42              assertTrue("Failure for '" + test + "'", result.isPresent());
43          }
44      }
45  
46      @Test
47      public void testInvalidIdentifierNames() {
48          String[] invalidNames = {
49                  null, // null value
50                  "", // empty string
51                  "/", // only slash
52                  "hello.world", // not a class file
53                  "foo/hello.world", // not a class file in subfolder
54                  "META-INF/test.class", // package name invalid
55                  "2.0/foo.class", // package name invalid
56                  "foo/bar/baz-blo/tst.class", // package name is invalid
57                  "META-INF/versions/9/foo/module-info.class", // module info in version sub package
58                  "META-INF/versions/9/foo/package-info.class" // package info in version sub package
59          };
60  
61  
62          for (String test : invalidNames) {
63              Optional<List<String>> result = ClasspathDescriptor.validateClassName(test);
64              assertFalse("Failure for '" + test + "'", result.isPresent());
65          }
66      }
67  
68      @Test
69      public void testMatchDefaultClassnames() {
70          String[] validClassNames = {
71                  "foo$bar",
72                  "hello.foo$bar",
73                  "package-info",
74                  "module-info",
75                  "demo.package-info",
76                  "foo.module-info"
77          };
78  
79          for (String test : validClassNames) {
80              assertTrue("Failure for '" + test + "'", ClasspathDescriptor.DEFAULT_IGNORED_CLASS_PREDICATE.apply(test));
81          }
82      }
83  
84  }