View Javadoc

1   package org.apache.maven.archiva.consumers.core.repository;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  
25  import org.apache.commons.io.FileUtils;
26  import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
27  import org.apache.maven.archiva.model.ArchivaArtifact;
28  import org.apache.maven.archiva.repository.ManagedRepositoryContent;
29  import org.apache.maven.archiva.repository.events.RepositoryListener;
30  import org.codehaus.plexus.spring.PlexusInSpringTestCase;
31  import org.easymock.MockControl;
32  
33  /**
34   */
35  public abstract class AbstractRepositoryPurgeTest
36      extends PlexusInSpringTestCase
37  {
38      public static final String TEST_REPO_ID = "test-repo";
39  
40      public static final String TEST_REPO_NAME = "Test Repository";
41  
42      public static final int TEST_RETENTION_COUNT = 2;
43  
44      public static final int TEST_DAYS_OLDER = 30;
45  
46      public static final String PATH_TO_BY_DAYS_OLD_ARTIFACT = "org/apache/maven/plugins/maven-install-plugin/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar";
47  
48      public static final String PATH_TO_BY_DAYS_OLD_METADATA_DRIVEN_ARTIFACT = "org/codehaus/plexus/plexus-utils/1.4.3-SNAPSHOT/plexus-utils-1.4.3-20070113.163208-4.jar";
49  
50      public static final String PATH_TO_BY_RETENTION_COUNT_ARTIFACT = "org/jruby/plugins/jruby-rake-plugin/1.0RC1-SNAPSHOT/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar";
51  
52      public static final String PATH_TO_BY_RETENTION_COUNT_POM = "org/codehaus/castor/castor-anttasks/1.1.2-SNAPSHOT/castor-anttasks-1.1.2-20070506.163513-2.pom";
53  
54      public static final String PATH_TO_TEST_ORDER_OF_DELETION = "org/apache/maven/plugins/maven-assembly-plugin/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar";
55  
56      protected static final String RELEASES_TEST_REPO_ID = "releases-test-repo-one";
57  
58      protected static final String RELEASES_TEST_REPO_NAME = "Releases Test Repo One";
59  
60      private ManagedRepositoryConfiguration config;
61  
62      private ManagedRepositoryContent repo;
63  
64      protected RepositoryPurge repoPurge;
65  
66      protected MockControl listenerControl;
67  
68      protected RepositoryListener listener;
69  
70      @Override
71      protected void setUp()
72          throws Exception
73      {
74          super.setUp();
75          
76          listenerControl = MockControl.createControl( RepositoryListener.class );
77  
78          listener = (RepositoryListener) listenerControl.getMock();
79      }
80      
81      @Override
82      protected void tearDown()
83          throws Exception
84      {
85          super.tearDown();
86          config = null;
87          repo = null;
88      }
89  
90      public ManagedRepositoryConfiguration getRepoConfiguration( String repoId, String repoName )
91      {
92          config = new ManagedRepositoryConfiguration();
93          config.setId( repoId );
94          config.setName( repoName );
95          config.setDaysOlder( TEST_DAYS_OLDER );
96          config.setLocation( getTestFile( "target/test-" + getName() + "/" + repoId ).getAbsolutePath() );
97          config.setReleases( true );
98          config.setSnapshots( true );
99          config.setDeleteReleasedSnapshots( true );
100         config.setRetentionCount( TEST_RETENTION_COUNT );
101         
102         return config;
103     }
104 
105     public ManagedRepositoryContent getRepository()
106         throws Exception
107     {
108         if ( repo == null )
109         {
110             repo = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );            
111             repo.setRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ) );
112         }
113 
114         return repo;
115     }
116 
117     protected void assertDeleted( String path )
118     {
119         assertFalse( "File should have been deleted: " + path, new File( path ).exists() );
120     }
121 
122     protected void assertExists( String path )
123     {
124         assertTrue( "File should exist: " + path, new File( path ).exists() );
125     }
126     
127     protected File getTestRepoRoot()
128     {
129         return getTestFile( "target/test-" + getName() + "/" + TEST_REPO_ID );
130     }
131 
132     protected String prepareTestRepos()
133         throws IOException
134     {
135         File testDir = getTestRepoRoot();
136         FileUtils.deleteDirectory( testDir );
137         FileUtils.copyDirectory( getTestFile( "target/test-classes/" + TEST_REPO_ID ), testDir );
138         
139         File releasesTestDir = getTestFile( "target/test-" + getName() + "/" + RELEASES_TEST_REPO_ID );
140         FileUtils.deleteDirectory( releasesTestDir );
141         FileUtils.copyDirectory( getTestFile( "target/test-classes/" + RELEASES_TEST_REPO_ID ), releasesTestDir );
142         
143         return testDir.getAbsolutePath();
144     }
145 
146     protected ArchivaArtifact createArtifact( String groupId, String artifactId, String version, String type )
147     {
148         return new ArchivaArtifact( groupId, artifactId, version, null, type, TEST_REPO_ID );
149     }
150 }