--- /dev/null
+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:
+ * <ul>
+ * <li>Policies: POLICY-ID.policy.</li>
+ * <li>Options: POLICY-ID.option.</li>
+ * </ul>
+ *
+ * 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");
+ }
+
+}
*
*/
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";
/**
* 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";
public AbstractUpdatePolicy()
{
+ super();
+ super.setOptionPrefix("update.option.");
options.add( ALWAYS );
options.add( HOURLY );
options.add( DAILY );
*/
@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 <strong>not</strong> checked.
public CachedFailuresPolicy()
{
+ super();
options.add( NO );
options.add( YES );
}
@Override
public String getId()
{
- return "cache-failures";
+ return ID;
}
- @Override
- public String getName()
- {
- return "Cache failures";
- }
@Override
public List<String> getOptions()
*/
@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
public ChecksumPolicy()
{
+ super();
options.add( FAIL );
options.add( FIX );
options.add( IGNORE );
@Override
public String getId()
{
- return "checksum";
- }
-
- @Override
- public String getName()
- {
- return "Checksum";
+ return ID;
}
@Override
* @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,
*/
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.
*
/**
* 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;
}
*/
@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.
/**
* 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.
@Override
public String getId()
{
- return "propagate-errors";
- }
-
- @Override
- public String getName()
- {
- return "On remote error";
+ return ID ;
}
@Override
*/
@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.
*/
/**
* 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<String> options = new ArrayList<>( 2 );
+ private static final List<String> options = new ArrayList<>( 2 );
public PropagateErrorsOnUpdateDownloadPolicy()
{
@Override
public String getId()
{
- return "propagate-errors-on-update";
+ return ID;
}
- @Override
- public String getName()
- {
- return "Return error when";
- }
@Override
public List<String> getOptions()
extends AbstractUpdatePolicy
implements PreDownloadPolicy
{
+
+ private static final String ID = "releases";
/**
* Defaults to {@link AbstractUpdatePolicy#HOURLY}
*/
@Override
public String getId()
{
- return "releases";
+ return ID;
}
- @Override
- public String getName()
- {
- return "Releases";
- }
}
extends AbstractUpdatePolicy
implements PreDownloadPolicy
{
+
+ private static final String ID = "snapshots";
+
/**
* Defaults to {@link AbstractUpdatePolicy#HOURLY}
*/
@Override
public String getId()
{
- return "snapshots";
+ return ID;
}
- @Override
- public String getName()
- {
- return "Snapshots";
- }
}
--- /dev/null
+#
+# 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.
+
+
+
+
+
--- /dev/null
+#
+# 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.
+#
--- /dev/null
+#
+# 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.
+#
--- /dev/null
+#
+# 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 javax.inject.Named;
import java.io.IOException;
import java.nio.file.Paths;
+import java.util.Locale;
+import java.util.MissingResourceException;
import java.util.Properties;
/**
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
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) {
+ //
+ }
+
+ }
}
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.*;
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) {
+ //
+ }
+
+ }
+
}
--- /dev/null
+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<String, Exception> 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<String, Exception> 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<String, Exception> 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) {
+ //
+ }
+
+ }
+}
--- /dev/null
+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<String, Exception> 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<String, Exception> 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) {
+ //
+ }
+
+ }
+}
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
*
// 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) {
+ //
+ }
+
+ }
}
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;
/**
// 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) {
+ //
+ }
+
+ }
}