*/
public static final String ONCE = "once";
- private List options = new ArrayList();
+ private List<String> options = new ArrayList<String>();
public 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;
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 ) )
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 ) )
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." );
}
}
*/
private UrlFailureCache urlFailureCache;
- private List options = new ArrayList();
+ private List<String> options = new ArrayList<String>();
public 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" );
{
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()
return "cache-failures";
}
- public List getOptions()
+ public List<String> getOptions()
{
return options;
}
* under the License.
*/
+import org.apache.commons.lang.StringUtils;
import org.apache.maven.archiva.common.utils.Checksums;
import org.codehaus.plexus.logging.AbstractLogEnabled;
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$
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" );
-
- // On failure. delete files.
- if ( sha1File.exists() )
- {
- sha1File.delete();
- }
+ return;
+ }
+
+ File sha1File = new File( localFile.getAbsolutePath() + ".sha1" );
+ File md5File = new File( localFile.getAbsolutePath() + ".md5" );
- if ( md5File.exists() )
- {
- md5File.delete();
- }
+ // On failure. delete files.
+ if ( sha1File.exists() )
+ {
+ sha1File.delete();
+ }
- localFile.delete();
+ if ( md5File.exists() )
+ {
+ md5File.delete();
}
- return checksPass;
+ 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()
* @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;
}
--- /dev/null
+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 );
+ }
+}
--- /dev/null
+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;
+
+/**
+ * PolicyViolationException
+ *
+ * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
+ * @version $Id$
+ */
+public class PolicyViolationException
+ extends ArchivaException
+{
+ public PolicyViolationException( String message, Throwable cause )
+ {
+ super( message, cause );
+ }
+
+ public PolicyViolationException( String message )
+ {
+ super( message );
+ }
+}
* under the License.
*/
-
import java.io.File;
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.
* @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;
}
* @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;
}
+++ /dev/null
-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 junit.framework.Test;
-import junit.framework.TestSuite;
-
-/**
- * Helper class for IDE testing.
- *
- * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
- * @version $Id$
- */
-public class AllTests
-{
-
- public static Test suite()
- {
- 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;
- }
-
-}
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()
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()
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.
+ }
}
}
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 )
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" );
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.
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 );
--- /dev/null
+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;
+ }
+}
--- /dev/null
+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;
+ }
+}
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;
File localFile = toLocalFile( repository, artifact );
Properties requestProperties = new Properties();
+ requestProperties.setProperty( "filetype", "artifact" );
requestProperties.setProperty( "version", artifact.getVersion() );
List<ProxyConnector> connectors = getProxyConnectors( repository );
File localFile = toLocalFile( repository, metadata );
Properties requestProperties = new Properties();
+ requestProperties.setProperty( "filetype", "metadata" );
boolean metadataNeedsUpdating = false;
long originalTimestamp = getLastModified( localFile );
File localFile = toLocalFile( repository, metadata );
Properties requestProperties = new Properties();
+ requestProperties.setProperty( "filetype", "metadata" );
boolean metadataNeedsUpdating = false;
long originalTimestamp = getLastModified( localFile );
}
// 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;
}
}
// 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;
* @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() )
{
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;
}
/**