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