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