1 package org.apache.maven.repository.discovery;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.maven.artifact.Artifact;
20 import org.apache.maven.artifact.factory.ArtifactFactory;
21 import org.apache.maven.artifact.repository.ArtifactRepository;
22 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
23 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
24 import org.codehaus.plexus.PlexusTestCase;
25 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
28 import java.io.IOException;
29 import java.text.ParseException;
30 import java.text.SimpleDateFormat;
31 import java.util.Date;
32 import java.util.List;
33 import java.util.Locale;
36 * @author Edwin Punzalan
38 public abstract class AbstractArtifactDiscovererTest
39 extends PlexusTestCase
41 protected ArtifactDiscoverer discoverer;
43 private ArtifactFactory factory;
45 protected ArtifactRepository repository;
47 protected static final String TEST_OPERATION = "test";
49 protected abstract String getLayout();
51 protected abstract File getRepositoryFile();
53 protected void setUp()
58 discoverer = (ArtifactDiscoverer) lookup( ArtifactDiscoverer.ROLE, getLayout() );
60 factory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
62 repository = getRepository();
64 removeTimestampMetadata();
67 protected ArtifactRepository getRepository()
70 File basedir = getRepositoryFile();
72 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
74 ArtifactRepositoryLayout layout =
75 (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, getLayout() );
77 return factory.createArtifactRepository( "discoveryRepo", "file://" + basedir, layout, null, null );
80 protected Artifact createArtifact( String groupId, String artifactId, String version )
82 Artifact artifact = factory.createArtifact( groupId, artifactId, version, null, "jar" );
83 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
84 artifact.setRepository( repository );
88 protected Artifact createArtifact( String groupId, String artifactId, String version, String type )
90 return factory.createArtifact( groupId, artifactId, version, null, type );
93 protected Artifact createArtifact( String groupId, String artifactId, String version, String type,
96 return factory.createArtifactWithClassifier( groupId, artifactId, version, type, classifier );
99 public void testUpdatedInRepository()
100 throws ComponentLookupException, DiscovererException, ParseException, IOException
102 // Set repository time to 1-1-2000, a time in the distant past so definitely updated
103 discoverer.setLastCheckedTime( repository, "update",
104 new SimpleDateFormat( "yyyy-MM-dd", Locale.US ).parse( "2000-01-01" ) );
106 List artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
107 assertNotNull( "Check artifacts not null", artifacts );
109 assertTrue( "Check included",
110 artifacts.contains( createArtifact( "org.apache.maven.update", "test-updated", "1.0" ) ) );
112 // try again with the updated timestamp
113 artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
114 assertNotNull( "Check artifacts not null", artifacts );
116 assertFalse( "Check not included",
117 artifacts.contains( createArtifact( "org.apache.maven.update", "test-updated", "1.0" ) ) );
120 public void testNotUpdatedInRepository()
121 throws ComponentLookupException, DiscovererException, IOException
123 // Set repository time to now, which is after any artifacts, so definitely not updated
124 discoverer.setLastCheckedTime( repository, "update", new Date() );
126 List artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
127 assertNotNull( "Check artifacts not null", artifacts );
129 assertFalse( "Check not included",
130 artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
133 public void testNotUpdatedInRepositoryForcedDiscovery()
134 throws ComponentLookupException, DiscovererException, IOException
136 discoverer.resetLastCheckedTime( repository, "update" );
138 List artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
139 assertNotNull( "Check artifacts not null", artifacts );
141 assertTrue( "Check included",
142 artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
144 // try again with the updated timestamp
145 artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
146 assertNotNull( "Check artifacts not null", artifacts );
148 assertFalse( "Check not included",
149 artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
152 public void testUpdatedInRepositoryBlackout()
153 throws ComponentLookupException, DiscovererException, IOException
155 discoverer.resetLastCheckedTime( repository, "update" );
157 Artifact artifact = createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" );
158 artifact.getFile().setLastModified( System.currentTimeMillis() );
160 List artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
161 assertNotNull( "Check artifacts not null", artifacts );
163 assertFalse( "Check not included", artifacts.contains( artifact ) );
165 // try again with the updated timestamp
166 artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
167 assertNotNull( "Check artifacts not null", artifacts );
169 assertFalse( "Check not included", artifacts.contains( artifact ) );
172 public void testUpdatedInRepositoryNotBlackout()
173 throws ComponentLookupException, DiscovererException, IOException
175 discoverer.resetLastCheckedTime( repository, "update" );
177 Artifact artifact = createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" );
178 artifact.getFile().setLastModified( System.currentTimeMillis() - 61000 );
180 List artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
181 assertNotNull( "Check artifacts not null", artifacts );
183 assertTrue( "Check included", artifacts.contains( artifact ) );
185 // try again with the updated timestamp
186 artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
187 assertNotNull( "Check artifacts not null", artifacts );
189 assertFalse( "Check not included", artifacts.contains( artifact ) );
192 public void testNotUpdatedInRepositoryForcedDiscoveryMetadataAlreadyExists()
193 throws ComponentLookupException, DiscovererException, IOException
195 discoverer.setLastCheckedTime( repository, "update", new Date() );
197 discoverer.resetLastCheckedTime( repository, "update" );
199 List artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
200 assertNotNull( "Check artifacts not null", artifacts );
202 assertTrue( "Check included",
203 artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
205 // try again with the updated timestamp
206 artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
207 assertNotNull( "Check artifacts not null", artifacts );
209 assertFalse( "Check not included",
210 artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
213 public void testNotUpdatedInRepositoryForcedDiscoveryOtherMetadataAlreadyExists()
214 throws ComponentLookupException, DiscovererException, IOException
216 discoverer.setLastCheckedTime( repository, "test", new Date() );
218 discoverer.resetLastCheckedTime( repository, "update" );
220 List artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
221 assertNotNull( "Check artifacts not null", artifacts );
223 assertTrue( "Check included",
224 artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
226 // try again with the updated timestamp
227 artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
228 assertNotNull( "Check artifacts not null", artifacts );
230 assertFalse( "Check not included",
231 artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
234 public void testNoRepositoryMetadata()
235 throws ComponentLookupException, DiscovererException, ParseException, IOException
237 removeTimestampMetadata();
240 List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
241 assertNotNull( "Check artifacts not null", artifacts );
243 assertTrue( "Check included",
244 artifacts.contains( createArtifact( "org.apache.maven.update", "test-updated", "1.0" ) ) );
247 private void removeTimestampMetadata()
249 // remove the metadata that tracks time
250 File file = new File( repository.getBasedir(), "maven-metadata.xml" );
252 assertFalse( file.exists() );