1 package org.apache.archiva.admin.repository;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import junit.framework.TestCase;
22 import org.apache.archiva.admin.mock.MockAuditListener;
23 import org.apache.archiva.admin.model.AuditInformation;
24 import org.apache.archiva.admin.model.beans.ManagedRepository;
25 import org.apache.archiva.admin.model.beans.RemoteRepository;
26 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
27 import org.apache.archiva.admin.model.proxyconnector.ProxyConnectorAdmin;
28 import org.apache.archiva.admin.model.proxyconnectorrule.ProxyConnectorRuleAdmin;
29 import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
30 import org.apache.archiva.configuration.ArchivaConfiguration;
31 import org.apache.archiva.redback.role.RoleManager;
32 import org.apache.archiva.redback.users.User;
33 import org.apache.archiva.redback.users.memory.SimpleUser;
34 import org.apache.archiva.repository.RepositoryRegistry;
35 import org.apache.archiva.repository.base.group.RepositoryGroupHandler;
36 import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler;
37 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
38 import org.apache.commons.lang3.StringUtils;
39 import org.junit.Before;
40 import org.junit.runner.RunWith;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.test.context.ContextConfiguration;
45 import javax.inject.Inject;
46 import java.io.IOException;
47 import java.nio.file.Files;
48 import java.nio.file.Path;
49 import java.nio.file.Paths;
50 import java.util.HashMap;
51 import java.util.List;
52 import java.util.Locale;
56 * @author Olivier Lamy
58 @RunWith (ArchivaSpringJUnit4ClassRunner.class)
59 @ContextConfiguration (locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
60 public abstract class AbstractRepositoryAdminTest
63 protected Logger log = LoggerFactory.getLogger( getClass() );
65 public static final String APPSERVER_BASE_PATH =
66 AbstractRepositoryAdminTest.fixPath( System.getProperty( "appserver.base" ) );
69 protected MockAuditListener mockAuditListener;
72 protected RoleManager roleManager;
75 protected RemoteRepositoryAdmin remoteRepositoryAdmin;
78 protected ManagedRepositoryAdmin managedRepositoryAdmin;
81 protected ProxyConnectorAdmin proxyConnectorAdmin;
84 protected ProxyConnectorRuleAdmin proxyConnectorRuleAdmin;
87 private ArchivaConfiguration archivaConfiguration;
90 protected RepositoryRegistry repositoryRegistry;
92 @SuppressWarnings( "unused" )
94 ManagedRepositoryHandler managedRepositoryHandler;
96 @SuppressWarnings( "unused" )
98 RepositoryGroupHandler repositoryGroupHandler;
102 public void initialize() {
103 Path confFile = Paths.get(APPSERVER_BASE_PATH, "conf/archiva.xml");
106 Files.deleteIfExists( confFile );
107 archivaConfiguration.reload();
109 catch ( IOException e )
115 protected AuditInformation getFakeAuditInformation()
117 AuditInformation auditInformation = new AuditInformation( getFakeUser(), "archiva-localhost" );
118 return auditInformation;
121 // make a nice repo path to allow unit test to run
122 private static String fixPath( String path )
125 if ( path.contains( SPACE ) )
127 LoggerFactory.getLogger( AbstractRepositoryAdminTest.class.getName() ).error(
128 "You are building and testing with {appserver.base}: \n {}"
129 + " containing space. Consider relocating.", path );
131 return path.replaceAll( SPACE, "&20" );
134 protected User getFakeUser()
136 SimpleUser user = new SimpleUser();
138 user.setUsername( "root" );
139 user.setFullName( "The top user" );
143 protected ManagedRepository getTestManagedRepository( String repoId, String repoLocation )
145 String repoLocationStr = Paths.get(repoLocation, ".index").toString();
146 return new ManagedRepository( Locale.getDefault( ), repoId, "test repo", repoLocation, "default", false, true, true, "0 0 * * * ?",
147 repoLocationStr, false, 1, 2, true, false );
150 protected Path clearRepoLocation(String path )
153 Path repoDir = Paths.get( path );
154 if ( Files.exists(repoDir) )
156 org.apache.archiva.common.utils.FileUtils.deleteDirectory( repoDir );
158 assertFalse( Files.exists(repoDir) );
162 protected ManagedRepository findManagedRepoById( List<ManagedRepository> repos, String id )
164 for ( ManagedRepository repo : repos )
166 if ( StringUtils.equals( id, repo.getId() ) )
174 protected RemoteRepository getRemoteRepository()
176 return getRemoteRepository( "foo" );
179 protected RemoteRepository getRemoteRepository( String id )
181 RemoteRepository remoteRepository = new RemoteRepository(Locale.getDefault());
182 remoteRepository.setUrl( "http://foo.com/maven-it-rocks" );
183 remoteRepository.setTimeout( 10 );
184 remoteRepository.setName( "maven foo" );
185 remoteRepository.setUserName( "foo-name" );
186 remoteRepository.setPassword( "toto" );
187 remoteRepository.setId( id );
188 remoteRepository.setRemoteDownloadNetworkProxyId( "foo" );
189 remoteRepository.setDescription( "cool apache repo" );
190 Map<String, String> extraParameters = new HashMap<>();
191 extraParameters.put( "foo", "bar" );
192 remoteRepository.setExtraParameters( extraParameters );
193 Map<String, String> extraHeaders = new HashMap<>();
194 extraHeaders.put( "beer", "wine" );
195 remoteRepository.setExtraHeaders( extraHeaders );
196 return remoteRepository;