View Javadoc

1   package org.apache.maven.archiva.reporting.artifact;
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.util.Date;
24  import java.util.List;
25  
26  import org.apache.commons.io.FileUtils;
27  import org.apache.maven.archiva.configuration.ArchivaConfiguration;
28  import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
29  import org.apache.maven.archiva.database.ArtifactDAO;
30  import org.apache.maven.archiva.database.updater.ArchivaArtifactConsumer;
31  import org.apache.maven.archiva.model.ArchivaArtifact;
32  import org.apache.maven.archiva.model.RepositoryProblem;
33  import org.apache.maven.archiva.reporting.DynamicReportSource;
34  
35  /**
36   * DuplicateArtifactReportTest
37   *
38   * @version $Id: DuplicateArtifactReportTest.java 755239 2009-03-17 13:40:10Z brett $
39   */
40  public class DuplicateArtifactReportTest
41      extends AbstractArtifactReportsTestCase
42  {
43      private static final String TESTABLE_REPO = "testable";
44  
45      private static final String HASH3 = "f3f653289f3217c65324830ab3415bc92feddefa";
46  
47      private static final String HASH2 = "a49810ad3eba8651677ab57cd40a0f76fdef9538";
48  
49      private static final String HASH1 = "232f01b24b1617c46a3d4b0ab3415bc9237dcdec";
50  
51      private ArtifactDAO artifactDao;
52  
53      @Override
54      protected void setUp()
55          throws Exception
56      {
57          super.setUp();
58  
59          artifactDao = dao.getArtifactDAO();
60  
61          ArchivaConfiguration config = (ArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), "default" );
62  
63          ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
64          repoConfig.setId( TESTABLE_REPO );
65          repoConfig.setLayout( "default" );
66          File testRepoDir = new File( getBasedir(), "target/test-repository" );
67          FileUtils.forceMkdir( testRepoDir );
68          repoConfig.setLocation( testRepoDir.getAbsolutePath() );
69          config.getConfiguration().addManagedRepository( repoConfig );
70      }
71  
72      public ArchivaArtifact createArtifact( String artifactId, String version )
73      {
74          ArchivaArtifact artifact =
75              artifactDao.createArtifact( "org.apache.maven.archiva.test", artifactId, version, "", "jar", TESTABLE_REPO );
76          artifact.getModel().setLastModified( new Date() );
77          return artifact;
78      }
79  
80      public void testSimpleReport()
81          throws Exception
82      {
83          ArchivaArtifact artifact;
84  
85          // Setup artifacts in fresh DB.
86          artifact = createArtifact( "test-one", "1.0" );
87          artifact.getModel().setChecksumSHA1( HASH1 );
88          artifactDao.saveArtifact( artifact );
89  
90          artifact = createArtifact( "test-one", "1.1" );
91          artifact.getModel().setChecksumSHA1( HASH1 );
92          artifactDao.saveArtifact( artifact );
93  
94          artifact = createArtifact( "test-one", "1.2" );
95          artifact.getModel().setChecksumSHA1( HASH1 );
96          artifactDao.saveArtifact( artifact );
97  
98          artifact = createArtifact( "test-two", "1.0" );
99          artifact.getModel().setChecksumSHA1( HASH1 );
100         artifactDao.saveArtifact( artifact );
101 
102         artifact = createArtifact( "test-two", "2.0" );
103         artifact.getModel().setChecksumSHA1( HASH3 );
104         artifactDao.saveArtifact( artifact );
105 
106         artifact = createArtifact( "test-two", "2.1" );
107         artifact.getModel().setChecksumSHA1( HASH2 );
108         artifactDao.saveArtifact( artifact );
109 
110         artifact = createArtifact( "test-two", "3.0" );
111         artifact.getModel().setChecksumSHA1( HASH2 );
112         artifactDao.saveArtifact( artifact );
113 
114         // Setup entries for bad/duplicate in problem DB.
115         pretendToRunDuplicateArtifactsConsumer();
116 
117         List<ArchivaArtifact> allArtifacts = artifactDao.queryArtifacts( null );
118         assertEquals( "Total Artifact Count", 7, allArtifacts.size() );
119 
120         DuplicateArtifactReport report =
121             (DuplicateArtifactReport) lookup( DynamicReportSource.class.getName(), "duplicate-artifacts" );
122 
123         List<RepositoryProblem> results = report.getData();
124 
125         System.out.println( "Results.size: " + results.size() );
126         int i = 0;
127         for ( RepositoryProblem problem : results )
128         {
129             System.out.println( "[" + ( i++ ) + "] " + problem.getMessage() );
130         }
131 
132         int hash1Count = 4;
133         int hash2Count = 2;
134         int hash3Count = 1;
135 
136         int totals = ( ( hash1Count * hash1Count ) - hash1Count ) + ( ( hash2Count * hash2Count ) - hash2Count ) +
137             ( ( hash3Count * hash3Count ) - hash3Count );
138         assertEquals( "Total report hits.", totals, results.size() );
139     }
140 
141     private void pretendToRunDuplicateArtifactsConsumer()
142         throws Exception
143     {
144         List<ArchivaArtifact> artifacts = dao.getArtifactDAO().queryArtifacts( null );
145         ArchivaArtifactConsumer consumer =
146             (ArchivaArtifactConsumer) lookup( ArchivaArtifactConsumer.class.getName(), "duplicate-artifacts" );
147         consumer.beginScan();
148         try
149         {
150             for ( ArchivaArtifact artifact : artifacts )
151             {
152                 consumer.processArchivaArtifact( artifact );
153             }
154         }
155         finally
156         {
157             consumer.completeScan();
158         }
159     }
160 }