]> source.dussan.org Git - archiva.git/blob
2c236a0c19da285f094cc59c99635d6d7d0013e9
[archiva.git] /
1 package org.apache.maven.archiva.proxy;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21
22 import junit.framework.TestCase;
23 import org.apache.commons.io.FileUtils;
24 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
25 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
26 import org.apache.maven.archiva.configuration.NetworkProxyConfiguration;
27 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
28 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
29 import org.apache.maven.archiva.model.ArtifactReference;
30 import org.apache.maven.archiva.policies.CachedFailuresPolicy;
31 import org.apache.maven.archiva.policies.ChecksumPolicy;
32 import org.apache.maven.archiva.policies.PropagateErrorsDownloadPolicy;
33 import org.apache.maven.archiva.policies.PropagateErrorsOnUpdateDownloadPolicy;
34 import org.apache.maven.archiva.policies.ReleasesPolicy;
35 import org.apache.maven.archiva.policies.SnapshotsPolicy;
36 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mortbay.jetty.Handler;
42 import org.mortbay.jetty.Request;
43 import org.mortbay.jetty.Server;
44 import org.mortbay.jetty.handler.AbstractHandler;
45 import org.springframework.context.ApplicationContext;
46 import org.springframework.test.context.ContextConfiguration;
47 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
48
49 import java.io.File;
50 import java.io.IOException;
51 import javax.inject.Inject;
52 import javax.servlet.ServletException;
53 import javax.servlet.http.HttpServletRequest;
54 import javax.servlet.http.HttpServletResponse;
55
56 /**
57  * Integration test for connecting over a HTTP proxy.
58  *
59  * @version $Id: ManagedDefaultTransferTest.java 677852 2008-07-18 08:16:24Z brett $
60  */
61 @RunWith( SpringJUnit4ClassRunner.class )
62 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
63 public class HttpProxyTransferTest
64     extends TestCase
65 {
66     private static final String PROXY_ID = "proxy";
67
68     private static final String MANAGED_ID = "default-managed-repository";
69
70     private static final String PROXIED_ID = "proxied1";
71
72     private static final String PROXIED_BASEDIR = "src/test/repositories/proxied1";
73
74     private RepositoryProxyConnectors proxyHandler;
75
76     private ArchivaConfiguration config;
77
78     private ManagedRepositoryContent managedDefaultRepository;
79
80     @Inject
81     private ApplicationContext applicationContext;
82
83     private Server server;
84
85     @Before
86     public void setUp()
87         throws Exception
88     {
89         super.setUp();
90
91         proxyHandler = applicationContext.getBean( "repositoryProxyConnectors#test", RepositoryProxyConnectors.class );
92
93         config = applicationContext.getBean( "archivaConfiguration#mock", ArchivaConfiguration.class );
94
95         // clear from previous tests - TODO the spring context should be initialised per test instead, or the config
96         // made a complete mock
97         config.getConfiguration().getProxyConnectors().clear();
98
99         // Setup source repository (using default layout)
100         String repoPath = "target/test-repository/managed/" + getName();
101
102         File destRepoDir = new File( repoPath );
103
104         // Cleanout destination dirs.
105         if ( destRepoDir.exists() )
106         {
107             FileUtils.deleteDirectory( destRepoDir );
108         }
109
110         // Make the destination dir.
111         destRepoDir.mkdirs();
112
113         ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
114         repo.setId( MANAGED_ID );
115         repo.setName( "Default Managed Repository" );
116         repo.setLocation( repoPath );
117         repo.setLayout( "default" );
118
119         ManagedRepositoryContent repoContent =
120             applicationContext.getBean( "managedRepositoryContent#default", ManagedRepositoryContent.class );
121
122         repoContent.setRepository( repo );
123         managedDefaultRepository = repoContent;
124
125         config.getConfiguration().addManagedRepository( repo );
126
127         Handler handler = new AbstractHandler()
128         {
129             public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch )
130                 throws IOException, ServletException
131             {
132                 response.setContentType( "text/plain" );
133                 response.setStatus( HttpServletResponse.SC_OK );
134                 response.getWriter().print( "get-default-layout-1.0.jar\n\n" );
135                 assertNotNull( request.getHeader( "Proxy-Connection" ) );
136
137                 ( (Request) request ).setHandled( true );
138             }
139         };
140
141         server = new Server( 0 );
142         server.setHandler( handler );
143         server.start();
144
145         int port = server.getConnectors()[0].getLocalPort();
146
147         NetworkProxyConfiguration proxyConfig = new NetworkProxyConfiguration();
148         proxyConfig.setHost( "localhost" );
149         proxyConfig.setPort( port );
150         proxyConfig.setProtocol( "http" );
151         proxyConfig.setId( PROXY_ID );
152         config.getConfiguration().addNetworkProxy( proxyConfig );
153
154         // Setup target (proxied to) repository.
155         RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
156
157         repoConfig.setId( PROXIED_ID );
158         repoConfig.setName( "Proxied Repository 1" );
159         repoConfig.setLayout( "default" );
160         repoConfig.setUrl( "http://www.example.com/" );
161
162         config.getConfiguration().addRemoteRepository( repoConfig );
163
164     }
165
166     @After
167     public void tearDown()
168         throws Exception
169     {
170         super.tearDown();
171
172         server.stop();
173     }
174
175     @Test
176     public void testGetOverHttpProxy()
177         throws Exception
178     {
179         assertNull( System.getProperty( "http.proxyHost" ) );
180         assertNull( System.getProperty( "http.proxyPort" ) );
181
182         String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
183
184         // Configure Connector (usually done within archiva.xml configuration)
185         addConnector();
186
187         File expectedFile = new File( new File( managedDefaultRepository.getRepoRoot() ), path );
188         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
189
190         // Attempt the proxy fetch.
191         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
192
193         File sourceFile = new File( PROXIED_BASEDIR, path );
194         assertNotNull( "Expected File should not be null.", expectedFile );
195         assertNotNull( "Actual File should not be null.", downloadedFile );
196
197         assertTrue( "Check actual file exists.", downloadedFile.exists() );
198         assertEquals( "Check filename path is appropriate.", expectedFile.getCanonicalPath(),
199                       downloadedFile.getCanonicalPath() );
200         assertEquals( "Check file path matches.", expectedFile.getAbsolutePath(), downloadedFile.getAbsolutePath() );
201
202         String expectedContents = FileUtils.readFileToString( sourceFile, null );
203         String actualContents = FileUtils.readFileToString( downloadedFile, null );
204         assertEquals( "Check file contents.", expectedContents, actualContents );
205
206         assertNull( System.getProperty( "http.proxyHost" ) );
207         assertNull( System.getProperty( "http.proxyPort" ) );
208     }
209
210     private void addConnector()
211     {
212         ProxyConnectorConfiguration connectorConfig = new ProxyConnectorConfiguration();
213         connectorConfig.setProxyId( PROXY_ID );
214         connectorConfig.setSourceRepoId( MANAGED_ID );
215         connectorConfig.setTargetRepoId( PROXIED_ID );
216         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, ChecksumPolicy.FIX );
217         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, ReleasesPolicy.ONCE );
218         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, SnapshotsPolicy.ONCE );
219         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, CachedFailuresPolicy.NO );
220         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS,
221                                    PropagateErrorsDownloadPolicy.QUEUE );
222         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS_ON_UPDATE,
223                                    PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT );
224
225         int count = config.getConfiguration().getProxyConnectors().size();
226         config.getConfiguration().addProxyConnector( connectorConfig );
227
228         // Proper Triggering ...
229         String prefix = "proxyConnectors.proxyConnector(" + count + ")";
230         ( (MockConfiguration) config ).triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );
231     }
232 }