From: Olivier Lamy Date: Wed, 21 May 2014 04:26:03 +0000 (+1000) Subject: [MRM-1843] provide mechanism to obtain the latest version of an artifact X-Git-Tag: archiva-2.1.0~30 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=450a8a3732f552ef9a1ed6944f674aa265f2a971;p=archiva.git [MRM-1843] provide mechanism to obtain the latest version of an artifact oups missed to add test classes --- diff --git a/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/AbstractDownloadTest.java b/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/AbstractDownloadTest.java index 70ae9dd82..0b176123d 100644 --- a/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/AbstractDownloadTest.java +++ b/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/AbstractDownloadTest.java @@ -70,7 +70,7 @@ public abstract class AbstractDownloadTest extends TestCase { - protected Logger log = LoggerFactory.getLogger( getClass() ); + protected final Logger log = LoggerFactory.getLogger( getClass() ); protected static String previousAppServerBase; diff --git a/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/DownloadArtifactFromQueryTest.java b/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/DownloadArtifactFromQueryTest.java new file mode 100644 index 000000000..ce39b6846 --- /dev/null +++ b/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/DownloadArtifactFromQueryTest.java @@ -0,0 +1,184 @@ +package org.apache.archiva.remotedownload; +/* + * 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.admin.model.beans.ManagedRepository; +import org.apache.archiva.rest.api.services.ManagedRepositoriesService; +import org.apache.archiva.rest.api.services.RepositoriesService; +import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner; +import org.apache.commons.io.FileUtils; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.ws.rs.core.Response; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * @author Olivier Lamy + */ +@RunWith( ArchivaBlockJUnit4ClassRunner.class ) +public class DownloadArtifactFromQueryTest + extends AbstractDownloadTest +{ + + @BeforeClass + public static void setAppServerBase() + throws IOException + { + previousAppServerBase = System.getProperty( "appserver.base" ); + System.setProperty( "appserver.base", + new File( System.getProperty( "java.io.tmpdir" ) ).getCanonicalPath() + "/target/" + + DownloadArtifactFromQueryTest.class.getName() + ); + } + + @AfterClass + public static void resetAppServerBase() + { + System.setProperty( "appserver.base", previousAppServerBase ); + } + + @Override + protected String getSpringConfigLocation() + { + return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-merge-index-download.xml"; + } + + @After + public void cleanup() + throws Exception + { + super.tearDown(); + Path tmpIndexDir = Paths.get( System.getProperty( "java.io.tmpdir" ), "tmpIndex" ); + if ( Files.exists( tmpIndexDir ) ) + { + FileUtils.deleteDirectory( tmpIndexDir.toFile() ); + } + } + + + protected String createAndScanRepo() + throws Exception + { + + Path tmpIndexDir = Paths.get( System.getProperty( "java.io.tmpdir" ), "tmpIndex" ); + if ( Files.exists( tmpIndexDir ) ) + { + FileUtils.deleteDirectory( tmpIndexDir.toFile() ); + } + String id = Long.toString( System.currentTimeMillis() ); + ManagedRepository managedRepository = new ManagedRepository(); + managedRepository.setId( id ); + managedRepository.setName( "name of " + id ); + managedRepository.setLocation( System.getProperty( "basedir" ) + "/src/test/repositories/test-repo" ); + managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex/" + id ); + + ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService(); + + if ( managedRepositoriesService.getManagedRepository( id ) != null ) + { + managedRepositoriesService.deleteManagedRepository( id, false ); + } + + getManagedRepositoriesService().addManagedRepository( managedRepository ); + + RepositoriesService repositoriesService = getRepositoriesService(); + + repositoriesService.scanRepositoryNow( id, true ); + + // wait a bit to ensure index is finished + int timeout = 20000; + while ( timeout > 0 && repositoriesService.alreadyScanning( id ) ) + { + Thread.sleep( 500 ); + timeout -= 500; + } + + return id; + + } + + @Test + public void downloadFixedVersion() + throws Exception + { + + String id = createAndScanRepo(); + + try + { + Response response = + getSearchService().redirectToArtifactFile( null, "org.apache.archiva", "archiva-test", "1.0", null, + null ); + + Assert.assertEquals( Response.Status.TEMPORARY_REDIRECT.getStatusCode(), response.getStatus() ); + + String location = String.class.cast( response.getMetadata().get( "Location" ).get( 0 ) ); + + /// http://localhost:57168/repository/1400639145722/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar + + Assert.assertEquals( "http://localhost:" + port + "/repository/" + id + + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar", location + ); + } + finally + { + getManagedRepositoriesService().deleteManagedRepository( id, false ); + } + + } + + + @Test + public void downloadLatestVersion() + throws Exception + { + String id = createAndScanRepo(); + + try + { + Response response = + getSearchService().redirectToArtifactFile( null, "org.apache.archiva", "archiva-test", "LATEST", null, + null ); + + Assert.assertEquals( Response.Status.TEMPORARY_REDIRECT.getStatusCode(), response.getStatus() ); + + String location = String.class.cast( response.getMetadata().get( "Location" ).get( 0 ) ); + + /// http://localhost:57168/repository/1400639145722/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar + + Assert.assertEquals( "http://localhost:" + port + "/repository/" + id + + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar", location + ); + } + finally + { + getManagedRepositoriesService().deleteManagedRepository( id, false ); + } + + } +} diff --git a/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/DownloadMergedIndexTest.java b/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/DownloadMergedIndexTest.java index 3a89d0e65..8741e24e0 100644 --- a/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/DownloadMergedIndexTest.java +++ b/archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/remotedownload/DownloadMergedIndexTest.java @@ -23,16 +23,16 @@ import org.apache.archiva.admin.model.beans.ProxyConnector; import org.apache.archiva.admin.model.beans.RemoteRepository; import org.apache.archiva.admin.model.beans.RepositoryGroup; import org.apache.archiva.maven2.model.Artifact; +import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants; +import org.apache.archiva.redback.rest.services.FakeCreateAdminService; import org.apache.archiva.rest.api.model.SearchRequest; import org.apache.archiva.rest.api.services.ManagedRepositoriesService; import org.apache.archiva.rest.api.services.ProxyConnectorService; import org.apache.archiva.rest.api.services.RepositoriesService; import org.apache.archiva.rest.api.services.RepositoryGroupService; import org.apache.archiva.rest.api.services.SearchService; +import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner; import org.apache.commons.io.FileUtils; -import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants; -import org.apache.archiva.redback.rest.services.FakeCreateAdminService; -import static org.assertj.core.api.Assertions.assertThat; import org.junit.After; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -41,15 +41,18 @@ import org.junit.runner.RunWith; import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.List; -import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Olivier Lamy */ -@RunWith(ArchivaBlockJUnit4ClassRunner.class) +@RunWith( ArchivaBlockJUnit4ClassRunner.class ) public class DownloadMergedIndexTest extends AbstractDownloadTest { @@ -61,7 +64,8 @@ public class DownloadMergedIndexTest previousAppServerBase = System.getProperty( "appserver.base" ); System.setProperty( "appserver.base", new File( System.getProperty( "java.io.tmpdir" ) ).getCanonicalPath() + "/target/" - + DownloadMergedIndexTest.class.getName() ); + + DownloadMergedIndexTest.class.getName() + ); } @AfterClass @@ -93,10 +97,10 @@ public class DownloadMergedIndexTest public void downloadMergedIndex() throws Exception { - File tmpIndexDir = new File( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex" ); - if ( tmpIndexDir.exists() ) + Path tmpIndexDir = Paths.get( System.getProperty( "java.io.tmpdir" ), "tmpIndex" ); + if ( Files.exists( tmpIndexDir ) ) { - FileUtils.deleteDirectory( tmpIndexDir ); + FileUtils.deleteDirectory( tmpIndexDir.toFile() ); } String id = Long.toString( System.currentTimeMillis() ); ManagedRepository managedRepository = new ManagedRepository(); @@ -165,8 +169,7 @@ public class DownloadMergedIndexTest remoteRepository.setUserName( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME ); remoteRepository.setPassword( FakeCreateAdminService.ADMIN_TEST_PWD ); - - if (getRemoteRepositoriesService().getRemoteRepository( remoteRepository.getId() ) != null) + if ( getRemoteRepositoriesService().getRemoteRepository( remoteRepository.getId() ) != null ) { getRemoteRepositoriesService().deleteRemoteRepository( remoteRepository.getId() ); }