Property changes on: . ___________________________________________________________________ Name: svn:ignore + target/* Index: src/test/java/org/apache/maven/archiva/consumer/plugin/MockRepositorySearch.java =================================================================== --- src/test/java/org/apache/maven/archiva/consumer/plugin/MockRepositorySearch.java (revision 0) +++ src/test/java/org/apache/maven/archiva/consumer/plugin/MockRepositorySearch.java (revision 0) @@ -0,0 +1,88 @@ +package org.apache.maven.archiva.consumer.plugin; + +import java.util.List; + +import org.apache.maven.archiva.indexer.search.CrossRepositorySearch; +import org.apache.maven.archiva.indexer.search.SearchResultLimits; +import org.apache.maven.archiva.indexer.search.SearchResults; +import org.codehaus.plexus.PlexusTestCase; + +public class MockRepositorySearch + implements CrossRepositorySearch +{ + /** + * @plexus.configuration default-value="mock-repo-search" + */ + private String id; + + private SearchResults searchResults = new SearchResults(); + + private String principal; + + private List selectedRepos; + + private String term; + + /** + * Allow a set of mocked {@link SearchResults} to be injected. These get stub returned from each search call + * + * @param searchResults + */ + public void setSearchResults( SearchResults searchResults ) + { + this.searchResults = searchResults; + } + + public SearchResults searchForBytecode( String principal, List selectedRepos, String term, + SearchResultLimits limits ) + { + storeParams( principal, selectedRepos, term ); + return searchResults; + } + + public SearchResults searchForChecksum( String principal, List selectedRepos, String checksum, + SearchResultLimits limits ) + { + storeParams( principal, selectedRepos, term ); + return searchResults; + } + + public SearchResults searchForTerm( String principal, List selectedRepos, String term, + SearchResultLimits limits ) + { + storeParams( principal, selectedRepos, term ); + return searchResults; + } + + + private void storeParams( String principal, List selectedRepos, String term ) + { + this.principal = principal; + this.selectedRepos = selectedRepos; + this.term = term; + } + + public void verify( String principal, List selectedRepos, String term ) + { + if(principal != null) + { + PlexusTestCase.assertEquals( principal, this.principal ); + } + + if(selectedRepos != null) + { + PlexusTestCase.assertEquals( selectedRepos, this.selectedRepos ); + } + + if(term != null) + { + PlexusTestCase.assertEquals( term, this.term ); + } + } + + public void setId( String id ) + { + this.id = id; + } + +} Index: src/test/java/org/apache/maven/archiva/consumer/plugin/DiscoverNewArtifactConsumerTest.java =================================================================== --- src/test/java/org/apache/maven/archiva/consumer/plugin/DiscoverNewArtifactConsumerTest.java (revision 0) +++ src/test/java/org/apache/maven/archiva/consumer/plugin/DiscoverNewArtifactConsumerTest.java (revision 0) @@ -0,0 +1,154 @@ +package org.apache.maven.archiva.consumer.plugin; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.File; +import java.io.FileInputStream; +import java.util.Collections; +import java.util.List; + +import org.apache.commons.io.IOUtils; +import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; +import org.apache.maven.archiva.consumers.ConsumerException; +import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer; +import org.apache.maven.archiva.indexer.filecontent.FileContentRecord; +import org.apache.maven.archiva.indexer.search.CrossRepositorySearch; +import org.apache.maven.archiva.indexer.search.SearchResults; +import org.codehaus.plexus.PlexusTestCase; +import org.codehaus.plexus.component.repository.ComponentDescriptor; + +/** + * @author Stephen Gargan + */ +public class DiscoverNewArtifactConsumerTest + extends PlexusTestCase +{ + private DiscoverNewArtifactConsumer consumer; + + private MockRepositorySearch mockSearch; + + private File repoDir; + + private File dumpFile; + + private ManagedRepositoryConfiguration testRepository; + + protected void setUp() + throws Exception + { + super.setUp(); + + String consumerRole = KnownRepositoryContentConsumer.class.getName(); + String searchRole = CrossRepositorySearch.class.getName(); + consumer = (DiscoverNewArtifactConsumer) lookup( consumerRole, "discover-new-artifact" ); + mockSearch = (MockRepositorySearch) lookup( searchRole, "mockSearch" ); + + setUpMockRepository(); + + dumpFile = new File( repoDir, DiscoverNewArtifactConsumer.DUMP_FILE_NAME ); + dumpFile.delete(); + } + + private void setUpMockRepository() + { + repoDir = new java.io.File( getBasedir(), "/target/test-consumer-repo" ); + repoDir.mkdirs(); + testRepository = new ManagedRepositoryConfiguration(); + testRepository.setName( "Test-Consumer-Repository" ); + testRepository.setId( "test-consumer-repository" ); + testRepository.setLocation( repoDir.getAbsolutePath() ); + } + + protected void tearDown() + throws Exception + { + super.tearDown(); + dumpFile.delete(); + } + + public void testBeginScan() + throws Exception + { + doTestBeginScan(); + } + + public void testScanFindsNewContent() + throws Exception + { + doTestBeginScan(); + + String artifactPath = "org/simple/test/testartifact/testartifact/1.0/testartifact-1.0.pom"; + consumer.processFile( artifactPath ); + mockSearch.verify( "guest", Collections.singletonList( testRepository.getId() ), artifactPath ); + verifyContentDumped( new String[] { artifactPath } ); + + // process another path + consumer.processFile( artifactPath ); + mockSearch.verify( "guest", Collections.singletonList( testRepository.getId() ), artifactPath ); + verifyContentDumped( new String[] { artifactPath, artifactPath } ); + } + + public void testScanFindsNoNewContent() + throws Exception + { + doTestBeginScan(); + + String artifactPath = "org/simple/test/testartifact/testartifact/1.0/testartifact-1.0.pom"; + + // setup the mockSearch to find the artifact + mockSearch.setSearchResults( createSearchResults( artifactPath ) ); + consumer.processFile( artifactPath ); + mockSearch.verify( "guest", Collections.singletonList( testRepository.getId() ), artifactPath ); + verifyContentDumped( new String[] {} ); + + } + + public SearchResults createSearchResults( String artifactPath ) + { + FileContentRecord fch = new FileContentRecord(); + fch.setFilename( artifactPath ); + fch.setRepositoryId( testRepository.getId() ); + SearchResults results = new SearchResults(); + results.addFileContentHit( fch ); + + return results; + } + + private void verifyContentDumped( String[] artifactPaths ) + throws Exception + { + List newArtifacts = IOUtils.readLines( new FileInputStream( dumpFile ) ); + assertEquals( artifactPaths.length, newArtifacts.size() ); + + for ( int x = 0; x < newArtifacts.size(); x++ ) + { + assertEquals( testRepository.getId() + "/" + artifactPaths[x], newArtifacts.get( x ) ); + } + } + + private void doTestBeginScan() + throws ConsumerException + { + assertFalse( dumpFile.exists() ); + consumer.beginScan( testRepository ); + assertTrue( dumpFile.exists() ); + } + +} Index: src/test/resources/org/apache/maven/archiva/consumer/plugin/DiscoverNewArtifactConsumerTest.xml =================================================================== --- src/test/resources/org/apache/maven/archiva/consumer/plugin/DiscoverNewArtifactConsumerTest.xml (revision 0) +++ src/test/resources/org/apache/maven/archiva/consumer/plugin/DiscoverNewArtifactConsumerTest.xml (revision 0) @@ -0,0 +1,35 @@ + + + + org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer + discover-new-artifact + org.apache.maven.archiva.consumer.plugin.DiscoverNewArtifactConsumer + per-lookup + + + + org.apache.maven.archiva.configuration.FileTypes + filetypes + + + org.apache.maven.archiva.indexer.search.CrossRepositorySearch + mockSearch + repoSearch + + + org.apache.maven.archiva.configuration.ArchivaConfiguration + configuration + + + + discover-new-artifact + Discover new artifacts in the repository. + + + + org.apache.maven.archiva.indexer.search.CrossRepositorySearch + mockSearch + org.apache.maven.archiva.consumer.plugin.MockRepositorySearch + + + Index: src/main/java/org/apache/maven/archiva/consumer/plugin/DiscoverNewArtifactConsumer.java =================================================================== --- src/main/java/org/apache/maven/archiva/consumer/plugin/DiscoverNewArtifactConsumer.java (revision 609860) +++ src/main/java/org/apache/maven/archiva/consumer/plugin/DiscoverNewArtifactConsumer.java (working copy) @@ -19,35 +19,34 @@ * under the License. */ +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.apache.commons.io.IOUtils; +import org.apache.maven.archiva.configuration.ArchivaConfiguration; +import org.apache.maven.archiva.configuration.FileTypes; +import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer; +import org.apache.maven.archiva.consumers.ConsumerException; import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer; -import org.apache.maven.archiva.consumers.ConsumerException; -import org.apache.maven.archiva.configuration.ArchivaConfiguration; -import org.apache.maven.archiva.configuration.FileTypes; -import org.apache.maven.archiva.model.ArchivaRepository; import org.apache.maven.archiva.indexer.search.CrossRepositorySearch; +import org.apache.maven.archiva.indexer.search.SearchResultHit; +import org.apache.maven.archiva.indexer.search.SearchResultLimits; import org.apache.maven.archiva.indexer.search.SearchResults; -import org.apache.maven.archiva.indexer.search.SearchResultLimits; -import org.apache.maven.archiva.indexer.search.SearchResultHit; -import org.apache.commons.io.IOUtils; -import org.codehaus.plexus.registry.RegistryListener; -import org.codehaus.plexus.registry.Registry; import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; +import org.codehaus.plexus.registry.Registry; +import org.codehaus.plexus.registry.RegistryListener; -import java.util.List; -import java.util.ArrayList; -import java.util.Iterator; -import java.io.File; -import java.io.IOException; -import java.io.FileInputStream; -import java.io.FileOutputStream; - /** * @author Maria Odea Ching * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer" - * role-hint="discover-new-artifact" - * instantiation-strategy="per-lookup" + * role-hint="discover-new-artifact" instantiation-strategy="per-lookup" */ public class DiscoverNewArtifactConsumer extends AbstractMonitoredConsumer @@ -82,11 +81,11 @@ private List includes = new ArrayList(); - private ArchivaRepository repository; + private ManagedRepositoryConfiguration repository; private File dumpFile; - private static final String DUMP_FILE_NAME = "new-artifacts.zzz"; + static final String DUMP_FILE_NAME = "new-artifacts.zzz"; public String getId() { @@ -113,24 +112,20 @@ return this.includes; } - public void beginScan( ArchivaRepository repository ) + public void beginScan( ManagedRepositoryConfiguration repository ) throws ConsumerException { - if ( !repository.isManaged() ) - { - throw new ConsumerException( "Consumer requires managed repository." ); - } - this.repository = repository; - dumpFile = new File( repository.getUrl().getPath() + "/" + DUMP_FILE_NAME ); + dumpFile = new File( repository.getLocation() + "/" + DUMP_FILE_NAME ); + try { if ( dumpFile.exists() ) { dumpFile.delete(); dumpFile = null; - dumpFile = new File( repository.getUrl().getPath() + "/" + DUMP_FILE_NAME ); + dumpFile = new File( repository.getLocation() + "/" + DUMP_FILE_NAME ); } dumpFile.createNewFile(); @@ -146,8 +141,21 @@ { String id = repository.getId() + "/" + path; - SearchResults results = repoSearch.searchForTerm( path, new SearchResultLimits( 0 ) ); + boolean found = isFoundInRepository( path, id ); + if ( !found ) + { + dumpToFile( id ); + } + } + + private boolean isFoundInRepository( String path, String id ) + { + List repoSearchList = new ArrayList(); + repoSearchList.add( repository.getId() ); + + SearchResults results = repoSearch.searchForTerm( "guest", repoSearchList, path, new SearchResultLimits( 0 ) ); + List hits = results.getHits(); boolean found = false; @@ -160,23 +168,12 @@ break; } } - - if ( !found ) - { - try - { - dumpToFile( id ); - } - catch ( IOException ie ) - { - throw new ConsumerException( ie.getMessage() ); - } - } + return found; } public void completeScan() { - /* do nothing */ + // dumpToFile( "Scan Complete" ); } public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue ) @@ -195,7 +192,6 @@ private void initIncludes() { includes.clear(); - includes.addAll( filetypes.getFileTypePatterns( FileTypes.INDEXABLE_CONTENT ) ); } @@ -215,9 +211,23 @@ } private void dumpToFile( String id ) - throws IOException + throws ConsumerException { - IOUtils.write( ( IOUtils.toString( new FileInputStream( dumpFile ) ) + "\n" + id ), - new FileOutputStream( dumpFile ) ); + try + { + + IOUtils.write( IOUtils.toString( new FileInputStream( dumpFile ) ) + id +"\n" , + new FileOutputStream( dumpFile ) ); + } + catch ( IOException e ) + { + throw new ConsumerException( "Error writing '" + id + "' to new artifacts file '" + + dumpFile.getPath() + "'", e ); + } } + + public CrossRepositorySearch getSearch() + { + return repoSearch; + } } Index: pom.xml =================================================================== --- pom.xml (revision 609860) +++ pom.xml (working copy) @@ -3,7 +3,7 @@ 4.0.0 org.apache.maven.archiva archiva-consumer-plugin - 1.0-beta-2-SNAPSHOT + 1.1-SNAPSHOT jar archiva-consumer-plugin http://maven.apache.org @@ -14,22 +14,22 @@ org.apache.maven.archiva archiva-consumer-api - 1.0-beta-2-SNAPSHOT + 1.1-SNAPSHOT org.apache.maven.archiva archiva-configuration - 1.0-beta-2-SNAPSHOT + 1.1-SNAPSHOT org.apache.maven.archiva archiva-repository-layer - 1.0-beta-2-SNAPSHOT + 1.1-SNAPSHOT org.apache.maven.archiva archiva-indexer - 1.0-beta-2-SNAPSHOT + 1.1-SNAPSHOT