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.RepositoryHandlerDependencies;
36 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
37 import org.apache.commons.lang3.StringUtils;
38 import org.junit.Before;
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;
89 protected RepositoryRegistry repositoryRegistry;
91 @SuppressWarnings( "unused" )
93 RepositoryHandlerDependencies repositoryHandlerDependencies;
96 public void initialize() {
97 Path confFile = Paths.get(APPSERVER_BASE_PATH, "conf/archiva.xml");
100 Files.deleteIfExists( confFile );
101 archivaConfiguration.reload();
103 catch ( IOException e )
109 protected AuditInformation getFakeAuditInformation()
111 AuditInformation auditInformation = new AuditInformation( getFakeUser(), "archiva-localhost" );
112 return auditInformation;
115 // make a nice repo path to allow unit test to run
116 private static String fixPath( String path )
119 if ( path.contains( SPACE ) )
121 LoggerFactory.getLogger( AbstractRepositoryAdminTest.class.getName() ).error(
122 "You are building and testing with {appserver.base}: \n {}"
123 + " containing space. Consider relocating.", path );
125 return path.replaceAll( SPACE, "&20" );
128 protected User getFakeUser()
130 SimpleUser user = new SimpleUser();
132 user.setUsername( "root" );
133 user.setFullName( "The top user" );
137 protected ManagedRepository getTestManagedRepository( String repoId, String repoLocation )
139 String repoLocationStr = Paths.get(repoLocation, ".index").toString();
140 return new ManagedRepository( Locale.getDefault( ), repoId, "test repo", repoLocation, "default", false, true, true, "0 0 * * * ?",
141 repoLocationStr, false, 1, 2, true, false );
144 protected Path clearRepoLocation(String path )
147 Path repoDir = Paths.get( path );
148 if ( Files.exists(repoDir) )
150 org.apache.archiva.common.utils.FileUtils.deleteDirectory( repoDir );
152 assertFalse( Files.exists(repoDir) );
156 protected ManagedRepository findManagedRepoById( List<ManagedRepository> repos, String id )
158 for ( ManagedRepository repo : repos )
160 if ( StringUtils.equals( id, repo.getId() ) )
168 protected RemoteRepository getRemoteRepository()
170 return getRemoteRepository( "foo" );
173 protected RemoteRepository getRemoteRepository( String id )
175 RemoteRepository remoteRepository = new RemoteRepository(Locale.getDefault());
176 remoteRepository.setUrl( "http://foo.com/maven-it-rocks" );
177 remoteRepository.setTimeout( 10 );
178 remoteRepository.setName( "maven foo" );
179 remoteRepository.setUserName( "foo-name" );
180 remoteRepository.setPassword( "toto" );
181 remoteRepository.setId( id );
182 remoteRepository.setRemoteDownloadNetworkProxyId( "foo" );
183 remoteRepository.setDescription( "cool apache repo" );
184 Map<String, String> extraParameters = new HashMap<>();
185 extraParameters.put( "foo", "bar" );
186 remoteRepository.setExtraParameters( extraParameters );
187 Map<String, String> extraHeaders = new HashMap<>();
188 extraHeaders.put( "beer", "wine" );
189 remoteRepository.setExtraHeaders( extraHeaders );
190 return remoteRepository;