123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923 |
- package org.apache.archiva.webdav;
-
- /*
- * 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 com.gargoylesoftware.htmlunit.*;
- import junit.framework.TestCase;
- import net.sf.ehcache.CacheManager;
- 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.indexer.ArchivaIndexingContext;
- import org.apache.archiva.repository.base.ArchivaRepositoryRegistry;
- import org.apache.archiva.repository.ManagedRepository;
- import org.apache.archiva.repository.RepositoryType;
- import org.apache.archiva.repository.base.group.RepositoryGroupHandler;
- import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
- import org.apache.archiva.webdav.httpunit.MkColMethodWebRequest;
- import org.apache.commons.io.FileUtils;
- import org.apache.commons.io.IOUtils;
- import org.apache.commons.lang3.StringUtils;
- import org.junit.After;
- import org.junit.Assert;
- import org.junit.Before;
- import org.junit.runner.RunWith;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.BeansException;
- import org.springframework.beans.factory.BeanFactory;
- import org.springframework.beans.factory.NoSuchBeanDefinitionException;
- import org.springframework.beans.factory.ObjectProvider;
- import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.ApplicationEvent;
- import org.springframework.context.MessageSourceResolvable;
- import org.springframework.context.NoSuchMessageException;
- import org.springframework.core.ResolvableType;
- import org.springframework.core.env.Environment;
- import org.springframework.core.io.Resource;
- import org.springframework.mock.web.MockHttpServletRequest;
- 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.WebApplicationContext;
-
- import javax.inject.Inject;
- import javax.servlet.Servlet;
- import javax.servlet.ServletContext;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.UnsupportedEncodingException;
- import java.lang.annotation.Annotation;
- import java.net.URL;
- import java.nio.charset.Charset;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.Locale;
- import java.util.Map;
- import java.util.concurrent.atomic.AtomicReference;
-
- /**
- * AbstractRepositoryServletTestCase
- */
- @RunWith( ArchivaSpringJUnit4ClassRunner.class )
- @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:spring-context.xml",
- "classpath*:/repository-servlet-simple.xml" } )
- public abstract class AbstractRepositoryServletTestCase
- extends TestCase
- {
- protected static final String REPOID_INTERNAL = "internal";
-
- protected Path repoRootInternal;
-
- protected Path repoRootLegacy;
-
- @Inject
- protected ArchivaConfiguration archivaConfiguration;
-
- @Inject
- protected ApplicationContext applicationContext;
-
- @SuppressWarnings( "unused" )
- @Inject
- RepositoryGroupHandler repositoryGroupHandler;
-
- @Inject
- ArchivaRepositoryRegistry repositoryRegistry;
-
- protected Logger log = LoggerFactory.getLogger( getClass() );
-
- private AtomicReference<Path> projectBase = new AtomicReference<>( );
- private AtomicReference<Path> appserverBase = new AtomicReference<>( );
-
-
- public Path getProjectBase() {
- if (this.projectBase.get()==null) {
- String pathVal = System.getProperty("mvn.project.base.dir");
- Path baseDir;
- if (StringUtils.isEmpty(pathVal)) {
- baseDir= Paths.get("").toAbsolutePath();
- } else {
- baseDir = Paths.get(pathVal).toAbsolutePath();
- }
- this.projectBase.compareAndSet(null, baseDir);
- }
- return this.projectBase.get();
- }
-
- public Path getAppserverBase() {
- if (appserverBase.get()==null)
- {
- String pathVal = System.getProperty( "appserver.base" );
- Path basePath;
- if ( StringUtils.isNotEmpty( pathVal ) )
- {
- basePath = Paths.get( pathVal );
- }
- else
- {
- log.warn("Using relative path to working directory, appserver.base was not set!");
- basePath = Paths.get( "target/appserver-base" );
- }
- appserverBase.set( basePath );
- }
- return appserverBase.get();
- }
-
- protected void saveConfiguration()
- throws Exception
- {
- repositoryRegistry.setArchivaConfiguration(archivaConfiguration);
- repositoryRegistry.reload();
- saveConfiguration( archivaConfiguration );
-
- }
-
- @Before
- @Override
- public void setUp()
- throws Exception
- {
-
- super.setUp();
-
- System.setProperty( "appserver.base", getAppserverBase().toAbsolutePath().toString());
- log.info("setUp appserverBase={}, projectBase={}, workingDir={}", getAppserverBase(), getProjectBase(), Paths.get("").toString());
-
- repositoryRegistry.getRepositories().stream().forEach(r -> r.close());
-
- org.apache.archiva.common.utils.FileUtils.deleteDirectory( getAppserverBase() );
-
- Path testConf = getProjectBase().resolve( "src/test/resources/repository-archiva.xml" );
- Path testConfDest = getAppserverBase().resolve("conf/archiva.xml" );
- if ( Files.exists(testConfDest) )
- {
- org.apache.archiva.common.utils.FileUtils.deleteQuietly( testConfDest );
- }
- FileUtils.copyFile( testConf.toFile(), testConfDest.toFile() );
-
- repoRootInternal = getAppserverBase().resolve("data/repositories/internal" );
- repoRootLegacy = getAppserverBase().resolve( "data/repositories/legacy" );
- Configuration config = archivaConfiguration.getConfiguration();
-
- config.getManagedRepositories().clear();
-
- config.addManagedRepository(
- createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal, true ) );
- config.getProxyConnectors().clear();
-
- config.getRemoteRepositories().clear();
-
- saveConfiguration( archivaConfiguration );
-
- ArchivaIndexingContext ctx = repositoryRegistry.getManagedRepository( REPOID_INTERNAL ).getIndexingContext( );
- try
- {
- if (repositoryRegistry.getIndexManager(RepositoryType.MAVEN)!=null) {
- repositoryRegistry.getIndexManager(RepositoryType.MAVEN).pack(ctx);
- }
- } finally
- {
- if (ctx!=null)
- {
- ctx.close( );
- }
- }
-
- CacheManager.getInstance().clearAll();
-
-
- }
-
- protected UnauthenticatedRepositoryServlet unauthenticatedRepositoryServlet =
- new UnauthenticatedRepositoryServlet();
-
- protected void startRepository()
- throws Exception
- {
-
- final MockServletContext mockServletContext = new MockServletContext();
-
- WebApplicationContext webApplicationContext =
- new TestWebapplicationContext( applicationContext, mockServletContext );
-
- mockServletContext.setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
- webApplicationContext );
-
- MockServletConfig mockServletConfig = new MockServletConfig()
- {
- @Override
- public ServletContext getServletContext()
- {
- return mockServletContext;
- }
- };
-
- unauthenticatedRepositoryServlet.init( mockServletConfig );
-
- }
-
- protected String createVersionMetadata(String groupId, String artifactId, String version) {
- return createVersionMetadata(groupId, artifactId, version, null, null, null);
- }
-
- protected String createVersionMetadata(String groupId, String artifactId, String version, String timestamp, String buildNumber, String lastUpdated) {
- StringBuilder buf = new StringBuilder();
- buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
- buf.append("<metadata>\n");
- buf.append(" <groupId>").append(groupId).append("</groupId>\n");
- buf.append(" <artifactId>").append(artifactId).append("</artifactId>\n");
- buf.append(" <version>").append(version).append("</version>\n");
- boolean hasSnapshot = StringUtils.isNotBlank(timestamp) || StringUtils.isNotBlank(buildNumber);
- boolean hasLastUpdated = StringUtils.isNotBlank(lastUpdated);
- if (hasSnapshot || hasLastUpdated) {
- buf.append(" <versioning>\n");
- if (hasSnapshot) {
- buf.append(" <snapshot>\n");
- buf.append(" <buildNumber>").append(buildNumber).append("</buildNumber>\n");
- buf.append(" <timestamp>").append(timestamp).append("</timestamp>\n");
- buf.append(" </snapshot>\n");
- }
- if (hasLastUpdated) {
- buf.append(" <lastUpdated>").append(lastUpdated).append("</lastUpdated>\n");
- }
- buf.append(" </versioning>\n");
- }
- buf.append("</metadata>");
- return buf.toString();
- }
-
-
- public static class TestWebapplicationContext
- implements WebApplicationContext
- {
- private ApplicationContext applicationContext;
-
- private ServletContext servletContext;
-
- TestWebapplicationContext( ApplicationContext applicationContext, ServletContext servletContext )
- {
- this.applicationContext = applicationContext;
- }
-
- @Override
- public ServletContext getServletContext()
- {
- return servletContext;
- }
-
- @Override
- public String getId()
- {
- return applicationContext.getId();
- }
-
- @Override
- public String getApplicationName()
- {
- return applicationContext.getApplicationName();
- }
-
- @Override
- public String getDisplayName()
- {
- return applicationContext.getDisplayName();
- }
-
- @Override
- public long getStartupDate()
- {
- return applicationContext.getStartupDate();
- }
-
- @Override
- public ApplicationContext getParent()
- {
- return applicationContext.getParent();
- }
-
- @Override
- public AutowireCapableBeanFactory getAutowireCapableBeanFactory()
- throws IllegalStateException
- {
- return applicationContext.getAutowireCapableBeanFactory();
- }
-
- @Override
- public void publishEvent( ApplicationEvent applicationEvent )
- {
- applicationContext.publishEvent( applicationEvent );
- }
-
- @Override
- public Environment getEnvironment()
- {
- return applicationContext.getEnvironment();
- }
-
- @Override
- public BeanFactory getParentBeanFactory()
- {
- return applicationContext.getParentBeanFactory();
- }
-
- @Override
- public boolean containsLocalBean( String s )
- {
- return applicationContext.containsLocalBean( s );
- }
-
- @Override
- public boolean containsBeanDefinition( String s )
- {
- return applicationContext.containsBeanDefinition( s );
- }
-
- @Override
- public int getBeanDefinitionCount()
- {
- return applicationContext.getBeanDefinitionCount();
- }
-
- @Override
- public String[] getBeanDefinitionNames()
- {
- return applicationContext.getBeanDefinitionNames();
- }
-
- @Override
- public <T> ObjectProvider<T> getBeanProvider( Class<T> aClass, boolean b )
- {
- return null;
- }
-
- @Override
- public <T> ObjectProvider<T> getBeanProvider( ResolvableType resolvableType, boolean b )
- {
- return null;
- }
-
- @Override
- public String[] getBeanNamesForType( Class<?> aClass )
- {
- return applicationContext.getBeanNamesForType( aClass );
- }
-
- @Override
- public String[] getBeanNamesForType( Class<?> aClass, boolean b, boolean b2 )
- {
- return applicationContext.getBeanNamesForType( aClass, b, b2 );
- }
-
- @Override
- public <T> Map<String, T> getBeansOfType( Class<T> tClass )
- throws BeansException
- {
- return applicationContext.getBeansOfType( tClass );
- }
-
- @Override
- public <T> Map<String, T> getBeansOfType( Class<T> tClass, boolean b, boolean b2 )
- throws BeansException
- {
- return applicationContext.getBeansOfType( tClass, b, b2 );
- }
-
- @Override
- public String[] getBeanNamesForAnnotation( Class<? extends Annotation> aClass )
- {
- return applicationContext.getBeanNamesForAnnotation( aClass );
- }
-
- @Override
- public Map<String, Object> getBeansWithAnnotation( Class<? extends Annotation> aClass )
- throws BeansException
- {
- return applicationContext.getBeansWithAnnotation( aClass );
- }
-
- @Override
- public <A extends Annotation> A findAnnotationOnBean( String s, Class<A> aClass )
- throws NoSuchBeanDefinitionException
- {
- return applicationContext.findAnnotationOnBean( s, aClass );
- }
-
- @Override
- public <T> T getBean( Class<T> aClass, Object... objects )
- throws BeansException
- {
- return applicationContext.getBean( aClass, objects );
- }
-
- @Override
- public <T> ObjectProvider<T> getBeanProvider( Class<T> aClass )
- {
- return null;
- }
-
- @Override
- public <T> ObjectProvider<T> getBeanProvider( ResolvableType resolvableType )
- {
- return null;
- }
-
- @Override
- public Object getBean( String s )
- throws BeansException
- {
- return applicationContext.getBean( s );
- }
-
- @Override
- public <T> T getBean( String s, Class<T> tClass )
- throws BeansException
- {
- return applicationContext.getBean( s, tClass );
- }
-
- @Override
- public <T> T getBean( Class<T> tClass )
- throws BeansException
- {
- return applicationContext.getBean( tClass );
- }
-
- @Override
- public Object getBean( String s, Object... objects )
- throws BeansException
- {
- return applicationContext.getBean( s, objects );
- }
-
- @Override
- public boolean containsBean( String s )
- {
- return applicationContext.containsBean( s );
- }
-
- @Override
- public boolean isSingleton( String s )
- throws NoSuchBeanDefinitionException
- {
- return applicationContext.isSingleton( s );
- }
-
- @Override
- public boolean isPrototype( String s )
- throws NoSuchBeanDefinitionException
- {
- return applicationContext.isPrototype( s );
- }
-
- @Override
- public boolean isTypeMatch( String s, Class<?> aClass )
- throws NoSuchBeanDefinitionException
- {
- return applicationContext.isTypeMatch( s, aClass );
- }
-
- @Override
- public Class<?> getType( String s )
- throws NoSuchBeanDefinitionException
- {
- return applicationContext.getType( s );
- }
-
- @Override
- public Class<?> getType( String s, boolean b ) throws NoSuchBeanDefinitionException
- {
- return null;
- }
-
- @Override
- public String[] getAliases( String s )
- {
- return applicationContext.getAliases( s );
- }
-
- @Override
- public String getMessage( String s, Object[] objects, String s2, Locale locale )
- {
- return applicationContext.getMessage( s, objects, s2, locale );
- }
-
- @Override
- public String getMessage( String s, Object[] objects, Locale locale )
- throws NoSuchMessageException
- {
- return applicationContext.getMessage( s, objects, locale );
- }
-
- @Override
- public String getMessage( MessageSourceResolvable messageSourceResolvable, Locale locale )
- throws NoSuchMessageException
- {
- return applicationContext.getMessage( messageSourceResolvable, locale );
- }
-
- @Override
- public Resource[] getResources( String s )
- throws IOException
- {
- return applicationContext.getResources( s );
- }
-
- @Override
- public void publishEvent( Object o )
- {
- // no op
- }
-
- @Override
- public String[] getBeanNamesForType( ResolvableType resolvableType )
- {
- return new String[0];
- }
-
- @Override
- public String[] getBeanNamesForType( ResolvableType resolvableType, boolean b, boolean b1 )
- {
- return new String[0];
- }
-
- @Override
- public boolean isTypeMatch( String s, ResolvableType resolvableType )
- throws NoSuchBeanDefinitionException
- {
- return false;
- }
-
- @Override
- public Resource getResource( String s )
- {
- return applicationContext.getResource( s );
- }
-
- @Override
- public ClassLoader getClassLoader()
- {
- return applicationContext.getClassLoader();
- }
- }
-
- protected Servlet findServlet( String name )
- throws Exception
- {
- return unauthenticatedRepositoryServlet;
-
- }
-
- protected String getSpringConfigLocation()
- {
- return "classpath*:/META-INF/spring-context.xml,classpath*:spring-context.xml";
- }
-
-
- protected static WebClient newClient()
- {
- final WebClient webClient = new WebClient();
- webClient.getOptions().setJavaScriptEnabled( false );
- webClient.getOptions().setCssEnabled( false );
- webClient.getOptions().setAppletEnabled( false );
- webClient.getOptions().setThrowExceptionOnFailingStatusCode( false );
- webClient.setAjaxController( new NicelyResynchronizingAjaxController() );
- return webClient;
- }
-
-
- protected WebResponse getWebResponse( String path )
- throws Exception
- {
- return getWebResponse( new GetMethodWebRequest( "http://localhost" + path ) );//, false );
- }
-
- protected WebResponse getWebResponse( WebRequest webRequest ) //, boolean followRedirect )
- throws Exception
- {
-
- MockHttpServletRequest request = new MockHttpServletRequest();
- request.setRequestURI( webRequest.getUrl().getPath() );
- request.addHeader( "User-Agent", "Apache Archiva unit test" );
-
- request.setMethod( webRequest.getHttpMethod().name() );
-
- if ( webRequest.getHttpMethod() == HttpMethod.PUT )
- {
- PutMethodWebRequest putRequest = PutMethodWebRequest.class.cast( webRequest );
- request.setContentType( putRequest.contentType );
- request.setContent( IOUtils.toByteArray( putRequest.inputStream ) );
- }
-
- if ( webRequest instanceof MkColMethodWebRequest )
- {
- request.setMethod( "MKCOL" );
- }
-
- final MockHttpServletResponse response = execute( request );
-
- if ( response.getStatus() == HttpServletResponse.SC_MOVED_PERMANENTLY
- || response.getStatus() == HttpServletResponse.SC_MOVED_TEMPORARILY )
- {
- String location = response.getHeader( "Location" );
- log.debug( "follow redirect to {}", location );
- return getWebResponse( new GetMethodWebRequest( location ) );
- }
-
- return new WebResponse( null, null, 1 )
- {
- @Override
- public String getContentAsString()
- {
- try
- {
- return response.getContentAsString();
- }
- catch ( UnsupportedEncodingException e )
- {
- throw new RuntimeException( e.getMessage(), e );
- }
- }
-
- @Override
- public int getStatusCode()
- {
- return response.getStatus();
- }
-
- @Override
- public String getResponseHeaderValue( String headerName )
- {
- return response.getHeader( headerName );
- }
- };
- }
-
- protected MockHttpServletResponse execute( HttpServletRequest request )
- throws Exception
- {
- MockHttpServletResponse response = new MockHttpServletResponse()
- {
- @Override
- public String getContentAsString()
- throws UnsupportedEncodingException
- {
- String errorMessage = getErrorMessage();
- return ( errorMessage != null ) ? errorMessage : super.getContentAsString();
- }
- };
- this.unauthenticatedRepositoryServlet.service( request, response );
- return response;
- }
-
- public static class GetMethodWebRequest
- extends WebRequest
- {
- String url;
-
- public GetMethodWebRequest( String url )
- throws Exception
- {
- super( new URL( url ) );
- this.url = url;
-
- }
- }
-
- public static class PutMethodWebRequest
- extends WebRequest
- {
- String url;
-
- InputStream inputStream;
-
- String contentType;
-
- public PutMethodWebRequest( String url, InputStream inputStream, String contentType )
- throws Exception
- {
- super( new URL( url ), HttpMethod.PUT );
- this.url = url;
- this.inputStream = inputStream;
- this.contentType = contentType;
- }
-
-
- }
-
- public static class ServletUnitClient
- {
-
- AbstractRepositoryServletTestCase abstractRepositoryServletTestCase;
-
- public ServletUnitClient( AbstractRepositoryServletTestCase abstractRepositoryServletTestCase )
- {
- this.abstractRepositoryServletTestCase = abstractRepositoryServletTestCase;
- }
-
- public WebResponse getResponse( WebRequest request )
- throws Exception
- {
- return getResponse( request, false );
- }
-
- public WebResponse getResponse( WebRequest request, boolean followRedirect )
- throws Exception
- {
- // alwasy following redirect as it's normal
- return abstractRepositoryServletTestCase.getWebResponse( request );//, followRedirect );
- }
-
- public WebResponse getResource( WebRequest request )
- throws Exception
- {
- return getResponse( request );
- }
- }
-
- public ServletUnitClient getServletUnitClient()
- {
- return new ServletUnitClient( this );
- }
-
- @Override
- @After
- public void tearDown()
- throws Exception
- {
- repositoryRegistry.getRepositories().stream().forEach(r -> r.close());
-
- if ( Files.exists(repoRootInternal) )
- {
- org.apache.archiva.common.utils.FileUtils.deleteQuietly( repoRootInternal );
- }
-
- if ( Files.exists(repoRootLegacy) )
- {
- org.apache.archiva.common.utils.FileUtils.deleteQuietly( repoRootLegacy );
- }
-
- String appserverBase = System.getProperty( "appserver.base" );
- if ( StringUtils.isNotEmpty( appserverBase ) )
- {
- org.apache.archiva.common.utils.FileUtils.deleteQuietly( Paths.get( appserverBase ) );
- }
-
- }
-
-
- protected void assertFileContents( String expectedContents, Path repoRoot, String subpath )
- throws IOException
- {
- String path = Paths.get(subpath).isAbsolute() ? subpath.substring( 1,subpath.length() ) : subpath;
- Path actualFile = repoRoot.resolve( path );
- assertTrue( "File <" + actualFile.toAbsolutePath() + "> should exist.", Files.exists(actualFile) );
- assertTrue( "File <" + actualFile.toAbsolutePath() + "> should be a file (not a dir/link/device/etc).",
- Files.isRegularFile( actualFile ) );
-
- String actualContents = org.apache.archiva.common.utils.FileUtils.readFileToString( actualFile, Charset.defaultCharset() );
- assertEquals( "File Contents of <" + actualFile.toAbsolutePath() + ">", expectedContents, actualContents );
- }
-
- protected void assertRepositoryValid( RepositoryServlet servlet, String repoId )
- throws Exception
- {
- ManagedRepository repository = servlet.getRepository( repoId );
- assertNotNull( "Archiva Managed Repository id:<" + repoId + "> should exist.", repository );
- Path repoRoot = Paths.get( repository.getLocation() );
- assertTrue( "Archiva Managed Repository id:<" + repoId + "> should have a valid location on disk.",
- Files.exists(repoRoot) && Files.isDirectory(repoRoot) );
- }
-
- protected void assertResponseOK( WebResponse response )
- {
- assertNotNull( "Should have recieved a response", response );
- Assert.assertEquals( "Should have been an OK response code", //
- HttpServletResponse.SC_OK, //
- response.getStatusCode() );
- }
-
- protected void assertResponseOK( WebResponse response, String path )
- {
- assertNotNull( "Should have recieved a response", response );
- Assert.assertEquals( "Should have been an OK response code for path: " + path, HttpServletResponse.SC_OK,
- response.getStatusCode() );
- }
-
- protected void assertResponseNotFound( WebResponse response )
- {
- assertNotNull( "Should have recieved a response", response );
- Assert.assertEquals( "Should have been an 404/Not Found response code.", HttpServletResponse.SC_NOT_FOUND,
- response.getStatusCode() );
- }
-
- protected void assertResponseInternalServerError( WebResponse response )
- {
- assertNotNull( "Should have recieved a response", response );
- Assert.assertEquals( "Should have been an 500/Internal Server Error response code.",
- HttpServletResponse.SC_INTERNAL_SERVER_ERROR, response.getStatusCode() );
- }
-
- protected void assertResponseConflictError( WebResponse response )
- {
- assertNotNull( "Should have received a response", response );
- Assert.assertEquals( "Should have been a 409/Conflict response code.", HttpServletResponse.SC_CONFLICT,
- response.getStatusCode() );
- }
-
- protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, Path location,
- boolean blockRedeployments )
- {
- ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
- repo.setId( id );
- repo.setName( name );
- repo.setLocation( location.toAbsolutePath().toString() );
- repo.setBlockRedeployments( blockRedeployments );
- repo.setType( "MAVEN" );
- repo.setIndexDir(".indexer");
- repo.setPackedIndexDir(".index");
-
- return repo;
- }
-
- protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, Path location,
- String layout, boolean blockRedeployments )
- {
- ManagedRepositoryConfiguration repo = createManagedRepository( id, name, location, blockRedeployments );
- repo.setLayout( layout );
- return repo;
- }
-
- protected RemoteRepositoryConfiguration createRemoteRepository( String id, String name, String url )
- {
- RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration();
- repo.setId( id );
- repo.setName( name );
- repo.setUrl( url );
- return repo;
- }
-
- protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
- throws Exception
- {
- repositoryRegistry.setArchivaConfiguration(archivaConfiguration);
- // repositoryRegistry.reload();
- archivaConfiguration.save( archivaConfiguration.getConfiguration() );
-
- }
-
-
- protected void setupCleanRepo( Path repoRootDir )
- throws IOException
- {
- org.apache.archiva.common.utils.FileUtils.deleteDirectory( repoRootDir );
- if ( !Files.exists(repoRootDir) )
- {
- Files.createDirectories( repoRootDir );
- }
- }
-
- protected void assertManagedFileNotExists( Path repoRootInternal, String resourcePath )
- {
- Path repoFile = repoRootInternal.resolve( resourcePath );
- assertFalse( "Managed Repository File <" + repoFile.toAbsolutePath() + "> should not exist.",
- Files.exists(repoFile) );
- }
-
- protected void setupCleanInternalRepo()
- throws Exception
- {
- setupCleanRepo( repoRootInternal );
- }
-
- protected Path populateRepo( Path repoRootManaged, String path, String contents )
- throws Exception
- {
- Path destFile = repoRootManaged.resolve( path );
- Files.createDirectories( destFile.getParent() );
- org.apache.archiva.common.utils.FileUtils.writeStringToFile( destFile, Charset.defaultCharset(), contents );
- return destFile;
- }
- }
|