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.test.utils.ArchivaSpringJUnit4ClassRunner;
34 import org.junit.After;
35 import org.junit.AfterClass;
36 import org.junit.Assert;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.springframework.test.context.ContextConfiguration;
43 import javax.inject.Inject;
44 import java.io.IOException;
45 import java.net.URISyntaxException;
47 import java.nio.file.Files;
48 import java.nio.file.Path;
49 import java.nio.file.Paths;
50 import java.nio.file.StandardCopyOption;
51 import java.util.Collection;
53 import static org.junit.Assert.*;
56 * Test for RepositoryRegistry
58 @RunWith(ArchivaSpringJUnit4ClassRunner.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 private static final Path userCfg = Paths.get(System.getProperty( "user.home" ), ".m2/archiva.xml");
71 private static Path cfgCopy;
72 private static Path archivaCfg;
75 public static void classSetup() throws IOException, URISyntaxException
77 URL archivaCfgUri = Thread.currentThread().getContextClassLoader().getResource( "archiva.xml" );
78 if (archivaCfgUri!=null) {
79 archivaCfg = Paths.get(archivaCfgUri.toURI());
80 cfgCopy = Files.createTempFile( "archiva-backup", ".xml" );
81 Files.copy( archivaCfg, cfgCopy, StandardCopyOption.REPLACE_EXISTING);
86 public static void classTearDown() throws IOException
89 Files.deleteIfExists( cfgCopy );
94 public void setUp( ) throws Exception
96 assertNotNull( repositoryRegistry );
97 Files.deleteIfExists( userCfg );
98 URL archivaCfgUri = Thread.currentThread().getContextClassLoader().getResource( "archiva.xml" );
99 if (archivaCfgUri!=null) {
100 archivaCfg = Paths.get(archivaCfgUri.toURI());
101 if (Files.exists(cfgCopy))
103 Files.copy( cfgCopy, archivaCfg , StandardCopyOption.REPLACE_EXISTING);
106 archivaConfiguration.reload();
107 repositoryRegistry.reload();
111 public void tearDown( ) throws Exception
113 Files.deleteIfExists( userCfg );
114 if (cfgCopy!=null && Files.exists(cfgCopy)) {
115 Files.copy(cfgCopy, archivaCfg, StandardCopyOption.REPLACE_EXISTING);
120 public void getRepositories( ) throws Exception
122 Collection<Repository> repos = repositoryRegistry.getRepositories( );
123 assertEquals( 5, repos.size( ) );
124 assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals("internal") ));
125 assertTrue( repos.stream( ).anyMatch( rep -> rep.getId( ).equals( "snapshots") ) );
126 assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals( "central") ));
130 public void getManagedRepositories( ) throws Exception
132 Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
133 assertEquals( 4, repos.size( ) );
134 assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals("internal") ));
135 assertTrue( repos.stream( ).anyMatch( rep -> rep.getId( ).equals( "snapshots") ) );
139 public void getRemoteRepositories( ) throws Exception
141 Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories( );
142 assertEquals( 1, repos.size( ) );
143 assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals( "central") ));
147 public void getRepository( ) throws Exception
149 Repository repo = repositoryRegistry.getRepository( "internal" );
151 assertEquals("internal", repo.getId());
152 assertEquals("Archiva Managed Internal Repository", repo.getName());
153 assertEquals("This is internal repository.", repo.getDescription());
154 assertEquals( "default", repo.getLayout( ) );
155 assertEquals("0 0 * * * ?", repo.getSchedulingDefinition());
156 assertTrue(repo instanceof ManagedRepository);
157 assertTrue( repo.hasIndex( ) );
158 assertTrue(repo.isScanned());
159 Assert.assertEquals( RepositoryType.MAVEN, repo.getType());
163 public void getManagedRepository( ) throws Exception
165 ManagedRepository repo = repositoryRegistry.getManagedRepository( "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.hasIndex( ) );
173 assertTrue(repo.isScanned());
174 assertEquals(RepositoryType.MAVEN, repo.getType());
175 assertTrue(repo.getActiveReleaseSchemes().contains( ReleaseScheme.RELEASE));
176 assertFalse( repo.getActiveReleaseSchemes( ).contains( ReleaseScheme.SNAPSHOT ) );
177 assertNotNull(repo.getContent());
179 assertNull(repositoryRegistry.getManagedRepository( "xyu" ));
184 public void getRemoteRepository( ) throws Exception
186 RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
188 assertEquals("central", repo.getId());
189 assertEquals("Central Repository", repo.getName());
190 assertEquals("", repo.getDescription());
191 assertEquals( "default", repo.getLayout( ) );
192 assertEquals("0 0 08 ? * SUN", repo.getSchedulingDefinition());
193 assertTrue( repo.hasIndex( ) );
194 assertTrue(repo.isScanned());
195 assertEquals(RepositoryType.MAVEN, repo.getType());
197 assertEquals(35, repo.getTimeout().getSeconds());
201 public void putManagedRepository( ) throws Exception
203 BasicManagedRepository managedRepository = BasicManagedRepository.newFilesystemInstance("test001", "Test repo", archivaConfiguration.getRepositoryBaseDir().resolve("test001"));
204 managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
205 repositoryRegistry.putRepository(managedRepository);
207 assertNotNull(managedRepository.getContent());
208 assertEquals(6, repositoryRegistry.getRepositories().size());
210 managedRepository = BasicManagedRepository.newFilesystemInstance("central", "Test repo", archivaConfiguration.getRepositoryBaseDir().resolve("central"));
211 managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
212 ManagedRepository updatedRepo = null;
214 repositoryRegistry.putRepository( managedRepository );
215 throw new RuntimeException("Repository exception should be thrown, if there exists a remote repository already with that id");
216 } catch ( RepositoryException e) {
219 managedRepository = BasicManagedRepository.newFilesystemInstance("internal", "Test repo", archivaConfiguration.getRepositoryBaseDir().resolve("internal"));
220 managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
221 updatedRepo = repositoryRegistry.putRepository( managedRepository );
223 assertTrue(updatedRepo==managedRepository);
224 assertNotNull(managedRepository.getContent());
225 assertEquals(6, repositoryRegistry.getRepositories().size());
226 ManagedRepository managedRepository1 = repositoryRegistry.getManagedRepository( "internal" );
227 assertEquals("Test repo", managedRepository1.getName());
228 assertTrue(managedRepository1==managedRepository);
233 public void putManagedRepositoryFromConfig( ) throws Exception
235 ManagedRepositoryConfiguration cfg = new ManagedRepositoryConfiguration();
236 cfg.setId("test002");
237 cfg.setName("This is test 002");
238 ManagedRepository repo = repositoryRegistry.putRepository( cfg );
240 assertEquals("test002", repo.getId());
241 assertEquals("This is test 002", repo.getName());
242 assertNotNull(repo.getContent());
243 archivaConfiguration.reload();
244 Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
245 assertEquals(5, repos.size());
247 ManagedRepository internalRepo = repositoryRegistry.getManagedRepository( "internal" );
248 cfg = new ManagedRepositoryConfiguration();
249 cfg.setId("internal");
250 cfg.setName("This is internal test 002");
251 repo = repositoryRegistry.putRepository( cfg );
252 assertTrue(internalRepo==repo);
253 assertEquals("This is internal test 002",repo.getName());
254 assertEquals(5, repositoryRegistry.getManagedRepositories().size());
256 repositoryRegistry.reload();
257 assertEquals(5, repositoryRegistry.getManagedRepositories().size());
262 public void putManagedRepositoryFromConfigWithoutSave( ) throws Exception
264 Configuration configuration = archivaConfiguration.getConfiguration();
265 ManagedRepositoryConfiguration cfg = new ManagedRepositoryConfiguration();
266 cfg.setId("test002");
267 cfg.setName("This is test 002");
268 ManagedRepository repo = repositoryRegistry.putRepository( cfg, configuration );
270 assertEquals("test002", repo.getId());
271 assertEquals("This is test 002", repo.getName());
272 assertNotNull(repo.getContent());
273 archivaConfiguration.reload();
274 assertEquals(3, archivaConfiguration.getConfiguration().getManagedRepositories().size());
275 Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
276 assertEquals(5, repos.size());
278 ManagedRepository internalRepo = repositoryRegistry.getManagedRepository( "internal" );
279 cfg = new ManagedRepositoryConfiguration();
280 cfg.setId("internal");
281 cfg.setName("This is internal test 002");
282 repo = repositoryRegistry.putRepository( cfg, configuration );
283 assertTrue(internalRepo==repo);
284 assertEquals("This is internal test 002",repo.getName());
285 assertEquals(5, repositoryRegistry.getManagedRepositories().size());
287 repositoryRegistry.reload();
288 assertEquals(4, repositoryRegistry.getManagedRepositories().size());
292 public void putRemoteRepository( ) throws Exception
294 BasicRemoteRepository remoteRepository = BasicRemoteRepository.newFilesystemInstance( "test001", "Test repo", archivaConfiguration.getRemoteRepositoryBaseDir() );
295 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
296 RemoteRepository newRepo = repositoryRegistry.putRepository(remoteRepository);
298 assertTrue(remoteRepository==newRepo);
299 assertNotNull(remoteRepository.getContent());
300 assertEquals(6, repositoryRegistry.getRepositories().size());
302 remoteRepository = BasicRemoteRepository.newFilesystemInstance( "internal", "Test repo", archivaConfiguration.getRemoteRepositoryBaseDir() );
303 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
304 RemoteRepository updatedRepo = null;
307 updatedRepo = repositoryRegistry.putRepository( remoteRepository );
308 throw new RuntimeException("Should throw repository exception, if repository exists already and is not the same type.");
309 } catch (RepositoryException e) {
313 remoteRepository = BasicRemoteRepository.newFilesystemInstance( "central", "Test repo", archivaConfiguration.getRemoteRepositoryBaseDir() );
314 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
315 updatedRepo = repositoryRegistry.putRepository( remoteRepository );
317 assertTrue(updatedRepo==remoteRepository);
318 assertNotNull(remoteRepository.getContent());
319 assertEquals(6, repositoryRegistry.getRepositories().size());
320 RemoteRepository remoteRepository1 = repositoryRegistry.getRemoteRepository( "central" );
321 assertEquals("Test repo", remoteRepository1.getName());
322 assertTrue(remoteRepository1==remoteRepository);
326 public void putRemoteRepositoryFromConfig( ) throws Exception
328 RemoteRepositoryConfiguration cfg = new RemoteRepositoryConfiguration();
329 cfg.setId("test002");
330 cfg.setName("This is test 002");
331 RemoteRepository repo = repositoryRegistry.putRepository( cfg );
333 assertEquals("test002", repo.getId());
334 assertEquals("This is test 002", repo.getName());
335 assertNotNull(repo.getContent());
336 archivaConfiguration.reload();
337 Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories();
338 assertEquals(2, repos.size());
340 RemoteRepository internalRepo = repositoryRegistry.getRemoteRepository( "central" );
341 cfg = new RemoteRepositoryConfiguration();
342 cfg.setId("central");
343 cfg.setName("This is central test 002");
344 repo = repositoryRegistry.putRepository( cfg );
345 assertTrue(internalRepo==repo);
346 assertEquals("This is central test 002",repo.getName());
347 assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
349 repositoryRegistry.reload();
350 assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
354 public void putRemoteRepositoryFromConfigWithoutSave( ) throws Exception
356 Configuration configuration = archivaConfiguration.getConfiguration();
357 RemoteRepositoryConfiguration cfg = new RemoteRepositoryConfiguration();
358 cfg.setId("test002");
359 cfg.setName("This is test 002");
360 RemoteRepository repo = repositoryRegistry.putRepository( cfg, configuration );
362 assertEquals("test002", repo.getId());
363 assertEquals("This is test 002", repo.getName());
364 assertNotNull(repo.getContent());
365 archivaConfiguration.reload();
366 assertEquals(1, archivaConfiguration.getConfiguration().getRemoteRepositories().size());
367 Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories();
368 assertEquals(2, repos.size());
370 RemoteRepository internalRepo = repositoryRegistry.getRemoteRepository( "central" );
371 cfg = new RemoteRepositoryConfiguration();
372 cfg.setId("central");
373 cfg.setName("This is central test 002");
374 repo = repositoryRegistry.putRepository( cfg, configuration );
375 assertTrue(internalRepo==repo);
376 assertEquals("This is central test 002",repo.getName());
377 assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
379 repositoryRegistry.reload();
380 assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
384 public void removeRepository( ) throws Exception
386 assertEquals(5, repositoryRegistry.getRepositories().size());
387 Repository repo = repositoryRegistry.getRepository( "snapshots" );
388 repositoryRegistry.removeRepository( repo );
389 assertEquals(4, repositoryRegistry.getRepositories().size());
390 assertTrue( repositoryRegistry.getRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
391 archivaConfiguration.reload();
392 repositoryRegistry.reload();
393 assertEquals(4, repositoryRegistry.getRepositories().size());
397 public void removeManagedRepository( ) throws Exception
400 assertEquals(4, repositoryRegistry.getManagedRepositories().size());
401 ManagedRepository repo = repositoryRegistry.getManagedRepository( "snapshots" );
402 repositoryRegistry.removeRepository( repo );
403 assertEquals(3, repositoryRegistry.getManagedRepositories().size());
404 assertTrue( repositoryRegistry.getManagedRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
405 archivaConfiguration.reload();
406 repositoryRegistry.reload();
407 assertEquals(3, repositoryRegistry.getManagedRepositories().size());
411 public void removeManagedRepositoryWithoutSave( ) throws Exception
413 Configuration configuration = archivaConfiguration.getConfiguration();
414 assertEquals(4, repositoryRegistry.getManagedRepositories().size());
415 ManagedRepository repo = repositoryRegistry.getManagedRepository( "snapshots" );
416 repositoryRegistry.removeRepository( repo, configuration );
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(4, repositoryRegistry.getManagedRepositories().size());
426 public void removeRemoteRepository( ) throws Exception
428 assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
429 RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
430 repositoryRegistry.removeRepository( repo );
431 assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
432 assertTrue( repositoryRegistry.getRemoteRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "central" ) ) );
433 archivaConfiguration.reload();
434 repositoryRegistry.reload();
435 assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
439 public void removeRemoteRepositoryWithoutSave( ) throws Exception
441 Configuration configuration = archivaConfiguration.getConfiguration();
442 assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
443 RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
444 repositoryRegistry.removeRepository( repo, configuration );
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(1, repositoryRegistry.getRemoteRepositories().size());
454 public void cloneManagedRepo( ) throws Exception
456 ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( "internal" );
460 repositoryRegistry.clone(managedRepository, "snapshots");
461 throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
463 catch ( RepositoryException e )
470 repositoryRegistry.clone(managedRepository, "central");
471 throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
473 catch ( RepositoryException e )
478 ManagedRepository clone = repositoryRegistry.clone( managedRepository, "newinternal" );
479 assertNotNull(clone);
480 assertNull(clone.getContent());
481 assertEquals("Archiva Managed Internal Repository", clone.getName());
482 assertFalse(managedRepository==clone);
487 public void cloneRemoteRepo( ) throws Exception
489 RemoteRepository remoteRepository = repositoryRegistry.getRemoteRepository( "central" );
493 repositoryRegistry.clone(remoteRepository, "snapshots");
494 throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
496 catch ( RepositoryException e )
503 repositoryRegistry.clone(remoteRepository, "central");
504 throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
506 catch ( RepositoryException e )
511 RemoteRepository clone = repositoryRegistry.clone( remoteRepository, "newCentral" );
512 assertNotNull(clone);
513 assertNull(clone.getContent());
514 assertEquals("Central Repository", clone.getName());
515 assertFalse(remoteRepository==clone);