View Javadoc

1   package org.apache.maven.archiva.consumers.core;
2   
3   import java.io.File;
4   import java.util.Calendar;
5   
6   import org.apache.archiva.checksum.ChecksumAlgorithm;
7   import org.apache.archiva.checksum.ChecksummedFile;
8   import org.apache.commons.io.FileUtils;
9   import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
10  
11  /*
12   * Licensed to the Apache Software Foundation (ASF) under one
13   * or more contributor license agreements.  See the NOTICE file
14   * distributed with this work for additional information
15   * regarding copyright ownership.  The ASF licenses this file
16   * to you under the Apache License, Version 2.0 (the
17   * "License"); you may not use this file except in compliance
18   * with the License.  You may obtain a copy of the License at
19   *
20   *   http://www.apache.org/licenses/LICENSE-2.0
21   *
22   * Unless required by applicable law or agreed to in writing,
23   * software distributed under the License is distributed on an
24   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
25   * KIND, either express or implied.  See the License for the
26   * specific language governing permissions and limitations
27   * under the License.
28   */
29  
30  public class ArtifactMissingChecksumsConsumerTest
31      extends AbstractArtifactConsumerTest
32  {
33      private ManagedRepositoryConfiguration repoConfig;
34  
35      @Override
36      protected void setUp()
37          throws Exception
38      {
39          super.setUp();
40  
41          repoConfig = new ManagedRepositoryConfiguration();
42          repoConfig.setId( "test-repo" );
43          repoConfig.setName( "Test Repository" );
44          repoConfig.setLayout( "default" );
45          repoConfig.setLocation( new File( getBasedir(), "target/test-classes/test-repo/" ).getPath() );
46  
47          consumer = (ArtifactMissingChecksumsConsumer) lookup( "artifactMissingChecksumsConsumer" );
48      }
49  
50      public void testNoExistingChecksums()
51          throws Exception
52      {
53          String path = "/no-checksums-artifact/1.0/no-checksums-artifact-1.0.jar";
54  
55          File sha1File = new File( repoConfig.getLocation(), path + ".sha1" );
56          File md5File = new File( repoConfig.getLocation(), path + ".md5" );
57  
58          sha1File.delete();
59          md5File.delete();
60  
61          assertFalse( sha1File.exists() );
62          assertFalse( md5File.exists() );
63  
64          consumer.beginScan( repoConfig, Calendar.getInstance().getTime() );
65  
66          consumer.processFile( path );
67  
68          assertTrue( sha1File.exists() );
69          assertTrue( md5File.exists() );
70      }
71  
72      public void testExistingIncorrectChecksums()
73          throws Exception
74      {
75          File newLocation = getTestFile( "target/test-repo" );
76          FileUtils.deleteDirectory( newLocation );
77          FileUtils.copyDirectory( new File( repoConfig.getLocation() ), newLocation );
78          repoConfig.setLocation( newLocation.getAbsolutePath() );
79  
80          String path = "/incorrect-checksums/1.0/incorrect-checksums-1.0.jar";
81  
82          File sha1File = new File( repoConfig.getLocation(), path + ".sha1" );
83          File md5File = new File( repoConfig.getLocation(), path + ".md5" );
84  
85          ChecksummedFile checksum = new ChecksummedFile( new File( repoConfig.getLocation(), path ) );
86  
87          assertTrue( sha1File.exists() );
88          assertTrue( md5File.exists() );
89          assertFalse( checksum.isValidChecksums( new ChecksumAlgorithm[] { ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 } ) );
90  
91          consumer.beginScan( repoConfig, Calendar.getInstance().getTime() );
92  
93          consumer.processFile( path );
94  
95          assertTrue( sha1File.exists() );
96          assertTrue( md5File.exists() );
97          assertTrue( checksum.isValidChecksums( new ChecksumAlgorithm[] { ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 } ) );        
98      }
99  }