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 junit.framework.TestCase;
20  import org.codehaus.plexus.compiler.util.scan.InclusionScanException;
21  
22  import java.io.File;
23  import java.util.HashSet;
24  import java.util.Set;
25  
26  /**
27   * @author jdcasey
28   */
29  public class SuffixMappingTest
30      extends TestCase
31  {
32      public void testShouldReturnSingleClassFileForSingleJavaFile()
33          throws InclusionScanException
34      {
35          String base = "path/to/file";
36  
37          File basedir = new File( "." );
38  
39          SuffixMapping mapping = new SuffixMapping( ".java", ".class" );
40  
41          Set<File> results = mapping.getTargetFiles( basedir, base + ".java" );
42  
43          assertEquals( "Returned wrong number of target files.", 1, results.size() );
44  
45          assertEquals( "Target file is wrong.", new File( basedir, base + ".class" ), results.iterator().next() );
46      }
47  
48      public void testShouldNotReturnClassFileWhenSourceFileHasWrongSuffix()
49          throws InclusionScanException
50      {
51          String base = "path/to/file";
52  
53          File basedir = new File( "." );
54  
55          SuffixMapping mapping = new SuffixMapping( ".java", ".class" );
56  
57          Set<File> results = mapping.getTargetFiles( basedir, base + ".xml" );
58  
59          assertTrue( "Returned wrong number of target files.", results.isEmpty() );
60      }
61  
62      public void testShouldReturnOneClassFileAndOneXmlFileForSingleJavaFile()
63          throws InclusionScanException
64      {
65          String base = "path/to/file";
66  
67          File basedir = new File( "." );
68  
69          Set<String> targets = new HashSet<String>();
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          assertEquals( "Returned wrong number of target files.", 2, results.size() );
78  
79          assertTrue( "Targets do not contain class target.", results.contains( new File( basedir, base + ".class" ) ) );
80  
81          assertTrue( "Targets do not contain class target.", results.contains( new File( basedir, base + ".xml" ) ) );
82      }
83  
84      public void testShouldReturnNoTargetFilesWhenSourceFileHasWrongSuffix()
85          throws InclusionScanException
86      {
87          String base = "path/to/file";
88  
89          File basedir = new File( "." );
90  
91          Set<String> targets = new HashSet<String>();
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          assertTrue( "Returned wrong number of target files.", results.isEmpty() );
100     }
101 
102     public void testSingleTargetMapper()
103         throws InclusionScanException
104     {
105         String base = "path/to/file";
106 
107         File basedir = new File( "target/" );
108 
109         SingleTargetSourceMapping mapping = new SingleTargetSourceMapping( ".cs", "/foo" );
110 
111         Set<File> results = mapping.getTargetFiles( basedir, base + ".apt" );
112 
113         assertTrue( results.isEmpty() );
114 
115         results = mapping.getTargetFiles( basedir, base + ".cs" );
116 
117         assertEquals( 1, results.size() );
118     }
119 }