/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* See LICENSE.txt included in this distribution for the specific
* language governing permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at LICENSE.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
*/
package org.opensolaris.opengrok.web;
import javax.servlet.http.HttpServletRequest;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.opensolaris.opengrok.history.HistoryGuru;
import org.opensolaris.opengrok.util.TestRepository;
import static org.junit.Assert.*;
/**
* Unit tests for the {@code PageConfig} class.
*/
public class PageConfigTest {
private static TestRepository repository = new TestRepository();
@SuppressWarnings("javadoc")
@BeforeClass
public static void setUpClass() throws Exception {
repository = new TestRepository();
repository.create(
HistoryGuru.class.getResourceAsStream("repositories.zip"));
HistoryGuru.getInstance().addRepositories(repository.getSourceRoot());
}
@SuppressWarnings("javadoc")
@AfterClass
public static void tearDownClass() {
repository.destroy();
repository = null;
}
@SuppressWarnings({ "javadoc", "static-method" })
@Test
public void canProcess() {
// Expect no redirection (that is, empty string is returned) for a
// file that exists.
assertCanProcess("", "/source", Prefix.XREF_P, "/mercurial/main.c");
assertCanProcess("", "/source", Prefix.HIST_L, "/mercurial/main.c");
assertCanProcess("", "/source", Prefix.DIFF_P, "/mercurial/main.c");
// Expect directories without trailing slash to get a trailing slash
// appended.
assertCanProcess("/source" + Prefix.XREF_P + "/mercurial/",
"/source", Prefix.XREF_P, "/mercurial");
// but not for diffs or pathes with history - pass through
assertCanProcess("", "/source", Prefix.HIST_L, "/mercurial");
assertCanProcess("", "/source", Prefix.DIFF_P, "/mercurial");
// if there is no repo, don't pass
assertCanProcess(null, "/source", Prefix.XREF_P, "/mercurial1");
assertCanProcess(null, "/source", Prefix.HIST_L, "/mercurial1");
// but diff takes care by itself - so always pass
assertCanProcess("", "/source", Prefix.DIFF_P, "/mercurial1");
// Expect no redirection (that is, empty string is returned) if the
// directories already have a trailing slash.
assertCanProcess("", "/source", Prefix.XREF_P, "/mercurial/");
assertCanProcess("", "/source", Prefix.HIST_L, "/mercurial/");
// if the file or directory doesn't exist, don't pass.
assertCanProcess(null, "/source", Prefix.XREF_P, "/mercurial/xyz");
assertCanProcess(null, "/source", Prefix.XREF_P, "/mercurial/xyz/main.c");
// but for history it might be a previously existing and now deleted path
// so let the servelt determine, what to do
assertCanProcess("", "/source", Prefix.HIST_L, "/mercurial/xyz");
assertCanProcess("", "/source", Prefix.HIST_L, "/mercurial/xyz/nomain.c");
assertCanProcess("", "/source", Prefix.HIST_L, "/mercurial/xyz/");
// but diff takes care by itself - so always pass
assertCanProcess("", "/source", Prefix.DIFF_P, "/mercurial/xyz/");
}
/**
* Assert that {@code canProcess()} returns the expected value for the
* specified path.
*
* @param expected the expected return value
* @param context the context path
* @param servlet the servlet path
* @param pathInfo the path info
*/
private static void assertCanProcess(String expected, String context,
Prefix servlet, String pathInfo)
{
PageConfig config = PageConfig
.get(createRequest(context, servlet.toString(), pathInfo));
assertEquals(expected, config.canProcess());
}
/**
* Create a request with the specified path elements.
*
* @param contextPath the context path
* @param servletPath the path of the servlet
* @param pathInfo the path info
* @return a servlet request for the specified path
*/
private static HttpServletRequest createRequest(
final String contextPath, final String servletPath,
final String pathInfo)
{
return new DummyHttpServletRequest() {
@Override
public String getContextPath() {
return contextPath;
}
@Override
public String getServletPath() {
return servletPath;
}
@Override
public String getPathInfo() {
return pathInfo;
}
};
}
}