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.redback.role.RoleManager;
31 import org.apache.archiva.redback.users.User;
32 import org.apache.archiva.redback.users.memory.SimpleUser;
33 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
34 import org.apache.commons.lang.StringUtils;
35 import org.junit.runner.RunWith;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.test.context.ContextConfiguration;
40 import javax.inject.Inject;
41 import java.nio.file.Files;
42 import java.nio.file.Path;
43 import java.nio.file.Paths;
44 import java.util.HashMap;
45 import java.util.List;
46 import java.util.Locale;
50 * @author Olivier Lamy
52 @RunWith (ArchivaSpringJUnit4ClassRunner.class)
53 @ContextConfiguration (locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
54 public abstract class AbstractRepositoryAdminTest
57 protected Logger log = LoggerFactory.getLogger( getClass() );
59 public static final String APPSERVER_BASE_PATH =
60 AbstractRepositoryAdminTest.fixPath( System.getProperty( "appserver.base" ) );
63 protected MockAuditListener mockAuditListener;
66 protected RoleManager roleManager;
69 protected RemoteRepositoryAdmin remoteRepositoryAdmin;
72 protected ManagedRepositoryAdmin managedRepositoryAdmin;
75 protected ProxyConnectorAdmin proxyConnectorAdmin;
78 protected ProxyConnectorRuleAdmin proxyConnectorRuleAdmin;
80 protected AuditInformation getFakeAuditInformation()
82 AuditInformation auditInformation = new AuditInformation( getFakeUser(), "archiva-localhost" );
83 return auditInformation;
86 // make a nice repo path to allow unit test to run
87 private static String fixPath( String path )
90 if ( path.contains( SPACE ) )
92 LoggerFactory.getLogger( AbstractRepositoryAdminTest.class.getName() ).error(
93 "You are building and testing with {appserver.base}: \n {}"
94 + " containing space. Consider relocating.", path );
96 return path.replaceAll( SPACE, "&20" );
99 protected User getFakeUser()
101 SimpleUser user = new SimpleUser();
103 user.setUsername( "root" );
104 user.setFullName( "The top user" );
108 protected ManagedRepository getTestManagedRepository( String repoId, String repoLocation )
110 String repoLocationStr = Paths.get(repoLocation, ".index").toString();
111 return new ManagedRepository( Locale.getDefault( ), repoId, "test repo", repoLocation, "default", false, true, true, "0 0 * * * ?",
112 repoLocationStr, false, 1, 2, true, false );
115 protected Path clearRepoLocation(String path )
118 Path repoDir = Paths.get( path );
119 if ( Files.exists(repoDir) )
121 org.apache.archiva.common.utils.FileUtils.deleteDirectory( repoDir );
123 assertFalse( Files.exists(repoDir) );
127 protected ManagedRepository findManagedRepoById( List<ManagedRepository> repos, String id )
129 for ( ManagedRepository repo : repos )
131 if ( StringUtils.equals( id, repo.getId() ) )
139 protected RemoteRepository getRemoteRepository()
141 return getRemoteRepository( "foo" );
144 protected RemoteRepository getRemoteRepository( String id )
146 RemoteRepository remoteRepository = new RemoteRepository(Locale.getDefault());
147 remoteRepository.setUrl( "http://foo.com/maven-it-rocks" );
148 remoteRepository.setTimeout( 10 );
149 remoteRepository.setName( "maven foo" );
150 remoteRepository.setUserName( "foo-name" );
151 remoteRepository.setPassword( "toto" );
152 remoteRepository.setId( id );
153 remoteRepository.setRemoteDownloadNetworkProxyId( "foo" );
154 remoteRepository.setDescription( "cool apache repo" );
155 Map<String, String> extraParameters = new HashMap<>();
156 extraParameters.put( "foo", "bar" );
157 remoteRepository.setExtraParameters( extraParameters );
158 Map<String, String> extraHeaders = new HashMap<>();
159 extraHeaders.put( "beer", "wine" );
160 remoteRepository.setExtraHeaders( extraHeaders );
161 return remoteRepository;