|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534 |
- package org.apache.archiva.repository.base;
-
- /*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
- import org.apache.archiva.configuration.ArchivaConfiguration;
- import org.apache.archiva.configuration.Configuration;
- import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
- import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
- import org.apache.archiva.repository.ManagedRepository;
- import org.apache.archiva.repository.ReleaseScheme;
- import org.apache.archiva.repository.RemoteRepository;
- import org.apache.archiva.repository.Repository;
- import org.apache.archiva.repository.RepositoryException;
- import org.apache.archiva.repository.RepositoryRegistry;
- import org.apache.archiva.repository.RepositoryType;
- import org.apache.archiva.repository.base.group.RepositoryGroupHandler;
- import org.apache.archiva.repository.base.managed.BasicManagedRepository;
- import org.apache.archiva.repository.base.remote.BasicRemoteRepository;
- import org.junit.jupiter.api.AfterAll;
- import org.junit.jupiter.api.AfterEach;
- import org.junit.jupiter.api.BeforeAll;
- import org.junit.jupiter.api.BeforeEach;
- import org.junit.jupiter.api.Test;
- import org.junit.jupiter.api.extension.ExtendWith;
- import org.springframework.test.context.ContextConfiguration;
- import org.springframework.test.context.junit.jupiter.SpringExtension;
-
- import javax.inject.Inject;
- import java.io.IOException;
- import java.net.URISyntaxException;
- import java.net.URL;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.nio.file.StandardCopyOption;
- import java.util.Collection;
-
- import static org.junit.jupiter.api.Assertions.*;
-
-
- /**
- * Test for RepositoryRegistry
- */
- @ExtendWith( SpringExtension.class)
- @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
- public class ArchivaRepositoryRegistryTest
- {
-
- @Inject
- RepositoryRegistry repositoryRegistry;
-
- @Inject
- ArchivaConfiguration archivaConfiguration;
-
- @SuppressWarnings( "unused" )
- @Inject
- RepositoryGroupHandler repositoryGroupHandler;
-
- private static final Path userCfg = Paths.get(System.getProperty( "user.home" ), ".m2/archiva.xml");
-
- private static Path cfgCopy;
- private static Path archivaCfg;
-
- @BeforeAll
- public static void classSetup() throws IOException, URISyntaxException
- {
- URL archivaCfgUri = Thread.currentThread().getContextClassLoader().getResource( "archiva.xml" );
- if (archivaCfgUri!=null) {
- archivaCfg = Paths.get(archivaCfgUri.toURI());
- cfgCopy = Files.createTempFile( "archiva-backup", ".xml" );
- Files.copy( archivaCfg, cfgCopy, StandardCopyOption.REPLACE_EXISTING);
- }
- }
-
- @AfterAll
- public static void classTearDown() throws IOException
- {
- if (cfgCopy!=null) {
- Files.deleteIfExists( cfgCopy );
- }
- }
-
- @BeforeEach
- public void setUp( ) throws Exception
- {
- assertNotNull( repositoryRegistry );
- Files.deleteIfExists( userCfg );
- URL archivaCfgUri = Thread.currentThread().getContextClassLoader().getResource( "archiva.xml" );
- if (archivaCfgUri!=null) {
- archivaCfg = Paths.get(archivaCfgUri.toURI());
- if (Files.exists(cfgCopy))
- {
- Files.copy( cfgCopy, archivaCfg , StandardCopyOption.REPLACE_EXISTING);
- }
- }
- archivaConfiguration.reload();
- repositoryRegistry.reload();
- }
-
- @AfterEach
- public void tearDown( ) throws Exception
- {
- Files.deleteIfExists( userCfg );
- if (cfgCopy!=null && Files.exists(cfgCopy)) {
- Files.copy(cfgCopy, archivaCfg, StandardCopyOption.REPLACE_EXISTING);
- }
- }
-
- @Test
- public void getRepositories( ) throws Exception
- {
- Collection<Repository> repos = repositoryRegistry.getRepositories( );
- assertEquals( 5, repos.size( ) );
- assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals("internal") ));
- assertTrue( repos.stream( ).anyMatch( rep -> rep.getId( ).equals( "snapshots") ) );
- assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals( "central") ));
- }
-
- @Test
- public void getManagedRepositories( ) throws Exception
- {
- Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
- assertEquals( 4, repos.size( ) );
- assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals("internal") ));
- assertTrue( repos.stream( ).anyMatch( rep -> rep.getId( ).equals( "snapshots") ) );
- }
-
- @Test
- public void getRemoteRepositories( ) throws Exception
- {
- Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories( );
- assertEquals( 1, repos.size( ) );
- assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals( "central") ));
- }
-
- @Test
- public void getRepository( ) throws Exception
- {
- Repository repo = repositoryRegistry.getRepository( "internal" );
- assertNotNull(repo);
- assertEquals("internal", repo.getId());
- assertEquals("Archiva Managed Internal Repository", repo.getName());
- assertEquals("This is internal repository.", repo.getDescription());
- assertEquals( "default", repo.getLayout( ) );
- assertEquals("0 0 * * * ?", repo.getSchedulingDefinition());
- assertTrue(repo instanceof ManagedRepository);
- assertTrue( repo.hasIndex( ) );
- assertTrue(repo.isScanned());
- assertEquals( RepositoryType.MAVEN, repo.getType());
- }
-
- @Test
- public void getManagedRepository( ) throws Exception
- {
- ManagedRepository repo = repositoryRegistry.getManagedRepository( "internal" );
- assertNotNull(repo);
- assertEquals("internal", repo.getId());
- assertEquals("Archiva Managed Internal Repository", repo.getName());
- assertEquals("This is internal repository.", repo.getDescription());
- assertEquals( "default", repo.getLayout( ) );
- assertEquals("0 0 * * * ?", repo.getSchedulingDefinition());
- assertTrue( repo.hasIndex( ) );
- assertTrue(repo.isScanned());
- assertEquals(RepositoryType.MAVEN, repo.getType());
- assertTrue(repo.getActiveReleaseSchemes().contains( ReleaseScheme.RELEASE));
- assertFalse( repo.getActiveReleaseSchemes( ).contains( ReleaseScheme.SNAPSHOT ) );
- assertNotNull(repo.getContent());
-
- assertNull(repositoryRegistry.getManagedRepository( "xyu" ));
-
- }
-
- @Test
- public void getRemoteRepository( ) throws Exception
- {
- RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
- assertNotNull(repo);
- assertEquals("central", repo.getId());
- assertEquals("Central Repository", repo.getName());
- assertEquals("", repo.getDescription());
- assertEquals( "default", repo.getLayout( ) );
- assertEquals("0 0 08 ? * SUN", repo.getSchedulingDefinition());
- assertTrue( repo.hasIndex( ) );
- assertTrue(repo.isScanned());
- assertEquals(RepositoryType.MAVEN, repo.getType());
-
- assertEquals(35, repo.getTimeout().getSeconds());
- }
-
- @Test
- public void putManagedRepository( ) throws Exception
- {
- BasicManagedRepository managedRepository = BasicManagedRepository.newFilesystemInstance("test001", "Test repo", archivaConfiguration.getRepositoryBaseDir().resolve("test001"));
- managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
- repositoryRegistry.putRepository(managedRepository);
-
- assertNotNull(managedRepository.getContent());
- assertEquals(6, repositoryRegistry.getRepositories().size());
-
- managedRepository = BasicManagedRepository.newFilesystemInstance("central", "Test repo", archivaConfiguration.getRepositoryBaseDir().resolve("central"));
- managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
- ManagedRepository updatedRepo;
- try {
- repositoryRegistry.putRepository( managedRepository );
- throw new RuntimeException("Repository exception should be thrown, if there exists a remote repository already with that id");
- } catch ( RepositoryException e) {
- // OK
- }
- managedRepository = BasicManagedRepository.newFilesystemInstance("internal", "Test repo", archivaConfiguration.getRepositoryBaseDir().resolve("internal"));
- managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
- updatedRepo = repositoryRegistry.putRepository( managedRepository );
-
- assertSame( updatedRepo, managedRepository );
- assertNotNull(managedRepository.getContent());
- assertEquals(6, repositoryRegistry.getRepositories().size());
- ManagedRepository managedRepository1 = repositoryRegistry.getManagedRepository( "internal" );
- assertEquals("Test repo", managedRepository1.getName());
- assertSame( managedRepository1, managedRepository );
-
- }
-
- @Test
- public void putManagedRepositoryFromConfig( ) throws Exception
- {
- ManagedRepositoryConfiguration cfg = new ManagedRepositoryConfiguration();
- cfg.setId("test002");
- cfg.setName("This is test 002");
- ManagedRepository repo = repositoryRegistry.putRepository( cfg );
- assertNotNull(repo);
- assertEquals("test002", repo.getId());
- assertEquals("This is test 002", repo.getName());
- assertNotNull(repo.getContent());
- archivaConfiguration.reload();
- Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
- assertEquals(5, repos.size());
-
- ManagedRepository internalRepo = repositoryRegistry.getManagedRepository( "internal" );
- cfg = new ManagedRepositoryConfiguration();
- cfg.setId("internal");
- cfg.setName("This is internal test 002");
- repo = repositoryRegistry.putRepository( cfg );
- assertSame( internalRepo, repo );
- assertEquals("This is internal test 002",repo.getName());
- assertEquals(5, repositoryRegistry.getManagedRepositories().size());
-
- repositoryRegistry.reload();
- assertEquals(5, repositoryRegistry.getManagedRepositories().size());
-
- }
-
- @Test
- public void putManagedRepositoryFromConfigWithoutSave( ) throws Exception
- {
- Configuration configuration = archivaConfiguration.getConfiguration();
- ManagedRepositoryConfiguration cfg = new ManagedRepositoryConfiguration();
- cfg.setId("test002");
- cfg.setName("This is test 002");
- ManagedRepository repo = repositoryRegistry.putRepository( cfg, configuration );
- assertNotNull(repo);
- assertEquals("test002", repo.getId());
- assertEquals("This is test 002", repo.getName());
- assertNotNull(repo.getContent());
- archivaConfiguration.reload();
- assertEquals(3, archivaConfiguration.getConfiguration().getManagedRepositories().size());
- Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
- assertEquals(5, repos.size());
-
- ManagedRepository internalRepo = repositoryRegistry.getManagedRepository( "internal" );
- cfg = new ManagedRepositoryConfiguration();
- cfg.setId("internal");
- cfg.setName("This is internal test 002");
- repo = repositoryRegistry.putRepository( cfg, configuration );
- assertSame( internalRepo, repo );
- assertEquals("This is internal test 002",repo.getName());
- assertEquals(5, repositoryRegistry.getManagedRepositories().size());
-
- repositoryRegistry.reload();
- assertEquals(4, repositoryRegistry.getManagedRepositories().size());
- }
-
- @Test
- public void putRemoteRepository( ) throws Exception
- {
- BasicRemoteRepository remoteRepository = BasicRemoteRepository.newFilesystemInstance( "test001", "Test repo", archivaConfiguration.getRemoteRepositoryBaseDir() );
- remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
- RemoteRepository newRepo = repositoryRegistry.putRepository(remoteRepository);
-
- assertSame( remoteRepository, newRepo );
- assertNotNull(remoteRepository.getContent());
- assertEquals(6, repositoryRegistry.getRepositories().size());
-
- remoteRepository = BasicRemoteRepository.newFilesystemInstance( "internal", "Test repo", archivaConfiguration.getRemoteRepositoryBaseDir() );
- remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
- RemoteRepository updatedRepo;
- try
- {
- updatedRepo = repositoryRegistry.putRepository( remoteRepository );
- assertSame( remoteRepository, updatedRepo );
- throw new RuntimeException("Should throw repository exception, if repository exists already and is not the same type.");
- } catch (RepositoryException e) {
- // OK
- }
-
- remoteRepository = BasicRemoteRepository.newFilesystemInstance( "central", "Test repo", archivaConfiguration.getRemoteRepositoryBaseDir() );
- remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
- updatedRepo = repositoryRegistry.putRepository( remoteRepository );
-
- assertSame( updatedRepo, remoteRepository );
- assertNotNull(remoteRepository.getContent());
- assertEquals(6, repositoryRegistry.getRepositories().size());
- RemoteRepository remoteRepository1 = repositoryRegistry.getRemoteRepository( "central" );
- assertEquals("Test repo", remoteRepository1.getName());
- assertSame( remoteRepository1, remoteRepository );
- }
-
- @Test
- public void putRemoteRepositoryFromConfig( ) throws Exception
- {
- RemoteRepositoryConfiguration cfg = new RemoteRepositoryConfiguration();
- cfg.setId("test002");
- cfg.setName("This is test 002");
- RemoteRepository repo = repositoryRegistry.putRepository( cfg );
- assertNotNull(repo);
- assertEquals("test002", repo.getId());
- assertEquals("This is test 002", repo.getName());
- assertNotNull(repo.getContent());
- archivaConfiguration.reload();
- Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories();
- assertEquals(2, repos.size());
-
- RemoteRepository internalRepo = repositoryRegistry.getRemoteRepository( "central" );
- cfg = new RemoteRepositoryConfiguration();
- cfg.setId("central");
- cfg.setName("This is central test 002");
- repo = repositoryRegistry.putRepository( cfg );
- assertSame( internalRepo, repo );
- assertEquals("This is central test 002",repo.getName());
- assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
-
- repositoryRegistry.reload();
- assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
- }
-
- @Test
- public void putRemoteRepositoryFromConfigWithoutSave( ) throws Exception
- {
- Configuration configuration = archivaConfiguration.getConfiguration();
- RemoteRepositoryConfiguration cfg = new RemoteRepositoryConfiguration();
- cfg.setId("test002");
- cfg.setName("This is test 002");
- RemoteRepository repo = repositoryRegistry.putRepository( cfg, configuration );
- assertNotNull(repo);
- assertEquals("test002", repo.getId());
- assertEquals("This is test 002", repo.getName());
- assertNotNull(repo.getContent());
- archivaConfiguration.reload();
- assertEquals(1, archivaConfiguration.getConfiguration().getRemoteRepositories().size());
- Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories();
- assertEquals(2, repos.size());
-
- RemoteRepository internalRepo = repositoryRegistry.getRemoteRepository( "central" );
- cfg = new RemoteRepositoryConfiguration();
- cfg.setId("central");
- cfg.setName("This is central test 002");
- repo = repositoryRegistry.putRepository( cfg, configuration );
- assertSame( internalRepo, repo );
- assertEquals("This is central test 002",repo.getName());
- assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
-
- repositoryRegistry.reload();
- assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
- }
-
- @Test
- public void removeRepository( ) throws Exception
- {
- assertEquals(5, repositoryRegistry.getRepositories().size());
- Repository repo = repositoryRegistry.getRepository( "snapshots" );
- repositoryRegistry.removeRepository( repo );
- assertEquals(4, repositoryRegistry.getRepositories().size());
- assertTrue( repositoryRegistry.getRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
- archivaConfiguration.reload();
- repositoryRegistry.reload();
- assertEquals(4, repositoryRegistry.getRepositories().size());
- }
-
- @Test
- public void removeManagedRepository( ) throws Exception
- {
-
- assertEquals(4, repositoryRegistry.getManagedRepositories().size());
- ManagedRepository repo = repositoryRegistry.getManagedRepository( "snapshots" );
- repositoryRegistry.removeRepository( repo );
- assertEquals(3, repositoryRegistry.getManagedRepositories().size());
- assertTrue( repositoryRegistry.getManagedRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
- archivaConfiguration.reload();
- repositoryRegistry.reload();
- assertEquals(3, repositoryRegistry.getManagedRepositories().size());
- }
-
- @Test
- public void removeManagedRepositoryWithoutSave( ) throws Exception
- {
- Configuration configuration = archivaConfiguration.getConfiguration();
- assertEquals(4, repositoryRegistry.getManagedRepositories().size());
- ManagedRepository repo = repositoryRegistry.getManagedRepository( "snapshots" );
- repositoryRegistry.removeRepository( repo, configuration );
- assertEquals(3, repositoryRegistry.getManagedRepositories().size());
- assertTrue( repositoryRegistry.getManagedRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
- archivaConfiguration.reload();
- repositoryRegistry.reload();
- assertEquals(4, repositoryRegistry.getManagedRepositories().size());
- }
-
-
- @Test
- public void removeRemoteRepository( ) throws Exception
- {
- assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
- RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
- repositoryRegistry.removeRepository( repo );
- assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
- assertTrue( repositoryRegistry.getRemoteRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "central" ) ) );
- archivaConfiguration.reload();
- repositoryRegistry.reload();
- assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
- }
-
- @Test
- public void removeRemoteRepositoryWithoutSave( ) throws Exception
- {
- Configuration configuration = archivaConfiguration.getConfiguration();
- assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
- RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
- repositoryRegistry.removeRepository( repo, configuration );
- assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
- assertTrue( repositoryRegistry.getRemoteRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "central" ) ) );
- archivaConfiguration.reload();
- repositoryRegistry.reload();
- assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
- }
-
-
- @Test
- public void cloneManagedRepo( ) throws Exception
- {
- ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( "internal" );
-
- try
- {
- repositoryRegistry.clone(managedRepository, "snapshots");
- throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
- }
- catch ( RepositoryException e )
- {
- // OK
- }
-
- try
- {
- repositoryRegistry.clone(managedRepository, "central");
- throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
- }
- catch ( RepositoryException e )
- {
- // OK
- }
-
- ManagedRepository clone = repositoryRegistry.clone( managedRepository, "newinternal" );
- assertNotNull(clone);
- assertNull(clone.getContent());
- assertEquals("Archiva Managed Internal Repository", clone.getName());
- assertNotSame( managedRepository, clone );
-
- }
-
- @Test
- public void cloneRemoteRepo( ) throws Exception
- {
- RemoteRepository remoteRepository = repositoryRegistry.getRemoteRepository( "central" );
-
- try
- {
- repositoryRegistry.clone(remoteRepository, "snapshots");
- throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
- }
- catch ( RepositoryException e )
- {
- // OK
- }
-
- try
- {
- repositoryRegistry.clone(remoteRepository, "central");
- throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
- }
- catch ( RepositoryException e )
- {
- // OK
- }
-
- RemoteRepository clone = repositoryRegistry.clone( remoteRepository, "newCentral" );
- assertNotNull(clone);
- assertNull(clone.getContent());
- assertEquals("Central Repository", clone.getName());
- assertNotSame( remoteRepository, clone );
-
- }
-
- @Test
- void validateRepository() {
- Repository repo = repositoryRegistry.getRepository( "internal" );
- assertNotNull( repo );
- assertTrue( repositoryRegistry.validateRepository( repo ).isValid() );
- }
-
- }
|