Quellcode durchsuchen

create a testrule to get a temp folder with a predictible name

tags/archiva-2.2.0
Eric Barboni vor 9 Jahren
Ursprung
Commit
705fbd95c4

+ 6
- 3
archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/AbstractRepositoryServletProxiedTestCase.java Datei anzeigen

import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.File; import java.io.File;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.file.Files;


import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Rule;


/** /**
* AbstractRepositoryServletProxiedTestCase * AbstractRepositoryServletProxiedTestCase


protected RemoteRepoInfo remoteSnapshots; protected RemoteRepoInfo remoteSnapshots;


@Rule
public ArchivaTemporaryFolderRule repoRootInternali = new ArchivaTemporaryFolderRule();
@Before @Before
@Override @Override
public void setUp() public void setUp()
RemoteRepoInfo repo = new RemoteRepoInfo(); RemoteRepoInfo repo = new RemoteRepoInfo();
repo.id = id; repo.id = id;
repo.context = "/" + 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. // Remove exising root contents.
if ( repo.root.exists() ) if ( repo.root.exists() )

+ 86
- 0
archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/ArchivaTemporaryFolderRule.java Datei anzeigen

/*
* 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();
}
}

+ 36
- 45
archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/webdav/RepositoryServletSecurityTest.java Datei anzeigen

*/ */




import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.WebResponse;
import junit.framework.TestCase; import junit.framework.TestCase;
import net.sf.ehcache.CacheManager; import net.sf.ehcache.CacheManager;
import org.apache.archiva.configuration.ArchivaConfiguration; import org.apache.archiva.configuration.ArchivaConfiguration;
import org.apache.archiva.webdav.util.MavenIndexerCleaner; import org.apache.archiva.webdav.util.MavenIndexerCleaner;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.jackrabbit.webdav.DavSessionProvider; import org.apache.jackrabbit.webdav.DavSessionProvider;
import org.easymock.EasyMock; import org.easymock.EasyMock;
import org.easymock.IMocksControl; import org.easymock.IMocksControl;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.mock.web.MockServletConfig; import org.springframework.mock.web.MockServletConfig;
import org.springframework.mock.web.MockServletContext; import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;


import javax.inject.Inject; import javax.inject.Inject;
import javax.servlet.Servlet;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;


import static org.easymock.EasyMock.anyObject; import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.eq; import static org.easymock.EasyMock.eq;
import org.junit.Rule;


/** /**
* RepositoryServletSecurityTest Test the flow of the authentication and authorization checks. This does not necessarily * RepositoryServletSecurityTest Test the flow of the authentication and authorization checks. This does not necessarily
{ {
protected static final String REPOID_INTERNAL = "internal"; protected static final String REPOID_INTERNAL = "internal";



protected File repoRootInternal;

@Inject @Inject
protected ArchivaConfiguration archivaConfiguration; protected ArchivaConfiguration archivaConfiguration;




@Inject @Inject
ApplicationContext applicationContext; ApplicationContext applicationContext;


@Rule
public ArchivaTemporaryFolderRule repoRootInternal = new ArchivaTemporaryFolderRule();
@Before @Before
@Override @Override
public void setUp() public void setUp()
throws Exception throws Exception
{ {
super.setUp(); super.setUp();


String appserverBase = String appserverBase =
File testConf = new File( "src/test/resources/repository-archiva.xml" ); File testConf = new File( "src/test/resources/repository-archiva.xml" );
File testConfDest = new File( appserverBase, "conf/archiva.xml" ); File testConfDest = new File( appserverBase, "conf/archiva.xml" );
FileUtils.copyFile( testConf, testConfDest ); FileUtils.copyFile( testConf, testConfDest );
repoRootInternal = new File( appserverBase, "data/repositories/internal" );
Configuration config = archivaConfiguration.getConfiguration(); Configuration config = archivaConfiguration.getConfiguration();

if ( !config.getManagedRepositoriesAsMap().containsKey( REPOID_INTERNAL ) )
{
config.addManagedRepository(
createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal ) );
// clear managed repository
List<ManagedRepositoryConfiguration> 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 ); saveConfiguration( archivaConfiguration );


CacheManager.getInstance().clearAll(); CacheManager.getInstance().clearAll();
return repo; return repo;
} }


protected void saveConfiguration()
/*protected void saveConfiguration()
throws Exception throws Exception
{ {
saveConfiguration( archivaConfiguration ); saveConfiguration( archivaConfiguration );
}
}*/


protected void saveConfiguration( ArchivaConfiguration archivaConfiguration ) protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
throws Exception throws Exception
{ {
archivaConfiguration.save( archivaConfiguration.getConfiguration() );
archivaConfiguration.save( archivaConfiguration.getConfiguration() );
} }


protected void setupCleanRepo( File repoRootDir )
/*protected void setupCleanRepo( File repoRootDir )
throws IOException throws IOException
{ {
FileUtils.deleteDirectory( repoRootDir );
if ( !repoRootDir.exists() )
{
repoRootDir.mkdirs();
}
}
}*/


@Override @Override
@After @After
throws Exception throws Exception
{ {


if ( repoRootInternal.exists() )
/* if ( repoRootInternal.exists() )
{ {
FileUtils.deleteDirectory( repoRootInternal ); FileUtils.deleteDirectory( repoRootInternal );
}
}*/


applicationContext.getBean( MavenIndexerCleaner.class ).cleanupIndex(); applicationContext.getBean( MavenIndexerCleaner.class ).cleanupIndex();


public void testPutWithInvalidUserAndGuestHasNoWriteAccess() public void testPutWithInvalidUserAndGuestHasNoWriteAccess()
throws Exception throws Exception
{ {
setupCleanRepo( repoRootInternal );

InputStream is = getClass().getResourceAsStream( "/artifact.jar" ); InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
assertNotNull( "artifact.jar inputstream", is ); assertNotNull( "artifact.jar inputstream", is );


public void testPutWithInvalidUserAndGuestHasWriteAccess() public void testPutWithInvalidUserAndGuestHasWriteAccess()
throws Exception throws Exception
{ {
setupCleanRepo( repoRootInternal );

servlet.setDavSessionProvider( davSessionProvider ); servlet.setDavSessionProvider( davSessionProvider );


ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory(); ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
public void testPutWithValidUserWithNoWriteAccess() public void testPutWithValidUserWithNoWriteAccess()
throws Exception throws Exception
{ {
setupCleanRepo( repoRootInternal );

servlet.setDavSessionProvider( davSessionProvider ); servlet.setDavSessionProvider( davSessionProvider );


ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory(); ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
public void testPutWithValidUserWithWriteAccess() public void testPutWithValidUserWithWriteAccess()
throws Exception throws Exception
{ {
setupCleanRepo( repoRootInternal );
assertTrue( repoRootInternal.exists() );
assertTrue( repoRootInternal.getRoot().exists() );


MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(); MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar"; String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar"; String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
String expectedArtifactContents = "dummy-commons-lang-artifact"; String expectedArtifactContents = "dummy-commons-lang-artifact";


File artifactFile = new File( repoRootInternal, commonsLangJar );
File artifactFile = new File( repoRootInternal.getRoot(), commonsLangJar );
artifactFile.getParentFile().mkdirs(); artifactFile.getParentFile().mkdirs();


FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() ); FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
mockHttpServletRequest.setMethod( "GET" ); mockHttpServletRequest.setMethod( "GET" );
mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar ); mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar );



MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();


servlet.service( mockHttpServletRequest, mockHttpServletResponse ); servlet.service( mockHttpServletRequest, mockHttpServletResponse );
String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar"; String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
String expectedArtifactContents = "dummy-commons-lang-artifact"; String expectedArtifactContents = "dummy-commons-lang-artifact";


File artifactFile = new File( repoRootInternal, commonsLangJar );
File artifactFile = new File( repoRootInternal.getRoot(), commonsLangJar );
artifactFile.getParentFile().mkdirs(); artifactFile.getParentFile().mkdirs();


FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() ); FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
mockHttpServletRequest.setMethod( "GET" ); mockHttpServletRequest.setMethod( "GET" );
mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar ); mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar );



MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();


servlet.service( mockHttpServletRequest, mockHttpServletResponse ); servlet.service( mockHttpServletRequest, mockHttpServletResponse );
String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar"; String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
String expectedArtifactContents = "dummy-commons-lang-artifact"; String expectedArtifactContents = "dummy-commons-lang-artifact";


File artifactFile = new File( repoRootInternal, commonsLangJar );
File artifactFile = new File( repoRootInternal.getRoot(), commonsLangJar );
artifactFile.getParentFile().mkdirs(); artifactFile.getParentFile().mkdirs();


FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() ); FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
mockHttpServletRequest.setMethod( "GET" ); mockHttpServletRequest.setMethod( "GET" );
mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar ); mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar );



MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();


servlet.service( mockHttpServletRequest, mockHttpServletResponse ); servlet.service( mockHttpServletRequest, mockHttpServletResponse );
String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar"; String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
String expectedArtifactContents = "dummy-commons-lang-artifact"; String expectedArtifactContents = "dummy-commons-lang-artifact";


File artifactFile = new File( repoRootInternal, commonsLangJar );
File artifactFile = new File( repoRootInternal.getRoot(), commonsLangJar );
artifactFile.getParentFile().mkdirs(); artifactFile.getParentFile().mkdirs();


FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() ); FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );


assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() ); assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
} }

} }

Laden…
Abbrechen
Speichern