From bc6065c1411229ba124283efa7fd4fce31f144f4 Mon Sep 17 00:00:00 2001 From: Martin Stockhammer Date: Wed, 4 Sep 2019 23:16:25 +0200 Subject: [PATCH] Update of policies. Adding descriptions and using locale. --- .../archiva/policies/AbstractPolicy.java | 98 ++++++++++++ .../policies/AbstractUpdatePolicy.java | 8 +- .../policies/CachedFailuresPolicy.java | 11 +- .../archiva/policies/ChecksumPolicy.java | 13 +- .../archiva/policies/DownloadErrorPolicy.java | 2 +- .../org/apache/archiva/policies/Policy.java | 44 +++++- .../PropagateErrorsDownloadPolicy.java | 13 +- ...PropagateErrorsOnUpdateDownloadPolicy.java | 15 +- .../archiva/policies/ReleasesPolicy.java | 9 +- .../archiva/policies/SnapshotsPolicy.java | 10 +- .../resources/archiva_policies.properties | 74 +++++++++ .../resources/archiva_policies_de.properties | 18 +++ .../resources/archiva_policies_en.properties | 18 +++ .../resources/archiva_policies_fr.properties | 18 +++ .../policies/CachedFailuresPolicyTest.java | 28 +++- .../archiva/policies/ChecksumPolicyTest.java | 25 +++ .../PropagateErrorsDownloadPolicyTest.java | 142 ++++++++++++++++++ ...agateErrorsOnUpdateDownloadPolicyTest.java | 133 ++++++++++++++++ .../archiva/policies/ReleasePolicyTest.java | 32 ++++ .../archiva/policies/SnapshotsPolicyTest.java | 29 ++++ 20 files changed, 688 insertions(+), 52 deletions(-) create mode 100644 archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/AbstractPolicy.java create mode 100644 archiva-modules/archiva-base/archiva-policies/src/main/resources/archiva_policies.properties create mode 100644 archiva-modules/archiva-base/archiva-policies/src/main/resources/archiva_policies_de.properties create mode 100644 archiva-modules/archiva-base/archiva-policies/src/main/resources/archiva_policies_en.properties create mode 100644 archiva-modules/archiva-base/archiva-policies/src/main/resources/archiva_policies_fr.properties create mode 100644 archiva-modules/archiva-base/archiva-policies/src/test/java/org/apache/archiva/policies/PropagateErrorsDownloadPolicyTest.java create mode 100644 archiva-modules/archiva-base/archiva-policies/src/test/java/org/apache/archiva/policies/PropagateErrorsOnUpdateDownloadPolicyTest.java diff --git a/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/AbstractPolicy.java b/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/AbstractPolicy.java new file mode 100644 index 000000000..2c6188687 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/AbstractPolicy.java @@ -0,0 +1,98 @@ +package org.apache.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 java.text.MessageFormat; +import java.util.Locale; +import java.util.ResourceBundle; +import java.util.stream.Collectors; + +/** + * Abstract policy class that handles the name and description loading with message bundles. + * + * The prefix for the keys is normally: + * + * + * This prefix can be changed by subclasses. + * + * For each policy and each option there must exist a name and description entry in the message bundle. + * + */ +public abstract class AbstractPolicy implements Policy { + + private String policyPrefix; + private String optionPrefix; + + public AbstractPolicy() { + policyPrefix = getId() + ".policy."; + optionPrefix = getId() + ".option."; + } + + protected String getPolicyPrefix() { + return policyPrefix; + } + + protected String getOptionPrefix() { + return optionPrefix; + } + + protected void setPolicyPrefix(String policyPrefix) { + this.policyPrefix = policyPrefix; + } + + public void setOptionPrefix(String optionPrefix) { + this.optionPrefix = optionPrefix; + } + + private static final ResourceBundle getBundle(Locale locale) { + return ResourceBundle.getBundle(RESOURCE_BUNDLE, locale); + } + + + @Override + public String getName() { + return getName(Locale.getDefault()); + } + + @Override + public String getName(Locale locale) { + return getBundle(locale).getString(getPolicyPrefix() + "name"); + } + + @Override + public String getDescription(Locale locale) { + return MessageFormat.format(getBundle(locale).getString(getPolicyPrefix() + "description") + , getOptions().stream().collect(Collectors.joining(","))); + } + + @Override + public String getOptionDescription(Locale locale, String option) { + return getBundle(locale).getString(getOptionPrefix()+option+".description"); + } + + @Override + public String getOptionName(Locale locale, String option) { + return getBundle(locale).getString(getOptionPrefix()+option+".name"); + } + +} diff --git a/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/AbstractUpdatePolicy.java b/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/AbstractUpdatePolicy.java index cb42e569b..ef2bc7bff 100644 --- a/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/AbstractUpdatePolicy.java +++ b/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/AbstractUpdatePolicy.java @@ -36,12 +36,12 @@ import java.util.Properties; * */ public abstract class AbstractUpdatePolicy - implements PreDownloadPolicy + extends AbstractPolicy implements PreDownloadPolicy { private Logger log = LoggerFactory.getLogger( AbstractUpdatePolicy.class ); /** - * The ALWAYS policy setting means that the artifact is always uipdated from the remote repo. + * The ALWAYS policy setting means that the artifact is always updated from the remote repo. */ public static final String ALWAYS = "always"; @@ -76,7 +76,7 @@ public abstract class AbstractUpdatePolicy /** * The ONCE policy means that the artifact retrieval occurs only if the - * local artifact is not present. This means that the retreival can only + * local artifact is not present. This means that the retrieval can only * occur once. */ public static final String ONCE = "once"; @@ -85,6 +85,8 @@ public abstract class AbstractUpdatePolicy public AbstractUpdatePolicy() { + super(); + super.setOptionPrefix("update.option."); options.add( ALWAYS ); options.add( HOURLY ); options.add( DAILY ); diff --git a/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/CachedFailuresPolicy.java b/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/CachedFailuresPolicy.java index 0a15c4a1a..98176fb2a 100644 --- a/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/CachedFailuresPolicy.java +++ b/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/CachedFailuresPolicy.java @@ -36,9 +36,10 @@ import java.util.Properties; */ @Service( "preDownloadPolicy#cache-failures" ) public class CachedFailuresPolicy - implements PreDownloadPolicy + extends AbstractPolicy implements PreDownloadPolicy { private Logger log = LoggerFactory.getLogger( CachedFailuresPolicy.class ); + private static final String ID = "cache-failures"; /** * The NO policy setting means that the the existence of old failures is not checked. @@ -59,6 +60,7 @@ public class CachedFailuresPolicy public CachedFailuresPolicy() { + super(); options.add( NO ); options.add( YES ); } @@ -105,14 +107,9 @@ public class CachedFailuresPolicy @Override public String getId() { - return "cache-failures"; + return ID; } - @Override - public String getName() - { - return "Cache failures"; - } @Override public List getOptions() diff --git a/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/ChecksumPolicy.java b/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/ChecksumPolicy.java index 2a158112d..940e54cf1 100644 --- a/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/ChecksumPolicy.java +++ b/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/ChecksumPolicy.java @@ -44,10 +44,12 @@ import java.util.Properties; */ @Service( "postDownloadPolicy#checksum" ) public class ChecksumPolicy - implements PostDownloadPolicy + extends AbstractPolicy implements PostDownloadPolicy { private Logger log = LoggerFactory.getLogger( ChecksumPolicy.class ); + private static final String ID = "checksum"; + /** * The IGNORE policy indicates that if the checksum policy is ignored, and * the state of, contents of, or validity of the checksum files are not @@ -75,6 +77,7 @@ public class ChecksumPolicy public ChecksumPolicy() { + super(); options.add( FAIL ); options.add( FIX ); options.add( IGNORE ); @@ -174,13 +177,7 @@ public class ChecksumPolicy @Override public String getId() { - return "checksum"; - } - - @Override - public String getName() - { - return "Checksum"; + return ID; } @Override diff --git a/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/DownloadErrorPolicy.java b/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/DownloadErrorPolicy.java index 43ae03e0f..63111a32e 100644 --- a/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/DownloadErrorPolicy.java +++ b/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/DownloadErrorPolicy.java @@ -41,7 +41,7 @@ public interface DownloadErrorPolicy * @param localFile * @param exception the exception that triggered the error * @param previousExceptions any previously triggered exceptions - * @return whether to process the exception or not + * @return True, if the exception should be processed, False if the exception should be ignored. * @throws PolicyConfigurationException if the policy is improperly configured */ boolean applyPolicy( String policySetting, Properties request, StorageAsset localFile, Exception exception, diff --git a/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/Policy.java b/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/Policy.java index cb960488c..3b3d06692 100644 --- a/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/Policy.java +++ b/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/Policy.java @@ -20,9 +20,19 @@ package org.apache.archiva.policies; */ import java.util.List; +import java.util.Locale; +import java.util.MissingResourceException; + +/** + * This is a generic interface for policies. Policies define different actions to apply to artifacts during the + * repository lifecycle, e.g. download, upload, errors. + */ public interface Policy { + + String RESOURCE_BUNDLE = "archiva_policies"; + /** * Get the list of options for this policy. * @@ -47,9 +57,39 @@ public interface Policy /** * Get the display name for this policy. * - * TODO todo i18n - * * @return the name for this policy */ String getName(); + + /** + * Get the policy name in the language of the given locale. + * @param locale The locale + * @return The policy name + */ + String getName(Locale locale); + + /** + * Return a description of the policy. + * @param locale The language + * @return The description + */ + String getDescription(Locale locale); + + /** + * Returns a description for the given option. + * @param locale The locale for the description. + * @param option The option to ask the description for. + * @return A description of the option in the requested language. + * @throws MissingResourceException if the option is not known by this policy. + */ + String getOptionDescription(Locale locale, String option) throws MissingResourceException; + + /** + * Returns a name for the given option. + * @param locale The locale for the name + * @param option The option identifier + * @return A name in the requested language. + * @throws MissingResourceException if the option is not known by this policy. + */ + String getOptionName(Locale locale, String option) throws MissingResourceException; } diff --git a/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/PropagateErrorsDownloadPolicy.java b/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/PropagateErrorsDownloadPolicy.java index 3fdf47599..a3ea38186 100644 --- a/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/PropagateErrorsDownloadPolicy.java +++ b/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/PropagateErrorsDownloadPolicy.java @@ -35,9 +35,10 @@ import java.util.Properties; */ @Service( "downloadErrorPolicy#propagate-errors" ) public class PropagateErrorsDownloadPolicy - implements DownloadErrorPolicy + extends AbstractPolicy implements DownloadErrorPolicy { private Logger log = LoggerFactory.getLogger( PropagateErrorsDownloadPolicy.class ); + private static final String ID = "propagate-errors"; /** * Signifies any error should stop searching for other proxies. @@ -47,7 +48,7 @@ public class PropagateErrorsDownloadPolicy /** * Propagate errors at the end after all are gathered, if there was no successful download from other proxies. */ - public static final String QUEUE = "queue error"; + public static final String QUEUE = "queue-error"; /** * Ignore errors and treat as if it were not found. @@ -108,13 +109,7 @@ public class PropagateErrorsDownloadPolicy @Override public String getId() { - return "propagate-errors"; - } - - @Override - public String getName() - { - return "On remote error"; + return ID ; } @Override diff --git a/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/PropagateErrorsOnUpdateDownloadPolicy.java b/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/PropagateErrorsOnUpdateDownloadPolicy.java index 7f349d3be..579980607 100644 --- a/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/PropagateErrorsOnUpdateDownloadPolicy.java +++ b/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/PropagateErrorsOnUpdateDownloadPolicy.java @@ -33,8 +33,10 @@ import java.util.Properties; */ @Service( "downloadErrorPolicy#propagate-errors-on-update" ) public class PropagateErrorsOnUpdateDownloadPolicy - implements DownloadErrorPolicy + extends AbstractPolicy implements DownloadErrorPolicy { + private static final String ID = "propagate-errors-on-update"; + /** * Signifies any error should cause a failure whether the artifact is already present or not. */ @@ -43,9 +45,9 @@ public class PropagateErrorsOnUpdateDownloadPolicy /** * Signifies any error should cause a failure only if the artifact is not already present. */ - public static final String NOT_PRESENT = "artifact not already present"; + public static final String NOT_PRESENT = "artifact-not-present"; - private List options = new ArrayList<>( 2 ); + private static final List options = new ArrayList<>( 2 ); public PropagateErrorsOnUpdateDownloadPolicy() { @@ -91,14 +93,9 @@ public class PropagateErrorsOnUpdateDownloadPolicy @Override public String getId() { - return "propagate-errors-on-update"; + return ID; } - @Override - public String getName() - { - return "Return error when"; - } @Override public List getOptions() diff --git a/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/ReleasesPolicy.java b/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/ReleasesPolicy.java index e757918dd..bd863a529 100644 --- a/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/ReleasesPolicy.java +++ b/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/ReleasesPolicy.java @@ -32,6 +32,8 @@ public class ReleasesPolicy extends AbstractUpdatePolicy implements PreDownloadPolicy { + + private static final String ID = "releases"; /** * Defaults to {@link AbstractUpdatePolicy#HOURLY} */ @@ -56,12 +58,7 @@ public class ReleasesPolicy @Override public String getId() { - return "releases"; + return ID; } - @Override - public String getName() - { - return "Releases"; - } } diff --git a/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/SnapshotsPolicy.java b/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/SnapshotsPolicy.java index 58f287ca3..2c37b80e2 100644 --- a/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/SnapshotsPolicy.java +++ b/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/SnapshotsPolicy.java @@ -32,6 +32,9 @@ public class SnapshotsPolicy extends AbstractUpdatePolicy implements PreDownloadPolicy { + + private static final String ID = "snapshots"; + /** * Defaults to {@link AbstractUpdatePolicy#HOURLY} */ @@ -56,12 +59,7 @@ public class SnapshotsPolicy @Override public String getId() { - return "snapshots"; + return ID; } - @Override - public String getName() - { - return "Snapshots"; - } } diff --git a/archiva-modules/archiva-base/archiva-policies/src/main/resources/archiva_policies.properties b/archiva-modules/archiva-base/archiva-policies/src/main/resources/archiva_policies.properties new file mode 100644 index 000000000..f654614ef --- /dev/null +++ b/archiva-modules/archiva-base/archiva-policies/src/main/resources/archiva_policies.properties @@ -0,0 +1,74 @@ +# +# 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. +# + +releases.policy.name=Release Artifact Update Policy +releases.policy.description=This policy tells, when a release artifact will be updated. This policy is applied before the artifact is downloaded.\ + It allows to set the following options: {0}. +snapshots.policy.name=Snapshot Artifact Update Policy +snapshots.policy.description=This policy tells, when a snapshot artifact will be updated. This policy is applied before the artifact is downloaded. \ + It allows to set the following options: {0}. +update.option.always.description=Updates the artifact on each download from the remote repository. +update.option.always.name=Update always +update.option.never.description=Update the artifact never from the remote repository. +update.option.never.name=Do not download from remote +update.option.daily.description=Downloads the artifact if it does not exist locally, or if the local modification timestamp is older than one day. +update.option.daily.name=Update, if older than a day +update.option.hourly.description=Downloads the artifact if it does not exist locally, or if the local modification timestamp is older than one hour. +update.option.hourly.name=Update, if older than a hour +update.option.once.description=Downloads the artifact only, if it does not exist locally. +update.option.once.name=Download only once + +cache-failures.policy.name=Cache Failures Policy +cache-failures.policy.description=This policies decides, if download failures will be cached. The policy is applied before the artifact is downloaded. \ + The option 'yes' enables the cache. +cache-failures.option.yes.description=Download failures are cached and download is not attempted, if it failed before. +cache-failures.option.yes.name=Yes +cache-failures.option.no.description=Download failures are not cached. It will try the download again, if the file is requested. +cache-failures.option.no.name=No + +checksum.policy.name=Checksum Policy +checksum.policy.description=This policy tells, what happens if the downloaded checksum of a artifact does not match. \ + The policy is applied after downloading the artifact. The following options can be set: {0}. +checksum.option.fail.description=The download fails and the artifact is removed locally. +checksum.option.fail.name=Fail, if no match +checksum.option.fix.description=The artifact will remain and the checksum will be generated locally. +checksum.option.fix.name=Fix, if no match +checksum.option.ignore.description=The error will be ignored. +checksum.option.ignore.name=Ignore, if no match + +propagate-errors.policy.name=Propagate Download Errors Policy +propagate-errors.policy.description=This policy tells, what happens, if an error occurs during download of the artifact. +propagate-errors.option.stop.name=Stop on error +propagate-errors.option.stop.description=Stops the artifact download. Further remote repositories will not be checked. +propagate-errors.option.queue-error.name=Continue on error +propagate-errors.option.queue-error.description=Checks further remote repositories for the artifact. If all downloads fail, the error is propagated. +propagate-errors.option.ignore.name=Ignore errors +propagate-errors.option.ignore.description=Treat the error as 'not found'. Check following repositories if defined. + +propagate-errors-on-update.policy.name=Propagate Errors on Update Policy +propagate-errors-on-update.policy.description=This policy tells what happens, if an error occurs during download of an artifact that exists already locally. +propagate-errors-on-update.option.always.name=Propagate always +propagate-errors-on-update.option.always.description=Propagates the error, even if the file exists already locally. +propagate-errors-on-update.option.not-present.name=Propagate only, if not exists +propagate-errors-on-update.option.not-present.description=Propagates the error only, if the file does not exist already locally. + + + + + diff --git a/archiva-modules/archiva-base/archiva-policies/src/main/resources/archiva_policies_de.properties b/archiva-modules/archiva-base/archiva-policies/src/main/resources/archiva_policies_de.properties new file mode 100644 index 000000000..30097ef04 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-policies/src/main/resources/archiva_policies_de.properties @@ -0,0 +1,18 @@ +# +# 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. +# diff --git a/archiva-modules/archiva-base/archiva-policies/src/main/resources/archiva_policies_en.properties b/archiva-modules/archiva-base/archiva-policies/src/main/resources/archiva_policies_en.properties new file mode 100644 index 000000000..30097ef04 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-policies/src/main/resources/archiva_policies_en.properties @@ -0,0 +1,18 @@ +# +# 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. +# diff --git a/archiva-modules/archiva-base/archiva-policies/src/main/resources/archiva_policies_fr.properties b/archiva-modules/archiva-base/archiva-policies/src/main/resources/archiva_policies_fr.properties new file mode 100644 index 000000000..30097ef04 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-policies/src/main/resources/archiva_policies_fr.properties @@ -0,0 +1,18 @@ +# +# 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. +# diff --git a/archiva-modules/archiva-base/archiva-policies/src/test/java/org/apache/archiva/policies/CachedFailuresPolicyTest.java b/archiva-modules/archiva-base/archiva-policies/src/test/java/org/apache/archiva/policies/CachedFailuresPolicyTest.java index d9919a550..f103f94bb 100644 --- a/archiva-modules/archiva-base/archiva-policies/src/test/java/org/apache/archiva/policies/CachedFailuresPolicyTest.java +++ b/archiva-modules/archiva-base/archiva-policies/src/test/java/org/apache/archiva/policies/CachedFailuresPolicyTest.java @@ -33,6 +33,8 @@ import javax.inject.Inject; import javax.inject.Named; import java.io.IOException; import java.nio.file.Paths; +import java.util.Locale; +import java.util.MissingResourceException; import java.util.Properties; /** @@ -103,7 +105,12 @@ public class CachedFailuresPolicyTest request.setProperty( "url", url ); // should not fail - policy.applyPolicy( CachedFailuresPolicy.YES, request, localFile ); + try { + policy.applyPolicy(CachedFailuresPolicy.YES, request, localFile); + } catch (PolicyViolationException e) { + // Converting to runtime exception, because it should be thrown later + throw new RuntimeException(e); + } // status Yes Not In cache // Yes in Cache @@ -114,4 +121,23 @@ public class CachedFailuresPolicyTest policy.applyPolicy( CachedFailuresPolicy.YES, request, localFile ); } + + @Test + public void testNamesAndDescriptions() throws Exception { + DownloadPolicy policy = lookupPolicy(); + assertEquals("Cache Failures Policy", policy.getName()); + assertTrue(policy.getDescription(Locale.US).contains("if download failures will be cached")); + assertEquals("Yes", policy.getOptionName(Locale.US, "yes")); + assertEquals("No", policy.getOptionName(Locale.US, "no")); + assertTrue(policy.getOptionDescription(Locale.US, "yes").contains("failures are cached and download is not attempted")); + assertTrue(policy.getOptionDescription(Locale.US, "no").contains("failures are not cached")); + try { + policy.getOptionName(Locale.US, "xxxx"); + // Exception should be thrown + assertTrue(false); + } catch (MissingResourceException e) { + // + } + + } } diff --git a/archiva-modules/archiva-base/archiva-policies/src/test/java/org/apache/archiva/policies/ChecksumPolicyTest.java b/archiva-modules/archiva-base/archiva-policies/src/test/java/org/apache/archiva/policies/ChecksumPolicyTest.java index d4ebf9f35..e13664547 100644 --- a/archiva-modules/archiva-base/archiva-policies/src/test/java/org/apache/archiva/policies/ChecksumPolicyTest.java +++ b/archiva-modules/archiva-base/archiva-policies/src/test/java/org/apache/archiva/policies/ChecksumPolicyTest.java @@ -38,6 +38,8 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Locale; +import java.util.MissingResourceException; import java.util.Properties; import static org.junit.Assert.*; @@ -379,4 +381,27 @@ public class ChecksumPolicyTest return filesystemStorage.getAsset( path ); } + + @Test + public void testNamesAndDescriptions() throws Exception { + + PostDownloadPolicy policy = lookupPolicy(); + assertEquals("Checksum Policy", policy.getName()); + assertTrue(policy.getDescription(Locale.US).contains("if the downloaded checksum of a artifact does not match")); + assertEquals("Fail, if no match", policy.getOptionName(Locale.US, "fail")); + assertEquals("Fix, if no match", policy.getOptionName(Locale.US, "fix")); + assertEquals("Ignore, if no match", policy.getOptionName(Locale.US, "ignore")); + assertTrue(policy.getOptionDescription(Locale.US, "fail").contains("download fails")); + assertTrue(policy.getOptionDescription(Locale.US, "fix").contains("artifact will remain")); + assertTrue(policy.getOptionDescription(Locale.US, "ignore").contains("error will be ignored")); + try { + policy.getOptionName(Locale.US, "xxxx"); + // Exception should be thrown + assertTrue(false); + } catch (MissingResourceException e) { + // + } + + } + } diff --git a/archiva-modules/archiva-base/archiva-policies/src/test/java/org/apache/archiva/policies/PropagateErrorsDownloadPolicyTest.java b/archiva-modules/archiva-base/archiva-policies/src/test/java/org/apache/archiva/policies/PropagateErrorsDownloadPolicyTest.java new file mode 100644 index 000000000..c169361fe --- /dev/null +++ b/archiva-modules/archiva-base/archiva-policies/src/test/java/org/apache/archiva/policies/PropagateErrorsDownloadPolicyTest.java @@ -0,0 +1,142 @@ +package org.apache.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.TestCase; +import org.apache.archiva.common.filelock.DefaultFileLockManager; +import org.apache.archiva.policies.urlcache.UrlFailureCache; +import org.apache.archiva.repository.storage.FilesystemStorage; +import org.apache.archiva.repository.storage.StorageAsset; +import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; + +import javax.inject.Inject; +import javax.inject.Named; +import java.io.IOException; +import java.nio.file.Paths; +import java.util.*; + +/** + * CachedFailuresPolicyTest + * + * + */ +@RunWith( ArchivaSpringJUnit4ClassRunner.class ) +@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } ) +public class PropagateErrorsDownloadPolicyTest + extends TestCase +{ + + + @Inject + private UrlFailureCache urlFailureCache; + + private FilesystemStorage filesystemStorage; + + @Inject + @Named( value = "downloadErrorPolicy#propagate-errors" ) + DownloadErrorPolicy downloadPolicy; + + private DownloadErrorPolicy lookupPolicy() + throws Exception + { + return downloadPolicy; + } + + private StorageAsset getFile() throws IOException { + if (filesystemStorage==null) { + filesystemStorage = new FilesystemStorage(Paths.get("target/cache-failures"), new DefaultFileLockManager()); + } + return filesystemStorage.getAsset( getName() + ".txt" ); + } + + private Properties createRequest() + { + Properties request = new Properties(); + + return request; + } + + @Test + public void testPolicyStop() + throws Exception + { + DownloadErrorPolicy policy = lookupPolicy(); + StorageAsset localFile = getFile(); + Properties request = createRequest(); + + Exception ex = new RuntimeException(); + Map exMap = new HashMap<>(); + + assertTrue(policy.applyPolicy( PropagateErrorsDownloadPolicy.STOP, request, localFile, ex, exMap )); + } + + @Test + public void testPolicyQueue() + throws Exception + { + DownloadErrorPolicy policy = lookupPolicy(); + StorageAsset localFile = getFile(); + Properties request = createRequest(); + + Exception ex = new RuntimeException(); + Map exMap = new HashMap<>(); + + assertTrue(policy.applyPolicy( PropagateErrorsDownloadPolicy.QUEUE, request, localFile, ex, exMap )); + } + + @Test + public void testPolicyIgnore() + throws Exception + { + DownloadErrorPolicy policy = lookupPolicy(); + StorageAsset localFile = getFile(); + Properties request = createRequest(); + + Exception ex = new RuntimeException(); + Map exMap = new HashMap<>(); + + assertFalse(policy.applyPolicy( PropagateErrorsDownloadPolicy.IGNORE, request, localFile, ex, exMap )); + } + + @Test + public void testNamesAndDescriptions() throws Exception { + + DownloadErrorPolicy policy = lookupPolicy(); + assertEquals("Propagate Download Errors Policy", policy.getName()); + assertTrue(policy.getDescription(Locale.US).contains("error occurs during download")); + assertEquals("Stop on error", policy.getOptionName(Locale.US, "stop")); + assertEquals("Continue on error", policy.getOptionName(Locale.US, "queue-error")); + assertEquals("Ignore errors", policy.getOptionName(Locale.US, "ignore")); + assertTrue(policy.getOptionDescription(Locale.US, "stop").contains("Stops the artifact download")); + assertTrue(policy.getOptionDescription(Locale.US, "queue-error").contains("Checks further")); + assertTrue(policy.getOptionDescription(Locale.US, "ignore").contains("not found")); + try { + policy.getOptionName(Locale.US, "xxxx"); + // Exception should be thrown + assertTrue(false); + } catch (MissingResourceException e) { + // + } + + } +} diff --git a/archiva-modules/archiva-base/archiva-policies/src/test/java/org/apache/archiva/policies/PropagateErrorsOnUpdateDownloadPolicyTest.java b/archiva-modules/archiva-base/archiva-policies/src/test/java/org/apache/archiva/policies/PropagateErrorsOnUpdateDownloadPolicyTest.java new file mode 100644 index 000000000..303e7a09c --- /dev/null +++ b/archiva-modules/archiva-base/archiva-policies/src/test/java/org/apache/archiva/policies/PropagateErrorsOnUpdateDownloadPolicyTest.java @@ -0,0 +1,133 @@ +package org.apache.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.TestCase; +import org.apache.archiva.common.filelock.DefaultFileLockManager; +import org.apache.archiva.policies.urlcache.UrlFailureCache; +import org.apache.archiva.repository.storage.FilesystemStorage; +import org.apache.archiva.repository.storage.StorageAsset; +import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; + +import javax.inject.Inject; +import javax.inject.Named; +import java.io.IOException; +import java.nio.file.Paths; +import java.util.*; + +/** + * CachedFailuresPolicyTest + * + * + */ +@RunWith( ArchivaSpringJUnit4ClassRunner.class ) +@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } ) +public class PropagateErrorsOnUpdateDownloadPolicyTest + extends TestCase +{ + + + @Inject + private UrlFailureCache urlFailureCache; + + private FilesystemStorage filesystemStorage; + + @Inject + @Named( value = "downloadErrorPolicy#propagate-errors-on-update" ) + DownloadErrorPolicy downloadPolicy; + + private DownloadErrorPolicy lookupPolicy() + throws Exception + { + return downloadPolicy; + } + + private StorageAsset getFile() throws IOException { + if (filesystemStorage==null) { + filesystemStorage = new FilesystemStorage(Paths.get("target/cache-failures"), new DefaultFileLockManager()); + } + return filesystemStorage.getAsset( getName() + ".txt" ); + } + + private Properties createRequest() + { + Properties request = new Properties(); + + return request; + } + + @Test + public void testPolicyStop() + throws Exception + { + DownloadErrorPolicy policy = lookupPolicy(); + StorageAsset localFile = getFile(); + Properties request = createRequest(); + + Exception ex = new RuntimeException(); + Map exMap = new HashMap<>(); + + assertTrue(policy.applyPolicy( PropagateErrorsOnUpdateDownloadPolicy.ALWAYS, request, localFile, ex, exMap )); + } + + @Test + public void testPolicyQueue() + throws Exception + { + DownloadErrorPolicy policy = lookupPolicy(); + StorageAsset localFile = getFile(); + Properties request = createRequest(); + + Exception ex = new RuntimeException(); + Map exMap = new HashMap<>(); + + if (localFile.exists()) { + localFile.getStorage().removeAsset(localFile); + } + assertTrue(policy.applyPolicy( PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT, request, localFile, ex, exMap )); + + localFile.create(); + assertFalse(policy.applyPolicy( PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT, request, localFile, ex, exMap )); + } + + + @Test + public void testNamesAndDescriptions() throws Exception { + + DownloadErrorPolicy policy = lookupPolicy(); + assertEquals("Propagate Errors on Update Policy", policy.getName()); + assertTrue(policy.getDescription(Locale.US).contains("during download of an artifact that exists already")); + assertEquals("Propagate always", policy.getOptionName(Locale.US, "always")); + assertEquals("Propagate only, if not exists", policy.getOptionName(Locale.US, "not-present")); + assertTrue(policy.getOptionDescription(Locale.US, "always").contains("even if the file exists")); + assertTrue(policy.getOptionDescription(Locale.US, "not-present").contains("if the file does not exist")); + try { + policy.getOptionName(Locale.US, "xxxx"); + // Exception should be thrown + assertTrue(false); + } catch (MissingResourceException e) { + // + } + + } +} diff --git a/archiva-modules/archiva-base/archiva-policies/src/test/java/org/apache/archiva/policies/ReleasePolicyTest.java b/archiva-modules/archiva-base/archiva-policies/src/test/java/org/apache/archiva/policies/ReleasePolicyTest.java index 3c5bae066..92d2ece89 100644 --- a/archiva-modules/archiva-base/archiva-policies/src/test/java/org/apache/archiva/policies/ReleasePolicyTest.java +++ b/archiva-modules/archiva-base/archiva-policies/src/test/java/org/apache/archiva/policies/ReleasePolicyTest.java @@ -33,8 +33,13 @@ import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.attribute.FileTime; +import java.util.Locale; +import java.util.MissingResourceException; import java.util.Properties; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + /** * ReleasePolicyTest * @@ -381,4 +386,31 @@ public class ReleasePolicyTest // reset delta to 0. generatedLocalFileUpdateDelta = 0; } + + + @Test + public void testNamesAndDescriptions() throws Exception { + + PreDownloadPolicy policy = lookupPolicy(); + assertEquals("Release Artifact Update Policy", policy.getName()); + assertTrue(policy.getDescription(Locale.US).contains("when a release artifact will be updated")); + assertEquals("Update always", policy.getOptionName(Locale.US, "always")); + assertEquals("Do not download from remote", policy.getOptionName(Locale.US, "never")); + assertEquals("Update, if older than a day", policy.getOptionName(Locale.US, "daily")); + assertEquals("Update, if older than a hour", policy.getOptionName(Locale.US, "hourly")); + assertEquals("Download only once", policy.getOptionName(Locale.US, "once")); + assertTrue(policy.getOptionDescription(Locale.US, "always").contains("each download")); + assertTrue(policy.getOptionDescription(Locale.US, "never").contains("never from the remote")); + assertTrue(policy.getOptionDescription(Locale.US, "daily").contains("older than one day")); + assertTrue(policy.getOptionDescription(Locale.US, "hourly").contains("older than one hour")); + assertTrue(policy.getOptionDescription(Locale.US, "once").contains("if it does not exist")); + try { + policy.getOptionName(Locale.US, "xxxx"); + // Exception should be thrown + assertTrue(false); + } catch (MissingResourceException e) { + // + } + + } } diff --git a/archiva-modules/archiva-base/archiva-policies/src/test/java/org/apache/archiva/policies/SnapshotsPolicyTest.java b/archiva-modules/archiva-base/archiva-policies/src/test/java/org/apache/archiva/policies/SnapshotsPolicyTest.java index a68ae8dd9..39a8afe4a 100644 --- a/archiva-modules/archiva-base/archiva-policies/src/test/java/org/apache/archiva/policies/SnapshotsPolicyTest.java +++ b/archiva-modules/archiva-base/archiva-policies/src/test/java/org/apache/archiva/policies/SnapshotsPolicyTest.java @@ -34,6 +34,8 @@ import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.attribute.FileTime; +import java.util.Locale; +import java.util.MissingResourceException; import java.util.Properties; /** @@ -383,4 +385,31 @@ public class SnapshotsPolicyTest // reset delta to 0. generatedLocalFileUpdateDelta = 0; } + + + @Test + public void testNamesAndDescriptions() throws Exception { + + PreDownloadPolicy policy = lookupPolicy(); + assertEquals("Snapshot Artifact Update Policy", policy.getName()); + assertTrue(policy.getDescription(Locale.US).contains("when a snapshot artifact will be updated")); + assertEquals("Update always", policy.getOptionName(Locale.US, "always")); + assertEquals("Do not download from remote", policy.getOptionName(Locale.US, "never")); + assertEquals("Update, if older than a day", policy.getOptionName(Locale.US, "daily")); + assertEquals("Update, if older than a hour", policy.getOptionName(Locale.US, "hourly")); + assertEquals("Download only once", policy.getOptionName(Locale.US, "once")); + assertTrue(policy.getOptionDescription(Locale.US, "always").contains("each download")); + assertTrue(policy.getOptionDescription(Locale.US, "never").contains("never from the remote")); + assertTrue(policy.getOptionDescription(Locale.US, "daily").contains("older than one day")); + assertTrue(policy.getOptionDescription(Locale.US, "hourly").contains("older than one hour")); + assertTrue(policy.getOptionDescription(Locale.US, "once").contains("if it does not exist")); + try { + policy.getOptionName(Locale.US, "xxxx"); + // Exception should be thrown + assertTrue(false); + } catch (MissingResourceException e) { + // + } + + } } -- 2.39.5