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.xwork2.Action;
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
25 import org.apache.maven.archiva.configuration.Configuration;
26 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
27 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
29 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
30 import org.apache.maven.archiva.configuration.functors.ProxyConnectorConfigurationOrderComparator;
31 import org.apache.maven.archiva.web.action.AbstractWebworkTestCase;
32 import org.codehaus.plexus.registry.RegistryException;
33 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
34 import org.easymock.MockControl;
36 import java.util.Collections;
37 import java.util.List;
40 * SortProxyConnectorsActionTest
44 public class SortProxyConnectorsActionTest
45 extends AbstractWebworkTestCase
47 private static final String JAVAX = "javax";
49 private static final String CENTRAL = "central";
51 private static final String CORPORATE = "corporate";
53 private static final String CODEHAUS = "codehaus";
55 private SortProxyConnectorsAction action;
57 private MockControl archivaConfigurationControl;
59 private ArchivaConfiguration archivaConfiguration;
61 public void testSecureActionBundle()
64 expectConfigurationRequests( 1 );
65 archivaConfigurationControl.replay();
67 SecureActionBundle bundle = action.getSecureActionBundle();
68 assertTrue( bundle.requiresAuthentication() );
69 assertEquals( 1, bundle.getAuthorizationTuples().size() );
72 public void testSortDown()
75 expectConfigurationRequests( 7 );
76 archivaConfigurationControl.replay();
78 action.setSource( CORPORATE );
79 action.setTarget( CENTRAL );
80 String status = action.sortDown();
81 assertEquals( Action.SUCCESS, status );
83 assertOrder( new String[]{ JAVAX, CENTRAL, CODEHAUS } );
86 public void testSortDownPastEnd()
89 expectConfigurationRequests( 7 );
90 archivaConfigurationControl.replay();
92 // Ask the last connector to sort down (essentially a no-op)
93 action.setSource( CORPORATE );
94 action.setTarget( CODEHAUS );
95 String status = action.sortDown();
96 assertEquals( Action.SUCCESS, status );
99 assertOrder( new String[]{ CENTRAL, JAVAX, CODEHAUS } );
102 public void testSortUp()
105 expectConfigurationRequests( 7 );
106 archivaConfigurationControl.replay();
108 action.setSource( CORPORATE );
109 action.setTarget( CODEHAUS );
110 String status = action.sortUp();
111 assertEquals( Action.SUCCESS, status );
113 assertOrder( new String[]{ CENTRAL, CODEHAUS, JAVAX } );
116 public void testSortUpPastBeginning()
119 expectConfigurationRequests( 7 );
120 archivaConfigurationControl.replay();
122 // Ask the first connector to sort up (essentially a no-op)
123 action.setSource( CORPORATE );
124 action.setTarget( CENTRAL );
125 String status = action.sortUp();
126 assertEquals( Action.SUCCESS, status );
129 assertOrder( new String[]{ CENTRAL, JAVAX, CODEHAUS } );
132 private void assertOrder( String[] targetRepoOrder )
134 List<ProxyConnectorConfiguration> connectors = archivaConfiguration.getConfiguration().getProxyConnectors();
135 Collections.sort( connectors, ProxyConnectorConfigurationOrderComparator.getInstance() );
137 for ( ProxyConnectorConfiguration connector : connectors )
139 assertEquals( "All connectors in list should have the same source id (in this test)", CORPORATE,
140 connector.getSourceRepoId() );
143 assertEquals( targetRepoOrder.length, connectors.size() );
145 int orderFailedAt = ( -1 );
146 for ( int i = 0; i < targetRepoOrder.length; i++ )
148 if ( !StringUtils.equals( targetRepoOrder[i], connectors.get( i ).getTargetRepoId() ) )
155 if ( orderFailedAt >= 0 )
157 StringBuffer msg = new StringBuffer();
159 msg.append( "Failed expected order of the proxy connectors <" );
160 msg.append( StringUtils.join( targetRepoOrder, ", " ) );
161 msg.append( ">, actual <" );
163 boolean needsComma = false;
164 for ( ProxyConnectorConfiguration proxy : connectors )
170 msg.append( proxy.getTargetRepoId() );
173 msg.append( "> failure at index <" ).append( orderFailedAt ).append( ">." );
175 fail( msg.toString() );
179 private Configuration createInitialConfiguration()
181 Configuration config = new Configuration();
183 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
184 managedRepo.setId( CORPORATE );
185 managedRepo.setLayout( "${java.io.tmpdir}/archiva-test/managed-repo" );
186 managedRepo.setReleases( true );
187 config.addManagedRepository( managedRepo );
189 RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
190 remoteRepo.setId( CENTRAL );
191 remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
192 config.addRemoteRepository( remoteRepo );
194 remoteRepo = new RemoteRepositoryConfiguration();
195 remoteRepo.setId( JAVAX );
196 remoteRepo.setUrl( "http://download.java.net/maven/2/" );
197 config.addRemoteRepository( remoteRepo );
199 remoteRepo = new RemoteRepositoryConfiguration();
200 remoteRepo.setId( CODEHAUS );
201 remoteRepo.setUrl( "http://repository.codehaus.org/" );
202 config.addRemoteRepository( remoteRepo );
204 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
205 connector.setSourceRepoId( CORPORATE );
206 connector.setTargetRepoId( CENTRAL );
207 connector.setOrder( 1 );
208 config.addProxyConnector( connector );
210 connector = new ProxyConnectorConfiguration();
211 connector.setSourceRepoId( CORPORATE );
212 connector.setTargetRepoId( JAVAX );
213 connector.setOrder( 2 );
214 config.addProxyConnector( connector );
216 connector = new ProxyConnectorConfiguration();
217 connector.setSourceRepoId( CORPORATE );
218 connector.setTargetRepoId( CODEHAUS );
219 connector.setOrder( 3 );
220 config.addProxyConnector( connector );
225 private void expectConfigurationRequests( int requestConfigCount )
226 throws RegistryException, IndeterminateConfigurationException
228 Configuration config = createInitialConfiguration();
230 for ( int i = 0; i < requestConfigCount; i++ )
232 archivaConfiguration.getConfiguration();
233 archivaConfigurationControl.setReturnValue( config );
236 archivaConfiguration.save( config );
240 protected void setUp()
245 //action = (SortProxyConnectorsAction) lookup( Action.class.getName(), "sortProxyConnectorsAction" );
246 action = (SortProxyConnectorsAction) getActionProxy( "/admin/sortUpProxyConnector.action" ).getAction();
248 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
249 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
250 action.setArchivaConfiguration( archivaConfiguration );