You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractRepositoryAdminTest.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package org.apache.archiva.admin.repository;
  2. /*
  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
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  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
  18. * under the License.
  19. */
  20. import junit.framework.TestCase;
  21. import org.apache.archiva.admin.mock.MockAuditListener;
  22. import org.apache.archiva.admin.model.AuditInformation;
  23. import org.apache.archiva.admin.model.beans.ManagedRepository;
  24. import org.apache.archiva.admin.model.beans.RemoteRepository;
  25. import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
  26. import org.apache.archiva.admin.model.proxyconnector.ProxyConnectorAdmin;
  27. import org.apache.archiva.admin.model.proxyconnectorrule.ProxyConnectorRuleAdmin;
  28. import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
  29. import org.apache.archiva.configuration.ArchivaConfiguration;
  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.repository.RepositoryRegistry;
  34. import org.apache.archiva.repository.base.RepositoryHandlerDependencies;
  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;
  42. import javax.inject.Inject;
  43. import java.io.IOException;
  44. import java.nio.file.Files;
  45. import java.nio.file.Path;
  46. import java.nio.file.Paths;
  47. import java.util.HashMap;
  48. import java.util.List;
  49. import java.util.Locale;
  50. import java.util.Map;
  51. /**
  52. * @author Olivier Lamy
  53. */
  54. @RunWith (ArchivaSpringJUnit4ClassRunner.class)
  55. @ContextConfiguration (locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
  56. public abstract class AbstractRepositoryAdminTest
  57. extends TestCase
  58. {
  59. protected Logger log = LoggerFactory.getLogger( getClass() );
  60. public static final String APPSERVER_BASE_PATH =
  61. AbstractRepositoryAdminTest.fixPath( System.getProperty( "appserver.base" ) );
  62. @Inject
  63. protected MockAuditListener mockAuditListener;
  64. @Inject
  65. protected RoleManager roleManager;
  66. @Inject
  67. protected RemoteRepositoryAdmin remoteRepositoryAdmin;
  68. @Inject
  69. protected ManagedRepositoryAdmin managedRepositoryAdmin;
  70. @Inject
  71. protected ProxyConnectorAdmin proxyConnectorAdmin;
  72. @Inject
  73. protected ProxyConnectorRuleAdmin proxyConnectorRuleAdmin;
  74. @Inject
  75. private ArchivaConfiguration archivaConfiguration;
  76. @Inject
  77. protected RepositoryRegistry repositoryRegistry;
  78. @SuppressWarnings( "unused" )
  79. @Inject
  80. RepositoryHandlerDependencies repositoryHandlerDependencies;
  81. @Before
  82. public void initialize() {
  83. Path confFile = Paths.get(APPSERVER_BASE_PATH, "conf/archiva.xml");
  84. try
  85. {
  86. Files.deleteIfExists( confFile );
  87. archivaConfiguration.reload();
  88. }
  89. catch ( IOException e )
  90. {
  91. // ignore
  92. }
  93. }
  94. protected AuditInformation getFakeAuditInformation()
  95. {
  96. AuditInformation auditInformation = new AuditInformation( getFakeUser(), "archiva-localhost" );
  97. return auditInformation;
  98. }
  99. // make a nice repo path to allow unit test to run
  100. private static String fixPath( String path )
  101. {
  102. String SPACE = " ";
  103. if ( path.contains( SPACE ) )
  104. {
  105. LoggerFactory.getLogger( AbstractRepositoryAdminTest.class.getName() ).error(
  106. "You are building and testing with {appserver.base}: \n {}"
  107. + " containing space. Consider relocating.", path );
  108. }
  109. return path.replaceAll( SPACE, "&20" );
  110. }
  111. protected User getFakeUser()
  112. {
  113. SimpleUser user = new SimpleUser();
  114. user.setUsername( "root" );
  115. user.setFullName( "The top user" );
  116. return user;
  117. }
  118. protected ManagedRepository getTestManagedRepository( String repoId, String repoLocation )
  119. {
  120. String repoLocationStr = Paths.get(repoLocation, ".index").toString();
  121. return new ManagedRepository( Locale.getDefault( ), repoId, "test repo", repoLocation, "default", false, true, true, "0 0 * * * ?",
  122. repoLocationStr, false, 1, 2, true, false );
  123. }
  124. protected Path clearRepoLocation(String path )
  125. throws Exception
  126. {
  127. Path repoDir = Paths.get( path );
  128. if ( Files.exists(repoDir) )
  129. {
  130. org.apache.archiva.common.utils.FileUtils.deleteDirectory( repoDir );
  131. }
  132. assertFalse( Files.exists(repoDir) );
  133. return repoDir;
  134. }
  135. protected ManagedRepository findManagedRepoById( List<ManagedRepository> repos, String id )
  136. {
  137. for ( ManagedRepository repo : repos )
  138. {
  139. if ( StringUtils.equals( id, repo.getId() ) )
  140. {
  141. return repo;
  142. }
  143. }
  144. return null;
  145. }
  146. protected RemoteRepository getRemoteRepository()
  147. {
  148. return getRemoteRepository( "foo" );
  149. }
  150. protected RemoteRepository getRemoteRepository( String id )
  151. {
  152. RemoteRepository remoteRepository = new RemoteRepository(Locale.getDefault());
  153. remoteRepository.setUrl( "http://foo.com/maven-it-rocks" );
  154. remoteRepository.setTimeout( 10 );
  155. remoteRepository.setName( "maven foo" );
  156. remoteRepository.setUserName( "foo-name" );
  157. remoteRepository.setPassword( "toto" );
  158. remoteRepository.setId( id );
  159. remoteRepository.setRemoteDownloadNetworkProxyId( "foo" );
  160. remoteRepository.setDescription( "cool apache repo" );
  161. Map<String, String> extraParameters = new HashMap<>();
  162. extraParameters.put( "foo", "bar" );
  163. remoteRepository.setExtraParameters( extraParameters );
  164. Map<String, String> extraHeaders = new HashMap<>();
  165. extraHeaders.put( "beer", "wine" );
  166. remoteRepository.setExtraHeaders( extraHeaders );
  167. return remoteRepository;
  168. }
  169. }