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.base.RepositoryGroupHandler;
35 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
36 import org.apache.commons.lang3.StringUtils;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.runner.RunWith;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.test.context.ContextConfiguration;
44 import javax.inject.Inject;
45 import java.io.IOException;
46 import java.nio.file.Files;
47 import java.nio.file.Path;
48 import java.nio.file.Paths;
49 import java.util.HashMap;
50 import java.util.List;
51 import java.util.Locale;
55 * @author Olivier Lamy
57 @RunWith (ArchivaSpringJUnit4ClassRunner.class)
58 @ContextConfiguration (locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
59 public abstract class AbstractRepositoryAdminTest
62 protected Logger log = LoggerFactory.getLogger( getClass() );
64 public static final String APPSERVER_BASE_PATH =
65 AbstractRepositoryAdminTest.fixPath( System.getProperty( "appserver.base" ) );
68 protected MockAuditListener mockAuditListener;
71 protected RoleManager roleManager;
74 protected RemoteRepositoryAdmin remoteRepositoryAdmin;
77 protected ManagedRepositoryAdmin managedRepositoryAdmin;
80 protected ProxyConnectorAdmin proxyConnectorAdmin;
83 protected ProxyConnectorRuleAdmin proxyConnectorRuleAdmin;
86 private ArchivaConfiguration archivaConfiguration;
88 @SuppressWarnings( "unused" )
90 RepositoryGroupHandler repositoryGroupHandler;
93 public void initialize() {
94 Path confFile = Paths.get(APPSERVER_BASE_PATH, "conf/archiva.xml");
97 Files.deleteIfExists( confFile );
98 archivaConfiguration.reload();
100 catch ( IOException e )
106 protected AuditInformation getFakeAuditInformation()
108 AuditInformation auditInformation = new AuditInformation( getFakeUser(), "archiva-localhost" );
109 return auditInformation;
112 // make a nice repo path to allow unit test to run
113 private static String fixPath( String path )
116 if ( path.contains( SPACE ) )
118 LoggerFactory.getLogger( AbstractRepositoryAdminTest.class.getName() ).error(
119 "You are building and testing with {appserver.base}: \n {}"
120 + " containing space. Consider relocating.", path );
122 return path.replaceAll( SPACE, "&20" );
125 protected User getFakeUser()
127 SimpleUser user = new SimpleUser();
129 user.setUsername( "root" );
130 user.setFullName( "The top user" );
134 protected ManagedRepository getTestManagedRepository( String repoId, String repoLocation )
136 String repoLocationStr = Paths.get(repoLocation, ".index").toString();
137 return new ManagedRepository( Locale.getDefault( ), repoId, "test repo", repoLocation, "default", false, true, true, "0 0 * * * ?",
138 repoLocationStr, false, 1, 2, true, false );
141 protected Path clearRepoLocation(String path )
144 Path repoDir = Paths.get( path );
145 if ( Files.exists(repoDir) )
147 org.apache.archiva.common.utils.FileUtils.deleteDirectory( repoDir );
149 assertFalse( Files.exists(repoDir) );
153 protected ManagedRepository findManagedRepoById( List<ManagedRepository> repos, String id )
155 for ( ManagedRepository repo : repos )
157 if ( StringUtils.equals( id, repo.getId() ) )
165 protected RemoteRepository getRemoteRepository()
167 return getRemoteRepository( "foo" );
170 protected RemoteRepository getRemoteRepository( String id )
172 RemoteRepository remoteRepository = new RemoteRepository(Locale.getDefault());
173 remoteRepository.setUrl( "http://foo.com/maven-it-rocks" );
174 remoteRepository.setTimeout( 10 );
175 remoteRepository.setName( "maven foo" );
176 remoteRepository.setUserName( "foo-name" );
177 remoteRepository.setPassword( "toto" );
178 remoteRepository.setId( id );
179 remoteRepository.setRemoteDownloadNetworkProxyId( "foo" );
180 remoteRepository.setDescription( "cool apache repo" );
181 Map<String, String> extraParameters = new HashMap<>();
182 extraParameters.put( "foo", "bar" );
183 remoteRepository.setExtraParameters( extraParameters );
184 Map<String, String> extraHeaders = new HashMap<>();
185 extraHeaders.put( "beer", "wine" );
186 remoteRepository.setExtraHeaders( extraHeaders );
187 return remoteRepository;