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