View Javadoc
1   package org.codehaus.plexus.components.io.resources;
2   
3   import junit.framework.TestCase;
4   import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
5   
6   import javax.annotation.Nonnull;
7   import java.io.ByteArrayInputStream;
8   import java.io.IOException;
9   import java.io.InputStream;
10  import java.net.URL;
11  import java.util.Arrays;
12  import java.util.Iterator;
13  
14  /**
15   * @author Kristian Rosenvold
16   */
17  public class AbstractPlexusIoResourceCollectionTest
18      extends TestCase
19  {
20      @SuppressWarnings( "ResultOfMethodCallIgnored" )
21      public void testGetIncludes()
22          throws Exception
23      {
24          AbstractPlexusIoResourceCollection sut = new AbstractPlexusIoResourceCollection()
25          {
26              public Iterator<PlexusIoResource> getResources()
27                  throws IOException
28              {
29                  return Arrays.asList( getResource( "r1" ), getResource( "r2" ) ).iterator();
30              }
31  
32              public Stream stream()
33              {
34                  throw new UnsupportedOperationException();
35              }
36  
37              public boolean isConcurrentAccessSupported()
38              {
39                  return true;
40              }
41  
42          };
43  
44          sut.setStreamTransformer( new InputStreamTransformer()
45          {
46              @Nonnull
47              public InputStream transform( @Nonnull PlexusIoResource resource, @Nonnull final InputStream inputStream )
48                  throws IOException
49              {
50                  final byte[] buf = new byte[2];
51                  buf[0] = (byte) inputStream.read();
52                  buf[1] = (byte) inputStream.read();
53                  return new ByteArrayInputStream( buf );
54              }
55          } );
56  
57          final PlexusIoResource next = sut.getResources().next();
58          final InputStream inputStream = sut.getInputStream( next );
59          inputStream.read();
60          inputStream.read();
61          assertEquals( -1, inputStream.read() );
62          inputStream.close();
63  
64      }
65  
66      private static PlexusIoResource getResource( final String r1 )
67      {
68          return new AbstractPlexusIoResource( r1, 0, 0, true, false, true )
69          {
70              @Nonnull
71              public InputStream getContents()
72                  throws IOException
73              {
74                  return new ByteArrayInputStream( (r1 + "Payload").getBytes() );
75              }
76  
77              public URL getURL()
78                  throws IOException
79              {
80                  throw new IllegalStateException( "Not implemented" );
81              }
82          };
83      }
84  
85  }
86