1 package org.apache.maven.archiva.reporting.artifact;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.commons.io.FileUtils;
23 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
24 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
25 import org.apache.maven.archiva.consumers.ArchivaArtifactConsumer;
26 import org.apache.maven.archiva.database.ArtifactDAO;
27 import org.apache.maven.archiva.model.ArchivaArtifact;
28 import org.apache.maven.archiva.model.RepositoryProblem;
29 import org.apache.maven.archiva.reporting.DynamicReportSource;
32 import java.util.Date;
33 import java.util.Iterator;
34 import java.util.List;
37 * DuplicateArtifactReportTest
41 public class DuplicateArtifactReportTest
42 extends AbstractArtifactReportsTestCase
44 private static final String TESTABLE_REPO = "testable";
46 private static final String HASH3 = "f3f653289f3217c65324830ab3415bc92feddefa";
48 private static final String HASH2 = "a49810ad3eba8651677ab57cd40a0f76fdef9538";
50 private static final String HASH1 = "232f01b24b1617c46a3d4b0ab3415bc9237dcdec";
52 private ArtifactDAO artifactDao;
54 protected void setUp()
59 artifactDao = dao.getArtifactDAO();
61 ArchivaConfiguration config = (ArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), "default" );
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 );
72 public ArchivaArtifact createArtifact( String artifactId, String version )
74 ArchivaArtifact artifact =
75 artifactDao.createArtifact( "org.apache.maven.archiva.test", artifactId, version, "", "jar" );
76 artifact.getModel().setLastModified( new Date() );
77 artifact.getModel().setRepositoryId( TESTABLE_REPO );
81 public void testSimpleReport()
84 ArchivaArtifact artifact;
86 // Setup artifacts in fresh DB.
87 artifact = createArtifact( "test-one", "1.0" );
88 artifact.getModel().setChecksumSHA1( HASH1 );
89 artifactDao.saveArtifact( artifact );
91 artifact = createArtifact( "test-one", "1.1" );
92 artifact.getModel().setChecksumSHA1( HASH1 );
93 artifactDao.saveArtifact( artifact );
95 artifact = createArtifact( "test-one", "1.2" );
96 artifact.getModel().setChecksumSHA1( HASH1 );
97 artifactDao.saveArtifact( artifact );
99 artifact = createArtifact( "test-two", "1.0" );
100 artifact.getModel().setChecksumSHA1( HASH1 );
101 artifactDao.saveArtifact( artifact );
103 artifact = createArtifact( "test-two", "2.0" );
104 artifact.getModel().setChecksumSHA1( HASH3 );
105 artifactDao.saveArtifact( artifact );
107 artifact = createArtifact( "test-two", "2.1" );
108 artifact.getModel().setChecksumSHA1( HASH2 );
109 artifactDao.saveArtifact( artifact );
111 artifact = createArtifact( "test-two", "3.0" );
112 artifact.getModel().setChecksumSHA1( HASH2 );
113 artifactDao.saveArtifact( artifact );
115 // Setup entries for bad/duplicate in problem DB.
116 pretendToRunDuplicateArtifactsConsumer();
118 List allArtifacts = artifactDao.queryArtifacts( null );
119 assertEquals( "Total Artifact Count", 7, allArtifacts.size() );
121 DuplicateArtifactReport report =
122 (DuplicateArtifactReport) lookup( DynamicReportSource.class.getName(), "duplicate-artifacts" );
124 List results = report.getData();
126 System.out.println( "Results.size: " + results.size() );
128 Iterator it = results.iterator();
129 while ( it.hasNext() )
131 RepositoryProblem problem = (RepositoryProblem) it.next();
132 System.out.println( "[" + ( i++ ) + "] " + problem.getMessage() );
139 int totals = ( ( hash1Count * hash1Count ) - hash1Count ) + ( ( hash2Count * hash2Count ) - hash2Count ) +
140 ( ( hash3Count * hash3Count ) - hash3Count );
141 assertEquals( "Total report hits.", totals, results.size() );
144 private void pretendToRunDuplicateArtifactsConsumer()
147 List artifacts = dao.getArtifactDAO().queryArtifacts( null );
148 ArchivaArtifactConsumer consumer =
149 (ArchivaArtifactConsumer) lookup( ArchivaArtifactConsumer.class.getName(), "duplicate-artifacts" );
150 consumer.beginScan();
153 Iterator it = artifacts.iterator();
154 while ( it.hasNext() )
156 ArchivaArtifact artifact = (ArchivaArtifact) it.next();
157 consumer.processArchivaArtifact( artifact );
162 consumer.completeScan();