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.group.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.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;
87 @SuppressWarnings( "unused" )
89 RepositoryGroupHandler repositoryGroupHandler;
92 public void initialize() {
93 Path confFile = Paths.get(APPSERVER_BASE_PATH, "conf/archiva.xml");
96 Files.deleteIfExists( confFile );
97 archivaConfiguration.reload();
99 catch ( IOException e )
105 protected AuditInformation getFakeAuditInformation()
107 AuditInformation auditInformation = new AuditInformation( getFakeUser(), "archiva-localhost" );
108 return auditInformation;
111 // make a nice repo path to allow unit test to run
112 private static String fixPath( String path )
115 if ( path.contains( SPACE ) )
117 LoggerFactory.getLogger( AbstractRepositoryAdminTest.class.getName() ).error(
118 "You are building and testing with {appserver.base}: \n {}"
119 + " containing space. Consider relocating.", path );
121 return path.replaceAll( SPACE, "&20" );
124 protected User getFakeUser()
126 SimpleUser user = new SimpleUser();
128 user.setUsername( "root" );
129 user.setFullName( "The top user" );
133 protected ManagedRepository getTestManagedRepository( String repoId, String repoLocation )
135 String repoLocationStr = Paths.get(repoLocation, ".index").toString();
136 return new ManagedRepository( Locale.getDefault( ), repoId, "test repo", repoLocation, "default", false, true, true, "0 0 * * * ?",
137 repoLocationStr, false, 1, 2, true, false );
140 protected Path clearRepoLocation(String path )
143 Path repoDir = Paths.get( path );
144 if ( Files.exists(repoDir) )
146 org.apache.archiva.common.utils.FileUtils.deleteDirectory( repoDir );
148 assertFalse( Files.exists(repoDir) );
152 protected ManagedRepository findManagedRepoById( List<ManagedRepository> repos, String id )
154 for ( ManagedRepository repo : repos )
156 if ( StringUtils.equals( id, repo.getId() ) )
164 protected RemoteRepository getRemoteRepository()
166 return getRemoteRepository( "foo" );
169 protected RemoteRepository getRemoteRepository( String id )
171 RemoteRepository remoteRepository = new RemoteRepository(Locale.getDefault());
172 remoteRepository.setUrl( "http://foo.com/maven-it-rocks" );
173 remoteRepository.setTimeout( 10 );
174 remoteRepository.setName( "maven foo" );
175 remoteRepository.setUserName( "foo-name" );
176 remoteRepository.setPassword( "toto" );
177 remoteRepository.setId( id );
178 remoteRepository.setRemoteDownloadNetworkProxyId( "foo" );
179 remoteRepository.setDescription( "cool apache repo" );
180 Map<String, String> extraParameters = new HashMap<>();
181 extraParameters.put( "foo", "bar" );
182 remoteRepository.setExtraParameters( extraParameters );
183 Map<String, String> extraHeaders = new HashMap<>();
184 extraHeaders.put( "beer", "wine" );
185 remoteRepository.setExtraHeaders( extraHeaders );
186 return remoteRepository;