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