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.junit.jupiter.api.AfterAll;
38 import org.junit.jupiter.api.AfterEach;
39 import org.junit.jupiter.api.BeforeAll;
40 import org.junit.jupiter.api.BeforeEach;
41 import org.junit.jupiter.api.Test;
42 import org.junit.jupiter.api.extension.ExtendWith;
43 import org.springframework.test.context.ContextConfiguration;
44 import org.springframework.test.context.junit.jupiter.SpringExtension;
46 import javax.inject.Inject;
47 import java.io.IOException;
48 import java.net.URISyntaxException;
50 import java.nio.file.Files;
51 import java.nio.file.Path;
52 import java.nio.file.Paths;
53 import java.nio.file.StandardCopyOption;
54 import java.util.Collection;
55 import java.util.List;
57 import static org.junit.jupiter.api.Assertions.*;
61 * Test for RepositoryRegistry
63 @ExtendWith( SpringExtension.class)
64 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
65 public class ArchivaRepositoryRegistryTest
69 RepositoryRegistry repositoryRegistry;
72 ArchivaConfiguration archivaConfiguration;
74 @SuppressWarnings( "unused" )
76 RepositoryGroupHandler repositoryGroupHandler;
79 ManagedRepositoryHandler managedRepositoryHandler;
81 private static final Path userCfg = Paths.get(System.getProperty( "user.home" ), ".m2/archiva.xml");
83 private static Path cfgCopy;
84 private static Path archivaCfg;
87 public static void classSetup() throws IOException, URISyntaxException
89 URL archivaCfgUri = Thread.currentThread().getContextClassLoader().getResource( "archiva.xml" );
90 if (archivaCfgUri!=null) {
91 archivaCfg = Paths.get(archivaCfgUri.toURI());
92 cfgCopy = Files.createTempFile( "archiva-backup", ".xml" );
93 Files.copy( archivaCfg, cfgCopy, StandardCopyOption.REPLACE_EXISTING);
98 public static void classTearDown() throws IOException
101 Files.deleteIfExists( cfgCopy );
106 public void setUp( ) throws Exception
108 assertNotNull( repositoryRegistry );
109 Files.deleteIfExists( userCfg );
110 URL archivaCfgUri = Thread.currentThread().getContextClassLoader().getResource( "archiva.xml" );
111 if (archivaCfgUri!=null) {
112 archivaCfg = Paths.get(archivaCfgUri.toURI());
113 if (Files.exists(cfgCopy))
115 Files.copy( cfgCopy, archivaCfg , StandardCopyOption.REPLACE_EXISTING);
118 archivaConfiguration.reload();
119 repositoryRegistry.reload();
123 public void tearDown( ) throws Exception
125 Files.deleteIfExists( userCfg );
126 if (cfgCopy!=null && Files.exists(cfgCopy)) {
127 Files.copy(cfgCopy, archivaCfg, StandardCopyOption.REPLACE_EXISTING);
132 public void getRepositories( ) throws Exception
134 Collection<Repository> repos = repositoryRegistry.getRepositories( );
135 assertEquals( 5, repos.size( ) );
136 assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals("internal") ));
137 assertTrue( repos.stream( ).anyMatch( rep -> rep.getId( ).equals( "snapshots") ) );
138 assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals( "central") ));
142 public void getManagedRepositories( ) throws Exception
144 Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
145 assertEquals( 4, repos.size( ) );
146 assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals("internal") ));
147 assertTrue( repos.stream( ).anyMatch( rep -> rep.getId( ).equals( "snapshots") ) );
151 public void getRemoteRepositories( ) throws Exception
153 Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories( );
154 assertEquals( 1, repos.size( ) );
155 assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals( "central") ));
159 public void getRepository( ) throws Exception
161 Repository repo = repositoryRegistry.getRepository( "internal" );
163 assertEquals("internal", repo.getId());
164 assertEquals("Archiva Managed Internal Repository", repo.getName());
165 assertEquals("This is internal repository.", repo.getDescription());
166 assertEquals( "default", repo.getLayout( ) );
167 assertEquals("0 0 * * * ?", repo.getSchedulingDefinition());
168 assertTrue(repo instanceof ManagedRepository);
169 assertTrue( repo.hasIndex( ) );
170 assertTrue(repo.isScanned());
171 assertEquals( RepositoryType.MAVEN, repo.getType());
175 public void getManagedRepository( ) throws Exception
177 ManagedRepository repo = repositoryRegistry.getManagedRepository( "internal" );
179 assertEquals("internal", repo.getId());
180 assertEquals("Archiva Managed Internal Repository", repo.getName());
181 assertEquals("This is internal repository.", repo.getDescription());
182 assertEquals( "default", repo.getLayout( ) );
183 assertEquals("0 0 * * * ?", repo.getSchedulingDefinition());
184 assertTrue( repo.hasIndex( ) );
185 assertTrue(repo.isScanned());
186 assertEquals(RepositoryType.MAVEN, repo.getType());
187 assertTrue(repo.getActiveReleaseSchemes().contains( ReleaseScheme.RELEASE));
188 assertFalse( repo.getActiveReleaseSchemes( ).contains( ReleaseScheme.SNAPSHOT ) );
189 assertNotNull(repo.getContent());
191 assertNull(repositoryRegistry.getManagedRepository( "xyu" ));
196 public void getRemoteRepository( ) throws Exception
198 RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
200 assertEquals("central", repo.getId());
201 assertEquals("Central Repository", repo.getName());
202 assertEquals("", repo.getDescription());
203 assertEquals( "default", repo.getLayout( ) );
204 assertEquals("0 0 08 ? * SUN", repo.getSchedulingDefinition());
205 assertTrue( repo.hasIndex( ) );
206 assertTrue(repo.isScanned());
207 assertEquals(RepositoryType.MAVEN, repo.getType());
209 assertEquals(35, repo.getTimeout().getSeconds());
213 public void putManagedRepository( ) throws Exception
215 BasicManagedRepository managedRepository = BasicManagedRepository.newFilesystemInstance("test001", "Test repo", archivaConfiguration.getRepositoryBaseDir().resolve("test001"));
216 managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
217 repositoryRegistry.putRepository(managedRepository);
219 assertNotNull(managedRepository.getContent());
220 assertEquals(6, repositoryRegistry.getRepositories().size());
222 managedRepository = BasicManagedRepository.newFilesystemInstance("central", "Test repo", archivaConfiguration.getRepositoryBaseDir().resolve("central"));
223 managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
224 ManagedRepository updatedRepo;
226 repositoryRegistry.putRepository( managedRepository );
227 throw new RuntimeException("Repository exception should be thrown, if there exists a remote repository already with that id");
228 } catch ( RepositoryException e) {
231 managedRepository = BasicManagedRepository.newFilesystemInstance("internal", "Test repo", archivaConfiguration.getRepositoryBaseDir().resolve("internal"));
232 managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
233 updatedRepo = repositoryRegistry.putRepository( managedRepository );
235 assertSame( updatedRepo, managedRepository );
236 assertNotNull(managedRepository.getContent());
237 assertEquals(6, repositoryRegistry.getRepositories().size());
238 ManagedRepository managedRepository1 = repositoryRegistry.getManagedRepository( "internal" );
239 assertEquals("Test repo", managedRepository1.getName());
240 assertSame( managedRepository1, managedRepository );
245 public void putManagedRepositoryFromConfig( ) throws Exception
247 ManagedRepositoryConfiguration cfg = new ManagedRepositoryConfiguration();
248 cfg.setId("test002");
249 cfg.setName("This is test 002");
250 ManagedRepository repo = repositoryRegistry.putRepository( cfg );
252 assertEquals("test002", repo.getId());
253 assertEquals("This is test 002", repo.getName());
254 assertNotNull(repo.getContent());
255 archivaConfiguration.reload();
256 Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
257 assertEquals(5, repos.size());
259 ManagedRepository internalRepo = repositoryRegistry.getManagedRepository( "internal" );
260 cfg = new ManagedRepositoryConfiguration();
261 cfg.setId("internal");
262 cfg.setName("This is internal test 002");
263 repo = repositoryRegistry.putRepository( cfg );
264 assertSame( internalRepo, repo );
265 assertEquals("This is internal test 002",repo.getName());
266 assertEquals(5, repositoryRegistry.getManagedRepositories().size());
268 repositoryRegistry.reload();
269 assertEquals(5, repositoryRegistry.getManagedRepositories().size());
274 public void putManagedRepositoryFromConfigWithoutSave( ) throws Exception
276 Configuration configuration = archivaConfiguration.getConfiguration();
277 int actualSize = configuration.getManagedRepositories( ).size( );
278 Configuration newConfiguration = new Configuration( );
279 ManagedRepositoryConfiguration cfg = new ManagedRepositoryConfiguration();
280 cfg.setId("test002");
281 cfg.setName("This is test 002");
282 ManagedRepository repo = repositoryRegistry.putRepository( cfg, newConfiguration );
284 assertEquals("test002", repo.getId());
285 assertEquals("This is test 002", repo.getName());
286 assertNotNull(repo.getContent());
287 archivaConfiguration.reload();
288 assertEquals(actualSize, configuration.getManagedRepositories().size());
289 List<ManagedRepositoryConfiguration> repos = newConfiguration.getManagedRepositories( );
290 assertEquals(1, repos.size());
292 ManagedRepository internalRepo = repositoryRegistry.getManagedRepository( "internal" );
293 cfg = new ManagedRepositoryConfiguration();
294 cfg.setId("internal");
295 cfg.setName("This is internal test 002");
296 repo = repositoryRegistry.putRepository( cfg, newConfiguration );
297 assertEquals("This is internal test 002",repo.getName());
298 assertEquals(2, newConfiguration.getManagedRepositories().size());
300 repositoryRegistry.reload();
301 assertEquals(actualSize, configuration.getManagedRepositories().size());
305 public void putRemoteRepository( ) throws Exception
307 BasicRemoteRepository remoteRepository = BasicRemoteRepository.newFilesystemInstance( "test001", "Test repo", archivaConfiguration.getRemoteRepositoryBaseDir() );
308 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
309 RemoteRepository newRepo = repositoryRegistry.putRepository(remoteRepository);
311 assertSame( remoteRepository, newRepo );
312 assertNotNull(remoteRepository.getContent());
313 assertEquals(6, repositoryRegistry.getRepositories().size());
315 remoteRepository = BasicRemoteRepository.newFilesystemInstance( "internal", "Test repo", archivaConfiguration.getRemoteRepositoryBaseDir() );
316 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
317 RemoteRepository updatedRepo;
320 updatedRepo = repositoryRegistry.putRepository( remoteRepository );
321 assertSame( remoteRepository, updatedRepo );
322 throw new RuntimeException("Should throw repository exception, if repository exists already and is not the same type.");
323 } catch (RepositoryException e) {
327 remoteRepository = BasicRemoteRepository.newFilesystemInstance( "central", "Test repo", archivaConfiguration.getRemoteRepositoryBaseDir() );
328 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
329 updatedRepo = repositoryRegistry.putRepository( remoteRepository );
331 assertSame( updatedRepo, remoteRepository );
332 assertNotNull(remoteRepository.getContent());
333 assertEquals(6, repositoryRegistry.getRepositories().size());
334 RemoteRepository remoteRepository1 = repositoryRegistry.getRemoteRepository( "central" );
335 assertEquals("Test repo", remoteRepository1.getName());
336 assertSame( remoteRepository1, remoteRepository );
340 public void putRemoteRepositoryFromConfig( ) throws Exception
342 RemoteRepositoryConfiguration cfg = new RemoteRepositoryConfiguration();
343 cfg.setId("test002");
344 cfg.setName("This is test 002");
345 RemoteRepository repo = repositoryRegistry.putRepository( cfg );
347 assertEquals("test002", repo.getId());
348 assertEquals("This is test 002", repo.getName());
349 assertNotNull(repo.getContent());
350 archivaConfiguration.reload();
351 Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories();
352 assertEquals(2, repos.size());
354 RemoteRepository internalRepo = repositoryRegistry.getRemoteRepository( "central" );
355 cfg = new RemoteRepositoryConfiguration();
356 cfg.setId("central");
357 cfg.setName("This is central test 002");
358 repo = repositoryRegistry.putRepository( cfg );
359 assertSame( internalRepo, repo );
360 assertEquals("This is central test 002",repo.getName());
361 assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
363 repositoryRegistry.reload();
364 assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
368 public void putRemoteRepositoryFromConfigWithoutSave( ) throws Exception
370 Configuration configuration = archivaConfiguration.getConfiguration();
371 RemoteRepositoryConfiguration cfg = new RemoteRepositoryConfiguration();
372 cfg.setId("test002");
373 cfg.setName("This is test 002");
374 RemoteRepository repo = repositoryRegistry.putRepository( cfg, configuration );
376 assertEquals("test002", repo.getId());
377 assertEquals("This is test 002", repo.getName());
378 assertNotNull(repo.getContent());
379 archivaConfiguration.reload();
380 assertEquals(1, archivaConfiguration.getConfiguration().getRemoteRepositories().size());
381 Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories();
382 assertEquals(2, repos.size());
384 RemoteRepository internalRepo = repositoryRegistry.getRemoteRepository( "central" );
385 cfg = new RemoteRepositoryConfiguration();
386 cfg.setId("central");
387 cfg.setName("This is central test 002");
388 repo = repositoryRegistry.putRepository( cfg, configuration );
389 assertSame( internalRepo, repo );
390 assertEquals("This is central test 002",repo.getName());
391 assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
393 repositoryRegistry.reload();
394 assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
398 public void removeRepository( ) throws Exception
400 assertEquals(5, repositoryRegistry.getRepositories().size());
401 Repository repo = repositoryRegistry.getRepository( "snapshots" );
402 repositoryRegistry.removeRepository( repo );
403 assertEquals(4, repositoryRegistry.getRepositories().size());
404 assertTrue( repositoryRegistry.getRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
405 archivaConfiguration.reload();
406 repositoryRegistry.reload();
407 assertEquals(4, repositoryRegistry.getRepositories().size());
411 public void removeManagedRepository( ) throws Exception
414 assertEquals(4, repositoryRegistry.getManagedRepositories().size());
415 ManagedRepository repo = repositoryRegistry.getManagedRepository( "snapshots" );
416 repositoryRegistry.removeRepository( repo );
417 assertEquals(3, repositoryRegistry.getManagedRepositories().size());
418 assertTrue( repositoryRegistry.getManagedRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
419 archivaConfiguration.reload();
420 repositoryRegistry.reload();
421 assertEquals(3, repositoryRegistry.getManagedRepositories().size());
425 public void removeManagedRepositoryWithoutSave( ) throws Exception
427 Configuration configuration = archivaConfiguration.getConfiguration();
428 assertEquals(4, repositoryRegistry.getManagedRepositories().size());
429 ManagedRepository repo = repositoryRegistry.getManagedRepository( "snapshots" );
430 repositoryRegistry.removeRepository( repo, configuration );
431 assertEquals(3, repositoryRegistry.getManagedRepositories().size());
432 assertTrue( repositoryRegistry.getManagedRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
433 archivaConfiguration.reload();
434 repositoryRegistry.reload();
435 assertEquals(4, repositoryRegistry.getManagedRepositories().size());
440 public void removeRemoteRepository( ) throws Exception
442 assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
443 RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
444 repositoryRegistry.removeRepository( repo );
445 assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
446 assertTrue( repositoryRegistry.getRemoteRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "central" ) ) );
447 archivaConfiguration.reload();
448 repositoryRegistry.reload();
449 assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
453 public void removeRemoteRepositoryWithoutSave( ) throws Exception
455 Configuration configuration = archivaConfiguration.getConfiguration();
456 assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
457 RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
458 repositoryRegistry.removeRepository( repo, configuration );
459 assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
460 assertTrue( repositoryRegistry.getRemoteRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "central" ) ) );
461 archivaConfiguration.reload();
462 repositoryRegistry.reload();
463 assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
468 public void cloneManagedRepo( ) throws Exception
470 ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( "internal" );
474 repositoryRegistry.clone(managedRepository, "snapshots");
475 throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
477 catch ( RepositoryException e )
484 repositoryRegistry.clone(managedRepository, "central");
485 throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
487 catch ( RepositoryException e )
492 ManagedRepository clone = repositoryRegistry.clone( managedRepository, "newinternal" );
493 assertNotNull(clone);
494 assertNull(clone.getContent());
495 assertEquals("Archiva Managed Internal Repository", clone.getName());
496 assertNotSame( managedRepository, clone );
501 public void cloneRemoteRepo( ) throws Exception
503 RemoteRepository remoteRepository = repositoryRegistry.getRemoteRepository( "central" );
507 repositoryRegistry.clone(remoteRepository, "snapshots");
508 throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
510 catch ( RepositoryException e )
517 repositoryRegistry.clone(remoteRepository, "central");
518 throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
520 catch ( RepositoryException e )
525 RemoteRepository clone = repositoryRegistry.clone( remoteRepository, "newCentral" );
526 assertNotNull(clone);
527 assertNull(clone.getContent());
528 assertEquals("Central Repository", clone.getName());
529 assertNotSame( remoteRepository, clone );
534 void validateRepository() {
535 Repository repo = repositoryRegistry.getRepository( "internal" );
536 assertNotNull( repo );
537 assertTrue( repositoryRegistry.validateRepository( repo ).isValid() );