View Javadoc
1   package org.codehaus.plexus.components.io.resources.proxy;
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 org.codehaus.plexus.PlexusTestCase;
20  import org.codehaus.plexus.components.io.resources.AbstractPlexusIoResource;
21  import org.codehaus.plexus.components.io.resources.AbstractPlexusIoResourceCollection;
22  import org.codehaus.plexus.components.io.resources.PlexusIoResource;
23  import org.codehaus.plexus.components.io.resources.Stream;
24  
25  import javax.annotation.Nonnull;
26  import java.io.Closeable;
27  import java.io.IOException;
28  import java.io.InputStream;
29  import java.net.URL;
30  import java.util.Iterator;
31  
32  
33  /**
34   * Test case for {@link PlexusIoProxyResourceCollection}.
35   */
36  public class PlexusIoProxyResourceCollectionTest
37      extends PlexusTestCase
38  {
39      private final String[] SAMPLE_INCLUDES = { "junk.*", "test/**", "dir*/file.xml" };
40  
41      private final String[] SAMPLE_EXCLUDES = { "*.junk", "somwhere/**" };
42  
43      public void testGetDefaultFileSelector()
44          throws Exception
45      {
46          PlexusIoProxyResourceCollection resCol = new PlexusIoProxyResourceCollection( null );
47  
48          // This will throw an exception if there is a bug
49          resCol.getDefaultFileSelector();
50  
51          resCol.setIncludes( SAMPLE_INCLUDES );
52          resCol.setExcludes( SAMPLE_EXCLUDES );
53  
54          // This will throw an exception if there is a bug
55          resCol.getDefaultFileSelector();
56  
57      }
58  
59      class CloseableIterator
60          implements Iterator<PlexusIoResource>, Closeable
61      {
62          boolean next = true;
63  
64          boolean closed = false;
65  
66          public void close()
67              throws IOException
68          {
69              closed = true;
70          }
71  
72          public boolean hasNext()
73          {
74              if ( next )
75              {
76                  next = false;
77                  return true;
78              }
79              return false;
80          }
81  
82          public PlexusIoResource next()
83          {
84              return new AbstractPlexusIoResource( "fud", 123, 22, true, false, false )
85              {
86                  @Nonnull
87                  public InputStream getContents()
88                      throws IOException
89                  {
90                      return null;
91                  }
92  
93                  public URL getURL()
94                      throws IOException
95                  {
96                      return null;
97                  }
98              };
99          }
100 
101         public void remove()
102         {
103             throw new UnsupportedOperationException();
104         }
105     }
106     
107     public void testClosing()
108         throws IOException
109     {
110         final CloseableIterator closeableIterator = new CloseableIterator();
111         PlexusIoProxyResourceCollection resCol =
112             new PlexusIoProxyResourceCollection( new AbstractPlexusIoResourceCollection()
113             {
114                 public Iterator<PlexusIoResource> getResources()
115                     throws IOException
116                 {
117                     return closeableIterator;
118                 }
119 
120                 public Stream stream()
121                 {
122                     throw new UnsupportedOperationException();
123                 }
124 
125                 public boolean isConcurrentAccessSupported()
126                 {
127                     return true;
128                 }
129             } );
130         Iterator<PlexusIoResource> resources1 = resCol.getResources();
131         resources1.hasNext();
132         resources1.next();
133         assertFalse( resources1.hasNext() );
134         ( (Closeable) resources1 ).close();
135         assertTrue( closeableIterator.closed );
136     }
137 }