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.managed.ManagedRepositoryHandler;
36 import org.apache.archiva.repository.base.remote.BasicRemoteRepository;
37 import org.apache.archiva.repository.base.remote.RemoteRepositoryHandler;
38 import org.junit.jupiter.api.AfterAll;
39 import org.junit.jupiter.api.AfterEach;
40 import org.junit.jupiter.api.BeforeAll;
41 import org.junit.jupiter.api.BeforeEach;
42 import org.junit.jupiter.api.Test;
43 import org.junit.jupiter.api.extension.ExtendWith;
44 import org.springframework.test.context.ContextConfiguration;
45 import org.springframework.test.context.junit.jupiter.SpringExtension;
47 import javax.inject.Inject;
48 import java.io.IOException;
49 import java.net.URISyntaxException;
51 import java.nio.file.Files;
52 import java.nio.file.Path;
53 import java.nio.file.Paths;
54 import java.nio.file.StandardCopyOption;
55 import java.util.Collection;
56 import java.util.List;
58 import static org.junit.jupiter.api.Assertions.*;
62 * Test for RepositoryRegistry
64 @ExtendWith( SpringExtension.class)
65 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
66 public class ArchivaRepositoryRegistryTest
70 RepositoryRegistry repositoryRegistry;
73 ArchivaConfiguration archivaConfiguration;
75 @SuppressWarnings( "unused" )
77 RepositoryGroupHandler repositoryGroupHandler;
80 ManagedRepositoryHandler managedRepositoryHandler;
83 RemoteRepositoryHandler remoteRepositoryHandler;
85 private static final Path userCfg = Paths.get(System.getProperty( "user.home" ), ".m2/archiva.xml");
87 private static Path cfgCopy;
88 private static Path archivaCfg;
91 public static void classSetup() throws IOException, URISyntaxException
93 URL archivaCfgUri = Thread.currentThread().getContextClassLoader().getResource( "archiva.xml" );
94 if (archivaCfgUri!=null) {
95 archivaCfg = Paths.get(archivaCfgUri.toURI());
96 cfgCopy = Files.createTempFile( "archiva-backup", ".xml" );
97 Files.copy( archivaCfg, cfgCopy, StandardCopyOption.REPLACE_EXISTING);
102 public static void classTearDown() throws IOException
105 Files.deleteIfExists( cfgCopy );
110 public void setUp( ) throws Exception
112 assertNotNull( repositoryRegistry );
113 Files.deleteIfExists( userCfg );
114 URL archivaCfgUri = Thread.currentThread().getContextClassLoader().getResource( "archiva.xml" );
115 if (archivaCfgUri!=null) {
116 archivaCfg = Paths.get(archivaCfgUri.toURI());
117 if (Files.exists(cfgCopy))
119 Files.copy( cfgCopy, archivaCfg , StandardCopyOption.REPLACE_EXISTING);
122 archivaConfiguration.reload();
123 repositoryRegistry.reload();
127 public void tearDown( ) throws Exception
129 Files.deleteIfExists( userCfg );
130 if (cfgCopy!=null && Files.exists(cfgCopy)) {
131 Files.copy(cfgCopy, archivaCfg, StandardCopyOption.REPLACE_EXISTING);
136 public void getRepositories( ) throws Exception
138 Collection<Repository> repos = repositoryRegistry.getRepositories( );
139 assertEquals( 5, repos.size( ) );
140 assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals("internal") ));
141 assertTrue( repos.stream( ).anyMatch( rep -> rep.getId( ).equals( "snapshots") ) );
142 assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals( "central") ));
146 public void getManagedRepositories( ) throws Exception
148 Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
149 assertEquals( 4, repos.size( ) );
150 assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals("internal") ));
151 assertTrue( repos.stream( ).anyMatch( rep -> rep.getId( ).equals( "snapshots") ) );
155 public void getRemoteRepositories( ) throws Exception
157 Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories( );
158 assertEquals( 1, repos.size( ) );
159 assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals( "central") ));
163 public void getRepository( ) throws Exception
165 Repository repo = repositoryRegistry.getRepository( "internal" );
167 assertEquals("internal", repo.getId());
168 assertEquals("Archiva Managed Internal Repository", repo.getName());
169 assertEquals("This is internal repository.", repo.getDescription());
170 assertEquals( "default", repo.getLayout( ) );
171 assertEquals("0 0 * * * ?", repo.getSchedulingDefinition());
172 assertTrue(repo instanceof ManagedRepository);
173 assertTrue( repo.hasIndex( ) );
174 assertTrue(repo.isScanned());
175 assertEquals( RepositoryType.MAVEN, repo.getType());
179 public void getManagedRepository( ) throws Exception
181 ManagedRepository repo = repositoryRegistry.getManagedRepository( "internal" );
183 assertEquals("internal", repo.getId());
184 assertEquals("Archiva Managed Internal Repository", repo.getName());
185 assertEquals("This is internal repository.", repo.getDescription());
186 assertEquals( "default", repo.getLayout( ) );
187 assertEquals("0 0 * * * ?", repo.getSchedulingDefinition());
188 assertTrue( repo.hasIndex( ) );
189 assertTrue(repo.isScanned());
190 assertEquals(RepositoryType.MAVEN, repo.getType());
191 assertTrue(repo.getActiveReleaseSchemes().contains( ReleaseScheme.RELEASE));
192 assertFalse( repo.getActiveReleaseSchemes( ).contains( ReleaseScheme.SNAPSHOT ) );
193 assertNotNull(repo.getContent());
195 assertNull(repositoryRegistry.getManagedRepository( "xyu" ));
200 public void getRemoteRepository( ) throws Exception
202 RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
204 assertEquals("central", repo.getId());
205 assertEquals("Central Repository", repo.getName());
206 assertEquals("", repo.getDescription());
207 assertEquals( "default", repo.getLayout( ) );
208 assertEquals("0 0 08 ? * SUN", repo.getSchedulingDefinition());
209 assertTrue( repo.hasIndex( ) );
210 assertTrue(repo.isScanned());
211 assertEquals(RepositoryType.MAVEN, repo.getType());
213 assertEquals(35, repo.getTimeout().getSeconds());
217 public void putManagedRepository( ) throws Exception
219 BasicManagedRepository managedRepository = BasicManagedRepository.newFilesystemInstance("test001", "Test repo", archivaConfiguration.getRepositoryBaseDir().resolve("test001"));
220 managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
221 repositoryRegistry.putRepository(managedRepository);
223 assertNotNull(managedRepository.getContent());
224 assertEquals(6, repositoryRegistry.getRepositories().size());
226 managedRepository = BasicManagedRepository.newFilesystemInstance("central", "Test repo", archivaConfiguration.getRepositoryBaseDir().resolve("central"));
227 managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
228 ManagedRepository updatedRepo;
230 repositoryRegistry.putRepository( managedRepository );
231 throw new RuntimeException("Repository exception should be thrown, if there exists a remote repository already with that id");
232 } catch ( RepositoryException e) {
235 managedRepository = BasicManagedRepository.newFilesystemInstance("internal", "Test repo", archivaConfiguration.getRepositoryBaseDir().resolve("internal"));
236 managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
237 updatedRepo = repositoryRegistry.putRepository( managedRepository );
239 assertSame( updatedRepo, managedRepository );
240 assertNotNull(managedRepository.getContent());
241 assertEquals(6, repositoryRegistry.getRepositories().size());
242 ManagedRepository managedRepository1 = repositoryRegistry.getManagedRepository( "internal" );
243 assertEquals("Test repo", managedRepository1.getName());
244 assertSame( managedRepository1, managedRepository );
249 public void putManagedRepositoryFromConfig( ) throws Exception
251 ManagedRepositoryConfiguration cfg = new ManagedRepositoryConfiguration();
252 cfg.setId("test002");
253 cfg.setName("This is test 002");
254 ManagedRepository repo = repositoryRegistry.putRepository( cfg );
256 assertEquals("test002", repo.getId());
257 assertEquals("This is test 002", repo.getName());
258 assertNotNull(repo.getContent());
259 archivaConfiguration.reload();
260 Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
261 assertEquals(5, repos.size());
263 ManagedRepository internalRepo = repositoryRegistry.getManagedRepository( "internal" );
264 cfg = new ManagedRepositoryConfiguration();
265 cfg.setId("internal");
266 cfg.setName("This is internal test 002");
267 repo = repositoryRegistry.putRepository( cfg );
268 assertSame( internalRepo, repo );
269 assertEquals("This is internal test 002",repo.getName());
270 assertEquals(5, repositoryRegistry.getManagedRepositories().size());
272 repositoryRegistry.reload();
273 assertEquals(5, repositoryRegistry.getManagedRepositories().size());
278 public void putManagedRepositoryFromConfigWithoutSave( ) throws Exception
280 Configuration configuration = archivaConfiguration.getConfiguration();
281 int actualSize = configuration.getManagedRepositories( ).size( );
282 Configuration newConfiguration = new Configuration( );
283 ManagedRepositoryConfiguration cfg = new ManagedRepositoryConfiguration();
284 cfg.setId("test002");
285 cfg.setName("This is test 002");
286 ManagedRepository repo = repositoryRegistry.putRepository( cfg, newConfiguration );
288 assertEquals("test002", repo.getId());
289 assertEquals("This is test 002", repo.getName());
290 assertNotNull(repo.getContent());
291 archivaConfiguration.reload();
292 assertEquals(actualSize, configuration.getManagedRepositories().size());
293 List<ManagedRepositoryConfiguration> repos = newConfiguration.getManagedRepositories( );
294 assertEquals(1, repos.size());
296 ManagedRepository internalRepo = repositoryRegistry.getManagedRepository( "internal" );
297 cfg = new ManagedRepositoryConfiguration();
298 cfg.setId("internal");
299 cfg.setName("This is internal test 002");
300 repo = repositoryRegistry.putRepository( cfg, newConfiguration );
301 assertEquals("This is internal test 002",repo.getName());
302 assertEquals(2, newConfiguration.getManagedRepositories().size());
304 repositoryRegistry.reload();
305 assertEquals(actualSize, configuration.getManagedRepositories().size());
309 public void putRemoteRepository( ) throws Exception
311 BasicRemoteRepository remoteRepository = BasicRemoteRepository.newFilesystemInstance( "test001", "Test repo", archivaConfiguration.getRemoteRepositoryBaseDir() );
312 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
313 RemoteRepository newRepo = repositoryRegistry.putRepository(remoteRepository);
315 assertSame( remoteRepository, newRepo );
316 assertNotNull(remoteRepository.getContent());
317 assertEquals(6, repositoryRegistry.getRepositories().size());
319 remoteRepository = BasicRemoteRepository.newFilesystemInstance( "internal", "Test repo", archivaConfiguration.getRemoteRepositoryBaseDir() );
320 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
321 RemoteRepository updatedRepo;
324 updatedRepo = repositoryRegistry.putRepository( remoteRepository );
325 assertSame( remoteRepository, updatedRepo );
326 throw new RuntimeException("Should throw repository exception, if repository exists already and is not the same type.");
327 } catch (RepositoryException e) {
331 remoteRepository = BasicRemoteRepository.newFilesystemInstance( "central", "Test repo", archivaConfiguration.getRemoteRepositoryBaseDir() );
332 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
333 updatedRepo = repositoryRegistry.putRepository( remoteRepository );
335 assertSame( updatedRepo, remoteRepository );
336 assertNotNull(remoteRepository.getContent());
337 assertEquals(6, repositoryRegistry.getRepositories().size());
338 RemoteRepository remoteRepository1 = repositoryRegistry.getRemoteRepository( "central" );
339 assertEquals("Test repo", remoteRepository1.getName());
340 assertSame( remoteRepository1, remoteRepository );
344 public void putRemoteRepositoryFromConfig( ) throws Exception
346 RemoteRepositoryConfiguration cfg = new RemoteRepositoryConfiguration();
347 cfg.setId("test002");
348 cfg.setName("This is test 002");
349 RemoteRepository repo = repositoryRegistry.putRepository( cfg );
351 assertEquals("test002", repo.getId());
352 assertEquals("This is test 002", repo.getName());
353 assertNotNull(repo.getContent());
354 archivaConfiguration.reload();
355 Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories();
356 assertEquals(2, repos.size());
358 RemoteRepository internalRepo = repositoryRegistry.getRemoteRepository( "central" );
359 cfg = new RemoteRepositoryConfiguration();
360 cfg.setId("central");
361 cfg.setName("This is central test 002");
362 repo = repositoryRegistry.putRepository( cfg );
363 assertEquals("This is central test 002",repo.getName());
364 assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
366 repositoryRegistry.reload();
367 assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
371 public void putRemoteRepositoryFromConfigWithoutSave( ) throws Exception
373 Configuration configuration = archivaConfiguration.getConfiguration();
374 RemoteRepositoryConfiguration cfg = new RemoteRepositoryConfiguration();
375 Configuration newConfiguration = new Configuration( );
377 cfg.setId("test002");
378 cfg.setName("This is test 002");
379 RemoteRepository repo = repositoryRegistry.putRepository( cfg, newConfiguration );
381 assertEquals("test002", repo.getId());
382 assertEquals("This is test 002", repo.getName());
383 assertNotNull(repo.getContent());
384 archivaConfiguration.reload();
385 assertEquals(1, archivaConfiguration.getConfiguration().getRemoteRepositories().size());
386 Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories();
387 assertEquals(1, repos.size());
388 assertEquals( 1, newConfiguration.getRemoteRepositories( ).size( ) );
390 RemoteRepository internalRepo = repositoryRegistry.getRemoteRepository( "central" );
391 cfg = new RemoteRepositoryConfiguration();
392 cfg.setId("central");
393 cfg.setName("This is central test 002");
394 repo = repositoryRegistry.putRepository( cfg, newConfiguration );
395 assertEquals("This is central test 002",repo.getName());
396 assertEquals( 2, newConfiguration.getRemoteRepositories( ).size( ) );
398 repositoryRegistry.reload();
399 assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
403 public void removeRepository( ) throws Exception
405 assertEquals(5, repositoryRegistry.getRepositories().size());
406 Repository repo = repositoryRegistry.getRepository( "snapshots" );
407 repositoryRegistry.removeRepository( repo );
408 assertEquals(4, repositoryRegistry.getRepositories().size());
409 assertTrue( repositoryRegistry.getRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
410 archivaConfiguration.reload();
411 repositoryRegistry.reload();
412 assertEquals(4, repositoryRegistry.getRepositories().size());
416 public void removeManagedRepository( ) throws Exception
419 assertEquals(4, repositoryRegistry.getManagedRepositories().size());
420 ManagedRepository repo = repositoryRegistry.getManagedRepository( "snapshots" );
421 repositoryRegistry.removeRepository( repo );
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(3, repositoryRegistry.getManagedRepositories().size());
430 public void removeManagedRepositoryWithoutSave( ) throws Exception
432 Configuration configuration = archivaConfiguration.getConfiguration();
433 assertEquals(4, repositoryRegistry.getManagedRepositories().size());
434 ManagedRepository repo = repositoryRegistry.getManagedRepository( "snapshots" );
435 repositoryRegistry.removeRepository( repo, configuration );
436 assertEquals(3, repositoryRegistry.getManagedRepositories().size());
437 assertTrue( repositoryRegistry.getManagedRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
438 archivaConfiguration.reload();
439 repositoryRegistry.reload();
440 assertEquals(4, repositoryRegistry.getManagedRepositories().size());
445 public void removeRemoteRepository( ) throws Exception
447 assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
448 RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
449 repositoryRegistry.removeRepository( repo );
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(0, repositoryRegistry.getRemoteRepositories().size());
458 public void removeRemoteRepositoryWithoutSave( ) throws Exception
460 Configuration configuration = archivaConfiguration.getConfiguration();
461 assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
462 RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
463 repositoryRegistry.removeRepository( repo, configuration );
464 assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
465 assertTrue( repositoryRegistry.getRemoteRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "central" ) ) );
466 archivaConfiguration.reload();
467 repositoryRegistry.reload();
468 assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
473 public void cloneManagedRepo( ) throws Exception
475 ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( "internal" );
479 repositoryRegistry.clone(managedRepository, "snapshots");
480 throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
482 catch ( RepositoryException e )
489 repositoryRegistry.clone(managedRepository, "central");
490 throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
492 catch ( RepositoryException e )
497 ManagedRepository clone = repositoryRegistry.clone( managedRepository, "newinternal" );
498 assertNotNull(clone);
499 assertNull(clone.getContent());
500 assertEquals("Archiva Managed Internal Repository", clone.getName());
501 assertNotSame( managedRepository, clone );
506 public void cloneRemoteRepo( ) throws Exception
508 RemoteRepository remoteRepository = repositoryRegistry.getRemoteRepository( "central" );
512 repositoryRegistry.clone(remoteRepository, "snapshots");
513 throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
515 catch ( RepositoryException e )
522 repositoryRegistry.clone(remoteRepository, "central");
523 throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
525 catch ( RepositoryException e )
530 RemoteRepository clone = repositoryRegistry.clone( remoteRepository, "newCentral" );
531 assertNotNull(clone);
532 assertNull(clone.getContent());
533 assertEquals("Central Repository", clone.getName());
534 assertNotSame( remoteRepository, clone );
539 void validateRepository() {
540 Repository repo = repositoryRegistry.getRepository( "internal" );
541 assertNotNull( repo );
542 assertTrue( repositoryRegistry.validateRepository( repo ).isValid() );