001package org.apache.maven.repository.legacy;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023
024import org.apache.maven.artifact.AbstractArtifactComponentTestCase;
025import org.apache.maven.artifact.Artifact;
026import org.apache.maven.artifact.factory.ArtifactFactory;
027import org.apache.maven.artifact.repository.ArtifactRepository;
028import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
029import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
030import org.apache.maven.repository.legacy.DefaultUpdateCheckManager;
031import org.codehaus.plexus.logging.Logger;
032import org.codehaus.plexus.logging.console.ConsoleLogger;
033
034public class DefaultUpdateCheckManagerTest
035    extends AbstractArtifactComponentTestCase
036{
037
038    DefaultUpdateCheckManager updateCheckManager;
039
040    @Override
041    protected String component()
042    {
043        return "updateCheckManager";
044    }
045
046    @Override
047    protected void setUp()
048        throws Exception
049    {
050        super.setUp();
051
052        updateCheckManager = new DefaultUpdateCheckManager( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) );
053    }
054
055    public void testArtifact() throws Exception
056    {
057        ArtifactRepository remoteRepository = remoteRepository();
058
059        ArtifactRepository localRepository = localRepository();
060
061        Artifact a = createArtifact( "a", "0.0.1-SNAPSHOT" );
062        File file = new File( localRepository.getBasedir(), 
063                              localRepository.pathOf( a ) );
064        file.delete();
065        a.setFile( file );
066
067        File touchFile = updateCheckManager.getTouchfile( a );
068        touchFile.delete();
069
070        assertTrue( updateCheckManager.isUpdateRequired( a, remoteRepository ) );
071
072        file.getParentFile().mkdirs();
073        file.createNewFile();
074        updateCheckManager.touch( a, remoteRepository, null );
075
076        assertFalse( updateCheckManager.isUpdateRequired( a, remoteRepository ) );
077
078        assertNull( updateCheckManager.readLastUpdated( touchFile,
079                                                        updateCheckManager.getRepositoryKey( remoteRepository ) ) );
080        
081        assertFalse( updateCheckManager.getTouchfile( a ).exists() );
082    }
083
084    public void testMissingArtifact()
085        throws Exception
086    {
087        ArtifactRepository remoteRepository = remoteRepository();
088
089        ArtifactRepository localRepository = localRepository();
090
091        Artifact a = createArtifact( "a", "0.0.1-SNAPSHOT" );
092        File file = new File( localRepository.getBasedir(), 
093                              localRepository.pathOf( a ) );
094        file.delete();
095        a.setFile( file );
096
097        File touchFile = updateCheckManager.getTouchfile( a );
098        touchFile.delete();
099
100        assertTrue( updateCheckManager.isUpdateRequired( a, remoteRepository ) );
101
102        updateCheckManager.touch( a, remoteRepository, null );
103
104        assertFalse( updateCheckManager.isUpdateRequired( a, remoteRepository ) );
105
106        assertFalse( file.exists() );
107        assertNotNull( updateCheckManager.readLastUpdated( touchFile,
108                                                           updateCheckManager.getRepositoryKey( remoteRepository ) ) );
109    }
110
111    public void testPom() throws Exception
112    {
113        ArtifactRepository remoteRepository = remoteRepository();
114
115        ArtifactRepository localRepository = localRepository();
116
117        Artifact a = createArtifact( "a", "0.0.1", "pom" );
118        File file = new File( localRepository.getBasedir(), 
119                              localRepository.pathOf( a ) );
120        file.delete();
121        a.setFile( file );
122
123        File touchFile = updateCheckManager.getTouchfile( a );
124        touchFile.delete();
125
126        assertTrue( updateCheckManager.isUpdateRequired( a, remoteRepository ) );
127
128        file.getParentFile().mkdirs();
129        file.createNewFile();
130        updateCheckManager.touch( a, remoteRepository, null );
131
132        assertFalse( updateCheckManager.isUpdateRequired( a, remoteRepository ) );
133
134        assertNull( updateCheckManager.readLastUpdated( touchFile,
135                                                        updateCheckManager.getRepositoryKey( remoteRepository ) ) );
136        
137        assertFalse( updateCheckManager.getTouchfile( a ).exists() );
138    }
139
140    public void testMissingPom()
141        throws Exception
142    {
143        ArtifactRepository remoteRepository = remoteRepository();
144
145        ArtifactRepository localRepository = localRepository();
146
147        Artifact a = createArtifact( "a", "0.0.1", "pom" );
148        File file = new File( localRepository.getBasedir(), 
149                              localRepository.pathOf( a ) );
150        file.delete();
151        a.setFile( file );
152
153        File touchFile = updateCheckManager.getTouchfile( a );
154        touchFile.delete();
155
156        assertTrue( updateCheckManager.isUpdateRequired( a, remoteRepository ) );
157
158        updateCheckManager.touch( a, remoteRepository, null );
159
160        assertFalse( updateCheckManager.isUpdateRequired( a, remoteRepository ) );
161
162        assertFalse( file.exists() );
163        assertNotNull( updateCheckManager.readLastUpdated( touchFile,
164                                                           updateCheckManager.getRepositoryKey( remoteRepository ) ) );
165    }
166
167    public void testMetadata() throws Exception
168    {
169        ArtifactRepository remoteRepository = remoteRepository();
170
171        ArtifactRepository localRepository = localRepository();
172
173        Artifact a = createRemoteArtifact( "a", "0.0.1-SNAPSHOT" );
174        RepositoryMetadata metadata = new ArtifactRepositoryMetadata( a );
175
176        File file = new File( localRepository.getBasedir(),
177                              localRepository.pathOfLocalRepositoryMetadata( metadata, localRepository ) );
178        file.delete();
179
180        File touchFile = updateCheckManager.getTouchfile( metadata, file );
181        touchFile.delete();
182
183        assertTrue( updateCheckManager.isUpdateRequired( metadata, remoteRepository, file ) );
184
185        file.getParentFile().mkdirs();
186        file.createNewFile();
187        updateCheckManager.touch( metadata, remoteRepository, file );
188
189        assertFalse( updateCheckManager.isUpdateRequired( metadata, remoteRepository, file ) );
190
191        assertNotNull( updateCheckManager.readLastUpdated( touchFile, updateCheckManager.getMetadataKey( remoteRepository, file ) ) );
192    }
193
194    public void testMissingMetadata() throws Exception
195    {
196        ArtifactRepository remoteRepository = remoteRepository();
197
198        ArtifactRepository localRepository = localRepository();
199
200        Artifact a = createRemoteArtifact( "a", "0.0.1-SNAPSHOT" );
201        RepositoryMetadata metadata = new ArtifactRepositoryMetadata( a );
202
203        File file = new File( localRepository.getBasedir(),
204                              localRepository.pathOfLocalRepositoryMetadata( metadata, localRepository ) );
205        file.delete();
206
207        File touchFile = updateCheckManager.getTouchfile( metadata, file );
208        touchFile.delete();
209
210        assertTrue( updateCheckManager.isUpdateRequired( metadata, remoteRepository, file ) );
211
212        updateCheckManager.touch( metadata, remoteRepository, file );
213
214        assertFalse( updateCheckManager.isUpdateRequired( metadata, remoteRepository, file ) );
215
216        assertNotNull( updateCheckManager.readLastUpdated( touchFile, updateCheckManager.getMetadataKey( remoteRepository, file ) ) );
217    }
218
219    public void testArtifactTouchFileName() throws Exception
220    {
221        ArtifactFactory artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
222
223        ArtifactRepository localRepository = localRepository();
224
225        Artifact a = artifactFactory.createArtifactWithClassifier( "groupdId", "a", "0.0.1-SNAPSHOT", "jar", null );
226        File file = new File( localRepository.getBasedir(), 
227                              localRepository.pathOf( a ) );
228        a.setFile( file );
229
230        assertEquals( "a-0.0.1-SNAPSHOT.jar.lastUpdated", updateCheckManager.getTouchfile( a ).getName() );
231
232        a = artifactFactory.createArtifactWithClassifier( "groupdId", "a", "0.0.1-SNAPSHOT", "jar", "classifier" );
233        file = new File( localRepository.getBasedir(), 
234                              localRepository.pathOf( a ) );
235        a.setFile( file );
236
237        assertEquals( "a-0.0.1-SNAPSHOT-classifier.jar.lastUpdated", updateCheckManager.getTouchfile( a ).getName() );
238    }
239
240}