1 package org.apache.archiva.repository.base;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.archiva.configuration.ArchivaConfiguration;
23 import org.apache.archiva.configuration.Configuration;
24 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
25 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
26 import org.apache.archiva.repository.ManagedRepository;
27 import org.apache.archiva.repository.ReleaseScheme;
28 import org.apache.archiva.repository.RemoteRepository;
29 import org.apache.archiva.repository.Repository;
30 import org.apache.archiva.repository.RepositoryException;
31 import org.apache.archiva.repository.RepositoryRegistry;
32 import org.apache.archiva.repository.RepositoryType;
33 import org.junit.jupiter.api.AfterAll;
34 import org.junit.jupiter.api.AfterEach;
35 import org.junit.jupiter.api.BeforeAll;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.api.extension.ExtendWith;
39 import org.springframework.test.context.ContextConfiguration;
40 import org.springframework.test.context.junit.jupiter.SpringExtension;
42 import javax.inject.Inject;
43 import java.io.IOException;
44 import java.net.URISyntaxException;
46 import java.nio.file.Files;
47 import java.nio.file.Path;
48 import java.nio.file.Paths;
49 import java.nio.file.StandardCopyOption;
50 import java.util.Collection;
52 import static org.junit.jupiter.api.Assertions.*;
56 * Test for RepositoryRegistry
58 @ExtendWith( SpringExtension.class)
59 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
60 public class ArchivaRepositoryRegistryTest
64 RepositoryRegistry repositoryRegistry;
67 ArchivaConfiguration archivaConfiguration;
69 @SuppressWarnings( "unused" )
71 RepositoryGroupHandler repositoryGroupHandler;
73 private static final Path userCfg = Paths.get(System.getProperty( "user.home" ), ".m2/archiva.xml");
75 private static Path cfgCopy;
76 private static Path archivaCfg;
79 public static void classSetup() throws IOException, URISyntaxException
81 URL archivaCfgUri = Thread.currentThread().getContextClassLoader().getResource( "archiva.xml" );
82 if (archivaCfgUri!=null) {
83 archivaCfg = Paths.get(archivaCfgUri.toURI());
84 cfgCopy = Files.createTempFile( "archiva-backup", ".xml" );
85 Files.copy( archivaCfg, cfgCopy, StandardCopyOption.REPLACE_EXISTING);
90 public static void classTearDown() throws IOException
93 Files.deleteIfExists( cfgCopy );
98 public void setUp( ) throws Exception
100 assertNotNull( repositoryRegistry );
101 Files.deleteIfExists( userCfg );
102 URL archivaCfgUri = Thread.currentThread().getContextClassLoader().getResource( "archiva.xml" );
103 if (archivaCfgUri!=null) {
104 archivaCfg = Paths.get(archivaCfgUri.toURI());
105 if (Files.exists(cfgCopy))
107 Files.copy( cfgCopy, archivaCfg , StandardCopyOption.REPLACE_EXISTING);
110 archivaConfiguration.reload();
111 repositoryRegistry.reload();
115 public void tearDown( ) throws Exception
117 Files.deleteIfExists( userCfg );
118 if (cfgCopy!=null && Files.exists(cfgCopy)) {
119 Files.copy(cfgCopy, archivaCfg, StandardCopyOption.REPLACE_EXISTING);
124 public void getRepositories( ) throws Exception
126 Collection<Repository> repos = repositoryRegistry.getRepositories( );
127 assertEquals( 5, repos.size( ) );
128 assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals("internal") ));
129 assertTrue( repos.stream( ).anyMatch( rep -> rep.getId( ).equals( "snapshots") ) );
130 assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals( "central") ));
134 public void getManagedRepositories( ) throws Exception
136 Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
137 assertEquals( 4, repos.size( ) );
138 assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals("internal") ));
139 assertTrue( repos.stream( ).anyMatch( rep -> rep.getId( ).equals( "snapshots") ) );
143 public void getRemoteRepositories( ) throws Exception
145 Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories( );
146 assertEquals( 1, repos.size( ) );
147 assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals( "central") ));
151 public void getRepository( ) throws Exception
153 Repository repo = repositoryRegistry.getRepository( "internal" );
155 assertEquals("internal", repo.getId());
156 assertEquals("Archiva Managed Internal Repository", repo.getName());
157 assertEquals("This is internal repository.", repo.getDescription());
158 assertEquals( "default", repo.getLayout( ) );
159 assertEquals("0 0 * * * ?", repo.getSchedulingDefinition());
160 assertTrue(repo instanceof ManagedRepository);
161 assertTrue( repo.hasIndex( ) );
162 assertTrue(repo.isScanned());
163 assertEquals( RepositoryType.MAVEN, repo.getType());
167 public void getManagedRepository( ) throws Exception
169 ManagedRepository repo = repositoryRegistry.getManagedRepository( "internal" );
171 assertEquals("internal", repo.getId());
172 assertEquals("Archiva Managed Internal Repository", repo.getName());
173 assertEquals("This is internal repository.", repo.getDescription());
174 assertEquals( "default", repo.getLayout( ) );
175 assertEquals("0 0 * * * ?", repo.getSchedulingDefinition());
176 assertTrue( repo.hasIndex( ) );
177 assertTrue(repo.isScanned());
178 assertEquals(RepositoryType.MAVEN, repo.getType());
179 assertTrue(repo.getActiveReleaseSchemes().contains( ReleaseScheme.RELEASE));
180 assertFalse( repo.getActiveReleaseSchemes( ).contains( ReleaseScheme.SNAPSHOT ) );
181 assertNotNull(repo.getContent());
183 assertNull(repositoryRegistry.getManagedRepository( "xyu" ));
188 public void getRemoteRepository( ) throws Exception
190 RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
192 assertEquals("central", repo.getId());
193 assertEquals("Central Repository", repo.getName());
194 assertEquals("", repo.getDescription());
195 assertEquals( "default", repo.getLayout( ) );
196 assertEquals("0 0 08 ? * SUN", repo.getSchedulingDefinition());
197 assertTrue( repo.hasIndex( ) );
198 assertTrue(repo.isScanned());
199 assertEquals(RepositoryType.MAVEN, repo.getType());
201 assertEquals(35, repo.getTimeout().getSeconds());
205 public void putManagedRepository( ) throws Exception
207 BasicManagedRepository managedRepository = BasicManagedRepository.newFilesystemInstance("test001", "Test repo", archivaConfiguration.getRepositoryBaseDir().resolve("test001"));
208 managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
209 repositoryRegistry.putRepository(managedRepository);
211 assertNotNull(managedRepository.getContent());
212 assertEquals(6, repositoryRegistry.getRepositories().size());
214 managedRepository = BasicManagedRepository.newFilesystemInstance("central", "Test repo", archivaConfiguration.getRepositoryBaseDir().resolve("central"));
215 managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
216 ManagedRepository updatedRepo;
218 repositoryRegistry.putRepository( managedRepository );
219 throw new RuntimeException("Repository exception should be thrown, if there exists a remote repository already with that id");
220 } catch ( RepositoryException e) {
223 managedRepository = BasicManagedRepository.newFilesystemInstance("internal", "Test repo", archivaConfiguration.getRepositoryBaseDir().resolve("internal"));
224 managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
225 updatedRepo = repositoryRegistry.putRepository( managedRepository );
227 assertSame( updatedRepo, managedRepository );
228 assertNotNull(managedRepository.getContent());
229 assertEquals(6, repositoryRegistry.getRepositories().size());
230 ManagedRepository managedRepository1 = repositoryRegistry.getManagedRepository( "internal" );
231 assertEquals("Test repo", managedRepository1.getName());
232 assertSame( managedRepository1, managedRepository );
237 public void putManagedRepositoryFromConfig( ) throws Exception
239 ManagedRepositoryConfiguration cfg = new ManagedRepositoryConfiguration();
240 cfg.setId("test002");
241 cfg.setName("This is test 002");
242 ManagedRepository repo = repositoryRegistry.putRepository( cfg );
244 assertEquals("test002", repo.getId());
245 assertEquals("This is test 002", repo.getName());
246 assertNotNull(repo.getContent());
247 archivaConfiguration.reload();
248 Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
249 assertEquals(5, repos.size());
251 ManagedRepository internalRepo = repositoryRegistry.getManagedRepository( "internal" );
252 cfg = new ManagedRepositoryConfiguration();
253 cfg.setId("internal");
254 cfg.setName("This is internal test 002");
255 repo = repositoryRegistry.putRepository( cfg );
256 assertSame( internalRepo, repo );
257 assertEquals("This is internal test 002",repo.getName());
258 assertEquals(5, repositoryRegistry.getManagedRepositories().size());
260 repositoryRegistry.reload();
261 assertEquals(5, repositoryRegistry.getManagedRepositories().size());
266 public void putManagedRepositoryFromConfigWithoutSave( ) throws Exception
268 Configuration configuration = archivaConfiguration.getConfiguration();
269 ManagedRepositoryConfiguration cfg = new ManagedRepositoryConfiguration();
270 cfg.setId("test002");
271 cfg.setName("This is test 002");
272 ManagedRepository repo = repositoryRegistry.putRepository( cfg, configuration );
274 assertEquals("test002", repo.getId());
275 assertEquals("This is test 002", repo.getName());
276 assertNotNull(repo.getContent());
277 archivaConfiguration.reload();
278 assertEquals(3, archivaConfiguration.getConfiguration().getManagedRepositories().size());
279 Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
280 assertEquals(5, repos.size());
282 ManagedRepository internalRepo = repositoryRegistry.getManagedRepository( "internal" );
283 cfg = new ManagedRepositoryConfiguration();
284 cfg.setId("internal");
285 cfg.setName("This is internal test 002");
286 repo = repositoryRegistry.putRepository( cfg, configuration );
287 assertSame( internalRepo, repo );
288 assertEquals("This is internal test 002",repo.getName());
289 assertEquals(5, repositoryRegistry.getManagedRepositories().size());
291 repositoryRegistry.reload();
292 assertEquals(4, repositoryRegistry.getManagedRepositories().size());
296 public void putRemoteRepository( ) throws Exception
298 BasicRemoteRepository remoteRepository = BasicRemoteRepository.newFilesystemInstance( "test001", "Test repo", archivaConfiguration.getRemoteRepositoryBaseDir() );
299 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
300 RemoteRepository newRepo = repositoryRegistry.putRepository(remoteRepository);
302 assertSame( remoteRepository, newRepo );
303 assertNotNull(remoteRepository.getContent());
304 assertEquals(6, repositoryRegistry.getRepositories().size());
306 remoteRepository = BasicRemoteRepository.newFilesystemInstance( "internal", "Test repo", archivaConfiguration.getRemoteRepositoryBaseDir() );
307 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
308 RemoteRepository updatedRepo;
311 updatedRepo = repositoryRegistry.putRepository( remoteRepository );
312 assertSame( remoteRepository, updatedRepo );
313 throw new RuntimeException("Should throw repository exception, if repository exists already and is not the same type.");
314 } catch (RepositoryException e) {
318 remoteRepository = BasicRemoteRepository.newFilesystemInstance( "central", "Test repo", archivaConfiguration.getRemoteRepositoryBaseDir() );
319 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
320 updatedRepo = repositoryRegistry.putRepository( remoteRepository );
322 assertSame( updatedRepo, remoteRepository );
323 assertNotNull(remoteRepository.getContent());
324 assertEquals(6, repositoryRegistry.getRepositories().size());
325 RemoteRepository remoteRepository1 = repositoryRegistry.getRemoteRepository( "central" );
326 assertEquals("Test repo", remoteRepository1.getName());
327 assertSame( remoteRepository1, remoteRepository );
331 public void putRemoteRepositoryFromConfig( ) throws Exception
333 RemoteRepositoryConfiguration cfg = new RemoteRepositoryConfiguration();
334 cfg.setId("test002");
335 cfg.setName("This is test 002");
336 RemoteRepository repo = repositoryRegistry.putRepository( cfg );
338 assertEquals("test002", repo.getId());
339 assertEquals("This is test 002", repo.getName());
340 assertNotNull(repo.getContent());
341 archivaConfiguration.reload();
342 Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories();
343 assertEquals(2, repos.size());
345 RemoteRepository internalRepo = repositoryRegistry.getRemoteRepository( "central" );
346 cfg = new RemoteRepositoryConfiguration();
347 cfg.setId("central");
348 cfg.setName("This is central test 002");
349 repo = repositoryRegistry.putRepository( cfg );
350 assertSame( internalRepo, repo );
351 assertEquals("This is central test 002",repo.getName());
352 assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
354 repositoryRegistry.reload();
355 assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
359 public void putRemoteRepositoryFromConfigWithoutSave( ) throws Exception
361 Configuration configuration = archivaConfiguration.getConfiguration();
362 RemoteRepositoryConfiguration cfg = new RemoteRepositoryConfiguration();
363 cfg.setId("test002");
364 cfg.setName("This is test 002");
365 RemoteRepository repo = repositoryRegistry.putRepository( cfg, configuration );
367 assertEquals("test002", repo.getId());
368 assertEquals("This is test 002", repo.getName());
369 assertNotNull(repo.getContent());
370 archivaConfiguration.reload();
371 assertEquals(1, archivaConfiguration.getConfiguration().getRemoteRepositories().size());
372 Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories();
373 assertEquals(2, repos.size());
375 RemoteRepository internalRepo = repositoryRegistry.getRemoteRepository( "central" );
376 cfg = new RemoteRepositoryConfiguration();
377 cfg.setId("central");
378 cfg.setName("This is central test 002");
379 repo = repositoryRegistry.putRepository( cfg, configuration );
380 assertSame( internalRepo, repo );
381 assertEquals("This is central test 002",repo.getName());
382 assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
384 repositoryRegistry.reload();
385 assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
389 public void removeRepository( ) throws Exception
391 assertEquals(5, repositoryRegistry.getRepositories().size());
392 Repository repo = repositoryRegistry.getRepository( "snapshots" );
393 repositoryRegistry.removeRepository( repo );
394 assertEquals(4, repositoryRegistry.getRepositories().size());
395 assertTrue( repositoryRegistry.getRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
396 archivaConfiguration.reload();
397 repositoryRegistry.reload();
398 assertEquals(4, repositoryRegistry.getRepositories().size());
402 public void removeManagedRepository( ) throws Exception
405 assertEquals(4, repositoryRegistry.getManagedRepositories().size());
406 ManagedRepository repo = repositoryRegistry.getManagedRepository( "snapshots" );
407 repositoryRegistry.removeRepository( repo );
408 assertEquals(3, repositoryRegistry.getManagedRepositories().size());
409 assertTrue( repositoryRegistry.getManagedRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
410 archivaConfiguration.reload();
411 repositoryRegistry.reload();
412 assertEquals(3, repositoryRegistry.getManagedRepositories().size());
416 public void removeManagedRepositoryWithoutSave( ) throws Exception
418 Configuration configuration = archivaConfiguration.getConfiguration();
419 assertEquals(4, repositoryRegistry.getManagedRepositories().size());
420 ManagedRepository repo = repositoryRegistry.getManagedRepository( "snapshots" );
421 repositoryRegistry.removeRepository( repo, configuration );
422 assertEquals(3, repositoryRegistry.getManagedRepositories().size());
423 assertTrue( repositoryRegistry.getManagedRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
424 archivaConfiguration.reload();
425 repositoryRegistry.reload();
426 assertEquals(4, repositoryRegistry.getManagedRepositories().size());
431 public void removeRemoteRepository( ) throws Exception
433 assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
434 RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
435 repositoryRegistry.removeRepository( repo );
436 assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
437 assertTrue( repositoryRegistry.getRemoteRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "central" ) ) );
438 archivaConfiguration.reload();
439 repositoryRegistry.reload();
440 assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
444 public void removeRemoteRepositoryWithoutSave( ) throws Exception
446 Configuration configuration = archivaConfiguration.getConfiguration();
447 assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
448 RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
449 repositoryRegistry.removeRepository( repo, configuration );
450 assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
451 assertTrue( repositoryRegistry.getRemoteRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "central" ) ) );
452 archivaConfiguration.reload();
453 repositoryRegistry.reload();
454 assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
459 public void cloneManagedRepo( ) throws Exception
461 ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( "internal" );
465 repositoryRegistry.clone(managedRepository, "snapshots");
466 throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
468 catch ( RepositoryException e )
475 repositoryRegistry.clone(managedRepository, "central");
476 throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
478 catch ( RepositoryException e )
483 ManagedRepository clone = repositoryRegistry.clone( managedRepository, "newinternal" );
484 assertNotNull(clone);
485 assertNull(clone.getContent());
486 assertEquals("Archiva Managed Internal Repository", clone.getName());
487 assertNotSame( managedRepository, clone );
492 public void cloneRemoteRepo( ) throws Exception
494 RemoteRepository remoteRepository = repositoryRegistry.getRemoteRepository( "central" );
498 repositoryRegistry.clone(remoteRepository, "snapshots");
499 throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
501 catch ( RepositoryException e )
508 repositoryRegistry.clone(remoteRepository, "central");
509 throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
511 catch ( RepositoryException e )
516 RemoteRepository clone = repositoryRegistry.clone( remoteRepository, "newCentral" );
517 assertNotNull(clone);
518 assertNull(clone.getContent());
519 assertEquals("Central Repository", clone.getName());
520 assertNotSame( remoteRepository, clone );
525 void validateRepository() {
526 Repository repo = repositoryRegistry.getRepository( "internal" );
527 assertNotNull( repo );
528 assertTrue( repositoryRegistry.validateRepository( repo ).isValid() );