001// Copyright 2007, 2008, 2009, 2013 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.internal.test;
016
017import org.apache.tapestry5.services.Context;
018import org.apache.tapestry5.test.TapestryRunnerConstants;
019
020import java.io.File;
021import java.net.MalformedURLException;
022import java.net.URL;
023import java.util.Collections;
024import java.util.List;
025
026public class PageTesterContext implements Context
027{
028    private final File contextRoot;
029
030    public PageTesterContext(String contextRoot)
031    {
032        this.contextRoot = new File(TapestryRunnerConstants.MODULE_BASE_DIR, contextRoot);
033    }
034
035    public String getInitParameter(String name)
036    {
037        return null;
038    }
039
040    public URL getResource(String path)
041    {
042        File f = new File(contextRoot + path);
043
044        if (!f.exists() || !f.isFile())
045        {
046            return null;
047        }
048        try
049        {
050            return f.toURL();
051        }
052        catch (MalformedURLException ex)
053        {
054            throw new RuntimeException(ex);
055        }
056    }
057
058    public List<String> getResourcePaths(String path)
059    {
060        throw new UnsupportedOperationException("getResourcePaths() is not supported for ContextForPageTester.");
061    }
062
063    public Object getAttribute(String name)
064    {
065        throw new UnsupportedOperationException("getAttribute() is not supported for ContextForPageTester.");
066    }
067
068    public List<String> getAttributeNames()
069    {
070        return Collections.emptyList();
071    }
072
073    public String getMimeType(String file)
074    {
075        return null;
076    }
077
078    /**
079     * Always returns null.
080     */
081    public File getRealFile(String path)
082    {
083        return null;
084    }
085}