View Javadoc
1   package org.codehaus.plexus.components.io.resources.proxy;
2   
3   import junit.framework.TestCase;
4   import org.codehaus.plexus.components.io.attributes.FileAttributes;
5   import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes;
6   import org.codehaus.plexus.components.io.attributes.SimpleResourceAttributes;
7   import org.codehaus.plexus.components.io.functions.ContentSupplier;
8   import org.codehaus.plexus.components.io.functions.FileSupplier;
9   import org.codehaus.plexus.components.io.functions.NameSupplier;
10  import org.codehaus.plexus.components.io.functions.ResourceAttributeSupplier;
11  import org.codehaus.plexus.components.io.functions.SizeSupplier;
12  import org.codehaus.plexus.components.io.functions.SymlinkDestinationSupplier;
13  import org.codehaus.plexus.components.io.resources.PlexusIoFileResource;
14  import org.codehaus.plexus.components.io.resources.PlexusIoResource;
15  
16  import javax.annotation.Nonnull;
17  import java.io.ByteArrayInputStream;
18  import java.io.File;
19  import java.io.IOException;
20  import java.io.InputStream;
21  
22  public class ProxyFactoryTest
23      extends TestCase
24  {
25  
26      public void testCreateProxy()
27          throws Exception
28      {
29          final PlexusIoResource proxy = ProxyFactory.createProxy( getPomResource(), null );
30          assertTrue( proxy instanceof ResourceAttributeSupplier );
31          assertTrue( proxy instanceof FileSupplier );
32          assertFalse( proxy instanceof SymlinkDestinationSupplier );
33      }
34  
35      public void testCreateProxyWithNameOverride()
36          throws Exception
37      {
38          NameSupplier ns = new NameSupplier()
39          {
40              public String getName()
41              {
42                  return "fred";
43              }
44          };
45          final PlexusIoResource proxy = ProxyFactory.createProxy( getPomResource(), ns );
46          assertEquals( "fred", proxy.getName() );
47      }
48  
49      public void testCreateProxyWithResourceAttributeOverride()
50          throws Exception
51      {
52          final PlexusIoResourceAttributes s = SimpleResourceAttributes.lastResortDummyAttributesForBrokenOS();
53          ResourceAttributeSupplier ns = new ResourceAttributeSupplier()
54          {
55              public PlexusIoResourceAttributes getAttributes()
56              {
57                  return s;
58              }
59          };
60          final PlexusIoResource proxy = ProxyFactory.createProxy( getPomResource(), ns );
61          assertSame( s, ( (ResourceAttributeSupplier) proxy ).getAttributes() );
62      }
63  
64      public void testCreateProxyWithSizeSupplierOverride()
65          throws Exception
66      {
67          final PlexusIoResourceAttributes s = SimpleResourceAttributes.lastResortDummyAttributesForBrokenOS();
68          SizeSupplier ns = new SizeSupplier()
69          {
70              public long getSize()
71              {
72                  return 42;
73              }
74          };
75          final PlexusIoResource proxy = ProxyFactory.createProxy( getPomResource(), ns );
76          assertEquals( 42, proxy.getSize() );
77      }
78  
79  
80      public void testCreateProxyWithContentSupplierOverride()
81          throws Exception
82      {
83          final InputStream s = new ByteArrayInputStream( new byte[10] );
84          ContentSupplier ns = new ContentSupplier()
85          {
86              public InputStream getContents()
87                  throws IOException
88              {
89                  return s;
90              }
91  
92          };
93          final PlexusIoResource proxy = ProxyFactory.createProxy( getPomResource(), ns );
94          assertEquals( s, proxy.getContents() );
95      }
96  
97      public void testCreateProxyWithSymlinkDestinationSupplierOverride()
98          throws Exception
99      {
100         SymlinkDestinationSupplier ns = new SymlinkDestinationSupplier()
101         {
102             public String getSymlinkDestination()
103                 throws IOException
104             {
105                 return "mordor";
106             }
107         };
108         final PlexusIoResource proxy = ProxyFactory.createProxy( getDummySymlinkResource(), ns );
109         assertEquals( "mordor", ( (SymlinkDestinationSupplier) proxy ).getSymlinkDestination() );
110     }
111 
112 
113     private PlexusIoFileResource getPomResource()
114         throws IOException
115     {
116         final File file = new File( "pom.xml" );
117         PlexusIoResourceAttributes attrs = FileAttributes.uncached( file );
118 
119         return new PlexusIoFileResource( file, "pom.xml", attrs ){};
120     }
121 
122     class Dummy extends PlexusIoFileResource
123         implements SymlinkDestinationSupplier
124     {
125         public Dummy( @Nonnull File file, @Nonnull PlexusIoResourceAttributes attrs )
126             throws IOException
127         {
128             super( file, file.getName(), attrs );
129         }
130 
131         public String getSymlinkDestination()
132             throws IOException
133         {
134             throw new IllegalStateException( "Unsupported" );
135         }
136     }
137     private Dummy getDummySymlinkResource()
138         throws IOException
139     {
140         final File file = new File( "pom.xml" );
141         PlexusIoResourceAttributes attrs = FileAttributes.uncached( file );
142 
143         return new Dummy( file, attrs );
144     }
145 
146 }