1 package org.codehaus.plexus.compiler.util.scan.mapping;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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 }