]> source.dussan.org Git - archiva.git/blob
1457f5952bf0a75a97e81d67ea61a83fe88ba1b1
[archiva.git] /
1 package org.apache.maven.archiva.web.action.admin.repositories;
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 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
24 import org.apache.maven.archiva.configuration.Configuration;
25 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
26 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
28 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
29 import org.codehaus.plexus.registry.RegistryException;
30 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
31 import org.easymock.MockControl;
32
33 import java.util.Collections;
34
35 /**
36  * DeleteRemoteRepositoryActionTest 
37  *
38  * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
39  * @version $Id$
40  */
41 public class DeleteRemoteRepositoryActionTest
42     extends PlexusInSpringTestCase
43 {
44     private static final String REPO_ID = "remote-repo-ident";
45
46     private DeleteRemoteRepositoryAction action;
47
48     private MockControl archivaConfigurationControl;
49
50     private ArchivaConfiguration archivaConfiguration;
51
52     protected void setUp()
53         throws Exception
54     {
55         super.setUp();
56
57         action = (DeleteRemoteRepositoryAction) lookup( Action.class.getName(), "deleteRemoteRepositoryAction" );
58
59         archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
60         archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
61         action.setArchivaConfiguration( archivaConfiguration );
62     }
63
64     public void testDeleteRemoteRepositoryConfirmation()
65         throws Exception
66     {
67         RemoteRepositoryConfiguration originalRepository = createRepository();
68         Configuration configuration = createConfigurationForEditing( originalRepository );
69
70         archivaConfiguration.getConfiguration();
71         archivaConfigurationControl.setReturnValue( configuration );
72         archivaConfigurationControl.replay();
73
74         action.setRepoid( REPO_ID );
75
76         action.prepare();
77         assertEquals( REPO_ID, action.getRepoid() );
78         RemoteRepositoryConfiguration repository = action.getRepository();
79         assertNotNull( repository );
80         assertRepositoryEquals( repository, createRepository() );
81
82         String status = action.confirmDelete();
83         assertEquals( Action.INPUT, status );
84         repository = action.getRepository();
85         assertRepositoryEquals( repository, createRepository() );
86         assertEquals( Collections.singletonList( originalRepository ), configuration.getRemoteRepositories() );
87     }
88
89     public void testDeleteRemoteRepository()
90         throws RegistryException, IndeterminateConfigurationException
91     {
92         Configuration configuration = createConfigurationForEditing( createRepository() );
93         configuration.addManagedRepository( createManagedRepository( "internal", getTestPath( "target/repo/internal" ) ) );
94         configuration.addManagedRepository( createManagedRepository( "snapshots", getTestPath( "target/repo/snapshots" ) ) );
95         configuration.addProxyConnector( createProxyConnector( "internal", REPO_ID) );
96         
97         archivaConfiguration.getConfiguration();
98         archivaConfigurationControl.setReturnValue( configuration, 4 );
99         
100         archivaConfiguration.save( configuration );
101         archivaConfigurationControl.replay();
102         
103         action.setRepoid( REPO_ID );
104         
105         action.prepare();
106         assertEquals( REPO_ID, action.getRepoid() );
107         RemoteRepositoryConfiguration repository = action.getRepository();
108         assertNotNull( repository );
109         assertRepositoryEquals( repository, createRepository() );
110         
111         assertEquals( 1, configuration.getProxyConnectors().size() );
112         
113         String status = action.delete();
114         assertEquals( Action.SUCCESS, status );
115
116         assertTrue( configuration.getRemoteRepositories().isEmpty() );
117         assertEquals( 0, configuration.getProxyConnectors().size() );
118     }
119
120     public void testDeleteRemoteRepositoryCancelled()
121         throws Exception
122     {
123         RemoteRepositoryConfiguration originalRepository = createRepository();
124         Configuration configuration = createConfigurationForEditing( originalRepository );
125
126         archivaConfiguration.getConfiguration();
127         archivaConfigurationControl.setReturnValue( configuration, 2 );
128
129         archivaConfiguration.save( configuration );
130         archivaConfigurationControl.replay();
131
132         action.setRepoid( REPO_ID );
133
134         action.prepare();
135         assertEquals( REPO_ID, action.getRepoid() );
136         RemoteRepositoryConfiguration repositoryConfiguration = action.getRepository();
137         assertNotNull( repositoryConfiguration );
138         assertRepositoryEquals( repositoryConfiguration, createRepository() );
139
140         String status = action.execute();
141         assertEquals( Action.SUCCESS, status );
142
143         RemoteRepositoryConfiguration repository = action.getRepository();
144         assertRepositoryEquals( repository, createRepository() );
145         assertEquals( Collections.singletonList( originalRepository ), configuration.getRemoteRepositories() );
146     }
147
148     private Configuration createConfigurationForEditing( RemoteRepositoryConfiguration repositoryConfiguration )
149     {
150         Configuration configuration = new Configuration();
151         configuration.addRemoteRepository( repositoryConfiguration );
152         return configuration;
153     }
154     
155     private RemoteRepositoryConfiguration createRepository()
156     {
157         RemoteRepositoryConfiguration r = new RemoteRepositoryConfiguration();
158         r.setId( REPO_ID );
159         populateRepository( r );
160         return r;
161     }
162     
163     private void assertRepositoryEquals( RemoteRepositoryConfiguration expectedRepository,
164                                          RemoteRepositoryConfiguration actualRepository )
165     {
166         assertEquals( expectedRepository.getId(), actualRepository.getId() );
167         assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
168         assertEquals( expectedRepository.getUrl(), actualRepository.getUrl() );
169         assertEquals( expectedRepository.getName(), actualRepository.getName() );
170     }
171     
172     private ManagedRepositoryConfiguration createManagedRepository( String string, String testPath )
173     {
174         ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
175         r.setId( REPO_ID );
176         r.setName( "repo name" );
177         r.setLocation( testPath );
178         r.setLayout( "default" );
179         r.setRefreshCronExpression( "* 0/5 * * * ?" );
180         r.setDaysOlder( 0 );
181         r.setRetentionCount( 0 );
182         r.setReleases( true );
183         r.setSnapshots( true );
184         r.setScanned( false );
185         r.setDeleteReleasedSnapshots( false );
186         return r;
187     }
188
189     private ProxyConnectorConfiguration createProxyConnector( String managedRepoId, String remoteRepoId )
190     {
191         ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
192         connector.setSourceRepoId( managedRepoId );
193         connector.setTargetRepoId( remoteRepoId );
194
195         return connector;
196     }
197
198     private void populateRepository( RemoteRepositoryConfiguration repository )
199     {
200         repository.setId( REPO_ID );
201         repository.setName( "repo name" );
202         repository.setUrl( "url" );
203         repository.setLayout( "default" );
204     }
205     
206     // TODO: what about removing proxied content if a proxy is removed?
207 }