[MRM-577] Release policy of disabled fails all metadata requests.

Fixing release / snapshot policies from applying tests on maven-metadata.xml files.
Creating unit tests for release / snapshot policies.
Switching from boolean return on .applyPolicy() to throwing exception, to gain better logging of why the transfer failed.
Logging policy failures better.



git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@590858 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Joakim Erdfelt 2007-10-31 22:47:50 +00:00
parent b62b18f378
commit 255949579d
13 changed files with 913 additions and 100 deletions

View File

@ -76,7 +76,7 @@ public abstract class AbstractUpdatePolicy
*/
public static final String ONCE = "once";
private List options = new ArrayList();
private List<String> options = new ArrayList<String>();
public AbstractUpdatePolicy()
{
@ -91,13 +91,20 @@ public abstract class AbstractUpdatePolicy
protected abstract String getUpdateMode();
public List getOptions()
public List<String> getOptions()
{
return options;
}
public boolean applyPolicy( String policySetting, Properties request, File localFile )
public void applyPolicy( String policySetting, Properties request, File localFile )
throws PolicyViolationException, PolicyConfigurationException
{
if ( !StringUtils.equals( request.getProperty( "filetype" ), "artifact" ) )
{
// Only process artifact file types.
return;
}
String version = request.getProperty( "version", "" );
boolean isSnapshotVersion = false;
@ -108,50 +115,48 @@ public abstract class AbstractUpdatePolicy
if ( !options.contains( policySetting ) )
{
// No valid code? false it is then.
getLogger().error( "Unknown artifact-update policyCode [" + policySetting + "]" );
return false;
// Not a valid code.
throw new PolicyConfigurationException( "Unknown " + getUpdateMode() + " policy setting [" + policySetting
+ "], valid settings are [" + StringUtils.join( options.iterator(), "," ) + "]" );
}
if ( IGNORED.equals( policySetting ) )
{
// Ignored means ok to update.
getLogger().debug( "OK to update, " + getUpdateMode() + " policy set to IGNORED." );
return true;
return;
}
// Test for mismatches.
if ( !isSnapshotVersion && isSnapshotPolicy() )
{
getLogger().debug( "OK to update, snapshot policy does not apply for non-snapshot versions." );
return true;
return;
}
if ( isSnapshotVersion && !isSnapshotPolicy() )
{
getLogger().debug( "OK to update, release policy does not apply for snapshot versions." );
return true;
return;
}
if ( DISABLED.equals( policySetting ) )
{
// Disabled means no.
getLogger().debug( "NO to update, " + getUpdateMode() + " policy set to DISABLED." );
return false;
throw new PolicyViolationException( "NO to update, " + getUpdateMode() + " policy set to DISABLED." );
}
if ( !localFile.exists() )
{
// No file means it's ok.
getLogger().debug( "OK to update " + getUpdateMode() + ", local file does not exist." );
return true;
return;
}
if ( ONCE.equals( policySetting ) )
{
// File exists, but policy is once.
getLogger().debug( "NO to update" + getUpdateMode() + ", local file exist (and policy is ONCE)." );
return false;
throw new PolicyViolationException( "NO to update " + getUpdateMode() + ", policy is ONCE, and local file exist." );
}
if ( DAILY.equals( policySetting ) )
@ -161,7 +166,16 @@ public abstract class AbstractUpdatePolicy
Calendar fileCal = Calendar.getInstance();
fileCal.setTimeInMillis( localFile.lastModified() );
return cal.after( fileCal );
if( cal.after( fileCal ) )
{
// Its ok.
return;
}
else
{
throw new PolicyViolationException( "NO to update " + getUpdateMode()
+ ", policy is DAILY, local file exist, and has been updated within the last day." );
}
}
if ( HOURLY.equals( policySetting ) )
@ -171,10 +185,19 @@ public abstract class AbstractUpdatePolicy
Calendar fileCal = Calendar.getInstance();
fileCal.setTimeInMillis( localFile.lastModified() );
return cal.after( fileCal );
if( cal.after( fileCal ) )
{
// Its ok.
return;
}
else
{
throw new PolicyViolationException( "NO to update " + getUpdateMode()
+ ", policy is HOURLY, local file exist, and has been updated within the last hour." );
}
}
getLogger().error( "Unhandled policyCode [" + policySetting + "]" );
return false;
throw new PolicyConfigurationException( "Unable to process " + getUpdateMode()
+ " policy of [" + policySetting + "], please file a bug report." );
}
}

View File

@ -52,7 +52,7 @@ public class CachedFailuresPolicy
*/
private UrlFailureCache urlFailureCache;
private List options = new ArrayList();
private List<String> options = new ArrayList<String>();
public CachedFailuresPolicy()
{
@ -60,20 +60,21 @@ public class CachedFailuresPolicy
options.add( CACHED );
}
public boolean applyPolicy( String policySetting, Properties request, File localFile )
public void applyPolicy( String policySetting, Properties request, File localFile )
throws PolicyViolationException, PolicyConfigurationException
{
if ( !options.contains( policySetting ) )
{
// No valid code? false it is then.
getLogger().error( "Unknown check-failures policyCode [" + policySetting + "]" );
return false;
// Not a valid code.
throw new PolicyConfigurationException( "Unknown cache-failues policy setting [" + policySetting
+ "], valid settings are [" + StringUtils.join( options.iterator(), "," ) + "]" );
}
if ( IGNORED.equals( policySetting ) )
{
// Ignore.
getLogger().debug( "OK to fetch, check-failures policy set to IGNORED." );
return true;
return;
}
String url = request.getProperty( "url" );
@ -82,14 +83,11 @@ public class CachedFailuresPolicy
{
if ( urlFailureCache.hasFailedBefore( url ) )
{
getLogger().debug( "NO to fetch, check-failures detected previous failure on url: " + url );
return false;
throw new PolicyViolationException( "NO to fetch, check-failures detected previous failure on url: " + url );
}
}
getLogger().debug( "OK to fetch, check-failures detected no issues." );
return true;
}
public String getDefaultOption()
@ -102,7 +100,7 @@ public class CachedFailuresPolicy
return "cache-failures";
}
public List getOptions()
public List<String> getOptions()
{
return options;
}

View File

@ -19,6 +19,7 @@ package org.apache.maven.archiva.policies;
* under the License.
*/
import org.apache.commons.lang.StringUtils;
import org.apache.maven.archiva.common.utils.Checksums;
import org.codehaus.plexus.logging.AbstractLogEnabled;
@ -28,7 +29,8 @@ import java.util.List;
import java.util.Properties;
/**
* ChecksumPolicy
* ChecksumPolicy - a policy applied after the download to see if the file has been downloaded
* successfully and completely (or not).
*
* @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
* @version $Id$
@ -68,60 +70,70 @@ public class ChecksumPolicy
options.add( IGNORED );
}
public boolean applyPolicy( String policySetting, Properties request, File localFile )
public void applyPolicy( String policySetting, Properties request, File localFile )
throws PolicyViolationException, PolicyConfigurationException
{
if ( !options.contains( policySetting ) )
{
// No valid code? false it is then.
getLogger().error( "Unknown checksum policyCode [" + policySetting + "]" );
return false;
// Not a valid code.
throw new PolicyConfigurationException( "Unknown checksum policy setting [" + policySetting
+ "], valid settings are [" + StringUtils.join( options.iterator(), "," ) + "]" );
}
if ( IGNORED.equals( policySetting ) )
{
// Ignore.
return true;
return;
}
if ( !localFile.exists() )
{
// Local File does not exist.
getLogger().debug( "Local file " + localFile.getAbsolutePath() + " does not exist." );
return false;
throw new PolicyViolationException( "Checksum policy failure, local file " + localFile.getAbsolutePath()
+ " does not exist to check." );
}
if ( FAIL.equals( policySetting ) )
{
boolean checksPass = checksums.check( localFile );
if( ! checksPass )
if( checksums.check( localFile ) )
{
File sha1File = new File( localFile.getAbsolutePath() + ".sha1" );
File md5File = new File( localFile.getAbsolutePath() + ".md5" );
return;
}
File sha1File = new File( localFile.getAbsolutePath() + ".sha1" );
File md5File = new File( localFile.getAbsolutePath() + ".md5" );
// On failure. delete files.
if ( sha1File.exists() )
{
sha1File.delete();
}
if ( md5File.exists() )
{
md5File.delete();
}
localFile.delete();
// On failure. delete files.
if ( sha1File.exists() )
{
sha1File.delete();
}
return checksPass;
if ( md5File.exists() )
{
md5File.delete();
}
localFile.delete();
throw new PolicyViolationException( "Checksums do not match, policy set to FAIL, "
+ "deleting checksum files and local file " + localFile.getAbsolutePath() + "." );
}
if ( FIX.equals( policySetting ) )
{
return checksums.update( localFile );
if( checksums.update( localFile ) )
{
return;
}
else
{
throw new PolicyViolationException( "Checksum policy set to FIX, "
+ "yet unable to update checksums for local file " + localFile.getAbsolutePath() + "." );
}
}
getLogger().error( "Unhandled policyCode [" + policySetting + "]" );
return false;
throw new PolicyConfigurationException( "Unable to process checksum policy of [" + policySetting
+ "], please file a bug report." );
}
public String getDefaultOption()

View File

@ -67,7 +67,8 @@ public interface DownloadPolicy
* @param request the list of request properties that the policy might use.
* @param localFile
*
* @return true if the policy passes.
* @throws PolicyViolationException if the policy has been violated.
*/
public boolean applyPolicy( String policySetting, Properties request, File localFile );
public void applyPolicy( String policySetting, Properties request, File localFile )
throws PolicyViolationException, PolicyConfigurationException;
}

View File

@ -0,0 +1,44 @@
package org.apache.maven.archiva.policies;
/*
* 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.maven.archiva.common.ArchivaException;
/**
* PolicyConfigurationException is thrown when a policy cannot be executed due to a
* configuration issue.
*
* @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
* @version $Id$
*/
public class PolicyConfigurationException
extends ArchivaException
{
public PolicyConfigurationException( String message, Throwable cause )
{
super( message, cause );
}
public PolicyConfigurationException( String message )
{
super( message );
}
}

View File

@ -19,26 +19,24 @@ package org.apache.maven.archiva.policies;
* under the License.
*/
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.maven.archiva.common.ArchivaException;
/**
* Helper class for IDE testing.
* PolicyViolationException
*
* @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
* @version $Id$
*/
public class AllTests
public class PolicyViolationException
extends ArchivaException
{
public static Test suite()
public PolicyViolationException( String message, Throwable cause )
{
TestSuite suite = new TestSuite( "Test for org.apache.maven.archiva.policies" );
//$JUnit-BEGIN$
suite.addTestSuite( ChecksumPolicyTest.class );
suite.addTestSuite( CachedFailuresPolicyTest.class );
//$JUnit-END$
return suite;
super( message, cause );
}
public PolicyViolationException( String message )
{
super( message );
}
}

View File

@ -19,7 +19,6 @@ package org.apache.maven.archiva.policies;
* under the License.
*/
import java.io.File;
import java.util.Properties;
@ -30,7 +29,8 @@ import java.util.Properties;
* @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
* @version $Id$
*/
public interface PostDownloadPolicy extends DownloadPolicy
public interface PostDownloadPolicy
extends DownloadPolicy
{
/**
* Apply the download policy.
@ -42,7 +42,8 @@ public interface PostDownloadPolicy extends DownloadPolicy
* @param request the list of request properties that the policy might use.
* @param localFile the local file that this policy affects
*
* @return true if the policy passes. false if not.
* @throws PolicyViolationException if the policy has been violated.
*/
public boolean applyPolicy( String policySetting, Properties request, File localFile );
public void applyPolicy( String policySetting, Properties request, File localFile )
throws PolicyViolationException, PolicyConfigurationException;
}

View File

@ -41,7 +41,8 @@ public interface PreDownloadPolicy extends DownloadPolicy
* @param request the list of request properties that the policy might use.
* @param localFile the local file that this policy affects
*
* @return true if the policy passes. false if not.
* @throws PolicyViolationException if the policy has been violated.
*/
public boolean applyPolicy( String policySetting, Properties request, File localFile );
public void applyPolicy( String policySetting, Properties request, File localFile )
throws PolicyViolationException, PolicyConfigurationException;
}

View File

@ -67,7 +67,7 @@ public class CachedFailuresPolicyTest
request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
assertTrue( policy.applyPolicy( CachedFailuresPolicy.IGNORED, request, localFile ) );
policy.applyPolicy( CachedFailuresPolicy.IGNORED, request, localFile );
}
public void testCachedNotInCache()
@ -79,7 +79,7 @@ public class CachedFailuresPolicyTest
request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );
assertTrue( policy.applyPolicy( CachedFailuresPolicy.CACHED, request, localFile ) );
policy.applyPolicy( CachedFailuresPolicy.CACHED, request, localFile );
}
public void testCachedInCache()
@ -97,6 +97,14 @@ public class CachedFailuresPolicyTest
request.setProperty( "url", url );
assertFalse( policy.applyPolicy( CachedFailuresPolicy.CACHED, request, localFile ) );
try
{
policy.applyPolicy( CachedFailuresPolicy.CACHED, request, localFile );
fail( "Expected a PolicyViolationException." );
}
catch ( PolicyViolationException e )
{
// expected path.
}
}
}

View File

@ -155,7 +155,7 @@ public class ChecksumPolicyTest
File localFile = createTestableFiles( null, null );
Properties request = createRequest();
assertTrue( policy.applyPolicy( ChecksumPolicy.IGNORED, request, localFile ) );
policy.applyPolicy( ChecksumPolicy.IGNORED, request, localFile );
}
private void assertFailSetting( boolean expectedResult, String md5State, String sha1State )
@ -165,11 +165,18 @@ public class ChecksumPolicyTest
File localFile = createTestableFiles( md5State, sha1State );
Properties request = createRequest();
boolean actualResult = policy.applyPolicy( ChecksumPolicy.FAIL, request, localFile );
String msg = createMessage( ChecksumPolicy.FAIL, md5State, sha1State );
if ( actualResult == false )
boolean actualResult;
try
{
policy.applyPolicy( ChecksumPolicy.FAIL, request, localFile );
actualResult = true;
}
catch ( PolicyViolationException e )
{
actualResult = false;
String msg = createMessage( ChecksumPolicy.FAIL, md5State, sha1State );
assertFalse( msg + " local file should not exist:", localFile.exists() );
File md5File = new File( localFile.getAbsolutePath() + ".sha1" );
File sha1File = new File( localFile.getAbsolutePath() + ".md5" );
@ -187,7 +194,18 @@ public class ChecksumPolicyTest
File localFile = createTestableFiles( md5State, sha1State );
Properties request = createRequest();
boolean actualResult = policy.applyPolicy( ChecksumPolicy.FIX, request, localFile );
boolean actualResult;
try
{
policy.applyPolicy( ChecksumPolicy.FIX, request, localFile );
actualResult = true;
}
catch ( PolicyViolationException e )
{
actualResult = false;
}
assertEquals( createMessage( ChecksumPolicy.FIX, md5State, sha1State ), expectedResult, actualResult );
// End result should be legitimate SHA1 and MD5 files.
@ -278,8 +296,8 @@ public class ChecksumPolicyTest
private File createTestableFiles( String md5State, String sha1State )
throws Exception
{
File sourceDir = new File( "src/test/resources/checksums/" );
File destDir = new File( "target/checksum-tests/" + getName() + "/" );
File sourceDir = getTestFile( "src/test/resources/checksums/" );
File destDir = getTestFile( "target/checksum-tests/" + getName() + "/" );
FileUtils.copyFileToDirectory( new File( sourceDir, "artifact.jar" ), destDir );

View File

@ -0,0 +1,347 @@
package org.apache.maven.archiva.policies;
/*
* 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.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.util.FileUtils;
import java.io.File;
import java.util.Properties;
/**
* ReleasePolicyTest
*
* @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
* @version $Id$
*/
public class ReleasePolicyTest
extends PlexusTestCase
{
private static final String PATH_VERSION_METADATA = "org/apache/archiva/archiva-testable/1.0-SNAPSHOT/maven-metadata.xml";
private static final String PATH_PROJECT_METADATA = "org/apache/archiva/archiva-testable/maven-metadata.xml";
private static final String PATH_SNAPSHOT_ARTIFACT = "org/apache/archiva/archiva-testable/1.0-SNAPSHOT/archiva-testable-1.0-SNAPSHOT.jar";
private static final String PATH_RELEASE_ARTIFACT = "org/apache/archiva/archiva-testable/2.0/archiva-testable-2.0.jar";
private static final boolean WITH_LOCAL = true;
private static final boolean NO_LOCAL = false;
protected static final long ONE_SECOND = ( 1000 /* milliseconds */);
protected static final long ONE_MINUTE = ( ONE_SECOND * 60 );
protected static final long ONE_HOUR = ( ONE_MINUTE * 60 );
protected static final long ONE_DAY = ( ONE_HOUR * 24 );
protected static final long OVER_ONE_HOUR = ( ONE_HOUR + ONE_MINUTE );
protected static final long OVER_ONE_DAY = ( ONE_DAY + ONE_HOUR );
protected static final long OLDER = ( -1 );
protected static final long NEWER = 0;
private long generatedLocalFileUpdateDelta = 0;
public void testReleasePolicyDailyProjectMetadata()
throws Exception
{
// Pass the policy when working with metadata, no matter what.
assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
generatedLocalFileUpdateDelta = OVER_ONE_DAY;
assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
}
public void testReleasePolicyDailyReleaseArtifact()
throws Exception
{
assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
assertReleasesPolicyViolation( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
generatedLocalFileUpdateDelta = OVER_ONE_DAY;
assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
assertReleasesPolicyViolation( ReleasesPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
}
public void testReleasePolicyDailySnapshotArtifact()
throws Exception
{
assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
generatedLocalFileUpdateDelta = OVER_ONE_DAY;
assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
}
public void testReleasePolicyDailyVersionedMetadata()
throws Exception
{
// Pass the policy when working with metadata, no matter what.
assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
generatedLocalFileUpdateDelta = OVER_ONE_DAY;
assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
}
public void testReleasePolicyDisabledProjectMetadata()
throws Exception
{
// Pass the policy when working with metadata, no matter what.
assertReleasesPolicy( ReleasesPolicy.DISABLED, PATH_PROJECT_METADATA, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.DISABLED, PATH_PROJECT_METADATA, WITH_LOCAL );
}
public void testReleasePolicyDisabledReleaseArtifact()
throws Exception
{
assertReleasesPolicyViolation( ReleasesPolicy.DISABLED, PATH_RELEASE_ARTIFACT, NO_LOCAL );
assertReleasesPolicyViolation( ReleasesPolicy.DISABLED, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
}
public void testReleasePolicyDisabledSnapshotArtifact()
throws Exception
{
assertReleasesPolicy( ReleasesPolicy.DISABLED, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.DISABLED, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
}
public void testReleasePolicyDisabledVersionedMetadata()
throws Exception
{
// Pass the policy when working with metadata, no matter what.
assertReleasesPolicy( ReleasesPolicy.DISABLED, PATH_VERSION_METADATA, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.DISABLED, PATH_VERSION_METADATA, WITH_LOCAL );
}
public void testReleasePolicyHourlyProjectMetadata()
throws Exception
{
// Pass the policy when working with metadata, no matter what.
assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
}
public void testReleasePolicyHourlyReleaseArtifact()
throws Exception
{
assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
assertReleasesPolicyViolation( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
assertReleasesPolicyViolation( ReleasesPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
}
public void testReleasePolicyHourlySnapshotArtifact()
throws Exception
{
assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
}
public void testReleasePolicyHourlyVersionedMetadata()
throws Exception
{
// Pass the policy when working with metadata, no matter what.
assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
}
public void testReleasePolicyIgnoredProjectMetadata()
throws Exception
{
// Pass the policy when working with metadata, no matter what.
assertReleasesPolicy( ReleasesPolicy.IGNORED, PATH_PROJECT_METADATA, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.IGNORED, PATH_PROJECT_METADATA, WITH_LOCAL );
}
public void testReleasePolicyIgnoredReleaseArtifact()
throws Exception
{
assertReleasesPolicy( ReleasesPolicy.IGNORED, PATH_RELEASE_ARTIFACT, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.IGNORED, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
}
public void testReleasePolicyIgnoredSnapshotArtifact()
throws Exception
{
assertReleasesPolicy( ReleasesPolicy.IGNORED, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.IGNORED, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
}
public void testReleasePolicyIgnoredVersionedMetadata()
throws Exception
{
// Pass the policy when working with metadata, no matter what.
assertReleasesPolicy( ReleasesPolicy.IGNORED, PATH_VERSION_METADATA, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.IGNORED, PATH_VERSION_METADATA, WITH_LOCAL );
}
public void testReleasePolicyOnceProjectMetadata()
throws Exception
{
// Pass the policy when working with metadata, no matter what.
assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_PROJECT_METADATA, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_PROJECT_METADATA, WITH_LOCAL );
}
public void testReleasePolicyOnceReleaseArtifact()
throws Exception
{
assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_RELEASE_ARTIFACT, NO_LOCAL );
assertReleasesPolicyViolation( ReleasesPolicy.ONCE, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
}
public void testReleasePolicyOnceSnapshotArtifact()
throws Exception
{
assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
}
public void testReleasePolicyOnceVersionedMetadata()
throws Exception
{
// Pass the policy when working with metadata, no matter what.
assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_VERSION_METADATA, NO_LOCAL );
assertReleasesPolicy( ReleasesPolicy.ONCE, PATH_VERSION_METADATA, WITH_LOCAL );
}
private void assertReleasesPolicy( String setting, String path, boolean createLocalFile )
throws Exception
{
PreDownloadPolicy policy = lookupPolicy();
Properties request = new Properties();
request.setProperty( "filetype", path.endsWith( "/maven-metadata.xml" ) ? "metadata" : "artifact" );
if ( path.contains( "1.0-SNAPSHOT" ) )
{
request.setProperty( "version", "1.0-SNAPSHOT" );
}
if ( path.contains( "2.0" ) )
{
request.setProperty( "version", "2.0" );
}
File targetDir = getTestFile( "target/test-policy/" );
File localFile = new File( targetDir, path );
if ( localFile.exists() )
{
localFile.delete();
}
if ( createLocalFile )
{
localFile.getParentFile().mkdirs();
FileUtils.fileWrite( localFile.getAbsolutePath(), "random-junk" );
localFile.setLastModified( localFile.lastModified() - generatedLocalFileUpdateDelta );
}
policy.applyPolicy( setting, request, localFile );
}
private void assertReleasesPolicyViolation( String setting, String path, boolean createLocalFile )
throws Exception
{
try
{
assertReleasesPolicy( setting, path, createLocalFile );
fail( "Expected a PolicyViolationException." );
}
catch ( PolicyViolationException e )
{
// expected path.
}
}
private PreDownloadPolicy lookupPolicy()
throws Exception
{
PreDownloadPolicy policy = (PreDownloadPolicy) lookup( PreDownloadPolicy.class.getName(), "releases" );
assertNotNull( policy );
return policy;
}
@Override
protected void setUp()
throws Exception
{
super.setUp();
// reset delta to 0.
generatedLocalFileUpdateDelta = 0;
}
}

View File

@ -0,0 +1,347 @@
package org.apache.maven.archiva.policies;
/*
* 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.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.util.FileUtils;
import java.io.File;
import java.util.Properties;
/**
* SnapshotsPolicyTest
*
* @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
* @version $Id$
*/
public class SnapshotsPolicyTest
extends PlexusTestCase
{
private static final String PATH_VERSION_METADATA = "org/apache/archiva/archiva-testable/1.0-SNAPSHOT/maven-metadata.xml";
private static final String PATH_PROJECT_METADATA = "org/apache/archiva/archiva-testable/maven-metadata.xml";
private static final String PATH_SNAPSHOT_ARTIFACT = "org/apache/archiva/archiva-testable/1.0-SNAPSHOT/archiva-testable-1.0-SNAPSHOT.jar";
private static final String PATH_RELEASE_ARTIFACT = "org/apache/archiva/archiva-testable/2.0/archiva-testable-2.0.jar";
private static final boolean WITH_LOCAL = true;
private static final boolean NO_LOCAL = false;
protected static final long ONE_SECOND = ( 1000 /* milliseconds */);
protected static final long ONE_MINUTE = ( ONE_SECOND * 60 );
protected static final long ONE_HOUR = ( ONE_MINUTE * 60 );
protected static final long ONE_DAY = ( ONE_HOUR * 24 );
protected static final long OVER_ONE_HOUR = ( ONE_HOUR + ONE_MINUTE );
protected static final long OVER_ONE_DAY = ( ONE_DAY + ONE_HOUR );
protected static final long OLDER = ( -1 );
protected static final long NEWER = 0;
private long generatedLocalFileUpdateDelta = 0;
public void testSnapshotPolicyDailyProjectMetadata()
throws Exception
{
// Pass the policy when working with metadata, no matter what.
assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
generatedLocalFileUpdateDelta = OVER_ONE_DAY;
assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_PROJECT_METADATA, WITH_LOCAL );
}
public void testSnapshotPolicyDailyReleaseArtifact()
throws Exception
{
assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
generatedLocalFileUpdateDelta = OVER_ONE_DAY;
assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
}
public void testSnapshotPolicyDailySnapshotArtifact()
throws Exception
{
assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
assertSnapshotPolicyViolation( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
generatedLocalFileUpdateDelta = OVER_ONE_DAY;
assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
assertSnapshotPolicyViolation( SnapshotsPolicy.DAILY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
}
public void testSnapshotPolicyDailyVersionedMetadata()
throws Exception
{
// Pass the policy when working with metadata, no matter what.
assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
generatedLocalFileUpdateDelta = OVER_ONE_DAY;
assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
generatedLocalFileUpdateDelta = ( ONE_HOUR * 22 );
assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.DAILY, PATH_VERSION_METADATA, WITH_LOCAL );
}
public void testSnapshotPolicyDisabledProjectMetadata()
throws Exception
{
// Pass the policy when working with metadata, no matter what.
assertSnapshotPolicy( SnapshotsPolicy.DISABLED, PATH_PROJECT_METADATA, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.DISABLED, PATH_PROJECT_METADATA, WITH_LOCAL );
}
public void testSnapshotPolicyDisabledReleaseArtifact()
throws Exception
{
assertSnapshotPolicy( SnapshotsPolicy.DISABLED, PATH_RELEASE_ARTIFACT, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.DISABLED, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
}
public void testSnapshotPolicyDisabledSnapshotArtifact()
throws Exception
{
assertSnapshotPolicyViolation( SnapshotsPolicy.DISABLED, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
assertSnapshotPolicyViolation( SnapshotsPolicy.DISABLED, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
}
public void testSnapshotPolicyDisabledVersionedMetadata()
throws Exception
{
// Pass the policy when working with metadata, no matter what.
assertSnapshotPolicy( SnapshotsPolicy.DISABLED, PATH_VERSION_METADATA, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.DISABLED, PATH_VERSION_METADATA, WITH_LOCAL );
}
public void testSnapshotPolicyHourlyProjectMetadata()
throws Exception
{
// Pass the policy when working with metadata, no matter what.
assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_PROJECT_METADATA, WITH_LOCAL );
}
public void testSnapshotPolicyHourlyReleaseArtifact()
throws Exception
{
assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
}
public void testSnapshotPolicyHourlySnapshotArtifact()
throws Exception
{
assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
assertSnapshotPolicyViolation( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
assertSnapshotPolicyViolation( SnapshotsPolicy.HOURLY, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
}
public void testSnapshotPolicyHourlyVersionedMetadata()
throws Exception
{
// Pass the policy when working with metadata, no matter what.
assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
generatedLocalFileUpdateDelta = OVER_ONE_HOUR;
assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
generatedLocalFileUpdateDelta = ( ONE_MINUTE * 45 );
assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.HOURLY, PATH_VERSION_METADATA, WITH_LOCAL );
}
public void testSnapshotPolicyIgnoredProjectMetadata()
throws Exception
{
// Pass the policy when working with metadata, no matter what.
assertSnapshotPolicy( SnapshotsPolicy.IGNORED, PATH_PROJECT_METADATA, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.IGNORED, PATH_PROJECT_METADATA, WITH_LOCAL );
}
public void testSnapshotPolicyIgnoredReleaseArtifact()
throws Exception
{
assertSnapshotPolicy( SnapshotsPolicy.IGNORED, PATH_RELEASE_ARTIFACT, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.IGNORED, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
}
public void testSnapshotPolicyIgnoredSnapshotArtifact()
throws Exception
{
assertSnapshotPolicy( SnapshotsPolicy.IGNORED, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.IGNORED, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
}
public void testSnapshotPolicyIgnoredVersionedMetadata()
throws Exception
{
// Pass the policy when working with metadata, no matter what.
assertSnapshotPolicy( SnapshotsPolicy.IGNORED, PATH_VERSION_METADATA, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.IGNORED, PATH_VERSION_METADATA, WITH_LOCAL );
}
public void testSnapshotPolicyOnceProjectMetadata()
throws Exception
{
// Pass the policy when working with metadata, no matter what.
assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_PROJECT_METADATA, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_PROJECT_METADATA, WITH_LOCAL );
}
public void testSnapshotPolicyOnceReleaseArtifact()
throws Exception
{
assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_RELEASE_ARTIFACT, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_RELEASE_ARTIFACT, WITH_LOCAL );
}
public void testSnapshotPolicyOnceSnapshotArtifact()
throws Exception
{
assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_SNAPSHOT_ARTIFACT, NO_LOCAL );
assertSnapshotPolicyViolation( SnapshotsPolicy.ONCE, PATH_SNAPSHOT_ARTIFACT, WITH_LOCAL );
}
public void testSnapshotPolicyOnceVersionedMetadata()
throws Exception
{
// Pass the policy when working with metadata, no matter what.
assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_VERSION_METADATA, NO_LOCAL );
assertSnapshotPolicy( SnapshotsPolicy.ONCE, PATH_VERSION_METADATA, WITH_LOCAL );
}
private void assertSnapshotPolicy( String setting, String path, boolean createLocalFile )
throws Exception
{
PreDownloadPolicy policy = lookupPolicy();
Properties request = new Properties();
request.setProperty( "filetype", path.endsWith( "/maven-metadata.xml" ) ? "metadata" : "artifact" );
if ( path.contains( "1.0-SNAPSHOT" ) )
{
request.setProperty( "version", "1.0-SNAPSHOT" );
}
if ( path.contains( "2.0" ) )
{
request.setProperty( "version", "2.0" );
}
File targetDir = getTestFile( "target/test-policy/" );
File localFile = new File( targetDir, path );
if ( localFile.exists() )
{
localFile.delete();
}
if ( createLocalFile )
{
localFile.getParentFile().mkdirs();
FileUtils.fileWrite( localFile.getAbsolutePath(), "random-junk" );
localFile.setLastModified( localFile.lastModified() - generatedLocalFileUpdateDelta );
}
policy.applyPolicy( setting, request, localFile );
}
private void assertSnapshotPolicyViolation( String setting, String path, boolean createLocalFile )
throws Exception
{
try
{
assertSnapshotPolicy( setting, path, createLocalFile );
fail( "Expected a PolicyViolationException." );
}
catch ( PolicyViolationException e )
{
// expected path.
}
}
private PreDownloadPolicy lookupPolicy()
throws Exception
{
PreDownloadPolicy policy = (PreDownloadPolicy) lookup( PreDownloadPolicy.class.getName(), "snapshots" );
assertNotNull( policy );
return policy;
}
@Override
protected void setUp()
throws Exception
{
super.setUp();
// reset delta to 0.
generatedLocalFileUpdateDelta = 0;
}
}

View File

@ -32,6 +32,8 @@ import org.apache.maven.archiva.model.ProjectReference;
import org.apache.maven.archiva.model.RepositoryURL;
import org.apache.maven.archiva.model.VersionedReference;
import org.apache.maven.archiva.policies.DownloadPolicy;
import org.apache.maven.archiva.policies.PolicyConfigurationException;
import org.apache.maven.archiva.policies.PolicyViolationException;
import org.apache.maven.archiva.policies.PostDownloadPolicy;
import org.apache.maven.archiva.policies.PreDownloadPolicy;
import org.apache.maven.archiva.policies.urlcache.UrlFailureCache;
@ -140,6 +142,7 @@ public class DefaultRepositoryProxyConnectors
File localFile = toLocalFile( repository, artifact );
Properties requestProperties = new Properties();
requestProperties.setProperty( "filetype", "artifact" );
requestProperties.setProperty( "version", artifact.getVersion() );
List<ProxyConnector> connectors = getProxyConnectors( repository );
@ -186,6 +189,7 @@ public class DefaultRepositoryProxyConnectors
File localFile = toLocalFile( repository, metadata );
Properties requestProperties = new Properties();
requestProperties.setProperty( "filetype", "metadata" );
boolean metadataNeedsUpdating = false;
long originalTimestamp = getLastModified( localFile );
@ -298,6 +302,7 @@ public class DefaultRepositoryProxyConnectors
File localFile = toLocalFile( repository, metadata );
Properties requestProperties = new Properties();
requestProperties.setProperty( "filetype", "metadata" );
boolean metadataNeedsUpdating = false;
long originalTimestamp = getLastModified( localFile );
@ -480,15 +485,20 @@ public class DefaultRepositoryProxyConnectors
}
// Handle pre-download policy
if ( !applyPolicies( this.preDownloadPolicies, connector.getPolicies(), requestProperties, localFile ) )
try
{
getLogger().debug( "Failed pre-download policies - " + localFile.getAbsolutePath() );
validatePolicies( this.preDownloadPolicies, connector.getPolicies(), requestProperties, localFile );
}
catch ( PolicyViolationException e )
{
String emsg = "Transfer not attempted on " + url + " : " + e.getMessage();
if ( fileExists( localFile ) )
{
getLogger().info( emsg + ": using already present local file." );
return localFile;
}
getLogger().info( emsg );
return null;
}
@ -543,10 +553,13 @@ public class DefaultRepositoryProxyConnectors
}
// Handle post-download policies.
if ( !applyPolicies( this.postDownloadPolicies, connector.getPolicies(), requestProperties, localFile ) )
try
{
getLogger().debug( "Failed post-download policies - " + localFile.getAbsolutePath() );
validatePolicies( this.postDownloadPolicies, connector.getPolicies(), requestProperties, localFile );
}
catch ( PolicyViolationException e )
{
getLogger().info( "Transfer invalidated from " + url + " : " + e.getMessage() );
if ( fileExists( localFile ) )
{
return localFile;
@ -697,10 +710,10 @@ public class DefaultRepositoryProxyConnectors
* @param settings the map of settings for the policies to execute. (Map of String policy keys, to String policy setting)
* @param request the request properties (utilized by the {@link DownloadPolicy#applyPolicy(String,Properties,File)})
* @param localFile the local file (utilized by the {@link DownloadPolicy#applyPolicy(String,Properties,File)})
* @return true if all of the policies passed, false if a policy failed.
*/
private boolean applyPolicies( Map<String, ? extends DownloadPolicy> policies, Map<String, String> settings,
private void validatePolicies( Map<String, ? extends DownloadPolicy> policies, Map<String, String> settings,
Properties request, File localFile )
throws PolicyViolationException
{
for ( Entry<String, ? extends DownloadPolicy> entry : policies.entrySet() )
{
@ -710,13 +723,15 @@ public class DefaultRepositoryProxyConnectors
String setting = StringUtils.defaultString( (String) settings.get( key ), defaultSetting );
getLogger().debug( "Applying [" + key + "] policy with [" + setting + "]" );
if ( !policy.applyPolicy( setting, request, localFile ) )
try
{
getLogger().debug( "Didn't pass the [" + key + "] policy." );
return false;
policy.applyPolicy( setting, request, localFile );
}
catch ( PolicyConfigurationException e )
{
getLogger().error( e.getMessage(), e );
}
}
return true;
}
/**