/** * Copyright (c) 2008-2011 Sonatype, Inc. * All rights reserved. Includes the third-party code listed at http://www.sonatype.com/products/nexus/attributions. * * This program is free software: you can redistribute it and/or modify it only under the terms of the GNU Affero General * Public License Version 3 as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License Version 3 * for more details. * * You should have received a copy of the GNU Affero General Public License Version 3 along with this program. If not, see * http://www.gnu.org/licenses. * * Sonatype Nexus (TM) Open Source Version is available from Sonatype, Inc. Sonatype and Sonatype Nexus are trademarks of * Sonatype, Inc. Apache Maven is a trademark of the Apache Foundation. M2Eclipse is a trademark of the Eclipse Foundation. * All other trademarks are the property of their respective owners. */ package org.sonatype.nexus.integrationtests.nexus2302; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertTrue; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.List; import org.apache.maven.index.artifact.Gav; import org.apache.maven.it.VerificationException; import org.apache.maven.it.Verifier; import org.codehaus.plexus.util.IOUtil; import org.restlet.data.MediaType; import org.sonatype.nexus.integrationtests.AbstractNexusIntegrationTest; import org.sonatype.nexus.rest.model.ContentListResource; import org.sonatype.nexus.rest.model.NexusArtifact; import org.sonatype.nexus.test.utils.ContentListMessageUtil; import org.sonatype.nexus.test.utils.FileTestingUtils; import org.sonatype.nexus.test.utils.MavenDeployer; import org.sonatype.nexus.test.utils.TaskScheduleUtil; import org.testng.annotations.Test; public class Nexus2302DeployEncodedFileIT extends AbstractNexusIntegrationTest { public Nexus2302DeployEncodedFileIT() { super.setTestRepositoryId( REPO_TEST_HARNESS_REPO ); } @Test public void plusSign() throws Exception { Gav gav = new Gav( "nexus2302", "artifact", "1.0", "c++", "jar", null, null, null, false, null, false, null ); testIt( gav ); } @Test public void version() throws Exception { Gav gav = new Gav( "nexus2302", "artifact", "1++0", null, "jar", null, null, null, false, null, false, null ); testIt( gav ); } @Test public void dolarSign() throws Exception { Gav gav = new Gav( "nexus2302", "artifact", "$dolar", "void", "jar", null, null, null, false, null, false, null ); testIt( gav ); } public void testIt( Gav gav ) throws VerificationException, IOException, Exception { final File file = getTestFile( "artifact.jar" ); Verifier v = MavenDeployer.deployAndGetVerifier( gav, getRepositoryUrl( REPO_TEST_HARNESS_REPO ), file, getOverridableFile( "settings.xml" ) ); v.verifyErrorFreeLog(); getEventInspectorsUtil().waitForCalmPeriod(); TaskScheduleUtil.waitForAllTasksToStop(); // direct download assertTrue( FileTestingUtils.compareFileSHA1s( file, downloadArtifact( gav, "target/nexus2302/" + gav.getArtifactId() + ".jar" ) ) ); // redirect download assertTrue( FileTestingUtils.compareFileSHA1s( file, downloadSnapshotArtifact( REPO_TEST_HARNESS_REPO, gav, new File( "target/nexus2302" ) ) ) ); checkFileSystem( gav ); checkIndex( gav ); checkRepoBrowse( gav ); checkRepoBrowse( gav ); checkBrowse( gav ); checkContentBrowse( gav, null ); checkContentBrowse( gav, "?abc=321" ); delete( gav ); } private void checkContentBrowse( Gav gav, String query ) throws Exception { if ( query == null ) { query = ""; } URL url = new URL( nexusBaseUrl + "content/repositories/" + REPO_TEST_HARNESS_REPO + "/" ); String content = IOUtil.toString( url.openStream() ); assertThat( content, containsString( gav.getGroupId() ) ); url = new URL( url.toString() + gav.getGroupId() + "/" ); assertThat( content, containsString( url.toString() ) ); content = IOUtil.toString( url.openStream() ); assertThat( content, containsString( gav.getArtifactId() ) ); url = new URL( url.toString() + gav.getArtifactId() + "/" ); assertThat( content, containsString( url.toString() ) ); content = IOUtil.toString( url.openStream() ); assertThat( content, containsString( gav.getVersion() ) ); url = new URL( url.toString() + gav.getVersion() + "/" ); assertThat( content, containsString( url.toString() ) ); content = IOUtil.toString( url.openStream() ); assertThat( content, containsString( gav.getArtifactId() ) ); String clas = gav.getClassifier() == null ? "" : "-" + gav.getClassifier(); url = new URL( url.toString() + gav.getArtifactId() + "-" + gav.getVersion() + clas + "." + gav.getExtension() ); assertThat( content, containsString( url.toString() ) ); } private void checkBrowse( Gav gav ) throws Exception { URL url = new URL( nexusBaseUrl + "service/local/repositories/" + REPO_TEST_HARNESS_REPO + "/content/" ); String content = IOUtil.toString( url.openStream() ); assertThat( content, containsString( url.toString() ) ); assertThat( content, containsString( gav.getGroupId() ) ); url = new URL( url.toString() + gav.getGroupId() + "/" ); content = IOUtil.toString( url.openStream() ); assertThat( content, containsString( url.toString() ) ); assertThat( content, containsString( gav.getArtifactId() ) ); url = new URL( url.toString() + gav.getArtifactId() + "/" ); content = IOUtil.toString( url.openStream() ); assertThat( content, containsString( url.toString() ) ); assertThat( content, containsString( gav.getVersion() ) ); url = new URL( url.toString() + gav.getVersion() + "/" ); content = IOUtil.toString( url.openStream() ); assertThat( content, containsString( url.toString() ) ); assertThat( content, containsString( gav.getArtifactId() ) ); if ( gav.getClassifier() != null ) { assertThat( content, containsString( gav.getClassifier() ) ); } } private void checkRepoBrowse( Gav gav ) throws Exception { ContentListMessageUtil contentUtil = new ContentListMessageUtil( this.getXMLXStream(), MediaType.APPLICATION_XML ); List<ContentListResource> result = contentUtil.getContentListResource( REPO_TEST_HARNESS_REPO, "/", false ); ContentListResource g = select( result, gav.getGroupId() ); assertThat( g.getResourceURI(), equalTo( nexusBaseUrl + "service/local/repositories/" + REPO_TEST_HARNESS_REPO + "/content/" + gav.getGroupId() + "/" ) ); result = contentUtil.getContentListResource( REPO_TEST_HARNESS_REPO, g.getRelativePath(), false ); ContentListResource a = select( result, gav.getArtifactId() ); assertThat( a.getResourceURI(), equalTo( nexusBaseUrl + "service/local/repositories/" + REPO_TEST_HARNESS_REPO + "/content/" + gav.getGroupId() + "/" + gav.getArtifactId() + "/" ) ); result = contentUtil.getContentListResource( REPO_TEST_HARNESS_REPO, a.getRelativePath(), false ); ContentListResource v = select( result, gav.getVersion() ); assertThat( v.getResourceURI(), equalTo( nexusBaseUrl + "service/local/repositories/" + REPO_TEST_HARNESS_REPO + "/content/" + gav.getGroupId() + "/" + gav.getArtifactId() + "/" + gav.getVersion() + "/" ) ); result = contentUtil.getContentListResource( REPO_TEST_HARNESS_REPO, v.getRelativePath(), false ); String clas = gav.getClassifier() == null ? "" : "-" + gav.getClassifier(); ContentListResource c = select( result, gav.getArtifactId() + "-" + gav.getVersion() + clas + "." + gav.getExtension() ); assertNotNull( c ); assertThat( c.getResourceURI(), equalTo( nexusBaseUrl + "service/local/repositories/" + REPO_TEST_HARNESS_REPO + "/content/" + gav.getGroupId() + "/" + gav.getArtifactId() + "/" + gav.getVersion() + "/" + gav.getArtifactId() + "-" + gav.getVersion() + clas + "." + gav.getExtension() ) ); } private ContentListResource select( List<ContentListResource> result, String text ) { assertFalse( result.isEmpty() ); ContentListResource g = null; for ( ContentListResource content : result ) { if ( content.getText().equals( text ) ) { g = content; } } assertNotNull( g, text + " not found" ); assertThat( g.getResourceURI(), containsString( text ) ); return g; } private void checkIndex( Gav gav ) throws Exception { List<NexusArtifact> result = getSearchMessageUtil().searchForGav( gav.getGroupId(), gav.getArtifactId(), gav.getVersion(), REPO_TEST_HARNESS_REPO ); assertResult( gav, result ); result = getSearchMessageUtil().searchForGav( gav.getGroupId(), gav.getArtifactId(), gav.getVersion(), gav.getExtension(), gav.getClassifier(), REPO_TEST_HARNESS_REPO ); assertResult( gav, result ); } private void assertResult( Gav gav, List<NexusArtifact> result ) { assertFalse( result.isEmpty() ); assertThat( result.get( 0 ).getGroupId(), equalTo( gav.getGroupId() ) ); assertThat( result.get( 0 ).getArtifactId(), equalTo( gav.getArtifactId() ) ); assertThat( result.get( 0 ).getVersion(), equalTo( gav.getVersion() ) ); assertThat( result.get( 0 ).getClassifier(), equalTo( gav.getClassifier() ) ); } private void checkFileSystem( Gav gav ) throws IOException { File artifact = new File( nexusWorkDir, "storage/" + REPO_TEST_HARNESS_REPO + "/" + getRelitiveArtifactPath( gav ) ); assertTrue( artifact.exists(), "File not found: " + artifact.getAbsolutePath() ); } private void delete( Gav gav ) throws IOException { assertTrue( deleteFromRepository( REPO_TEST_HARNESS_REPO, getRelitiveArtifactPath( gav ) ) ); } }