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.5KB

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