]> source.dussan.org Git - archiva.git/blob
32f552965134530e6d6a2a444ffd7aa599fb3b8b
[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 org.apache.commons.io.FileUtils;
23 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
24 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
25 import org.apache.maven.archiva.configuration.NetworkProxyConfiguration;
26 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
27 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
28 import org.apache.maven.archiva.model.ArtifactReference;
29 import org.apache.maven.archiva.policies.CachedFailuresPolicy;
30 import org.apache.maven.archiva.policies.ChecksumPolicy;
31 import org.apache.maven.archiva.policies.PropagateErrorsDownloadPolicy;
32 import org.apache.maven.archiva.policies.PropagateErrorsOnUpdateDownloadPolicy;
33 import org.apache.maven.archiva.policies.ReleasesPolicy;
34 import org.apache.maven.archiva.policies.SnapshotsPolicy;
35 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mortbay.jetty.Handler;
41 import org.mortbay.jetty.Request;
42 import org.mortbay.jetty.Server;
43 import org.mortbay.jetty.handler.AbstractHandler;
44 import org.springframework.context.ApplicationContext;
45 import org.springframework.test.context.ContextConfiguration;
46 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
47
48 import java.io.File;
49 import java.io.IOException;
50 import javax.inject.Inject;
51 import javax.servlet.ServletException;
52 import javax.servlet.http.HttpServletRequest;
53 import javax.servlet.http.HttpServletResponse;
54
55 import static org.junit.Assert.*;
56
57 /**
58  * Integration test for connecting over a HTTP proxy.
59  *
60  * @version $Id: ManagedDefaultTransferTest.java 677852 2008-07-18 08:16:24Z brett $
61  */
62 @RunWith( SpringJUnit4ClassRunner.class )
63 @ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml"} )
64 public class HttpProxyTransferTest
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         proxyHandler = applicationContext.getBean( "repositoryProxyConnectors#test", RepositoryProxyConnectors.class );
90
91         config = applicationContext.getBean( "archivaConfiguration#mock", ArchivaConfiguration.class );
92
93         // clear from previous tests - TODO the spring context should be initialised per test instead, or the config
94         // made a complete mock
95         config.getConfiguration().getProxyConnectors().clear();
96
97         // Setup source repository (using default layout)
98         String repoPath = "target/test-repository/managed/" + getClass().getSimpleName();
99
100         File destRepoDir = new File( repoPath );
101
102         // Cleanout destination dirs.
103         if ( destRepoDir.exists() )
104         {
105             FileUtils.deleteDirectory( destRepoDir );
106         }
107
108         // Make the destination dir.
109         destRepoDir.mkdirs();
110
111         ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
112         repo.setId( MANAGED_ID );
113         repo.setName( "Default Managed Repository" );
114         repo.setLocation( repoPath );
115         repo.setLayout( "default" );
116
117         ManagedRepositoryContent repoContent = applicationContext.getBean( "managedRepositoryContent#default",
118                                                                            ManagedRepositoryContent.class );
119
120         repoContent.setRepository( repo );
121         managedDefaultRepository = repoContent;
122
123         config.getConfiguration().addManagedRepository( repo );
124
125         Handler handler = new AbstractHandler()
126         {
127             public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch )
128                 throws IOException, ServletException
129             {
130                 response.setContentType( "text/plain" );
131                 response.setStatus( HttpServletResponse.SC_OK );
132                 response.getWriter().print( "get-default-layout-1.0.jar\n\n" );
133                 assertNotNull( request.getHeader( "Proxy-Connection" ) );
134
135                 ( (Request) request ).setHandled( true );
136             }
137         };
138
139         server = new Server( 0 );
140         server.setHandler( handler );
141         server.start();
142
143         int port = server.getConnectors()[0].getLocalPort();
144
145         NetworkProxyConfiguration proxyConfig = new NetworkProxyConfiguration();
146         proxyConfig.setHost( "localhost" );
147         proxyConfig.setPort( port );
148         proxyConfig.setProtocol( "http" );
149         proxyConfig.setId( PROXY_ID );
150         config.getConfiguration().addNetworkProxy( proxyConfig );
151
152         // Setup target (proxied to) repository.
153         RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
154
155         repoConfig.setId( PROXIED_ID );
156         repoConfig.setName( "Proxied Repository 1" );
157         repoConfig.setLayout( "default" );
158         repoConfig.setUrl( "http://www.example.com/" );
159
160         config.getConfiguration().addRemoteRepository( repoConfig );
161
162     }
163
164     @After
165     public void tearDown()
166         throws Exception
167     {
168         server.stop();
169     }
170
171     @Test
172     public void testGetOverHttpProxy()
173         throws Exception
174     {
175         assertNull( System.getProperty( "http.proxyHost" ) );
176         assertNull( System.getProperty( "http.proxyPort" ) );
177
178         String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
179
180         // Configure Connector (usually done within archiva.xml configuration)
181         addConnector();
182
183         File expectedFile = new File( new File( managedDefaultRepository.getRepoRoot() ), path );
184         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
185
186         // Attempt the proxy fetch.
187         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
188
189         File sourceFile = new File( PROXIED_BASEDIR, path );
190         assertNotNull( "Expected File should not be null.", expectedFile );
191         assertNotNull( "Actual File should not be null.", downloadedFile );
192
193         assertTrue( "Check actual file exists.", downloadedFile.exists() );
194         assertEquals( "Check filename path is appropriate.", expectedFile.getCanonicalPath(),
195                       downloadedFile.getCanonicalPath() );
196         assertEquals( "Check file path matches.", expectedFile.getAbsolutePath(), downloadedFile.getAbsolutePath() );
197
198         String expectedContents = FileUtils.readFileToString( sourceFile, null );
199         String actualContents = FileUtils.readFileToString( downloadedFile, null );
200         assertEquals( "Check file contents.", expectedContents, actualContents );
201
202         assertNull( System.getProperty( "http.proxyHost" ) );
203         assertNull( System.getProperty( "http.proxyPort" ) );
204     }
205
206     private void addConnector()
207     {
208         ProxyConnectorConfiguration connectorConfig = new ProxyConnectorConfiguration();
209         connectorConfig.setProxyId( PROXY_ID );
210         connectorConfig.setSourceRepoId( MANAGED_ID );
211         connectorConfig.setTargetRepoId( PROXIED_ID );
212         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, ChecksumPolicy.FIX );
213         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, ReleasesPolicy.ONCE );
214         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, SnapshotsPolicy.ONCE );
215         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, CachedFailuresPolicy.NO );
216         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS,
217                                    PropagateErrorsDownloadPolicy.QUEUE );
218         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS_ON_UPDATE,
219                                    PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT );
220
221         int count = config.getConfiguration().getProxyConnectors().size();
222         config.getConfiguration().addProxyConnector( connectorConfig );
223
224         // Proper Triggering ...
225         String prefix = "proxyConnectors.proxyConnector(" + count + ")";
226         ( (MockConfiguration) config ).triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );
227     }
228 }