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