View Javadoc

1   package org.apache.archiva.consumers.lucene;
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.List;
24  
25  import org.apache.commons.io.FileUtils;
26  import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
27  import org.apache.maven.archiva.model.ArchivaArtifact;
28  import org.apache.maven.archiva.repository.ManagedRepositoryContent;
29  import org.apache.maven.archiva.repository.RepositoryContentFactory;
30  import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
31  import org.apache.maven.archiva.scheduled.ArchivaTaskScheduler;
32  import org.apache.maven.archiva.scheduled.tasks.ArtifactIndexingTask;
33  import org.apache.maven.archiva.scheduled.tasks.TaskCreator;
34  import org.codehaus.plexus.spring.PlexusInSpringTestCase;
35  import org.codehaus.plexus.taskqueue.TaskQueue;
36  import org.easymock.MockControl;
37  import org.easymock.classextension.MockClassControl;
38  
39  /**
40   * LuceneCleanupRemoveIndexedConsumerTest
41   */
42  public class LuceneCleanupRemoveIndexedConsumerTest
43      extends PlexusInSpringTestCase
44  {
45      private LuceneCleanupRemoveIndexedConsumer consumer;
46  
47      private RepositoryContentFactory repoFactory;
48  
49      private MockControl repoFactoryControl;
50  
51      private ManagedRepositoryConfiguration repositoryConfig;
52  
53      private TaskQueue indexingQueue;
54  
55      public void setUp()
56          throws Exception
57      {
58          super.setUp();
59  
60          repoFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class );
61          repoFactory = (RepositoryContentFactory) repoFactoryControl.getMock();
62  
63          ArchivaTaskScheduler scheduler = (ArchivaTaskScheduler) lookup( ArchivaTaskScheduler.class );
64  
65          indexingQueue = (TaskQueue) lookup( TaskQueue.ROLE, "indexing" );
66  
67          consumer = new LuceneCleanupRemoveIndexedConsumer( repoFactory, scheduler );
68  
69          repositoryConfig = new ManagedRepositoryConfiguration();
70          repositoryConfig.setId( "test-repo" );
71          repositoryConfig.setLocation( getBasedir() + "/target/test-classes/test-repo" );
72          repositoryConfig.setLayout( "default" );
73          repositoryConfig.setName( "Test Repository" );
74          repositoryConfig.setScanned( true );
75          repositoryConfig.setSnapshots( false );
76          repositoryConfig.setReleases( true );
77          repositoryConfig.setIndexDir( getBasedir() + "/target/test-classes/test-repo/.cleanup-index" );
78      }
79  
80      public void tearDown()
81          throws Exception
82      {
83          FileUtils.deleteDirectory( new File( repositoryConfig.getIndexDir() ) );
84  
85          super.tearDown();
86      }
87  
88      @SuppressWarnings( "unchecked" )
89      public void testProcessArtifactArtifactDoesNotExist()
90          throws Exception
91      {
92          assertTrue( indexingQueue.getQueueSnapshot().isEmpty() );
93  
94          ArchivaArtifact artifact =
95              new ArchivaArtifact( "org.apache.archiva", "archiva-lucene-consumers", "1.2", null, "jar", "test-repo" );
96  
97          ManagedRepositoryContent repoContent = new ManagedDefaultRepositoryContent();
98          repoContent.setRepository( repositoryConfig );
99  
100         File artifactFile = new File( repoContent.getRepoRoot(), repoContent.toPath( artifact ) );
101 
102         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( repositoryConfig.getId() ),
103                                             repoContent );
104 
105         repoFactoryControl.replay();
106 
107         consumer.processArchivaArtifact( artifact );
108 
109         repoFactoryControl.verify();
110 
111         List<ArtifactIndexingTask> queue = indexingQueue.getQueueSnapshot();
112         assertEquals( 2, queue.size() );
113         ArtifactIndexingTask task =
114             TaskCreator.createIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.DELETE, null );
115         assertEquals( task, queue.get( 0 ) );
116         task =
117             TaskCreator.createIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH, null );
118         assertEquals( task, queue.get( 1 ) );
119     }
120 
121     public void testProcessArtifactArtifactExists()
122         throws Exception
123     {
124         assertTrue( indexingQueue.getQueueSnapshot().isEmpty() );
125 
126         ArchivaArtifact artifact =
127             new ArchivaArtifact( "org.apache.maven.archiva", "archiva-lucene-cleanup", "1.0", null, "jar", "test-repo" );
128         ManagedRepositoryContent repoContent = new ManagedDefaultRepositoryContent();
129         repoContent.setRepository( repositoryConfig );
130 
131         repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( repositoryConfig.getId() ),
132                                             repoContent );
133 
134         repoFactoryControl.replay();
135 
136         consumer.processArchivaArtifact( artifact );
137 
138         repoFactoryControl.verify();
139 
140         assertTrue( indexingQueue.getQueueSnapshot().isEmpty() );
141     }
142 
143     @Override
144     protected String getPlexusConfigLocation()
145     {
146         return "/org/apache/archiva/consumers/lucene/LuceneConsumersTest.xml";
147     }
148 }