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.apache.archiva.repository.base.group.RepositoryGroupHandler;
34 import org.apache.archiva.repository.base.managed.BasicManagedRepository;
35 import org.apache.archiva.repository.base.remote.BasicRemoteRepository;
36 import org.junit.jupiter.api.AfterAll;
37 import org.junit.jupiter.api.AfterEach;
38 import org.junit.jupiter.api.BeforeAll;
39 import org.junit.jupiter.api.BeforeEach;
40 import org.junit.jupiter.api.Test;
41 import org.junit.jupiter.api.extension.ExtendWith;
42 import org.springframework.test.context.ContextConfiguration;
43 import org.springframework.test.context.junit.jupiter.SpringExtension;
45 import javax.inject.Inject;
46 import java.io.IOException;
47 import java.net.URISyntaxException;
49 import java.nio.file.Files;
50 import java.nio.file.Path;
51 import java.nio.file.Paths;
52 import java.nio.file.StandardCopyOption;
53 import java.util.Collection;
55 import static org.junit.jupiter.api.Assertions.*;
59 * Test for RepositoryRegistry
61 @ExtendWith( SpringExtension.class)
62 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
63 public class ArchivaRepositoryRegistryTest
67 RepositoryRegistry repositoryRegistry;
70 ArchivaConfiguration archivaConfiguration;
72 @SuppressWarnings( "unused" )
74 RepositoryGroupHandler repositoryGroupHandler;
76 private static final Path userCfg = Paths.get(System.getProperty( "user.home" ), ".m2/archiva.xml");
78 private static Path cfgCopy;
79 private static Path archivaCfg;
82 public static void classSetup() throws IOException, URISyntaxException
84 URL archivaCfgUri = Thread.currentThread().getContextClassLoader().getResource( "archiva.xml" );
85 if (archivaCfgUri!=null) {
86 archivaCfg = Paths.get(archivaCfgUri.toURI());
87 cfgCopy = Files.createTempFile( "archiva-backup", ".xml" );
88 Files.copy( archivaCfg, cfgCopy, StandardCopyOption.REPLACE_EXISTING);
93 public static void classTearDown() throws IOException
96 Files.deleteIfExists( cfgCopy );
101 public void setUp( ) throws Exception
103 assertNotNull( repositoryRegistry );
104 Files.deleteIfExists( userCfg );
105 URL archivaCfgUri = Thread.currentThread().getContextClassLoader().getResource( "archiva.xml" );
106 if (archivaCfgUri!=null) {
107 archivaCfg = Paths.get(archivaCfgUri.toURI());
108 if (Files.exists(cfgCopy))
110 Files.copy( cfgCopy, archivaCfg , StandardCopyOption.REPLACE_EXISTING);
113 archivaConfiguration.reload();
114 repositoryRegistry.reload();
118 public void tearDown( ) throws Exception
120 Files.deleteIfExists( userCfg );
121 if (cfgCopy!=null && Files.exists(cfgCopy)) {
122 Files.copy(cfgCopy, archivaCfg, StandardCopyOption.REPLACE_EXISTING);
127 public void getRepositories( ) throws Exception
129 Collection<Repository> repos = repositoryRegistry.getRepositories( );
130 assertEquals( 5, repos.size( ) );
131 assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals("internal") ));
132 assertTrue( repos.stream( ).anyMatch( rep -> rep.getId( ).equals( "snapshots") ) );
133 assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals( "central") ));
137 public void getManagedRepositories( ) throws Exception
139 Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
140 assertEquals( 4, repos.size( ) );
141 assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals("internal") ));
142 assertTrue( repos.stream( ).anyMatch( rep -> rep.getId( ).equals( "snapshots") ) );
146 public void getRemoteRepositories( ) throws Exception
148 Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories( );
149 assertEquals( 1, repos.size( ) );
150 assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals( "central") ));
154 public void getRepository( ) throws Exception
156 Repository repo = repositoryRegistry.getRepository( "internal" );
158 assertEquals("internal", repo.getId());
159 assertEquals("Archiva Managed Internal Repository", repo.getName());
160 assertEquals("This is internal repository.", repo.getDescription());
161 assertEquals( "default", repo.getLayout( ) );
162 assertEquals("0 0 * * * ?", repo.getSchedulingDefinition());
163 assertTrue(repo instanceof ManagedRepository);
164 assertTrue( repo.hasIndex( ) );
165 assertTrue(repo.isScanned());
166 assertEquals( RepositoryType.MAVEN, repo.getType());
170 public void getManagedRepository( ) throws Exception
172 ManagedRepository repo = repositoryRegistry.getManagedRepository( "internal" );
174 assertEquals("internal", repo.getId());
175 assertEquals("Archiva Managed Internal Repository", repo.getName());
176 assertEquals("This is internal repository.", repo.getDescription());
177 assertEquals( "default", repo.getLayout( ) );
178 assertEquals("0 0 * * * ?", repo.getSchedulingDefinition());
179 assertTrue( repo.hasIndex( ) );
180 assertTrue(repo.isScanned());
181 assertEquals(RepositoryType.MAVEN, repo.getType());
182 assertTrue(repo.getActiveReleaseSchemes().contains( ReleaseScheme.RELEASE));
183 assertFalse( repo.getActiveReleaseSchemes( ).contains( ReleaseScheme.SNAPSHOT ) );
184 assertNotNull(repo.getContent());
186 assertNull(repositoryRegistry.getManagedRepository( "xyu" ));
191 public void getRemoteRepository( ) throws Exception
193 RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
195 assertEquals("central", repo.getId());
196 assertEquals("Central Repository", repo.getName());
197 assertEquals("", repo.getDescription());
198 assertEquals( "default", repo.getLayout( ) );
199 assertEquals("0 0 08 ? * SUN", repo.getSchedulingDefinition());
200 assertTrue( repo.hasIndex( ) );
201 assertTrue(repo.isScanned());
202 assertEquals(RepositoryType.MAVEN, repo.getType());
204 assertEquals(35, repo.getTimeout().getSeconds());
208 public void putManagedRepository( ) throws Exception
210 BasicManagedRepository managedRepository = BasicManagedRepository.newFilesystemInstance("test001", "Test repo", archivaConfiguration.getRepositoryBaseDir().resolve("test001"));
211 managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
212 repositoryRegistry.putRepository(managedRepository);
214 assertNotNull(managedRepository.getContent());
215 assertEquals(6, repositoryRegistry.getRepositories().size());
217 managedRepository = BasicManagedRepository.newFilesystemInstance("central", "Test repo", archivaConfiguration.getRepositoryBaseDir().resolve("central"));
218 managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
219 ManagedRepository updatedRepo;
221 repositoryRegistry.putRepository( managedRepository );
222 throw new RuntimeException("Repository exception should be thrown, if there exists a remote repository already with that id");
223 } catch ( RepositoryException e) {
226 managedRepository = BasicManagedRepository.newFilesystemInstance("internal", "Test repo", archivaConfiguration.getRepositoryBaseDir().resolve("internal"));
227 managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
228 updatedRepo = repositoryRegistry.putRepository( managedRepository );
230 assertSame( updatedRepo, managedRepository );
231 assertNotNull(managedRepository.getContent());
232 assertEquals(6, repositoryRegistry.getRepositories().size());
233 ManagedRepository managedRepository1 = repositoryRegistry.getManagedRepository( "internal" );
234 assertEquals("Test repo", managedRepository1.getName());
235 assertSame( managedRepository1, managedRepository );
240 public void putManagedRepositoryFromConfig( ) throws Exception
242 ManagedRepositoryConfiguration cfg = new ManagedRepositoryConfiguration();
243 cfg.setId("test002");
244 cfg.setName("This is test 002");
245 ManagedRepository repo = repositoryRegistry.putRepository( cfg );
247 assertEquals("test002", repo.getId());
248 assertEquals("This is test 002", repo.getName());
249 assertNotNull(repo.getContent());
250 archivaConfiguration.reload();
251 Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
252 assertEquals(5, repos.size());
254 ManagedRepository internalRepo = repositoryRegistry.getManagedRepository( "internal" );
255 cfg = new ManagedRepositoryConfiguration();
256 cfg.setId("internal");
257 cfg.setName("This is internal test 002");
258 repo = repositoryRegistry.putRepository( cfg );
259 assertSame( internalRepo, repo );
260 assertEquals("This is internal test 002",repo.getName());
261 assertEquals(5, repositoryRegistry.getManagedRepositories().size());
263 repositoryRegistry.reload();
264 assertEquals(5, repositoryRegistry.getManagedRepositories().size());
269 public void putManagedRepositoryFromConfigWithoutSave( ) throws Exception
271 Configuration configuration = archivaConfiguration.getConfiguration();
272 ManagedRepositoryConfiguration cfg = new ManagedRepositoryConfiguration();
273 cfg.setId("test002");
274 cfg.setName("This is test 002");
275 ManagedRepository repo = repositoryRegistry.putRepository( cfg, configuration );
277 assertEquals("test002", repo.getId());
278 assertEquals("This is test 002", repo.getName());
279 assertNotNull(repo.getContent());
280 archivaConfiguration.reload();
281 assertEquals(3, archivaConfiguration.getConfiguration().getManagedRepositories().size());
282 Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
283 assertEquals(5, repos.size());
285 ManagedRepository internalRepo = repositoryRegistry.getManagedRepository( "internal" );
286 cfg = new ManagedRepositoryConfiguration();
287 cfg.setId("internal");
288 cfg.setName("This is internal test 002");
289 repo = repositoryRegistry.putRepository( cfg, configuration );
290 assertSame( internalRepo, repo );
291 assertEquals("This is internal test 002",repo.getName());
292 assertEquals(5, repositoryRegistry.getManagedRepositories().size());
294 repositoryRegistry.reload();
295 assertEquals(4, repositoryRegistry.getManagedRepositories().size());
299 public void putRemoteRepository( ) throws Exception
301 BasicRemoteRepository remoteRepository = BasicRemoteRepository.newFilesystemInstance( "test001", "Test repo", archivaConfiguration.getRemoteRepositoryBaseDir() );
302 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
303 RemoteRepository newRepo = repositoryRegistry.putRepository(remoteRepository);
305 assertSame( remoteRepository, newRepo );
306 assertNotNull(remoteRepository.getContent());
307 assertEquals(6, repositoryRegistry.getRepositories().size());
309 remoteRepository = BasicRemoteRepository.newFilesystemInstance( "internal", "Test repo", archivaConfiguration.getRemoteRepositoryBaseDir() );
310 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
311 RemoteRepository updatedRepo;
314 updatedRepo = repositoryRegistry.putRepository( remoteRepository );
315 assertSame( remoteRepository, updatedRepo );
316 throw new RuntimeException("Should throw repository exception, if repository exists already and is not the same type.");
317 } catch (RepositoryException e) {
321 remoteRepository = BasicRemoteRepository.newFilesystemInstance( "central", "Test repo", archivaConfiguration.getRemoteRepositoryBaseDir() );
322 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
323 updatedRepo = repositoryRegistry.putRepository( remoteRepository );
325 assertSame( updatedRepo, remoteRepository );
326 assertNotNull(remoteRepository.getContent());
327 assertEquals(6, repositoryRegistry.getRepositories().size());
328 RemoteRepository remoteRepository1 = repositoryRegistry.getRemoteRepository( "central" );
329 assertEquals("Test repo", remoteRepository1.getName());
330 assertSame( remoteRepository1, remoteRepository );
334 public void putRemoteRepositoryFromConfig( ) throws Exception
336 RemoteRepositoryConfiguration cfg = new RemoteRepositoryConfiguration();
337 cfg.setId("test002");
338 cfg.setName("This is test 002");
339 RemoteRepository repo = repositoryRegistry.putRepository( cfg );
341 assertEquals("test002", repo.getId());
342 assertEquals("This is test 002", repo.getName());
343 assertNotNull(repo.getContent());
344 archivaConfiguration.reload();
345 Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories();
346 assertEquals(2, repos.size());
348 RemoteRepository internalRepo = repositoryRegistry.getRemoteRepository( "central" );
349 cfg = new RemoteRepositoryConfiguration();
350 cfg.setId("central");
351 cfg.setName("This is central test 002");
352 repo = repositoryRegistry.putRepository( cfg );
353 assertSame( internalRepo, repo );
354 assertEquals("This is central test 002",repo.getName());
355 assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
357 repositoryRegistry.reload();
358 assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
362 public void putRemoteRepositoryFromConfigWithoutSave( ) throws Exception
364 Configuration configuration = archivaConfiguration.getConfiguration();
365 RemoteRepositoryConfiguration cfg = new RemoteRepositoryConfiguration();
366 cfg.setId("test002");
367 cfg.setName("This is test 002");
368 RemoteRepository repo = repositoryRegistry.putRepository( cfg, configuration );
370 assertEquals("test002", repo.getId());
371 assertEquals("This is test 002", repo.getName());
372 assertNotNull(repo.getContent());
373 archivaConfiguration.reload();
374 assertEquals(1, archivaConfiguration.getConfiguration().getRemoteRepositories().size());
375 Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories();
376 assertEquals(2, repos.size());
378 RemoteRepository internalRepo = repositoryRegistry.getRemoteRepository( "central" );
379 cfg = new RemoteRepositoryConfiguration();
380 cfg.setId("central");
381 cfg.setName("This is central test 002");
382 repo = repositoryRegistry.putRepository( cfg, configuration );
383 assertSame( internalRepo, repo );
384 assertEquals("This is central test 002",repo.getName());
385 assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
387 repositoryRegistry.reload();
388 assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
392 public void removeRepository( ) throws Exception
394 assertEquals(5, repositoryRegistry.getRepositories().size());
395 Repository repo = repositoryRegistry.getRepository( "snapshots" );
396 repositoryRegistry.removeRepository( repo );
397 assertEquals(4, repositoryRegistry.getRepositories().size());
398 assertTrue( repositoryRegistry.getRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
399 archivaConfiguration.reload();
400 repositoryRegistry.reload();
401 assertEquals(4, repositoryRegistry.getRepositories().size());
405 public void removeManagedRepository( ) throws Exception
408 assertEquals(4, repositoryRegistry.getManagedRepositories().size());
409 ManagedRepository repo = repositoryRegistry.getManagedRepository( "snapshots" );
410 repositoryRegistry.removeRepository( repo );
411 assertEquals(3, repositoryRegistry.getManagedRepositories().size());
412 assertTrue( repositoryRegistry.getManagedRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
413 archivaConfiguration.reload();
414 repositoryRegistry.reload();
415 assertEquals(3, repositoryRegistry.getManagedRepositories().size());
419 public void removeManagedRepositoryWithoutSave( ) throws Exception
421 Configuration configuration = archivaConfiguration.getConfiguration();
422 assertEquals(4, repositoryRegistry.getManagedRepositories().size());
423 ManagedRepository repo = repositoryRegistry.getManagedRepository( "snapshots" );
424 repositoryRegistry.removeRepository( repo, configuration );
425 assertEquals(3, repositoryRegistry.getManagedRepositories().size());
426 assertTrue( repositoryRegistry.getManagedRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
427 archivaConfiguration.reload();
428 repositoryRegistry.reload();
429 assertEquals(4, repositoryRegistry.getManagedRepositories().size());
434 public void removeRemoteRepository( ) throws Exception
436 assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
437 RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
438 repositoryRegistry.removeRepository( repo );
439 assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
440 assertTrue( repositoryRegistry.getRemoteRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "central" ) ) );
441 archivaConfiguration.reload();
442 repositoryRegistry.reload();
443 assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
447 public void removeRemoteRepositoryWithoutSave( ) throws Exception
449 Configuration configuration = archivaConfiguration.getConfiguration();
450 assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
451 RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
452 repositoryRegistry.removeRepository( repo, configuration );
453 assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
454 assertTrue( repositoryRegistry.getRemoteRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "central" ) ) );
455 archivaConfiguration.reload();
456 repositoryRegistry.reload();
457 assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
462 public void cloneManagedRepo( ) throws Exception
464 ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( "internal" );
468 repositoryRegistry.clone(managedRepository, "snapshots");
469 throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
471 catch ( RepositoryException e )
478 repositoryRegistry.clone(managedRepository, "central");
479 throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
481 catch ( RepositoryException e )
486 ManagedRepository clone = repositoryRegistry.clone( managedRepository, "newinternal" );
487 assertNotNull(clone);
488 assertNull(clone.getContent());
489 assertEquals("Archiva Managed Internal Repository", clone.getName());
490 assertNotSame( managedRepository, clone );
495 public void cloneRemoteRepo( ) throws Exception
497 RemoteRepository remoteRepository = repositoryRegistry.getRemoteRepository( "central" );
501 repositoryRegistry.clone(remoteRepository, "snapshots");
502 throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
504 catch ( RepositoryException e )
511 repositoryRegistry.clone(remoteRepository, "central");
512 throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
514 catch ( RepositoryException e )
519 RemoteRepository clone = repositoryRegistry.clone( remoteRepository, "newCentral" );
520 assertNotNull(clone);
521 assertNull(clone.getContent());
522 assertEquals("Central Repository", clone.getName());
523 assertNotSame( remoteRepository, clone );
528 void validateRepository() {
529 Repository repo = repositoryRegistry.getRepository( "internal" );
530 assertNotNull( repo );
531 assertTrue( repositoryRegistry.validateRepository( repo ).isValid() );