1 package org.apache.maven.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.xwork.Action;
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;
37 import java.util.Collections;
38 import java.util.List;
41 * SortProxyConnectorsActionTest
43 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
46 public class SortProxyConnectorsActionTest
47 extends AbstractWebworkTestCase
49 private static final String JAVAX = "javax";
51 private static final String CENTRAL = "central";
53 private static final String CORPORATE = "corporate";
55 private static final String CODEHAUS = "codehaus";
57 private SortProxyConnectorsAction action;
59 private MockControl archivaConfigurationControl;
61 private ArchivaConfiguration archivaConfiguration;
63 public void testSecureActionBundle()
66 expectConfigurationRequests( 1 );
67 archivaConfigurationControl.replay();
69 SecureActionBundle bundle = action.getSecureActionBundle();
70 assertTrue( bundle.requiresAuthentication() );
71 assertEquals( 1, bundle.getAuthorizationTuples().size() );
74 public void testSortDown()
77 expectConfigurationRequests( 7 );
78 archivaConfigurationControl.replay();
80 action.setSource( CORPORATE );
81 action.setTarget( CENTRAL );
82 String status = action.sortDown();
83 assertEquals( Action.SUCCESS, status );
85 assertOrder( new String[] { JAVAX, CENTRAL, CODEHAUS } );
88 public void testSortDownPastEnd()
91 expectConfigurationRequests( 7 );
92 archivaConfigurationControl.replay();
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 );
101 assertOrder( new String[] { CENTRAL, JAVAX, CODEHAUS } );
104 public void testSortUp()
107 expectConfigurationRequests( 7 );
108 archivaConfigurationControl.replay();
110 action.setSource( CORPORATE );
111 action.setTarget( CODEHAUS );
112 String status = action.sortUp();
113 assertEquals( Action.SUCCESS, status );
115 assertOrder( new String[] { CENTRAL, CODEHAUS, JAVAX } );
118 public void testSortUpPastBeginning()
121 expectConfigurationRequests( 7 );
122 archivaConfigurationControl.replay();
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 );
131 assertOrder( new String[] { CENTRAL, JAVAX, CODEHAUS } );
134 private void assertOrder( String[] targetRepoOrder )
136 List<ProxyConnectorConfiguration> connectors = archivaConfiguration.getConfiguration().getProxyConnectors();
137 Collections.sort( connectors, ProxyConnectorConfigurationOrderComparator.getInstance() );
139 for ( ProxyConnectorConfiguration connector : connectors )
141 assertEquals( "All connectors in list should have the same source id (in this test)", CORPORATE, connector
142 .getSourceRepoId() );
145 assertEquals( targetRepoOrder.length, connectors.size() );
147 int orderFailedAt = ( -1 );
148 for ( int i = 0; i < targetRepoOrder.length; i++ )
150 if ( !StringUtils.equals( targetRepoOrder[i], connectors.get( i ).getTargetRepoId() ) )
157 if ( orderFailedAt >= 0 )
159 StringBuffer msg = new StringBuffer();
161 msg.append( "Failed expected order of the proxy connectors <" );
162 msg.append( StringUtils.join( targetRepoOrder, ", " ) );
163 msg.append( ">, actual <" );
165 boolean needsComma = false;
166 for ( ProxyConnectorConfiguration proxy : connectors )
172 msg.append( proxy.getTargetRepoId() );
175 msg.append( "> failure at index <" ).append( orderFailedAt ).append( ">." );
177 fail( msg.toString() );
181 private Configuration createInitialConfiguration()
183 Configuration config = new Configuration();
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 );
191 RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
192 remoteRepo.setId( CENTRAL );
193 remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
194 config.addRemoteRepository( remoteRepo );
196 remoteRepo = new RemoteRepositoryConfiguration();
197 remoteRepo.setId( JAVAX );
198 remoteRepo.setUrl( "http://download.java.net/maven/2/" );
199 config.addRemoteRepository( remoteRepo );
201 remoteRepo = new RemoteRepositoryConfiguration();
202 remoteRepo.setId( CODEHAUS );
203 remoteRepo.setUrl( "http://repository.codehaus.org/" );
204 config.addRemoteRepository( remoteRepo );
206 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
207 connector.setSourceRepoId( CORPORATE );
208 connector.setTargetRepoId( CENTRAL );
209 connector.setOrder( 1 );
210 config.addProxyConnector( connector );
212 connector = new ProxyConnectorConfiguration();
213 connector.setSourceRepoId( CORPORATE );
214 connector.setTargetRepoId( JAVAX );
215 connector.setOrder( 2 );
216 config.addProxyConnector( connector );
218 connector = new ProxyConnectorConfiguration();
219 connector.setSourceRepoId( CORPORATE );
220 connector.setTargetRepoId( CODEHAUS );
221 connector.setOrder( 3 );
222 config.addProxyConnector( connector );
227 private void expectConfigurationRequests( int requestConfigCount )
228 throws RegistryException, IndeterminateConfigurationException
230 Configuration config = createInitialConfiguration();
232 for ( int i = 0; i < requestConfigCount; i++ )
234 archivaConfiguration.getConfiguration();
235 archivaConfigurationControl.setReturnValue( config );
238 archivaConfiguration.save( config );
242 protected void setUp()
247 action = (SortProxyConnectorsAction) lookup( Action.class.getName(), "sortProxyConnectorsAction" );
249 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
250 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
251 action.setArchivaConfiguration( archivaConfiguration );