]> source.dussan.org Git - archiva.git/blob
58965ed18944a79cc0026cb1ca70cea34f4771a7
[archiva.git] /
1 package org.apache.archiva.repository.base;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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;
42
43 import javax.inject.Inject;
44 import java.io.IOException;
45 import java.net.URISyntaxException;
46 import java.net.URL;
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;
52
53 import static org.junit.Assert.*;
54
55 /**
56  * Test for RepositoryRegistry
57  */
58 @RunWith(ArchivaSpringJUnit4ClassRunner.class)
59 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
60 public class ArchivaRepositoryRegistryTest
61 {
62
63     @Inject
64     RepositoryRegistry repositoryRegistry;
65
66     @Inject
67     ArchivaConfiguration archivaConfiguration;
68
69     private static final Path userCfg = Paths.get(System.getProperty( "user.home" ), ".m2/archiva.xml");
70
71     private static Path cfgCopy;
72     private static Path archivaCfg;
73
74     @BeforeClass
75     public static void classSetup() throws IOException, URISyntaxException
76     {
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);
82         }
83     }
84
85     @AfterClass
86     public static void classTearDown() throws IOException
87     {
88         if (cfgCopy!=null) {
89             Files.deleteIfExists( cfgCopy );
90         }
91     }
92
93     @Before
94     public void setUp( ) throws Exception
95     {
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))
102             {
103                 Files.copy( cfgCopy, archivaCfg , StandardCopyOption.REPLACE_EXISTING);
104             }
105         }
106         archivaConfiguration.reload();
107         repositoryRegistry.reload();
108     }
109
110     @After
111     public void tearDown( ) throws Exception
112     {
113         Files.deleteIfExists( userCfg );
114         if (cfgCopy!=null && Files.exists(cfgCopy)) {
115             Files.copy(cfgCopy, archivaCfg, StandardCopyOption.REPLACE_EXISTING);
116         }
117     }
118
119     @Test
120     public void getRepositories( ) throws Exception
121     {
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") ));
127     }
128
129     @Test
130     public void getManagedRepositories( ) throws Exception
131     {
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") ) );
136     }
137
138     @Test
139     public void getRemoteRepositories( ) throws Exception
140     {
141         Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories( );
142         assertEquals( 1, repos.size( ) );
143         assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals( "central") ));
144     }
145
146     @Test
147     public void getRepository( ) throws Exception
148     {
149         Repository repo = repositoryRegistry.getRepository( "internal" );
150         assertNotNull(repo);
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());
160     }
161
162     @Test
163     public void getManagedRepository( ) throws Exception
164     {
165         ManagedRepository repo = repositoryRegistry.getManagedRepository( "internal" );
166         assertNotNull(repo);
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());
178
179         assertNull(repositoryRegistry.getManagedRepository( "xyu" ));
180
181     }
182
183     @Test
184     public void getRemoteRepository( ) throws Exception
185     {
186         RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
187         assertNotNull(repo);
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());
196
197         assertEquals(35, repo.getTimeout().getSeconds());
198     }
199
200     @Test
201     public void putManagedRepository( ) throws Exception
202     {
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);
206
207         assertNotNull(managedRepository.getContent());
208         assertEquals(6, repositoryRegistry.getRepositories().size());
209
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;
213         try {
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) {
217             // OK
218         }
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 );
222
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);
229
230     }
231
232     @Test
233     public void putManagedRepositoryFromConfig( ) throws Exception
234     {
235         ManagedRepositoryConfiguration cfg = new ManagedRepositoryConfiguration();
236         cfg.setId("test002");
237         cfg.setName("This is test 002");
238         ManagedRepository repo = repositoryRegistry.putRepository( cfg );
239         assertNotNull(repo);
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());
246
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());
255
256         repositoryRegistry.reload();
257         assertEquals(5, repositoryRegistry.getManagedRepositories().size());
258
259     }
260
261     @Test
262     public void putManagedRepositoryFromConfigWithoutSave( ) throws Exception
263     {
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 );
269         assertNotNull(repo);
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());
277
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());
286
287         repositoryRegistry.reload();
288         assertEquals(4, repositoryRegistry.getManagedRepositories().size());
289     }
290
291     @Test
292     public void putRemoteRepository( ) throws Exception
293     {
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);
297
298         assertTrue(remoteRepository==newRepo);
299         assertNotNull(remoteRepository.getContent());
300         assertEquals(6, repositoryRegistry.getRepositories().size());
301
302         remoteRepository = BasicRemoteRepository.newFilesystemInstance( "internal", "Test repo", archivaConfiguration.getRemoteRepositoryBaseDir() );
303         remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
304         RemoteRepository updatedRepo = null;
305         try
306         {
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) {
310             // OK
311         }
312
313         remoteRepository = BasicRemoteRepository.newFilesystemInstance( "central", "Test repo", archivaConfiguration.getRemoteRepositoryBaseDir() );
314         remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
315         updatedRepo = repositoryRegistry.putRepository( remoteRepository );
316
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);
323     }
324
325     @Test
326     public void putRemoteRepositoryFromConfig( ) throws Exception
327     {
328         RemoteRepositoryConfiguration cfg = new RemoteRepositoryConfiguration();
329         cfg.setId("test002");
330         cfg.setName("This is test 002");
331         RemoteRepository repo = repositoryRegistry.putRepository( cfg );
332         assertNotNull(repo);
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());
339
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());
348
349         repositoryRegistry.reload();
350         assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
351     }
352
353     @Test
354     public void putRemoteRepositoryFromConfigWithoutSave( ) throws Exception
355     {
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 );
361         assertNotNull(repo);
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());
369
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());
378
379         repositoryRegistry.reload();
380         assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
381     }
382
383     @Test
384     public void removeRepository( ) throws Exception
385     {
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());
394     }
395
396     @Test
397     public void removeManagedRepository( ) throws Exception
398     {
399
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());
408     }
409
410     @Test
411     public void removeManagedRepositoryWithoutSave( ) throws Exception
412     {
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());
422     }
423
424
425     @Test
426     public void removeRemoteRepository( ) throws Exception
427     {
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());
436     }
437
438     @Test
439     public void removeRemoteRepositoryWithoutSave( ) throws Exception
440     {
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());
450     }
451
452
453     @Test
454     public void cloneManagedRepo( ) throws Exception
455     {
456         ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( "internal" );
457
458         try
459         {
460             repositoryRegistry.clone(managedRepository, "snapshots");
461             throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
462         }
463         catch ( RepositoryException e )
464         {
465             // OK
466         }
467
468         try
469         {
470             repositoryRegistry.clone(managedRepository, "central");
471             throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
472         }
473         catch ( RepositoryException e )
474         {
475             // OK
476         }
477
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);
483
484     }
485
486     @Test
487     public void cloneRemoteRepo( ) throws Exception
488     {
489         RemoteRepository remoteRepository = repositoryRegistry.getRemoteRepository( "central" );
490
491         try
492         {
493             repositoryRegistry.clone(remoteRepository, "snapshots");
494             throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
495         }
496         catch ( RepositoryException e )
497         {
498             // OK
499         }
500
501         try
502         {
503             repositoryRegistry.clone(remoteRepository, "central");
504             throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
505         }
506         catch ( RepositoryException e )
507         {
508             // OK
509         }
510
511         RemoteRepository clone = repositoryRegistry.clone( remoteRepository, "newCentral" );
512         assertNotNull(clone);
513         assertNull(clone.getContent());
514         assertEquals("Central Repository", clone.getName());
515         assertFalse(remoteRepository==clone);
516
517     }
518
519 }