From 6e1b60e430d6553951912d2f5634e557fbbafbd5 Mon Sep 17 00:00:00 2001 From: Brett Porter Date: Wed, 30 Jul 2014 15:33:46 +1000 Subject: [PATCH] update archetype for repository metadata use mocks instead of full implementations for testing, cut down dependencies --- .../archiva-consumer-archetype/pom.xml | 24 ++++- .../META-INF/maven/archetype-metadata.xml | 23 +--- .../src/main/java/SimpleArtifactConsumer.java | 50 +++++++-- .../test/java/SimpleArtifactConsumerTest.java | 58 ++++++---- .../src/test/resources/log4j2-test.xml | 4 +- .../src/test/resources/spring-context.xml | 30 ++---- .../archetype-resources/pom.xml | 100 +++++------------- .../projects/compile/archetype.properties | 9 +- pom.xml | 12 ++- 9 files changed, 151 insertions(+), 159 deletions(-) diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/pom.xml b/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/pom.xml index 630fb1b67..8456350af 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/pom.xml +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/pom.xml @@ -48,17 +48,33 @@ org.apache.archiva - archiva-repository-admin-default + archiva-repository-admin-api test org.apache.archiva - metadata-store-jcr + archiva-model test - org.slf4j - jcl-over-slf4j + org.apache.archiva + archiva-repository-layer + test + + + org.apache.archiva + metadata-repository-api + test + + + org.apache.archiva + maven2-repository + test + + + + org.kubek2k + springockito test diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/META-INF/maven/archetype-metadata.xml b/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/META-INF/maven/archetype-metadata.xml index 10c82acb7..fdcb1f874 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/META-INF/maven/archetype-metadata.xml +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/META-INF/maven/archetype-metadata.xml @@ -49,32 +49,17 @@ ${spring.version} - - ${jackrabbit.version} - ${guava.version} - - ${maven3x.version} - ${redback.registry.version} - - ${redback.quartz.version} - - - ${commons-dbcp.version} - - - ${commons-pool.version} - - - ${lucene.version} + + ${springockito.version} - - ${derbyVersion} + + ${mockito.version} diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/main/java/SimpleArtifactConsumer.java b/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/main/java/SimpleArtifactConsumer.java index d5cd396e0..940a4e0c9 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/main/java/SimpleArtifactConsumer.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/main/java/SimpleArtifactConsumer.java @@ -28,13 +28,23 @@ import org.apache.archiva.configuration.FileTypes; import org.apache.archiva.consumers.AbstractMonitoredConsumer; import org.apache.archiva.consumers.ConsumerException; import org.apache.archiva.consumers.KnownRepositoryContentConsumer; +import org.apache.archiva.metadata.repository.MetadataResolutionException; +import org.apache.archiva.metadata.repository.RepositorySession; +import org.apache.archiva.metadata.repository.RepositorySessionFactory; +import org.apache.archiva.model.ArtifactReference; import org.apache.archiva.redback.components.registry.Registry; import org.apache.archiva.redback.components.registry.RegistryListener; +import org.apache.archiva.repository.ManagedRepositoryContent; +import org.apache.archiva.repository.RepositoryContentFactory; +import org.apache.archiva.repository.RepositoryException; +import org.apache.archiva.repository.layout.LayoutException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.annotation.PostConstruct; import javax.inject.Inject; +import javax.inject.Named; + import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; import org.apache.archiva.admin.model.beans.ManagedRepository; @@ -81,18 +91,19 @@ public class SimpleArtifactConsumer /** current repository being scanned */ private ManagedRepository repository; - public void beginScan( ManagedRepository repository ) - throws ConsumerException - { - this.repository = repository; - log.info( "Beginning scan of repository [" + this.repository.getId() + "]" ); - } + @Inject + @Named( value = "repositoryContentFactory#default" ) + private RepositoryContentFactory repositoryContentFactory; + + @Inject + private RepositorySessionFactory repositorySessionFactory; + + private RepositorySession repositorySession; public void beginScan( ManagedRepository repository, Date whenGathered ) throws ConsumerException { - this.repository = repository; - log.info( "Beginning scan of repository [" + this.repository.getId() + "]" ); + beginScan( repository, whenGathered, true ); } public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo ) @@ -100,28 +111,45 @@ public class SimpleArtifactConsumer { this.repository = repository; log.info( "Beginning scan of repository [" + this.repository.getId() + "]" ); + + repositorySession = repositorySessionFactory.createSession(); } public void processFile( String path ) throws ConsumerException { - log.info( "Processing entry [" + path + "] from repository [" + this.repository.getId() + "]" ); + processFile( path, true ); } public void processFile( String path, boolean executeOnEntireRepo ) throws ConsumerException { log.info( "Processing entry [" + path + "] from repository [" + this.repository.getId() + "]" ); + + try + { + ManagedRepositoryContent repositoryContent = repositoryContentFactory.getManagedRepositoryContent( repository.getId() ); + ArtifactReference artifact = repositoryContent.toArtifactReference( path ); + + repositorySession.getRepository().getArtifacts( repository.getId(), artifact.getGroupId(), + artifact.getArtifactId(), artifact.getVersion() ); + } + catch ( RepositoryException | LayoutException | MetadataResolutionException e ) + { + throw new ConsumerException( e.getLocalizedMessage(), e ); + } } public void completeScan() { - log.info( "Finished scan of repository [" + this.repository.getId() + "]" ); + completeScan( true ); } public void completeScan( boolean executeOnEntireRepo ) { log.info( "Finished scan of repository [" + this.repository.getId() + "]" ); + + repositorySession.close(); } @@ -153,7 +181,7 @@ public class SimpleArtifactConsumer private void initIncludes() { includes.clear(); - includes.addAll( filetypes.getFileTypePatterns( FileTypes.INDEXABLE_CONTENT ) ); + includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) ); } @PostConstruct diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/test/java/SimpleArtifactConsumerTest.java b/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/test/java/SimpleArtifactConsumerTest.java index 413fed7ed..095873c62 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/test/java/SimpleArtifactConsumerTest.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/test/java/SimpleArtifactConsumerTest.java @@ -19,53 +19,65 @@ package $package; * under the License. */ -import java.io.File; - +import org.apache.archiva.admin.model.RepositoryAdminException; import org.apache.archiva.admin.model.beans.ManagedRepository; -import org.apache.archiva.consumers.KnownRepositoryContentConsumer; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import javax.inject.Inject; +import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin; +import org.apache.archiva.metadata.repository.MetadataRepository; +import org.apache.archiva.metadata.repository.RepositorySession; +import org.apache.archiva.metadata.repository.RepositorySessionFactory; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import junit.framework.TestCase; + +import javax.inject.Inject; +import java.io.File; +import java.util.Date; + +import static org.mockito.Mockito.*; /** * SimpleArtifactConsumerTest */ -@RunWith( SpringJUnit4ClassRunner.class ) -@ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml","classpath:/spring-context.xml"} ) +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" }) public class SimpleArtifactConsumerTest - extends TestCase { @Inject private SimpleArtifactConsumer consumer; - private File repoDir; + @Inject + private ManagedRepositoryAdmin managedRepositoryAdmin; + + @Inject + private RepositorySessionFactory repositorySessionFactory; private ManagedRepository testRepository; private Logger log = LoggerFactory.getLogger( SimpleArtifactConsumer.class ); + private MetadataRepository metadataRepository; + @Before public void setUp() throws Exception { - super.setUp(); - String consumerRole = KnownRepositoryContentConsumer.class.getName(); - setUpMockRepository(); - } - + RepositorySession repositorySession = mock( RepositorySession.class ); + when( repositorySessionFactory.createSession() ).thenReturn( repositorySession ); + metadataRepository = mock( MetadataRepository.class ); + when( repositorySession.getRepository() ).thenReturn( metadataRepository ); + } private void setUpMockRepository() + throws RepositoryAdminException { - repoDir = new java.io.File( "target/test-consumer-repo" ); + File repoDir = new File( "target/test-consumer-repo" ); repoDir.mkdirs(); repoDir.deleteOnExit(); @@ -73,6 +85,8 @@ public class SimpleArtifactConsumerTest testRepository.setName( "Test-Consumer-Repository" ); testRepository.setId( "test-consumer-repository" ); testRepository.setLocation( repoDir.getAbsolutePath() ); + + when( managedRepositoryAdmin.getManagedRepository( testRepository.getId() ) ).thenReturn( testRepository ); } @Test @@ -81,17 +95,21 @@ public class SimpleArtifactConsumerTest { log.info( "Beginning scan of repository [test-consumer-repository]" ); - consumer.beginScan( testRepository ); - + consumer.beginScan( testRepository, new Date() ); } @Test public void testProcessFile() throws Exception { - consumer.beginScan( testRepository ); + consumer.beginScan( testRepository, new Date() ); consumer.processFile( "org/simple/test/testartifact/testartifact/1.0/testartifact-1.0.pom" ); consumer.processFile( "org/simple/test/testartifact/testartifact/1.1/testartifact-1.1.pom" ); + + verify( metadataRepository ).getArtifacts( testRepository.getId(), "org.simple.test.testartifact", + "testartifact", "1.0" ); + verify( metadataRepository ).getArtifacts( testRepository.getId(), "org.simple.test.testartifact", + "testartifact", "1.1" ); } } diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/test/resources/log4j2-test.xml b/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/test/resources/log4j2-test.xml index 814779b2d..1b662d7f2 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/test/resources/log4j2-test.xml +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/test/resources/log4j2-test.xml @@ -19,7 +19,7 @@ --> - + @@ -27,7 +27,7 @@ - + diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/test/resources/spring-context.xml b/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/test/resources/spring-context.xml index 3b08f16d9..f17d5b4d6 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/test/resources/spring-context.xml +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/test/resources/spring-context.xml @@ -20,29 +20,15 @@ --> - - - - - - - + + + - - - - scheduler1 - org.quartz.simpl.SimpleThreadPool - 2 - 4 - org.quartz.simpl.RAMJobStore - - - - - - + + \ No newline at end of file diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resourcesstatic/archetype-resources/pom.xml b/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resourcesstatic/archetype-resources/pom.xml index fc7edf915..a12b75d2d 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resourcesstatic/archetype-resources/pom.xml +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resourcesstatic/archetype-resources/pom.xml @@ -33,16 +33,11 @@ ${archivaVersion} ${slf4jVersion} ${log4jVersion} - ${jackrabbitVersion} ${springVersion} - ${guavaVersion} - ${maven3xVersion} ${redbackRegistryVersion} - ${redbackQuartzVersion} - ${commonsDbcpVersion} - ${commonsPoolVersion} - ${luceneVersion} - ${derbyVersion} + ${guavaVersion} + ${springockitoVersion} + ${mockitoVersion} @@ -60,6 +55,21 @@ archiva-repository-admin-api ${archiva.version} + + org.apache.archiva + archiva-model + ${archiva.version} + + + org.apache.archiva + archiva-repository-layer + ${archiva.version} + + + org.apache.archiva + metadata-repository-api + ${archiva.version} + org.apache.archiva.redback.components.registry spring-registry-api @@ -117,32 +127,14 @@ org.apache.archiva - metadata-store-jcr + maven2-repository ${archiva.version} test - org.apache.jackrabbit - jackrabbit-core - ${jackrabbit.version} - test - - - commons-logging - commons-logging - - - - - org.apache.archiva.redback.components - spring-quartz - ${redback.quartz.version} - test - - - org.slf4j - jcl-over-slf4j - ${slf4j.version} + org.apache.logging.log4j + log4j-core + ${log4j.version} test @@ -152,9 +144,9 @@ test - org.apache.logging.log4j - log4j-core - ${log4j.version} + org.kubek2k + springockito + ${springockito.version} test @@ -168,31 +160,9 @@ ${guava.version} - org.apache.maven - maven-model - ${maven3x.version} - - - commons-dbcp - commons-dbcp - ${commons.dbcp.version} - runtime - - - commons-pool - commons-pool - ${commons.pool.version} - runtime - - - org.apache.lucene - lucene-core - ${lucene.version} - - - org.apache.derby - derby - ${derby.version} + org.mockito + mockito-all + ${mockito.version} @@ -236,20 +206,6 @@ 1.7 - - org.apache.maven.plugins - maven-surefire-plugin - - - ${project.build.directory}/appserver-base - ${project.build.directory}/archiva.xml - ${project.build.testOutputDirectory} - jdbc:derby:memory:users-test;create=true - org.apache.derby.jdbc.EmbeddedDriver - ${project.build.directory}/temp - - -