]> source.dussan.org Git - archiva.git/blob
904a360a4481263f5337ea051454d133624969c6
[archiva.git] /
1 package org.apache.archiva.scheduler.repository;
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 junit.framework.TestCase;
23 import org.apache.archiva.common.utils.FileUtils;
24 import org.apache.archiva.configuration.ArchivaConfiguration;
25 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
26 import org.apache.archiva.metadata.repository.MetadataRepository;
27 import org.apache.archiva.metadata.repository.stats.model.RepositoryStatisticsManager;
28 import org.apache.archiva.mock.MockRepositorySessionFactory;
29 import org.apache.archiva.components.taskqueue.execution.TaskExecutor;
30 import org.apache.archiva.repository.ManagedRepository;
31 import org.apache.archiva.repository.RepositoryRegistry;
32 import org.apache.archiva.repository.base.group.RepositoryGroupHandler;
33 import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler;
34 import org.apache.archiva.scheduler.repository.model.RepositoryTask;
35 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.runner.RunWith;
39 import org.springframework.test.annotation.DirtiesContext;
40 import org.springframework.test.context.ContextConfiguration;
41
42 import javax.inject.Inject;
43 import javax.inject.Named;
44 import java.io.IOException;
45 import java.nio.file.FileSystems;
46 import java.nio.file.FileVisitOption;
47 import java.nio.file.Files;
48 import java.nio.file.Path;
49 import java.nio.file.PathMatcher;
50 import java.nio.file.Paths;
51 import java.nio.file.attribute.FileTime;
52 import java.util.Calendar;
53 import java.util.Comparator;
54 import java.util.stream.Stream;
55
56 import static org.mockito.Mockito.mock;
57
58 /**
59  * ArchivaRepositoryScanningTaskExecutorPhase1Test
60  */
61 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
62 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
63 @DirtiesContext( classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD )
64 public abstract class AbstractArchivaRepositoryScanningTaskExecutorTest
65     extends TestCase
66 {
67     @Inject
68     RepositoryRegistry repositoryRegistry;
69
70     @SuppressWarnings( "unused" )
71     @Inject
72     ManagedRepositoryHandler managedRepositoryHandler;
73
74     @SuppressWarnings( "unused" )
75     @Inject
76     RepositoryGroupHandler groupHandler;
77
78     @Inject
79     @Named( value = "taskExecutor#test-repository-scanning" )
80     protected TaskExecutor<RepositoryTask> taskExecutor;
81
82     @Inject
83     @Named( value = "archivaConfiguration#test-repository-scanning" )
84     protected ArchivaConfiguration archivaConfig;
85
86     @Inject
87     @Named( value = "repositoryStatisticsManager#test" )
88     protected RepositoryStatisticsManager repositoryStatisticsManager;
89
90     @Inject
91     @Named( value = "knownRepositoryContentConsumer#test-consumer" )
92     protected TestConsumer testConsumer;
93
94     @Inject
95     @Named( value = "repositorySessionFactory#mock" )
96     private MockRepositorySessionFactory factory;
97
98     protected Path repoDir;
99
100     protected static final String TEST_REPO_ID = "testRepo";
101
102     protected MetadataRepository metadataRepository;
103
104     @Before
105     @Override
106     public void setUp()
107         throws Exception
108     {
109         super.setUp();
110
111         Path sourceRepoDir = Paths.get( "src/test/repositories/default-repository" );
112         repoDir = Paths.get( "target/default-repository" );
113
114         FileUtils.deleteDirectory( repoDir );
115         assertFalse( "Default Test Repository should not exist.", Files.exists(repoDir) );
116
117         Files.createDirectories(repoDir);
118
119         org.apache.commons.io.FileUtils.copyDirectory( sourceRepoDir.toFile(), repoDir.toFile() );
120         // set the timestamps to a time well in the past
121         Calendar cal = Calendar.getInstance();
122         cal.add( Calendar.YEAR, -1 );
123         try(Stream<Path> stream = Files.walk( repoDir,FileVisitOption.FOLLOW_LINKS)) {
124             stream.forEach( path ->
125             {
126                 try
127                 {
128                     Files.setLastModifiedTime( path, FileTime.fromMillis( cal.getTimeInMillis( ) ) );
129                 }
130                 catch ( IOException e )
131                 {
132                     e.printStackTrace( );
133                 }
134             } );
135         }
136         PathMatcher m = FileSystems.getDefault().getPathMatcher( "glob:**/.svn" );
137         Files.walk(repoDir, FileVisitOption.FOLLOW_LINKS).filter(Files::isDirectory)
138             .sorted( Comparator.reverseOrder( ))
139             .filter( path -> m.matches( path ) )
140             .forEach( path ->
141                 org.apache.archiva.common.utils.FileUtils.deleteQuietly( path )
142             );
143
144         assertTrue( "Default Test Repository should exist.", Files.exists(repoDir) && Files.isDirectory( repoDir) );
145
146         assertNotNull( archivaConfig );
147
148         // Create it
149         ManagedRepositoryConfiguration repositoryConfiguration = new ManagedRepositoryConfiguration();
150         repositoryConfiguration.setId( TEST_REPO_ID );
151         repositoryConfiguration.setName( "Test Repository" );
152         repositoryConfiguration.setLocation( repoDir.toAbsolutePath().toString() );
153         for ( ManagedRepository repo : repositoryRegistry.getManagedRepositories()) {
154             repositoryRegistry.removeRepository( repo );
155         }
156         repositoryRegistry.putRepository( repositoryConfiguration );
157
158         metadataRepository = mock( MetadataRepository.class );
159
160         factory.setRepository( metadataRepository );
161     }
162
163     @After
164     @Override
165     public void tearDown()
166         throws Exception
167     {
168         org.apache.archiva.common.utils.FileUtils.deleteDirectory( repoDir );
169
170         assertFalse( Files.exists(repoDir) );
171
172         super.tearDown();
173     }
174
175 }