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.io.IOException;
20  import java.io.InputStream;
21  import java.net.URL;
22  
23  import org.codehaus.plexus.PlexusTestCase;
24  import org.codehaus.plexus.components.io.fileselectors.AllFilesFileSelector;
25  import org.codehaus.plexus.components.io.fileselectors.FileSelector;
26  import org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector;
27  import org.codehaus.plexus.components.io.resources.AbstractPlexusIoResource;
28  
29  import javax.annotation.Nonnull;
30  
31  
32  /**
33   * Test case for implementations of {@link FileSelector}.
34   */
35  public class FileSelectorTest extends PlexusTestCase
36  {
37      protected void testFileSelector( FileSelector pSelector, String[] pInput, boolean[] pOutput)
38          throws IOException
39      {
40          for ( int i = 0; i < pInput.length; i++ )
41          {
42              final String name = pInput[i];
43              AbstractPlexusIoResource resource = new AbstractPlexusIoResource( name, 0, 0, true, false, true )
44              {
45                  @Nonnull
46                  public InputStream getContents() throws IOException
47                  {
48                      throw new IllegalStateException( "Not implemented" );
49                  }
50  
51                  public URL getURL() throws IOException
52                  {
53                      throw new IllegalStateException( "Not implemented" );
54                  }
55              };
56              boolean result = pSelector.isSelected( resource );
57              if ( result != pOutput[i] )
58              {
59                  fail( "Test fails for selector " + pSelector.getClass().getName()
60                        + " and input " + name + ": Expected "
61                        + pOutput[i] );
62              }
63          }
64      }
65  
66      protected static final String[] SAMPLES =
67          new String[]
68          {
69              "foo/x.gif",
70              "foo/y.png",
71              "bar/x.gif"
72          };
73  
74      protected void testFileSelector( AllFilesFileSelector pSelector ) throws Exception
75      {
76          final boolean[] trues = getAllTrues();
77          testFileSelector( pSelector, SAMPLES, trues );
78      }
79  
80      private boolean[] getAllTrues()
81      {
82          final boolean[] trues = new boolean[SAMPLES.length];
83          for ( int i = 0; i < trues.length; i++ )
84          {
85              trues[i] = true;
86          }
87          return trues;
88      }
89  
90      public void testAllFilesFileSelector() throws Exception
91      {
92          testFileSelector( new AllFilesFileSelector() );
93          testFileSelector( (AllFilesFileSelector) lookup( FileSelector.ROLE, FileSelector.DEFAULT_ROLE_HINT ) );
94          testFileSelector( (AllFilesFileSelector) lookup( FileSelector.ROLE, AllFilesFileSelector.ROLE_HINT ) );
95      }
96  
97      protected boolean[] getIncludeGifs( String[] pSamples )
98      {
99          boolean[] result = new boolean[pSamples.length];
100         for ( int i = 0; i < pSamples.length; i++ )
101         {
102             result[i] = pSamples[i].endsWith( ".gif" );
103         }
104         return result;
105     }
106 
107     protected boolean[] getExcludeBar( String[] pSamples, boolean[] pResult )
108     {
109         for ( int i = 0; i < pSamples.length; i++ )
110         {
111             if ( pSamples[i].startsWith( "bar/" ) )
112             {
113                 pResult[i] = false;
114             }
115         }
116         return pResult;
117     }
118     
119     protected void testFileSelector( IncludeExcludeFileSelector pSelector ) throws Exception
120     {
121         testFileSelector( pSelector, SAMPLES, getAllTrues() );
122         pSelector.setIncludes( new String[] { "**/*.gif" } );
123         testFileSelector( pSelector, SAMPLES, getIncludeGifs( SAMPLES ) );
124         pSelector.setExcludes( new String[] { "bar/*" } );
125         testFileSelector( pSelector, SAMPLES, getExcludeBar( SAMPLES, getIncludeGifs( SAMPLES ) ) );
126         pSelector.setIncludes( null );
127         testFileSelector( pSelector, SAMPLES, getExcludeBar( SAMPLES, getAllTrues() ) );
128     }
129 
130     public void testIncludeExcludeFileSelector() throws Exception
131     {
132         testFileSelector( new IncludeExcludeFileSelector() );
133         testFileSelector( (IncludeExcludeFileSelector) lookup( FileSelector.ROLE,
134                                                                IncludeExcludeFileSelector.ROLE_HINT ) );
135     }
136     
137     public void testIncludeExcludeFileSelector_SetExcludes() throws Exception
138     {
139         IncludeExcludeFileSelector selector = new IncludeExcludeFileSelector();
140 
141         // Test that the setExcludes method does not modify the excludes.
142         selector.setExcludes( SAMPLES );
143         String[] sltrExcludes = selector.getExcludes();
144         assertEquals( SAMPLES.length, sltrExcludes.length );
145         for ( int i = 0; i < sltrExcludes.length; ++i )
146         {
147             assertEquals( SAMPLES[i], sltrExcludes[i] );
148         }
149         
150     }
151 }