// Copyright 2010, 2013 The Apache Software Foundation
//
// Licensed 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.

package org.apache.tapestry5.internal.services.assets;

import org.apache.tapestry5.internal.services.ResourceStreamer;
import org.apache.tapestry5.ioc.Resource;
import org.apache.tapestry5.ioc.test.TestBase;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.Response;
import org.apache.tapestry5.services.assets.AssetChecksumGenerator;
import org.apache.tapestry5.services.assets.AssetRequestHandler;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.IOException;

public class ContextAssetRequestHandlerTest extends TestBase
{
    @DataProvider
    public Object[][] invalid_paths()
    {
        return new Object[][]
                {
                        {"web-Inf/classes/hibernate.cfg.xml"},
                        {"Meta-Inf/MANIFEST.mf"},
                        {"Index.tml"},
                        {"folder/FolderIndex.TML"}};
    }

    @Test(dataProvider = "invalid_paths")
    public void ensure_assets_are_rejected(String path) throws IOException
    {
        ContextAssetRequestHandler handler = new ContextAssetRequestHandler(null, null);

        assertFalse(handler.handleAssetRequest(null, null, "fake-checksum/" + path),
                "Handler should return false for invalid path.");
    }

    @Test
    public void invalid_checksums_are_treated_as_if_missing() throws IOException
    {
        ResourceStreamer streamer = newMock(ResourceStreamer.class);
        AssetChecksumGenerator gen = newMock(AssetChecksumGenerator.class);
        Resource root = newMock(Resource.class);
        Resource r = newMock(Resource.class);
        Request req = newMock(Request.class);
        Response res = newMock(Response.class);

        expect(root.forFile("folder/icon.png")).andReturn(r);
        expect(r.exists()).andReturn(true);
        expect(gen.generateChecksum(r)).andReturn("def");

        replay();

        AssetRequestHandler h = new ContextAssetRequestHandler(streamer, root);

        assertFalse(h.handleAssetRequest(req, res, "abc/folder/icon.png"));

        verify();
    }
}

@@ -692,356 +692,24 @@
 y5.i
-nternal.services.ResourceStreamer;%0Aimport org.apache.tapestry5.ioc.Resource;%0Aimport org.apache.tapestry5.ioc.test.TestBase;%0Aimport org.apache.tapestry5.services.Request;%0Aimport org.apache.tapestry5.services.Response;%0Aimport org.apache.tapestry5.services.assets.AssetChecksumGenerator;%0Aimport org.apache.tapestry5.services.assets.AssetRequestHandler
+oc.test.TestBase
 ;%0Aim
@@ -1499,16 +1499,16 @@
  path),%0A
+
         
@@ -1574,806 +1574,7 @@
    %7D
-%0A%0A    @Test%0A    public void invalid_checksums_are_treated_as_if_missing() throws IOException%0A    %7B%0A        ResourceStreamer streamer = newMock(ResourceStreamer.class);%0A        AssetChecksumGenerator gen = newMock(AssetChecksumGenerator.class);%0A        Resource root = newMock(Resource.class);%0A        Resource r = newMock(Resource.class);%0A        Request req = newMock(Request.class);%0A        Response res = newMock(Response.class);%0A%0A        expect(root.forFile(%22folder/icon.png%22)).andReturn(r);%0A        expect(r.exists()).andReturn(true);%0A        expect(gen.generateChecksum(r)).andReturn(%22def%22);%0A%0A        replay();%0A%0A        AssetRequestHandler h = new ContextAssetRequestHandler(streamer, root);%0A%0A        assertFalse(h.handleAssetRequest(req, res, %22abc/folder/icon.png%22));%0A%0A        verify();%0A    %7D
 %0A%7D%0A

Correct unit tests broken by recent refactorings