View Javadoc
1   package org.codehaus.plexus.compiler.util.scan.mapping;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.File;
20  import java.util.HashSet;
21  import java.util.Set;
22  
23  import org.junit.jupiter.api.Test;
24  
25  import static org.hamcrest.MatcherAssert.assertThat;
26  import static org.hamcrest.Matchers.containsInAnyOrder;
27  import static org.hamcrest.Matchers.empty;
28  import static org.hamcrest.Matchers.is;
29  
30  /**
31   * @author jdcasey
32   */
33  public class SuffixMappingTest {
34  
35      @Test
36      public void testShouldReturnSingleClassFileForSingleJavaFile() {
37          String base = "path/to/file";
38  
39          File basedir = new File(".");
40  
41          SuffixMapping mapping = new SuffixMapping(".java", ".class");
42  
43          Set<File> results = mapping.getTargetFiles(basedir, base + ".java");
44  
45          assertThat("Returned wrong number of target files.", results.size(), is(1));
46  
47          assertThat("Target file is wrong.", results.iterator().next(), is(new File(basedir, base + ".class")));
48      }
49  
50      @Test
51      public void testShouldNotReturnClassFileWhenSourceFileHasWrongSuffix() {
52          String base = "path/to/file";
53  
54          File basedir = new File(".");
55  
56          SuffixMapping mapping = new SuffixMapping(".java", ".class");
57  
58          Set<File> results = mapping.getTargetFiles(basedir, base + ".xml");
59  
60          assertThat("Returned wrong number of target files.", results, empty());
61      }
62  
63      @Test
64      public void testShouldReturnOneClassFileAndOneXmlFileForSingleJavaFile() {
65          String base = "path/to/file";
66  
67          File basedir = new File(".");
68  
69          Set<String> targets = new HashSet<>();
70          targets.add(".class");
71          targets.add(".xml");
72  
73          SuffixMapping mapping = new SuffixMapping(".java", targets);
74  
75          Set<File> results = mapping.getTargetFiles(basedir, base + ".java");
76  
77          assertThat("Returned wrong number of target files.", results.size(), is(2));
78  
79          assertThat(
80                  "Targets do not contain class target.",
81                  results,
82                  containsInAnyOrder(new File(basedir, base + ".class"), new File(basedir, base + ".xml")));
83      }
84  
85      @Test
86      public void testShouldReturnNoTargetFilesWhenSourceFileHasWrongSuffix() {
87          String base = "path/to/file";
88  
89          File basedir = new File(".");
90  
91          Set<String> targets = new HashSet<>();
92          targets.add(".class");
93          targets.add(".xml");
94  
95          SuffixMapping mapping = new SuffixMapping(".java", targets);
96  
97          Set<File> results = mapping.getTargetFiles(basedir, base + ".apt");
98  
99          assertThat("Returned wrong number of target files.", results, empty());
100     }
101 
102     @Test
103     public void testSingleTargetMapper() throws Exception {
104         String base = "path/to/file";
105 
106         File basedir = new File("target/");
107 
108         SingleTargetSourceMapping mapping = new SingleTargetSourceMapping(".cs", "/foo");
109 
110         Set<File> results = mapping.getTargetFiles(basedir, base + ".apt");
111 
112         assertThat(results, empty());
113 
114         results = mapping.getTargetFiles(basedir, base + ".cs");
115 
116         assertThat(results.size(), is(1));
117     }
118 }