View Javadoc
1   package org.codehaus.plexus.components.io.filemappers;
2   
3   /*
4    * Copyright 2007 The Codehaus 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.lang.reflect.UndeclaredThrowableException;
20  
21  import org.codehaus.plexus.PlexusTestCase;
22  
23  /**
24   * Test case for the various file mappers.
25   */
26  public class FileMapperTest extends PlexusTestCase
27  {
28      protected void testFileMapper( FileMapper pMapper, String[] pInput, String[] pOutput )
29      {
30          for ( int i = 0; i < pInput.length; i++ )
31          {
32              final String input = pInput[i];
33              final String output = pOutput[i];
34              final String result;
35              try
36              {
37                  if ( output == null )
38                  {
39                      try
40                      {
41                          pMapper.getMappedFileName( input );
42                          fail( "Expected IllegalArgumentException for mapper " + pMapper.getClass().getName()
43                                          + " and input: " + input );
44                      }
45                      catch ( IllegalArgumentException e )
46                      {
47                          // Ok
48                      }
49                      continue;
50                  }
51                  result = pMapper.getMappedFileName( input );
52                  if ( output.equals( result ) )
53                  {
54                      continue;
55                  }
56              }
57              catch ( Throwable t )
58              {
59                  throw new UndeclaredThrowableException( t, "Mapper " + pMapper.getClass().getName()
60                                  + " failed for input " + input + ": " + t.getMessage() );
61              }
62              if ( !output.equals( result ) )
63              {
64                  fail( "Mapper " + pMapper.getClass().getName() + " failed for input nr. " + i
65                  		+ ", " + input + ": Expected " + output
66                          + ", got " + result );
67              }
68          }
69      }
70  
71      protected static final String[] SAMPLES =
72          new String[] { null, "", "a", "xyz.gif", "b/a", "b/xyz.gif", "b\\a", "b\\xyz.gif", "c.c/a", "c.c/xyz.gif",
73              "c.c\\a", "c.c\\xyz.gif" };
74  
75      public void testIdentityMapper() throws Exception
76      {
77          final String[] results = getIdentityResults();
78          testFileMapper( new IdentityMapper(), SAMPLES, results );
79      }
80  
81      private String[] getIdentityResults()
82      {
83          final String[] results = new String[SAMPLES.length];
84          System.arraycopy( SAMPLES, 0, results, 0, SAMPLES.length );
85          results[1] = null;
86          return results;
87      }
88  
89      public void testDefaultMapper() throws Exception
90      {
91          final String[] results = getIdentityResults();
92          testFileMapper( (FileMapper) lookup( FileMapper.ROLE ), SAMPLES, results );
93          testFileMapper( (FileMapper) lookup( FileMapper.ROLE, IdentityMapper.ROLE_HINT ), SAMPLES, results );
94          testFileMapper( (FileMapper) lookup( FileMapper.ROLE, FileMapper.DEFAULT_ROLE_HINT ), SAMPLES, results );
95      }
96  
97      public void testFileExtensionMapper() throws Exception
98      {
99          final String[] results = getIdentityResults();
100         for ( int i = 2; i <= 10; i += 2 )
101         {
102             results[i] += ".png";
103         }
104         for ( int i = 3; i <= 11; i += 2 )
105         {
106             results[i] = results[i].substring( 0, results[i].length() - ".gif".length() ) + ".png";
107         }
108         testFileExtensionMapper( results, new FileExtensionMapper() );
109         testFileExtensionMapper( results, (FileExtensionMapper) lookup( FileMapper.ROLE, FileExtensionMapper.ROLE_HINT ) );
110     }
111 
112     private void testFileExtensionMapper( final String[] results, final FileExtensionMapper mapper )
113     {
114         mapper.setTargetExtension( "png" );
115         testFileMapper( mapper, SAMPLES, results );
116         mapper.setTargetExtension( ".png" );
117         testFileMapper( mapper, SAMPLES, results );
118     }
119 
120     public void testFlattenMapper() throws Exception
121     {
122         final String[] results = getIdentityResults();
123         results[4] = results[6] = results[8] = results[10] = results[2];
124         results[5] = results[7] = results[9] = results[11] = results[3];
125         testFileMapper( new FlattenFileMapper(), SAMPLES, results );
126         testFileMapper( (FileMapper) lookup( FileMapper.ROLE, FlattenFileMapper.ROLE_HINT ), SAMPLES, results );
127     }
128 
129     private void testMergeMapper( String pTargetName, String[] pResults, MergeFileMapper pMapper )
130     {
131         pMapper.setTargetName( pTargetName );
132         testFileMapper( pMapper, SAMPLES, pResults );
133     }
134 
135     public void testMergeMapper() throws Exception
136     {
137         final String[] results = getIdentityResults();
138         final String targetName = "zgh";
139         for ( int i = 2; i < results.length; i++ )
140         {
141             results[i] = targetName;
142         }
143         testMergeMapper( targetName, results, new MergeFileMapper() );
144         testMergeMapper( targetName, results, (MergeFileMapper) lookup( FileMapper.ROLE, MergeFileMapper.ROLE_HINT ) );
145     }
146 
147     public void testPrefixMapper() throws Exception
148     {
149         final String prefix = "x7Rtf";
150         final String[] results = getIdentityResults();
151         testFileMapper( new PrefixFileMapper(), SAMPLES, results );
152         testFileMapper( (PrefixFileMapper) lookup( FileMapper.ROLE, PrefixFileMapper.ROLE_HINT ), SAMPLES, results );
153         for ( int i = 0; i < results.length; i++ )
154         {
155             if ( results[i] != null )
156             {
157                 results[i] = prefix + results[i];
158             }
159         }
160         PrefixFileMapper mapper = new PrefixFileMapper();
161         mapper.setPrefix( prefix );
162         testFileMapper( mapper, SAMPLES, results );
163         mapper = (PrefixFileMapper) lookup( FileMapper.ROLE, PrefixFileMapper.ROLE_HINT );
164         mapper.setPrefix( prefix );
165         testFileMapper( mapper, SAMPLES, results );
166     }
167 
168     private RegExpFileMapper configure( RegExpFileMapper pMapper, String pPattern, String pReplacement )
169     {
170         pMapper.setPattern( pPattern );
171         pMapper.setReplacement( pReplacement );
172         return pMapper;
173     }
174 
175     public void testRegExpFileMapper() throws Exception
176     {
177         final String[] results = getIdentityResults();
178         results[3] = "xyz.jpg";
179         results[5] = "b/xyz.jpg";
180         results[7] = "b\\xyz.jpg";
181         results[9] = "c.c/xyz.jpg";
182         results[11] = "c.c\\xyz.jpg";
183         testFileMapper( configure(new RegExpFileMapper(), "\\.gif$", ".jpg"), SAMPLES, results );
184         testFileMapper( configure(new RegExpFileMapper(), "^(.*)\\.gif$", "$1.jpg"), SAMPLES, results );
185         testFileMapper( configure((RegExpFileMapper) lookup( FileMapper.ROLE, RegExpFileMapper.ROLE_HINT ), "\\.gif$", ".jpg"), SAMPLES, results );
186         final RegExpFileMapper mapper = configure( new RegExpFileMapper(), "c", "f" );
187         mapper.setReplaceAll( true );
188         final String[] fResults = getIdentityResults();
189         fResults[8] = "f.f/a";
190         fResults[9] = "f.f/xyz.gif";
191         fResults[10] = "f.f\\a";
192         fResults[11] = "f.f\\xyz.gif";
193         testFileMapper( mapper, SAMPLES, fResults );
194     }
195 }