]> source.dussan.org Git - archiva.git/blob
6601ad2c001b3408b65d16e7ff9a523604a8e417
[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.repository.base.group.RepositoryGroupHandler;
34 import org.apache.archiva.repository.base.managed.BasicManagedRepository;
35 import org.apache.archiva.repository.base.remote.BasicRemoteRepository;
36 import org.junit.jupiter.api.AfterAll;
37 import org.junit.jupiter.api.AfterEach;
38 import org.junit.jupiter.api.BeforeAll;
39 import org.junit.jupiter.api.BeforeEach;
40 import org.junit.jupiter.api.Test;
41 import org.junit.jupiter.api.extension.ExtendWith;
42 import org.springframework.test.context.ContextConfiguration;
43 import org.springframework.test.context.junit.jupiter.SpringExtension;
44
45 import javax.inject.Inject;
46 import java.io.IOException;
47 import java.net.URISyntaxException;
48 import java.net.URL;
49 import java.nio.file.Files;
50 import java.nio.file.Path;
51 import java.nio.file.Paths;
52 import java.nio.file.StandardCopyOption;
53 import java.util.Collection;
54
55 import static org.junit.jupiter.api.Assertions.*;
56
57
58 /**
59  * Test for RepositoryRegistry
60  */
61 @ExtendWith( SpringExtension.class)
62 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
63 public class ArchivaRepositoryRegistryTest
64 {
65
66     @Inject
67     RepositoryRegistry repositoryRegistry;
68
69     @Inject
70     ArchivaConfiguration archivaConfiguration;
71
72     @SuppressWarnings( "unused" )
73     @Inject
74     RepositoryGroupHandler repositoryGroupHandler;
75
76     private static final Path userCfg = Paths.get(System.getProperty( "user.home" ), ".m2/archiva.xml");
77
78     private static Path cfgCopy;
79     private static Path archivaCfg;
80
81     @BeforeAll
82     public static void classSetup() throws IOException, URISyntaxException
83     {
84         URL archivaCfgUri = Thread.currentThread().getContextClassLoader().getResource( "archiva.xml" );
85         if (archivaCfgUri!=null) {
86             archivaCfg = Paths.get(archivaCfgUri.toURI());
87             cfgCopy = Files.createTempFile( "archiva-backup", ".xml" );
88             Files.copy( archivaCfg, cfgCopy, StandardCopyOption.REPLACE_EXISTING);
89         }
90     }
91
92     @AfterAll
93     public static void classTearDown() throws IOException
94     {
95         if (cfgCopy!=null) {
96             Files.deleteIfExists( cfgCopy );
97         }
98     }
99
100     @BeforeEach
101     public void setUp( ) throws Exception
102     {
103         assertNotNull( repositoryRegistry );
104         Files.deleteIfExists( userCfg );
105         URL archivaCfgUri = Thread.currentThread().getContextClassLoader().getResource( "archiva.xml" );
106         if (archivaCfgUri!=null) {
107             archivaCfg = Paths.get(archivaCfgUri.toURI());
108             if (Files.exists(cfgCopy))
109             {
110                 Files.copy( cfgCopy, archivaCfg , StandardCopyOption.REPLACE_EXISTING);
111             }
112         }
113         archivaConfiguration.reload();
114         repositoryRegistry.reload();
115     }
116
117     @AfterEach
118     public void tearDown( ) throws Exception
119     {
120         Files.deleteIfExists( userCfg );
121         if (cfgCopy!=null && Files.exists(cfgCopy)) {
122             Files.copy(cfgCopy, archivaCfg, StandardCopyOption.REPLACE_EXISTING);
123         }
124     }
125
126     @Test
127     public void getRepositories( ) throws Exception
128     {
129         Collection<Repository> repos = repositoryRegistry.getRepositories( );
130         assertEquals( 5, repos.size( ) );
131         assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals("internal") ));
132         assertTrue( repos.stream( ).anyMatch( rep -> rep.getId( ).equals( "snapshots") ) );
133         assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals( "central") ));
134     }
135
136     @Test
137     public void getManagedRepositories( ) throws Exception
138     {
139         Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
140         assertEquals( 4, repos.size( ) );
141         assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals("internal") ));
142         assertTrue( repos.stream( ).anyMatch( rep -> rep.getId( ).equals( "snapshots") ) );
143     }
144
145     @Test
146     public void getRemoteRepositories( ) throws Exception
147     {
148         Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories( );
149         assertEquals( 1, repos.size( ) );
150         assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals( "central") ));
151     }
152
153     @Test
154     public void getRepository( ) throws Exception
155     {
156         Repository repo = repositoryRegistry.getRepository( "internal" );
157         assertNotNull(repo);
158         assertEquals("internal", repo.getId());
159         assertEquals("Archiva Managed Internal Repository", repo.getName());
160         assertEquals("This is internal repository.", repo.getDescription());
161         assertEquals( "default", repo.getLayout( ) );
162         assertEquals("0 0 * * * ?", repo.getSchedulingDefinition());
163         assertTrue(repo instanceof ManagedRepository);
164         assertTrue( repo.hasIndex( ) );
165         assertTrue(repo.isScanned());
166         assertEquals( RepositoryType.MAVEN, repo.getType());
167     }
168
169     @Test
170     public void getManagedRepository( ) throws Exception
171     {
172         ManagedRepository repo = repositoryRegistry.getManagedRepository( "internal" );
173         assertNotNull(repo);
174         assertEquals("internal", repo.getId());
175         assertEquals("Archiva Managed Internal Repository", repo.getName());
176         assertEquals("This is internal repository.", repo.getDescription());
177         assertEquals( "default", repo.getLayout( ) );
178         assertEquals("0 0 * * * ?", repo.getSchedulingDefinition());
179         assertTrue( repo.hasIndex( ) );
180         assertTrue(repo.isScanned());
181         assertEquals(RepositoryType.MAVEN, repo.getType());
182         assertTrue(repo.getActiveReleaseSchemes().contains( ReleaseScheme.RELEASE));
183         assertFalse( repo.getActiveReleaseSchemes( ).contains( ReleaseScheme.SNAPSHOT ) );
184         assertNotNull(repo.getContent());
185
186         assertNull(repositoryRegistry.getManagedRepository( "xyu" ));
187
188     }
189
190     @Test
191     public void getRemoteRepository( ) throws Exception
192     {
193         RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
194         assertNotNull(repo);
195         assertEquals("central", repo.getId());
196         assertEquals("Central Repository", repo.getName());
197         assertEquals("", repo.getDescription());
198         assertEquals( "default", repo.getLayout( ) );
199         assertEquals("0 0 08 ? * SUN", repo.getSchedulingDefinition());
200         assertTrue( repo.hasIndex( ) );
201         assertTrue(repo.isScanned());
202         assertEquals(RepositoryType.MAVEN, repo.getType());
203
204         assertEquals(35, repo.getTimeout().getSeconds());
205     }
206
207     @Test
208     public void putManagedRepository( ) throws Exception
209     {
210         BasicManagedRepository managedRepository = BasicManagedRepository.newFilesystemInstance("test001", "Test repo", archivaConfiguration.getRepositoryBaseDir().resolve("test001"));
211         managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
212         repositoryRegistry.putRepository(managedRepository);
213
214         assertNotNull(managedRepository.getContent());
215         assertEquals(6, repositoryRegistry.getRepositories().size());
216
217         managedRepository = BasicManagedRepository.newFilesystemInstance("central", "Test repo", archivaConfiguration.getRepositoryBaseDir().resolve("central"));
218         managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
219         ManagedRepository updatedRepo;
220         try {
221             repositoryRegistry.putRepository( managedRepository );
222             throw new RuntimeException("Repository exception should be thrown, if there exists a remote repository already with that id");
223         } catch ( RepositoryException e) {
224             // OK
225         }
226         managedRepository = BasicManagedRepository.newFilesystemInstance("internal", "Test repo", archivaConfiguration.getRepositoryBaseDir().resolve("internal"));
227         managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
228         updatedRepo = repositoryRegistry.putRepository( managedRepository );
229
230         assertSame( updatedRepo, managedRepository );
231         assertNotNull(managedRepository.getContent());
232         assertEquals(6, repositoryRegistry.getRepositories().size());
233         ManagedRepository managedRepository1 = repositoryRegistry.getManagedRepository( "internal" );
234         assertEquals("Test repo", managedRepository1.getName());
235         assertSame( managedRepository1, managedRepository );
236
237     }
238
239     @Test
240     public void putManagedRepositoryFromConfig( ) throws Exception
241     {
242         ManagedRepositoryConfiguration cfg = new ManagedRepositoryConfiguration();
243         cfg.setId("test002");
244         cfg.setName("This is test 002");
245         ManagedRepository repo = repositoryRegistry.putRepository( cfg );
246         assertNotNull(repo);
247         assertEquals("test002", repo.getId());
248         assertEquals("This is test 002", repo.getName());
249         assertNotNull(repo.getContent());
250         archivaConfiguration.reload();
251         Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
252         assertEquals(5, repos.size());
253
254         ManagedRepository internalRepo = repositoryRegistry.getManagedRepository( "internal" );
255         cfg = new ManagedRepositoryConfiguration();
256         cfg.setId("internal");
257         cfg.setName("This is internal test 002");
258         repo = repositoryRegistry.putRepository( cfg );
259         assertSame( internalRepo, repo );
260         assertEquals("This is internal test 002",repo.getName());
261         assertEquals(5, repositoryRegistry.getManagedRepositories().size());
262
263         repositoryRegistry.reload();
264         assertEquals(5, repositoryRegistry.getManagedRepositories().size());
265
266     }
267
268     @Test
269     public void putManagedRepositoryFromConfigWithoutSave( ) throws Exception
270     {
271         Configuration configuration = archivaConfiguration.getConfiguration();
272         ManagedRepositoryConfiguration cfg = new ManagedRepositoryConfiguration();
273         cfg.setId("test002");
274         cfg.setName("This is test 002");
275         ManagedRepository repo = repositoryRegistry.putRepository( cfg, configuration );
276         assertNotNull(repo);
277         assertEquals("test002", repo.getId());
278         assertEquals("This is test 002", repo.getName());
279         assertNotNull(repo.getContent());
280         archivaConfiguration.reload();
281         assertEquals(3, archivaConfiguration.getConfiguration().getManagedRepositories().size());
282         Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
283         assertEquals(5, repos.size());
284
285         ManagedRepository internalRepo = repositoryRegistry.getManagedRepository( "internal" );
286         cfg = new ManagedRepositoryConfiguration();
287         cfg.setId("internal");
288         cfg.setName("This is internal test 002");
289         repo = repositoryRegistry.putRepository( cfg, configuration );
290         assertSame( internalRepo, repo );
291         assertEquals("This is internal test 002",repo.getName());
292         assertEquals(5, repositoryRegistry.getManagedRepositories().size());
293
294         repositoryRegistry.reload();
295         assertEquals(4, repositoryRegistry.getManagedRepositories().size());
296     }
297
298     @Test
299     public void putRemoteRepository( ) throws Exception
300     {
301         BasicRemoteRepository remoteRepository = BasicRemoteRepository.newFilesystemInstance( "test001", "Test repo", archivaConfiguration.getRemoteRepositoryBaseDir() );
302         remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
303         RemoteRepository newRepo = repositoryRegistry.putRepository(remoteRepository);
304
305         assertSame( remoteRepository, newRepo );
306         assertNotNull(remoteRepository.getContent());
307         assertEquals(6, repositoryRegistry.getRepositories().size());
308
309         remoteRepository = BasicRemoteRepository.newFilesystemInstance( "internal", "Test repo", archivaConfiguration.getRemoteRepositoryBaseDir() );
310         remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
311         RemoteRepository updatedRepo;
312         try
313         {
314             updatedRepo = repositoryRegistry.putRepository( remoteRepository );
315             assertSame( remoteRepository, updatedRepo );
316             throw new RuntimeException("Should throw repository exception, if repository exists already and is not the same type.");
317         } catch (RepositoryException e) {
318             // OK
319         }
320
321         remoteRepository = BasicRemoteRepository.newFilesystemInstance( "central", "Test repo", archivaConfiguration.getRemoteRepositoryBaseDir() );
322         remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
323         updatedRepo = repositoryRegistry.putRepository( remoteRepository );
324
325         assertSame( updatedRepo, remoteRepository );
326         assertNotNull(remoteRepository.getContent());
327         assertEquals(6, repositoryRegistry.getRepositories().size());
328         RemoteRepository remoteRepository1 = repositoryRegistry.getRemoteRepository( "central" );
329         assertEquals("Test repo", remoteRepository1.getName());
330         assertSame( remoteRepository1, remoteRepository );
331     }
332
333     @Test
334     public void putRemoteRepositoryFromConfig( ) throws Exception
335     {
336         RemoteRepositoryConfiguration cfg = new RemoteRepositoryConfiguration();
337         cfg.setId("test002");
338         cfg.setName("This is test 002");
339         RemoteRepository repo = repositoryRegistry.putRepository( cfg );
340         assertNotNull(repo);
341         assertEquals("test002", repo.getId());
342         assertEquals("This is test 002", repo.getName());
343         assertNotNull(repo.getContent());
344         archivaConfiguration.reload();
345         Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories();
346         assertEquals(2, repos.size());
347
348         RemoteRepository internalRepo = repositoryRegistry.getRemoteRepository( "central" );
349         cfg = new RemoteRepositoryConfiguration();
350         cfg.setId("central");
351         cfg.setName("This is central test 002");
352         repo = repositoryRegistry.putRepository( cfg );
353         assertSame( internalRepo, repo );
354         assertEquals("This is central test 002",repo.getName());
355         assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
356
357         repositoryRegistry.reload();
358         assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
359     }
360
361     @Test
362     public void putRemoteRepositoryFromConfigWithoutSave( ) throws Exception
363     {
364         Configuration configuration = archivaConfiguration.getConfiguration();
365         RemoteRepositoryConfiguration cfg = new RemoteRepositoryConfiguration();
366         cfg.setId("test002");
367         cfg.setName("This is test 002");
368         RemoteRepository repo = repositoryRegistry.putRepository( cfg, configuration );
369         assertNotNull(repo);
370         assertEquals("test002", repo.getId());
371         assertEquals("This is test 002", repo.getName());
372         assertNotNull(repo.getContent());
373         archivaConfiguration.reload();
374         assertEquals(1, archivaConfiguration.getConfiguration().getRemoteRepositories().size());
375         Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories();
376         assertEquals(2, repos.size());
377
378         RemoteRepository internalRepo = repositoryRegistry.getRemoteRepository( "central" );
379         cfg = new RemoteRepositoryConfiguration();
380         cfg.setId("central");
381         cfg.setName("This is central test 002");
382         repo = repositoryRegistry.putRepository( cfg, configuration );
383         assertSame( internalRepo, repo );
384         assertEquals("This is central test 002",repo.getName());
385         assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
386
387         repositoryRegistry.reload();
388         assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
389     }
390
391     @Test
392     public void removeRepository( ) throws Exception
393     {
394         assertEquals(5, repositoryRegistry.getRepositories().size());
395         Repository repo = repositoryRegistry.getRepository( "snapshots" );
396         repositoryRegistry.removeRepository( repo );
397         assertEquals(4, repositoryRegistry.getRepositories().size());
398         assertTrue( repositoryRegistry.getRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
399         archivaConfiguration.reload();
400         repositoryRegistry.reload();
401         assertEquals(4, repositoryRegistry.getRepositories().size());
402     }
403
404     @Test
405     public void removeManagedRepository( ) throws Exception
406     {
407
408         assertEquals(4, repositoryRegistry.getManagedRepositories().size());
409         ManagedRepository repo = repositoryRegistry.getManagedRepository( "snapshots" );
410         repositoryRegistry.removeRepository( repo );
411         assertEquals(3, repositoryRegistry.getManagedRepositories().size());
412         assertTrue( repositoryRegistry.getManagedRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
413         archivaConfiguration.reload();
414         repositoryRegistry.reload();
415         assertEquals(3, repositoryRegistry.getManagedRepositories().size());
416     }
417
418     @Test
419     public void removeManagedRepositoryWithoutSave( ) throws Exception
420     {
421         Configuration configuration = archivaConfiguration.getConfiguration();
422         assertEquals(4, repositoryRegistry.getManagedRepositories().size());
423         ManagedRepository repo = repositoryRegistry.getManagedRepository( "snapshots" );
424         repositoryRegistry.removeRepository( repo, configuration );
425         assertEquals(3, repositoryRegistry.getManagedRepositories().size());
426         assertTrue( repositoryRegistry.getManagedRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
427         archivaConfiguration.reload();
428         repositoryRegistry.reload();
429         assertEquals(4, repositoryRegistry.getManagedRepositories().size());
430     }
431
432
433     @Test
434     public void removeRemoteRepository( ) throws Exception
435     {
436         assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
437         RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
438         repositoryRegistry.removeRepository( repo );
439         assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
440         assertTrue( repositoryRegistry.getRemoteRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "central" ) ) );
441         archivaConfiguration.reload();
442         repositoryRegistry.reload();
443         assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
444     }
445
446     @Test
447     public void removeRemoteRepositoryWithoutSave( ) throws Exception
448     {
449         Configuration configuration = archivaConfiguration.getConfiguration();
450         assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
451         RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
452         repositoryRegistry.removeRepository( repo, configuration );
453         assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
454         assertTrue( repositoryRegistry.getRemoteRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "central" ) ) );
455         archivaConfiguration.reload();
456         repositoryRegistry.reload();
457         assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
458     }
459
460
461     @Test
462     public void cloneManagedRepo( ) throws Exception
463     {
464         ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( "internal" );
465
466         try
467         {
468             repositoryRegistry.clone(managedRepository, "snapshots");
469             throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
470         }
471         catch ( RepositoryException e )
472         {
473             // OK
474         }
475
476         try
477         {
478             repositoryRegistry.clone(managedRepository, "central");
479             throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
480         }
481         catch ( RepositoryException e )
482         {
483             // OK
484         }
485
486         ManagedRepository clone = repositoryRegistry.clone( managedRepository, "newinternal" );
487         assertNotNull(clone);
488         assertNull(clone.getContent());
489         assertEquals("Archiva Managed Internal Repository", clone.getName());
490         assertNotSame( managedRepository, clone );
491
492     }
493
494     @Test
495     public void cloneRemoteRepo( ) throws Exception
496     {
497         RemoteRepository remoteRepository = repositoryRegistry.getRemoteRepository( "central" );
498
499         try
500         {
501             repositoryRegistry.clone(remoteRepository, "snapshots");
502             throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
503         }
504         catch ( RepositoryException e )
505         {
506             // OK
507         }
508
509         try
510         {
511             repositoryRegistry.clone(remoteRepository, "central");
512             throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
513         }
514         catch ( RepositoryException e )
515         {
516             // OK
517         }
518
519         RemoteRepository clone = repositoryRegistry.clone( remoteRepository, "newCentral" );
520         assertNotNull(clone);
521         assertNull(clone.getContent());
522         assertEquals("Central Repository", clone.getName());
523         assertNotSame( remoteRepository, clone );
524
525     }
526
527     @Test
528     void validateRepository() {
529         Repository repo = repositoryRegistry.getRepository( "internal" );
530         assertNotNull( repo );
531         assertTrue( repositoryRegistry.validateRepository( repo ).isValid() );
532     }
533
534 }