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