Return-Path: X-Original-To: apmail-archiva-commits-archive@www.apache.org Delivered-To: apmail-archiva-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 68C05178B4 for ; Tue, 30 Sep 2014 21:12:15 +0000 (UTC) Received: (qmail 2432 invoked by uid 500); 30 Sep 2014 21:12:15 -0000 Delivered-To: apmail-archiva-commits-archive@archiva.apache.org Received: (qmail 2387 invoked by uid 500); 30 Sep 2014 21:12:15 -0000 Mailing-List: contact commits-help@archiva.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@archiva.apache.org Delivered-To: mailing list commits@archiva.apache.org Received: (qmail 2364 invoked by uid 99); 30 Sep 2014 21:12:15 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 30 Sep 2014 21:12:15 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 09E50A0C51D; Tue, 30 Sep 2014 21:12:15 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: skygo@apache.org To: commits@archiva.apache.org Date: Tue, 30 Sep 2014 21:12:15 -0000 Message-Id: <663a97dbbd4a44d3a741fc286ca52949@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [1/2] git commit: create a testrule to get a temp folder with a predictible name Repository: archiva Updated Branches: refs/heads/master a2d62a3c0 -> e4a7f04a3 create a testrule to get a temp folder with a predictible name Project: http://git-wip-us.apache.org/repos/asf/archiva/repo Commit: http://git-wip-us.apache.org/repos/asf/archiva/commit/705fbd95 Tree: http://git-wip-us.apache.org/repos/asf/archiva/tree/705fbd95 Diff: http://git-wip-us.apache.org/repos/asf/archiva/diff/705fbd95 Branch: refs/heads/master Commit: 705fbd95c416a0ac4a1825e8737a0112c933ed31 Parents: a2d62a3 Author: Eric Barboni Authored: Tue Sep 30 22:39:24 2014 +0200 Committer: Eric Barboni Committed: Tue Sep 30 23:06:56 2014 +0200 ---------------------------------------------------------------------- ...bstractRepositoryServletProxiedTestCase.java | 9 +- .../webdav/ArchivaTemporaryFolderRule.java | 86 ++++++++++++++++++++ .../webdav/RepositoryServletSecurityTest.java | 81 ++++++++---------- 3 files changed, 128 insertions(+), 48 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/archiva/blob/705fbd95/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/AbstractRepositoryServletProxiedTestCase.java ---------------------------------------------------------------------- diff --git a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/AbstractRepositoryServletProxiedTestCase.java b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/AbstractRepositoryServletProxiedTestCase.java index 3d8a5e6..0c0a718 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/AbstractRepositoryServletProxiedTestCase.java +++ b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/AbstractRepositoryServletProxiedTestCase.java @@ -39,9 +39,9 @@ import org.junit.Before; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.nio.charset.Charset; -import java.nio.file.Files; import static org.assertj.core.api.Assertions.assertThat; +import org.junit.Rule; /** * AbstractRepositoryServletProxiedTestCase @@ -94,6 +94,9 @@ public abstract class AbstractRepositoryServletProxiedTestCase protected RemoteRepoInfo remoteSnapshots; + @Rule + public ArchivaTemporaryFolderRule repoRootInternali = new ArchivaTemporaryFolderRule(); + @Before @Override public void setUp() @@ -119,8 +122,8 @@ public abstract class AbstractRepositoryServletProxiedTestCase RemoteRepoInfo repo = new RemoteRepoInfo(); repo.id = id; repo.context = "/" + id; - repo.root = Files.createTempDirectory( - "temp" ).toFile();// new File( System.getProperty( "basedir" ) + "target/remote-repos/" + id + "/" ); + repo.root = repoRootInternali.getRoot();/*Files.createTempDirectory( + "temp" ).toFile();*/// new File( System.getProperty( "basedir" ) + "target/remote-repos/" + id + "/" ); // Remove exising root contents. if ( repo.root.exists() ) http://git-wip-us.apache.org/repos/asf/archiva/blob/705fbd95/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/ArchivaTemporaryFolderRule.java ---------------------------------------------------------------------- diff --git a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/ArchivaTemporaryFolderRule.java b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/ArchivaTemporaryFolderRule.java new file mode 100644 index 0000000..8ca9fd5 --- /dev/null +++ b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/ArchivaTemporaryFolderRule.java @@ -0,0 +1,86 @@ +/* + * Copyright 2014 The Apache Software Foundation. + * + * Licensed 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. + */ +package org.apache.archiva.webdav; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import org.apache.commons.io.FileUtils; +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +/** + * Rule to help creating folder for repository based on testmethod name + * @author Eric + */ +public class ArchivaTemporaryFolderRule implements TestRule { + private File d; + private Description desc = Description.EMPTY; + + public void before() throws IOException { + // hard coded maven target file + File f1 = new File("target" + File.separator + "archivarepo" + File.separator + ArchivaTemporaryFolderRule.resumepackage(desc.getClassName()) + File.separator + desc.getMethodName()); + f1.mkdirs(); + Path p = Files.createDirectories(f1.toPath()); + d = p.toFile(); + } + + public File getRoot() { + return d; + } + + public void after() throws IOException { + FileUtils.deleteDirectory(getRoot()); + } + + @Override + public Statement apply(Statement base, Description description) { + desc = description; + return statement(base); + } + + private Statement statement(final Statement base) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + before(); + try { + base.evaluate(); + } finally { + after(); + } + } + }; + } + /** + * Return a filepath from FQN class name with only first char of package and classname + * @param packagename + * @return + */ + public static String resumepackage(String packagename) { + StringBuilder sb = new StringBuilder(); + String[] p = packagename.split("\\."); + for (int i = 0; i < p.length - 2; i++) + { + sb.append(p[i].charAt(0)).append(File.separator); + } + sb.append(p[p.length - 1]); + return sb.toString(); + } + +} http://git-wip-us.apache.org/repos/asf/archiva/blob/705fbd95/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/RepositoryServletSecurityTest.java ---------------------------------------------------------------------- diff --git a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/RepositoryServletSecurityTest.java b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/RepositoryServletSecurityTest.java index e040e00..3958947 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/RepositoryServletSecurityTest.java +++ b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/RepositoryServletSecurityTest.java @@ -20,8 +20,6 @@ package org.apache.archiva.webdav; */ -import com.gargoylesoftware.htmlunit.WebRequest; -import com.gargoylesoftware.htmlunit.WebResponse; import junit.framework.TestCase; import net.sf.ehcache.CacheManager; import org.apache.archiva.configuration.ArchivaConfiguration; @@ -42,13 +40,11 @@ import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; import org.apache.archiva.webdav.util.MavenIndexerCleaner; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; -import org.apache.commons.lang.StringUtils; import org.apache.jackrabbit.webdav.DavSessionProvider; import org.easymock.EasyMock; import org.easymock.IMocksControl; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.context.ApplicationContext; @@ -57,22 +53,22 @@ import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockServletConfig; import org.springframework.mock.web.MockServletContext; import org.springframework.test.context.ContextConfiguration; -import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.WebApplicationContext; import javax.inject.Inject; -import javax.servlet.Servlet; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.File; -import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; import static org.easymock.EasyMock.anyObject; import static org.easymock.EasyMock.eq; +import org.junit.Rule; /** * RepositoryServletSecurityTest Test the flow of the authentication and authorization checks. This does not necessarily @@ -85,9 +81,6 @@ public class RepositoryServletSecurityTest { protected static final String REPOID_INTERNAL = "internal"; - - protected File repoRootInternal; - @Inject protected ArchivaConfiguration archivaConfiguration; @@ -105,12 +98,17 @@ public class RepositoryServletSecurityTest @Inject ApplicationContext applicationContext; + + @Rule + public ArchivaTemporaryFolderRule repoRootInternal = new ArchivaTemporaryFolderRule(); + @Before @Override public void setUp() throws Exception { + super.setUp(); String appserverBase = @@ -119,16 +117,20 @@ public class RepositoryServletSecurityTest File testConf = new File( "src/test/resources/repository-archiva.xml" ); File testConfDest = new File( appserverBase, "conf/archiva.xml" ); FileUtils.copyFile( testConf, testConfDest ); - - repoRootInternal = new File( appserverBase, "data/repositories/internal" ); - + + + Configuration config = archivaConfiguration.getConfiguration(); - - if ( !config.getManagedRepositoriesAsMap().containsKey( REPOID_INTERNAL ) ) - { - config.addManagedRepository( - createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal ) ); + // clear managed repository + List f1 = new ArrayList<>(config.getManagedRepositories()); + for (ManagedRepositoryConfiguration f: f1 ) { + config.removeManagedRepository(f); } + assertEquals(0,config.getManagedRepositories().size()); + // add internal repo + config.addManagedRepository( + createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal.getRoot() ) ); + saveConfiguration( archivaConfiguration ); CacheManager.getInstance().clearAll(); @@ -175,27 +177,22 @@ public class RepositoryServletSecurityTest return repo; } - protected void saveConfiguration() + /*protected void saveConfiguration() throws Exception { saveConfiguration( archivaConfiguration ); - } + }*/ protected void saveConfiguration( ArchivaConfiguration archivaConfiguration ) throws Exception { - archivaConfiguration.save( archivaConfiguration.getConfiguration() ); + archivaConfiguration.save( archivaConfiguration.getConfiguration() ); } - protected void setupCleanRepo( File repoRootDir ) + /*protected void setupCleanRepo( File repoRootDir ) throws IOException { - FileUtils.deleteDirectory( repoRootDir ); - if ( !repoRootDir.exists() ) - { - repoRootDir.mkdirs(); - } - } + }*/ @Override @After @@ -203,10 +200,10 @@ public class RepositoryServletSecurityTest throws Exception { - if ( repoRootInternal.exists() ) + /* if ( repoRootInternal.exists() ) { FileUtils.deleteDirectory( repoRootInternal ); - } + }*/ applicationContext.getBean( MavenIndexerCleaner.class ).cleanupIndex(); @@ -221,8 +218,7 @@ public class RepositoryServletSecurityTest public void testPutWithInvalidUserAndGuestHasNoWriteAccess() throws Exception { - setupCleanRepo( repoRootInternal ); - + InputStream is = getClass().getResourceAsStream( "/artifact.jar" ); assertNotNull( "artifact.jar inputstream", is ); @@ -266,8 +262,7 @@ public class RepositoryServletSecurityTest public void testPutWithInvalidUserAndGuestHasWriteAccess() throws Exception { - setupCleanRepo( repoRootInternal ); - + servlet.setDavSessionProvider( davSessionProvider ); ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory(); @@ -337,8 +332,7 @@ public class RepositoryServletSecurityTest public void testPutWithValidUserWithNoWriteAccess() throws Exception { - setupCleanRepo( repoRootInternal ); - + servlet.setDavSessionProvider( davSessionProvider ); ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory(); @@ -403,8 +397,7 @@ public class RepositoryServletSecurityTest public void testPutWithValidUserWithWriteAccess() throws Exception { - setupCleanRepo( repoRootInternal ); - assertTrue( repoRootInternal.exists() ); + assertTrue( repoRootInternal.getRoot().exists() ); MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(); String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar"; @@ -480,7 +473,7 @@ public class RepositoryServletSecurityTest String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar"; String expectedArtifactContents = "dummy-commons-lang-artifact"; - File artifactFile = new File( repoRootInternal, commonsLangJar ); + File artifactFile = new File( repoRootInternal.getRoot(), commonsLangJar ); artifactFile.getParentFile().mkdirs(); FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() ); @@ -532,7 +525,6 @@ public class RepositoryServletSecurityTest mockHttpServletRequest.setMethod( "GET" ); mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar ); - MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); servlet.service( mockHttpServletRequest, mockHttpServletResponse ); @@ -553,7 +545,7 @@ public class RepositoryServletSecurityTest String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar"; String expectedArtifactContents = "dummy-commons-lang-artifact"; - File artifactFile = new File( repoRootInternal, commonsLangJar ); + File artifactFile = new File( repoRootInternal.getRoot(), commonsLangJar ); artifactFile.getParentFile().mkdirs(); FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() ); @@ -581,7 +573,6 @@ public class RepositoryServletSecurityTest mockHttpServletRequest.setMethod( "GET" ); mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar ); - MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); servlet.service( mockHttpServletRequest, mockHttpServletResponse ); @@ -600,7 +591,7 @@ public class RepositoryServletSecurityTest String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar"; String expectedArtifactContents = "dummy-commons-lang-artifact"; - File artifactFile = new File( repoRootInternal, commonsLangJar ); + File artifactFile = new File( repoRootInternal.getRoot(), commonsLangJar ); artifactFile.getParentFile().mkdirs(); FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() ); @@ -647,7 +638,6 @@ public class RepositoryServletSecurityTest mockHttpServletRequest.setMethod( "GET" ); mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar ); - MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); servlet.service( mockHttpServletRequest, mockHttpServletResponse ); @@ -667,7 +657,7 @@ public class RepositoryServletSecurityTest String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar"; String expectedArtifactContents = "dummy-commons-lang-artifact"; - File artifactFile = new File( repoRootInternal, commonsLangJar ); + File artifactFile = new File( repoRootInternal.getRoot(), commonsLangJar ); artifactFile.getParentFile().mkdirs(); FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() ); @@ -725,4 +715,5 @@ public class RepositoryServletSecurityTest assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() ); } + }