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