View Javadoc
1   package org.apache.maven.shared.io.scan.mapping;
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 static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.io.File;
26  import java.util.HashSet;
27  import java.util.Set;
28  
29  import org.apache.maven.shared.io.scan.InclusionScanException;
30  import org.junit.Test;
31  
32  /**
33   * @author jdcasey
34   */
35  public class SuffixMappingTest
36  {
37      @Test
38      public void testShouldReturnSingleClassFileForSingleJavaFile()
39          throws InclusionScanException
40      {
41          String base = "path/to/file";
42  
43          File basedir = new File( "." );
44  
45          System.out.println( "basedir:" + basedir.getAbsolutePath() );
46          SuffixMapping mapping = new SuffixMapping( ".java", ".class" );
47  
48          Set<File> results = mapping.getTargetFiles( basedir, base + ".java" );
49  
50          assertEquals( "Returned wrong number of target files.", 1, results.size() );
51  
52          assertEquals( "Target file is wrong.", new File( basedir, base + ".class" ), results.iterator().next() );
53      }
54  
55      @Test
56      public void testShouldNotReturnClassFileWhenSourceFileHasWrongSuffix()
57          throws InclusionScanException
58      {
59          String base = "path/to/file";
60  
61          File basedir = new File( "." );
62  
63          SuffixMapping mapping = new SuffixMapping( ".java", ".class" );
64  
65          Set<File> results = mapping.getTargetFiles( basedir, base + ".xml" );
66  
67          assertTrue( "Returned wrong number of target files.", results.isEmpty() );
68      }
69  
70      @Test
71      public void testShouldReturnOneClassFileAndOneXmlFileForSingleJavaFile()
72          throws InclusionScanException
73      {
74          String base = "path/to/file";
75  
76          File basedir = new File( "." );
77  
78          Set<String> targets = new HashSet<String>();
79          targets.add( ".class" );
80          targets.add( ".xml" );
81  
82          SuffixMapping mapping = new SuffixMapping( ".java", targets );
83  
84          Set<File> results = mapping.getTargetFiles( basedir, base + ".java" );
85  
86          assertEquals( "Returned wrong number of target files.", 2, results.size() );
87  
88          assertTrue( "Targets do not contain class target.", results.contains( new File( basedir, base + ".class" ) ) );
89  
90          assertTrue( "Targets do not contain class target.", results.contains( new File( basedir, base + ".xml" ) ) );
91      }
92  
93      @Test
94      public void testShouldReturnNoTargetFilesWhenSourceFileHasWrongSuffix()
95          throws InclusionScanException
96      {
97          String base = "path/to/file";
98  
99          File basedir = new File( "." );
100 
101         Set<String> targets = new HashSet<String>();
102         targets.add( ".class" );
103         targets.add( ".xml" );
104 
105         SuffixMapping mapping = new SuffixMapping( ".java", targets );
106 
107         Set<File> results = mapping.getTargetFiles( basedir, base + ".apt" );
108 
109         assertTrue( "Returned wrong number of target files.", results.isEmpty() );
110     }
111 
112     @Test
113     public void testSingleTargetMapper()
114         throws InclusionScanException
115     {
116         String base = "path/to/file";
117 
118         File basedir = new File( "target/" );
119 
120         SingleTargetMapping mapping = new SingleTargetMapping( ".cs", "/foo" );
121 
122         Set<File> results = mapping.getTargetFiles( basedir, base + ".apt" );
123 
124         assertTrue( results.isEmpty() );
125 
126         results = mapping.getTargetFiles( basedir, base + ".cs" );
127 
128         assertEquals( 1, results.size() );
129     }
130 }