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.test.utils.ArchivaSpringJUnit4ClassRunner;
35 import org.apache.commons.lang3.StringUtils;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.runner.RunWith;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.test.context.ContextConfiguration;
43 import javax.inject.Inject;
44 import java.io.IOException;
45 import java.nio.file.Files;
46 import java.nio.file.Path;
47 import java.nio.file.Paths;
48 import java.util.HashMap;
49 import java.util.List;
50 import java.util.Locale;
54 * @author Olivier Lamy
56 @RunWith (ArchivaSpringJUnit4ClassRunner.class)
57 @ContextConfiguration (locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
58 public abstract class AbstractRepositoryAdminTest
61 protected Logger log = LoggerFactory.getLogger( getClass() );
63 public static final String APPSERVER_BASE_PATH =
64 AbstractRepositoryAdminTest.fixPath( System.getProperty( "appserver.base" ) );
67 protected MockAuditListener mockAuditListener;
70 protected RoleManager roleManager;
73 protected RemoteRepositoryAdmin remoteRepositoryAdmin;
76 protected ManagedRepositoryAdmin managedRepositoryAdmin;
79 protected ProxyConnectorAdmin proxyConnectorAdmin;
82 protected ProxyConnectorRuleAdmin proxyConnectorRuleAdmin;
85 private ArchivaConfiguration archivaConfiguration;
88 public void initialize() {
89 Path confFile = Paths.get(APPSERVER_BASE_PATH, "conf/archiva.xml");
92 Files.deleteIfExists( confFile );
93 archivaConfiguration.reload();
95 catch ( IOException e )
101 protected AuditInformation getFakeAuditInformation()
103 AuditInformation auditInformation = new AuditInformation( getFakeUser(), "archiva-localhost" );
104 return auditInformation;
107 // make a nice repo path to allow unit test to run
108 private static String fixPath( String path )
111 if ( path.contains( SPACE ) )
113 LoggerFactory.getLogger( AbstractRepositoryAdminTest.class.getName() ).error(
114 "You are building and testing with {appserver.base}: \n {}"
115 + " containing space. Consider relocating.", path );
117 return path.replaceAll( SPACE, "&20" );
120 protected User getFakeUser()
122 SimpleUser user = new SimpleUser();
124 user.setUsername( "root" );
125 user.setFullName( "The top user" );
129 protected ManagedRepository getTestManagedRepository( String repoId, String repoLocation )
131 String repoLocationStr = Paths.get(repoLocation, ".index").toString();
132 return new ManagedRepository( Locale.getDefault( ), repoId, "test repo", repoLocation, "default", false, true, true, "0 0 * * * ?",
133 repoLocationStr, false, 1, 2, true, false );
136 protected Path clearRepoLocation(String path )
139 Path repoDir = Paths.get( path );
140 if ( Files.exists(repoDir) )
142 org.apache.archiva.common.utils.FileUtils.deleteDirectory( repoDir );
144 assertFalse( Files.exists(repoDir) );
148 protected ManagedRepository findManagedRepoById( List<ManagedRepository> repos, String id )
150 for ( ManagedRepository repo : repos )
152 if ( StringUtils.equals( id, repo.getId() ) )
160 protected RemoteRepository getRemoteRepository()
162 return getRemoteRepository( "foo" );
165 protected RemoteRepository getRemoteRepository( String id )
167 RemoteRepository remoteRepository = new RemoteRepository(Locale.getDefault());
168 remoteRepository.setUrl( "http://foo.com/maven-it-rocks" );
169 remoteRepository.setTimeout( 10 );
170 remoteRepository.setName( "maven foo" );
171 remoteRepository.setUserName( "foo-name" );
172 remoteRepository.setPassword( "toto" );
173 remoteRepository.setId( id );
174 remoteRepository.setRemoteDownloadNetworkProxyId( "foo" );
175 remoteRepository.setDescription( "cool apache repo" );
176 Map<String, String> extraParameters = new HashMap<>();
177 extraParameters.put( "foo", "bar" );
178 remoteRepository.setExtraParameters( extraParameters );
179 Map<String, String> extraHeaders = new HashMap<>();
180 extraHeaders.put( "beer", "wine" );
181 remoteRepository.setExtraHeaders( extraHeaders );
182 return remoteRepository;