]> source.dussan.org Git - archiva.git/blob
1e1d6b3351654a6a8aa50e5534c6fd2ae75bb666
[archiva.git] /
1 package org.apache.maven.archiva.web.action.admin.connectors.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 com.opensymphony.xwork.Action;
23
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
26 import org.apache.maven.archiva.configuration.Configuration;
27 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
28 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
29 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
30 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
31 import org.apache.maven.archiva.configuration.functors.ProxyConnectorConfigurationOrderComparator;
32 import org.apache.maven.archiva.web.action.AbstractWebworkTestCase;
33 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
34 import org.codehaus.plexus.registry.RegistryException;
35 import org.easymock.MockControl;
36
37 import java.util.Collections;
38 import java.util.List;
39
40 /**
41  * SortProxyConnectorsActionTest 
42  *
43  * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
44  * @version $Id$
45  */
46 public class SortProxyConnectorsActionTest
47     extends AbstractWebworkTestCase
48 {
49     private static final String JAVAX = "javax";
50
51     private static final String CENTRAL = "central";
52
53     private static final String CORPORATE = "corporate";
54
55     private static final String CODEHAUS = "codehaus";
56
57     private SortProxyConnectorsAction action;
58
59     private MockControl archivaConfigurationControl;
60
61     private ArchivaConfiguration archivaConfiguration;
62
63     public void testSecureActionBundle()
64         throws Exception
65     {
66         expectConfigurationRequests( 1 );
67         archivaConfigurationControl.replay();
68
69         SecureActionBundle bundle = action.getSecureActionBundle();
70         assertTrue( bundle.requiresAuthentication() );
71         assertEquals( 1, bundle.getAuthorizationTuples().size() );
72     }
73
74     public void testSortDown()
75         throws Exception
76     {
77         expectConfigurationRequests( 7 );
78         archivaConfigurationControl.replay();
79
80         action.setSource( CORPORATE );
81         action.setTarget( CENTRAL );
82         String status = action.sortDown();
83         assertEquals( Action.SUCCESS, status );
84
85         assertOrder( new String[] { JAVAX, CENTRAL, CODEHAUS } );
86     }
87
88     public void testSortDownPastEnd()
89         throws Exception
90     {
91         expectConfigurationRequests( 7 );
92         archivaConfigurationControl.replay();
93
94         // Ask the last connector to sort down (essentially a no-op)
95         action.setSource( CORPORATE );
96         action.setTarget( CODEHAUS );
97         String status = action.sortDown();
98         assertEquals( Action.SUCCESS, status );
99
100         // No order change.
101         assertOrder( new String[] { CENTRAL, JAVAX, CODEHAUS } );
102     }
103
104     public void testSortUp()
105         throws Exception
106     {
107         expectConfigurationRequests( 7 );
108         archivaConfigurationControl.replay();
109
110         action.setSource( CORPORATE );
111         action.setTarget( CODEHAUS );
112         String status = action.sortUp();
113         assertEquals( Action.SUCCESS, status );
114
115         assertOrder( new String[] { CENTRAL, CODEHAUS, JAVAX } );
116     }
117
118     public void testSortUpPastBeginning()
119     throws Exception
120     {
121         expectConfigurationRequests( 7 );
122         archivaConfigurationControl.replay();
123
124         // Ask the first connector to sort up (essentially a no-op)
125         action.setSource( CORPORATE );
126         action.setTarget( CENTRAL );
127         String status = action.sortUp();
128         assertEquals( Action.SUCCESS, status );
129
130         // No order change.
131         assertOrder( new String[] { CENTRAL, JAVAX, CODEHAUS } );
132     }
133
134     private void assertOrder( String[] targetRepoOrder )
135     {
136         List<ProxyConnectorConfiguration> connectors = archivaConfiguration.getConfiguration().getProxyConnectors();
137         Collections.sort( connectors, ProxyConnectorConfigurationOrderComparator.getInstance() );
138
139         for ( ProxyConnectorConfiguration connector : connectors )
140         {
141             assertEquals( "All connectors in list should have the same source id (in this test)", CORPORATE, connector
142                 .getSourceRepoId() );
143         }
144
145         assertEquals( targetRepoOrder.length, connectors.size() );
146
147         int orderFailedAt = ( -1 );
148         for ( int i = 0; i < targetRepoOrder.length; i++ )
149         {
150             if ( !StringUtils.equals( targetRepoOrder[i], connectors.get( i ).getTargetRepoId() ) )
151             {
152                 orderFailedAt = i;
153                 break;
154             }
155         }
156
157         if ( orderFailedAt >= 0 )
158         {
159             StringBuffer msg = new StringBuffer();
160
161             msg.append( "Failed expected order of the proxy connectors <" );
162             msg.append( StringUtils.join( targetRepoOrder, ", " ) );
163             msg.append( ">, actual <" );
164
165             boolean needsComma = false;
166             for ( ProxyConnectorConfiguration proxy : connectors )
167             {
168                 if ( needsComma )
169                 {
170                     msg.append( ", " );
171                 }
172                 msg.append( proxy.getTargetRepoId() );
173                 needsComma = true;
174             }
175             msg.append( "> failure at index <" ).append( orderFailedAt ).append( ">." );
176
177             fail( msg.toString() );
178         }
179     }
180
181     private Configuration createInitialConfiguration()
182     {
183         Configuration config = new Configuration();
184
185         ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
186         managedRepo.setId( CORPORATE );
187         managedRepo.setLayout( "${java.io.tmpdir}/archiva-test/managed-repo" );
188         managedRepo.setReleases( true );
189         config.addManagedRepository( managedRepo );
190
191         RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
192         remoteRepo.setId( CENTRAL );
193         remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
194         config.addRemoteRepository( remoteRepo );
195
196         remoteRepo = new RemoteRepositoryConfiguration();
197         remoteRepo.setId( JAVAX );
198         remoteRepo.setUrl( "http://download.java.net/maven/2/" );
199         config.addRemoteRepository( remoteRepo );
200
201         remoteRepo = new RemoteRepositoryConfiguration();
202         remoteRepo.setId( CODEHAUS );
203         remoteRepo.setUrl( "http://repository.codehaus.org/" );
204         config.addRemoteRepository( remoteRepo );
205
206         ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
207         connector.setSourceRepoId( CORPORATE );
208         connector.setTargetRepoId( CENTRAL );
209         connector.setOrder( 1 );
210         config.addProxyConnector( connector );
211
212         connector = new ProxyConnectorConfiguration();
213         connector.setSourceRepoId( CORPORATE );
214         connector.setTargetRepoId( JAVAX );
215         connector.setOrder( 2 );
216         config.addProxyConnector( connector );
217
218         connector = new ProxyConnectorConfiguration();
219         connector.setSourceRepoId( CORPORATE );
220         connector.setTargetRepoId( CODEHAUS );
221         connector.setOrder( 3 );
222         config.addProxyConnector( connector );
223
224         return config;
225     }
226
227     private void expectConfigurationRequests( int requestConfigCount )
228         throws RegistryException, IndeterminateConfigurationException
229     {
230         Configuration config = createInitialConfiguration();
231
232         for ( int i = 0; i < requestConfigCount; i++ )
233         {
234             archivaConfiguration.getConfiguration();
235             archivaConfigurationControl.setReturnValue( config );
236         }
237
238         archivaConfiguration.save( config );
239     }
240
241     @Override
242     protected void setUp()
243         throws Exception
244     {
245         super.setUp();
246
247         action = (SortProxyConnectorsAction) lookup( Action.class.getName(), "sortProxyConnectorsAction" );
248
249         archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
250         archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
251         action.setArchivaConfiguration( archivaConfiguration );
252     }
253 }