1 package org.apache.archiva.web.action.admin.connectors.proxy;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import com.opensymphony.xwork2.Action;
23 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
24 import org.apache.archiva.admin.repository.proxyconnector.DefaultProxyConnectorAdmin;
25 import org.apache.archiva.admin.repository.remote.DefaultRemoteRepositoryAdmin;
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.archiva.configuration.ArchivaConfiguration;
28 import org.apache.archiva.configuration.Configuration;
29 import org.apache.archiva.configuration.IndeterminateConfigurationException;
30 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
31 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
32 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
33 import org.apache.archiva.configuration.functors.ProxyConnectorConfigurationOrderComparator;
34 import org.apache.archiva.web.action.AbstractWebworkTestCase;
35 import org.apache.archiva.redback.components.registry.RegistryException;
36 import org.apache.archiva.redback.integration.interceptor.SecureActionBundle;
37 import org.easymock.MockControl;
39 import java.util.Collections;
40 import java.util.List;
43 * SortProxyConnectorsActionTest
47 public class SortProxyConnectorsActionTest
48 extends AbstractWebworkTestCase
50 private static final String JAVAX = "javax";
52 private static final String CENTRAL = "central";
54 private static final String CORPORATE = "corporate";
56 private static final String CODEHAUS = "codehaus";
58 private SortProxyConnectorsAction action;
60 private MockControl archivaConfigurationControl;
62 private ArchivaConfiguration archivaConfiguration;
65 protected void setUp()
70 action = (SortProxyConnectorsAction) getActionProxy( "/admin/sortUpProxyConnector.action" ).getAction();
72 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
73 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
74 ( (DefaultManagedRepositoryAdmin) action.getManagedRepositoryAdmin() ).setArchivaConfiguration(
75 archivaConfiguration );
76 ( (DefaultRemoteRepositoryAdmin) action.getRemoteRepositoryAdmin() ).setArchivaConfiguration(
77 archivaConfiguration );
78 ( (DefaultProxyConnectorAdmin) action.getProxyConnectorAdmin() ).setArchivaConfiguration(
79 archivaConfiguration );
82 private void expectConfigurationRequests( int requestConfigCount )
83 throws RegistryException, IndeterminateConfigurationException
85 Configuration config = createInitialConfiguration();
87 archivaConfiguration.getConfiguration();
88 archivaConfigurationControl.setReturnValue( config, requestConfigCount );
90 archivaConfiguration.save( config );
93 public void testSecureActionBundle()
96 expectConfigurationRequests( 1 );
97 archivaConfigurationControl.replay();
99 SecureActionBundle bundle = action.getSecureActionBundle();
100 assertTrue( bundle.requiresAuthentication() );
101 assertEquals( 1, bundle.getAuthorizationTuples().size() );
104 public void testSortDown()
107 expectConfigurationRequests( 7 );
108 archivaConfigurationControl.replay();
110 action.setSource( CORPORATE );
111 action.setTarget( CENTRAL );
112 String status = action.sortDown();
113 assertEquals( Action.SUCCESS, status );
115 assertOrder( new String[]{ JAVAX, CENTRAL, CODEHAUS } );
118 public void testSortDownPastEnd()
121 expectConfigurationRequests( 7 );
122 archivaConfigurationControl.replay();
124 // Ask the last connector to sort down (essentially a no-op)
125 action.setSource( CORPORATE );
126 action.setTarget( CODEHAUS );
127 String status = action.sortDown();
128 assertEquals( Action.SUCCESS, status );
131 assertOrder( new String[]{ CENTRAL, JAVAX, CODEHAUS } );
134 public void testSortUp()
137 expectConfigurationRequests( 7 );
138 archivaConfigurationControl.replay();
140 action.setSource( CORPORATE );
141 action.setTarget( CODEHAUS );
142 String status = action.sortUp();
143 assertEquals( Action.SUCCESS, status );
145 assertOrder( new String[]{ CENTRAL, CODEHAUS, JAVAX } );
148 public void testSortUpPastBeginning()
151 expectConfigurationRequests( 7 );
152 archivaConfigurationControl.replay();
154 // Ask the first connector to sort up (essentially a no-op)
155 action.setSource( CORPORATE );
156 action.setTarget( CENTRAL );
157 String status = action.sortUp();
158 assertEquals( Action.SUCCESS, status );
161 assertOrder( new String[]{ CENTRAL, JAVAX, CODEHAUS } );
164 private void assertOrder( String[] targetRepoOrder )
166 List<ProxyConnectorConfiguration> connectors = archivaConfiguration.getConfiguration().getProxyConnectors();
167 Collections.sort( connectors, ProxyConnectorConfigurationOrderComparator.getInstance() );
169 for ( ProxyConnectorConfiguration connector : connectors )
171 assertEquals( "All connectors in list should have the same source id (in this test)", CORPORATE,
172 connector.getSourceRepoId() );
175 assertEquals( targetRepoOrder.length, connectors.size() );
177 int orderFailedAt = ( -1 );
178 for ( int i = 0; i < targetRepoOrder.length; i++ )
180 if ( !StringUtils.equals( targetRepoOrder[i], connectors.get( i ).getTargetRepoId() ) )
187 if ( orderFailedAt >= 0 )
189 StringBuffer msg = new StringBuffer();
191 msg.append( "Failed expected order of the proxy connectors <" );
192 msg.append( StringUtils.join( targetRepoOrder, ", " ) );
193 msg.append( ">, actual <" );
195 boolean needsComma = false;
196 for ( ProxyConnectorConfiguration proxy : connectors )
202 msg.append( proxy.getTargetRepoId() );
205 msg.append( "> failure at index <" ).append( orderFailedAt ).append( ">." );
207 fail( msg.toString() );
211 private Configuration createInitialConfiguration()
213 Configuration config = new Configuration();
215 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
216 managedRepo.setId( CORPORATE );
217 managedRepo.setLayout( "${java.io.tmpdir}/archiva-test/managed-repo" );
218 managedRepo.setReleases( true );
219 config.addManagedRepository( managedRepo );
221 RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
222 remoteRepo.setId( CENTRAL );
223 remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
224 config.addRemoteRepository( remoteRepo );
226 remoteRepo = new RemoteRepositoryConfiguration();
227 remoteRepo.setId( JAVAX );
228 remoteRepo.setUrl( "http://download.java.net/maven/2/" );
229 config.addRemoteRepository( remoteRepo );
231 remoteRepo = new RemoteRepositoryConfiguration();
232 remoteRepo.setId( CODEHAUS );
233 remoteRepo.setUrl( "http://repository.codehaus.org/" );
234 config.addRemoteRepository( remoteRepo );
236 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
237 connector.setSourceRepoId( CORPORATE );
238 connector.setTargetRepoId( CENTRAL );
239 connector.setOrder( 1 );
240 config.addProxyConnector( connector );
242 connector = new ProxyConnectorConfiguration();
243 connector.setSourceRepoId( CORPORATE );
244 connector.setTargetRepoId( JAVAX );
245 connector.setOrder( 2 );
246 config.addProxyConnector( connector );
248 connector = new ProxyConnectorConfiguration();
249 connector.setSourceRepoId( CORPORATE );
250 connector.setTargetRepoId( CODEHAUS );
251 connector.setOrder( 3 );
252 config.addProxyConnector( connector );