1 package org.apache.archiva.repository;
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.test.utils.ArchivaSpringJUnit4ClassRunner;
27 import org.junit.After;
28 import org.junit.AfterClass;
29 import org.junit.Before;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.springframework.test.context.ContextConfiguration;
35 import javax.inject.Inject;
36 import java.io.IOException;
37 import java.net.URISyntaxException;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.nio.file.Paths;
42 import java.nio.file.StandardCopyOption;
43 import java.util.Collection;
45 import static org.junit.Assert.*;
48 * Test for RepositoryRegistry
50 @RunWith(ArchivaSpringJUnit4ClassRunner.class)
51 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
52 public class RepositoryRegistryTest
56 RepositoryRegistry repositoryRegistry;
59 ArchivaConfiguration archivaConfiguration;
61 private static final Path userCfg = Paths.get(System.getProperty( "user.home" ), ".m2/archiva.xml");
63 private static Path cfgCopy;
64 private static Path archivaCfg;
67 public static void classSetup() throws IOException, URISyntaxException
69 URL archivaCfgUri = Thread.currentThread().getContextClassLoader().getResource( "archiva.xml" );
70 if (archivaCfgUri!=null) {
71 archivaCfg = Paths.get(archivaCfgUri.toURI());
72 cfgCopy = Files.createTempFile( "archiva-backup", ".xml" );
73 Files.copy( archivaCfg, cfgCopy, StandardCopyOption.REPLACE_EXISTING);
78 public static void classTearDown() throws IOException
81 Files.deleteIfExists( cfgCopy );
86 public void setUp( ) throws Exception
88 assertNotNull( repositoryRegistry );
89 Files.deleteIfExists( userCfg );
90 URL archivaCfgUri = Thread.currentThread().getContextClassLoader().getResource( "archiva.xml" );
91 if (archivaCfgUri!=null) {
92 archivaCfg = Paths.get(archivaCfgUri.toURI());
93 if (Files.exists(cfgCopy))
95 Files.copy( cfgCopy, archivaCfg , StandardCopyOption.REPLACE_EXISTING);
98 archivaConfiguration.reload();
99 repositoryRegistry.reload();
103 public void tearDown( ) throws Exception
105 Files.deleteIfExists( userCfg );
106 if (cfgCopy!=null && Files.exists(cfgCopy)) {
107 Files.copy(cfgCopy, archivaCfg, StandardCopyOption.REPLACE_EXISTING);
112 public void getRepositories( ) throws Exception
114 Collection<Repository> repos = repositoryRegistry.getRepositories( );
115 assertEquals( 5, repos.size( ) );
116 assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals("internal") ));
117 assertTrue( repos.stream( ).anyMatch( rep -> rep.getId( ).equals( "snapshots") ) );
118 assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals( "central") ));
122 public void getManagedRepositories( ) throws Exception
124 Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
125 assertEquals( 4, repos.size( ) );
126 assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals("internal") ));
127 assertTrue( repos.stream( ).anyMatch( rep -> rep.getId( ).equals( "snapshots") ) );
131 public void getRemoteRepositories( ) throws Exception
133 Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories( );
134 assertEquals( 1, repos.size( ) );
135 assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals( "central") ));
139 public void getRepository( ) throws Exception
141 Repository repo = repositoryRegistry.getRepository( "internal" );
143 assertEquals("internal", repo.getId());
144 assertEquals("Archiva Managed Internal Repository", repo.getName());
145 assertEquals("This is internal repository.", repo.getDescription());
146 assertEquals( "default", repo.getLayout( ) );
147 assertEquals("0 0 * * * ?", repo.getSchedulingDefinition());
148 assertTrue(repo instanceof ManagedRepository);
149 assertTrue( repo.hasIndex( ) );
150 assertTrue(repo.isScanned());
151 assertEquals(RepositoryType.MAVEN, repo.getType());
155 public void getManagedRepository( ) throws Exception
157 ManagedRepository repo = repositoryRegistry.getManagedRepository( "internal" );
159 assertEquals("internal", repo.getId());
160 assertEquals("Archiva Managed Internal Repository", repo.getName());
161 assertEquals("This is internal repository.", repo.getDescription());
162 assertEquals( "default", repo.getLayout( ) );
163 assertEquals("0 0 * * * ?", repo.getSchedulingDefinition());
164 assertTrue( repo.hasIndex( ) );
165 assertTrue(repo.isScanned());
166 assertEquals(RepositoryType.MAVEN, repo.getType());
167 assertTrue(repo.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE));
168 assertFalse( repo.getActiveReleaseSchemes( ).contains( ReleaseScheme.SNAPSHOT ) );
169 assertNotNull(repo.getContent());
171 assertNull(repositoryRegistry.getManagedRepository( "xyu" ));
176 public void getRemoteRepository( ) throws Exception
178 RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
180 assertEquals("central", repo.getId());
181 assertEquals("Central Repository", repo.getName());
182 assertEquals("", repo.getDescription());
183 assertEquals( "default", repo.getLayout( ) );
184 assertEquals("0 0 08 ? * SUN", repo.getSchedulingDefinition());
185 assertTrue( repo.hasIndex( ) );
186 assertTrue(repo.isScanned());
187 assertEquals(RepositoryType.MAVEN, repo.getType());
189 assertEquals(35, repo.getTimeout().getSeconds());
193 public void putManagedRepository( ) throws Exception
195 BasicManagedRepository managedRepository = new BasicManagedRepository( "test001", "Test repo" );
196 managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
197 repositoryRegistry.putRepository(managedRepository);
199 assertNotNull(managedRepository.getContent());
200 assertEquals(6, repositoryRegistry.getRepositories().size());
202 managedRepository = new BasicManagedRepository( "central", "Test repo" );
203 managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
204 ManagedRepository updatedRepo = null;
206 repositoryRegistry.putRepository( managedRepository );
207 throw new RuntimeException("Repository exception should be thrown, if there exists a remote repository already with that id");
208 } catch (RepositoryException e) {
211 managedRepository = new BasicManagedRepository( "internal", "Test repo" );
212 managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
213 updatedRepo = repositoryRegistry.putRepository( managedRepository );
215 assertTrue(updatedRepo==managedRepository);
216 assertNotNull(managedRepository.getContent());
217 assertEquals(6, repositoryRegistry.getRepositories().size());
218 ManagedRepository managedRepository1 = repositoryRegistry.getManagedRepository( "internal" );
219 assertEquals("Test repo", managedRepository1.getName());
220 assertTrue(managedRepository1==managedRepository);
225 public void putManagedRepositoryFromConfig( ) throws Exception
227 ManagedRepositoryConfiguration cfg = new ManagedRepositoryConfiguration();
228 cfg.setId("test002");
229 cfg.setName("This is test 002");
230 ManagedRepository repo = repositoryRegistry.putRepository( cfg );
232 assertEquals("test002", repo.getId());
233 assertEquals("This is test 002", repo.getName());
234 assertNotNull(repo.getContent());
235 archivaConfiguration.reload();
236 Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
237 assertEquals(5, repos.size());
239 ManagedRepository internalRepo = repositoryRegistry.getManagedRepository( "internal" );
240 cfg = new ManagedRepositoryConfiguration();
241 cfg.setId("internal");
242 cfg.setName("This is internal test 002");
243 repo = repositoryRegistry.putRepository( cfg );
244 assertTrue(internalRepo==repo);
245 assertEquals("This is internal test 002",repo.getName());
246 assertEquals(5, repositoryRegistry.getManagedRepositories().size());
248 repositoryRegistry.reload();
249 assertEquals(5, repositoryRegistry.getManagedRepositories().size());
254 public void putManagedRepositoryFromConfigWithoutSave( ) throws Exception
256 Configuration configuration = archivaConfiguration.getConfiguration();
257 ManagedRepositoryConfiguration cfg = new ManagedRepositoryConfiguration();
258 cfg.setId("test002");
259 cfg.setName("This is test 002");
260 ManagedRepository repo = repositoryRegistry.putRepository( cfg, configuration );
262 assertEquals("test002", repo.getId());
263 assertEquals("This is test 002", repo.getName());
264 assertNotNull(repo.getContent());
265 archivaConfiguration.reload();
266 assertEquals(3, archivaConfiguration.getConfiguration().getManagedRepositories().size());
267 Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
268 assertEquals(5, repos.size());
270 ManagedRepository internalRepo = repositoryRegistry.getManagedRepository( "internal" );
271 cfg = new ManagedRepositoryConfiguration();
272 cfg.setId("internal");
273 cfg.setName("This is internal test 002");
274 repo = repositoryRegistry.putRepository( cfg, configuration );
275 assertTrue(internalRepo==repo);
276 assertEquals("This is internal test 002",repo.getName());
277 assertEquals(5, repositoryRegistry.getManagedRepositories().size());
279 repositoryRegistry.reload();
280 assertEquals(4, repositoryRegistry.getManagedRepositories().size());
284 public void putRemoteRepository( ) throws Exception
286 BasicRemoteRepository remoteRepository = new BasicRemoteRepository( "test001", "Test repo" );
287 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
288 RemoteRepository newRepo = repositoryRegistry.putRepository(remoteRepository);
290 assertTrue(remoteRepository==newRepo);
291 assertNotNull(remoteRepository.getContent());
292 assertEquals(6, repositoryRegistry.getRepositories().size());
294 remoteRepository = new BasicRemoteRepository( "internal", "Test repo" );
295 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
296 RemoteRepository updatedRepo = null;
299 updatedRepo = repositoryRegistry.putRepository( remoteRepository );
300 throw new RuntimeException("Should throw repository exception, if repository exists already and is not the same type.");
301 } catch (RepositoryException e) {
305 remoteRepository = new BasicRemoteRepository( "central", "Test repo" );
306 remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
307 updatedRepo = repositoryRegistry.putRepository( remoteRepository );
309 assertTrue(updatedRepo==remoteRepository);
310 assertNotNull(remoteRepository.getContent());
311 assertEquals(6, repositoryRegistry.getRepositories().size());
312 RemoteRepository remoteRepository1 = repositoryRegistry.getRemoteRepository( "central" );
313 assertEquals("Test repo", remoteRepository1.getName());
314 assertTrue(remoteRepository1==remoteRepository);
318 public void putRemoteRepositoryFromConfig( ) throws Exception
320 RemoteRepositoryConfiguration cfg = new RemoteRepositoryConfiguration();
321 cfg.setId("test002");
322 cfg.setName("This is test 002");
323 RemoteRepository repo = repositoryRegistry.putRepository( cfg );
325 assertEquals("test002", repo.getId());
326 assertEquals("This is test 002", repo.getName());
327 assertNotNull(repo.getContent());
328 archivaConfiguration.reload();
329 Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories();
330 assertEquals(2, repos.size());
332 RemoteRepository internalRepo = repositoryRegistry.getRemoteRepository( "central" );
333 cfg = new RemoteRepositoryConfiguration();
334 cfg.setId("central");
335 cfg.setName("This is central test 002");
336 repo = repositoryRegistry.putRepository( cfg );
337 assertTrue(internalRepo==repo);
338 assertEquals("This is central test 002",repo.getName());
339 assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
341 repositoryRegistry.reload();
342 assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
346 public void putRemoteRepositoryFromConfigWithoutSave( ) throws Exception
348 Configuration configuration = archivaConfiguration.getConfiguration();
349 RemoteRepositoryConfiguration cfg = new RemoteRepositoryConfiguration();
350 cfg.setId("test002");
351 cfg.setName("This is test 002");
352 RemoteRepository repo = repositoryRegistry.putRepository( cfg, configuration );
354 assertEquals("test002", repo.getId());
355 assertEquals("This is test 002", repo.getName());
356 assertNotNull(repo.getContent());
357 archivaConfiguration.reload();
358 assertEquals(1, archivaConfiguration.getConfiguration().getRemoteRepositories().size());
359 Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories();
360 assertEquals(2, repos.size());
362 RemoteRepository internalRepo = repositoryRegistry.getRemoteRepository( "central" );
363 cfg = new RemoteRepositoryConfiguration();
364 cfg.setId("central");
365 cfg.setName("This is central test 002");
366 repo = repositoryRegistry.putRepository( cfg, configuration );
367 assertTrue(internalRepo==repo);
368 assertEquals("This is central test 002",repo.getName());
369 assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
371 repositoryRegistry.reload();
372 assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
376 public void removeRepository( ) throws Exception
378 assertEquals(5, repositoryRegistry.getRepositories().size());
379 Repository repo = repositoryRegistry.getRepository( "snapshots" );
380 repositoryRegistry.removeRepository( repo );
381 assertEquals(4, repositoryRegistry.getRepositories().size());
382 assertTrue( repositoryRegistry.getRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
383 archivaConfiguration.reload();
384 repositoryRegistry.reload();
385 assertEquals(4, repositoryRegistry.getRepositories().size());
389 public void removeManagedRepository( ) throws Exception
392 assertEquals(4, repositoryRegistry.getManagedRepositories().size());
393 ManagedRepository repo = repositoryRegistry.getManagedRepository( "snapshots" );
394 repositoryRegistry.removeRepository( repo );
395 assertEquals(3, repositoryRegistry.getManagedRepositories().size());
396 assertTrue( repositoryRegistry.getManagedRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
397 archivaConfiguration.reload();
398 repositoryRegistry.reload();
399 assertEquals(3, repositoryRegistry.getManagedRepositories().size());
403 public void removeManagedRepositoryWithoutSave( ) throws Exception
405 Configuration configuration = archivaConfiguration.getConfiguration();
406 assertEquals(4, repositoryRegistry.getManagedRepositories().size());
407 ManagedRepository repo = repositoryRegistry.getManagedRepository( "snapshots" );
408 repositoryRegistry.removeRepository( repo, configuration );
409 assertEquals(3, repositoryRegistry.getManagedRepositories().size());
410 assertTrue( repositoryRegistry.getManagedRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
411 archivaConfiguration.reload();
412 repositoryRegistry.reload();
413 assertEquals(4, repositoryRegistry.getManagedRepositories().size());
418 public void removeRemoteRepository( ) throws Exception
420 assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
421 RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
422 repositoryRegistry.removeRepository( repo );
423 assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
424 assertTrue( repositoryRegistry.getRemoteRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "central" ) ) );
425 archivaConfiguration.reload();
426 repositoryRegistry.reload();
427 assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
431 public void removeRemoteRepositoryWithoutSave( ) throws Exception
433 Configuration configuration = archivaConfiguration.getConfiguration();
434 assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
435 RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
436 repositoryRegistry.removeRepository( repo, configuration );
437 assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
438 assertTrue( repositoryRegistry.getRemoteRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "central" ) ) );
439 archivaConfiguration.reload();
440 repositoryRegistry.reload();
441 assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
446 public void cloneManagedRepo( ) throws Exception
448 ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( "internal" );
452 repositoryRegistry.clone(managedRepository, "snapshots");
453 throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
455 catch ( RepositoryException e )
462 repositoryRegistry.clone(managedRepository, "central");
463 throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
465 catch ( RepositoryException e )
470 ManagedRepository clone = repositoryRegistry.clone( managedRepository, "newinternal" );
471 assertNotNull(clone);
472 assertNull(clone.getContent());
473 assertEquals("Archiva Managed Internal Repository", clone.getName());
474 assertFalse(managedRepository==clone);
479 public void cloneRemoteRepo( ) throws Exception
481 RemoteRepository remoteRepository = repositoryRegistry.getRemoteRepository( "central" );
485 repositoryRegistry.clone(remoteRepository, "snapshots");
486 throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
488 catch ( RepositoryException e )
495 repositoryRegistry.clone(remoteRepository, "central");
496 throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
498 catch ( RepositoryException e )
503 RemoteRepository clone = repositoryRegistry.clone( remoteRepository, "newCentral" );
504 assertNotNull(clone);
505 assertNull(clone.getContent());
506 assertEquals("Central Repository", clone.getName());
507 assertFalse(remoteRepository==clone);