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.policies.urlcache.UrlFailureCache;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.inject.Inject;
import javax.inject.Named;
import java.io.File;
import java.util.Properties;

/**
 * CachedFailuresPolicyTest
 *
 * @version $Id$
 */
@RunWith( value = SpringJUnit4ClassRunner.class )
@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
public class CachedFailuresPolicyTest
    extends TestCase
{


    @Inject
    private UrlFailureCache urlFailureCache;

    @Inject @Named(value="preDownloadPolicy#cache-failures")
    DownloadPolicy downloadPolicy;

    private DownloadPolicy lookupPolicy()
        throws Exception
    {
        return downloadPolicy;
    }

    private File getFile()
    {
        return new File( "target/cache-failures/" + getName() + ".txt" );
    }

    private Properties createRequest()
    {
        Properties request = new Properties();

        return request;
    }

    @Test
    public void testPolicyNo()
        throws Exception
    {
        DownloadPolicy policy = lookupPolicy();
        File localFile = getFile();
        Properties request = createRequest();

        request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );

        policy.applyPolicy( CachedFailuresPolicy.NO, request, localFile );
    }

    @Test
    public void testPolicyYesNotInCache()
        throws Exception
    {

        //CacheManager.getInstance().clearAll();

        DownloadPolicy policy = lookupPolicy();
        File localFile = getFile();
        Properties request = createRequest();

        request.setProperty( "url", "http://a.bad.hostname.maven.org/path/to/resource.txt" );

        policy.applyPolicy( CachedFailuresPolicy.YES, request, localFile );
    }

    @Test
    public void testPolicyYesInCache()
        throws Exception
    {
        DownloadPolicy policy = lookupPolicy();
        File localFile = getFile();
        Properties request = createRequest();

        String url = "http://a.bad.hostname.maven.org/path/to/resource.txt";


        urlFailureCache.cacheFailure( url );

        request.setProperty( "url", url );

        try
        {
            policy.applyPolicy( CachedFailuresPolicy.YES, request, localFile );
            fail( "Expected a PolicyViolationException." );
        }
        catch ( PolicyViolationException e )
        {
            // expected path.
        }
    }
}

@@ -1224,24 +1224,71 @@
 .Properties;
+%0Aimport org.springframework.cache.CacheManager;
 %0A%0A/**%0A * Cac
@@ -2490,21 +2490,11 @@
 yYes
-NotInCache
 ()%0A
+
     
@@ -2525,58 +2525,8 @@
  %7B%0A%0A
-        //CacheManager.getInstance().clearAll();%0A%0A
     
@@ -2739,32 +2739,59 @@
 source.txt%22 );%0A%0A
+        // should not fail%0A
         policy.a
@@ -2853,226 +2853,84 @@
 e );
-%0A
     
-%7D%0A%0A
     
-@Test%0A    public void testPolicyYesInCache()%0A        throws Exception%0A    %7B%0A        DownloadPolicy policy = lookupPolicy();%0A        File localFile = getFile();%0A        Properties request = createRequest();%0A
+%0A        // status Yes Not In cache%0A        %0A        // Yes in Cache
 %0A   
@@ -3003,17 +3003,25 @@
 e.txt%22;%0A
-%0A
+         
 %0A       
@@ -3370,16 +3370,19 @@
       %7D%0A
-
     %7D
+   
 %0A%7D%0A

fix an other test failure on windauze Submitted by Eric Barboni .