PageConfigTest.java revision 1271
1270N/A/*
1270N/A * CDDL HEADER START
1270N/A *
1270N/A * The contents of this file are subject to the terms of the
1270N/A * Common Development and Distribution License (the "License").
1270N/A * You may not use this file except in compliance with the License.
1270N/A *
1270N/A * See LICENSE.txt included in this distribution for the specific
1270N/A * language governing permissions and limitations under the License.
1270N/A *
1270N/A * When distributing Covered Code, include this CDDL HEADER in each
1270N/A * file and include the License file at LICENSE.txt.
1270N/A * If applicable, add the following below this CDDL HEADER, with the
1270N/A * fields enclosed by brackets "[]" replaced with your own identifying
1270N/A * information: Portions Copyright [yyyy] [name of copyright owner]
1270N/A *
1270N/A * CDDL HEADER END
1270N/A */
1270N/A
1270N/A/*
1270N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
1270N/A */
1270N/Apackage org.opensolaris.opengrok.web;
1270N/A
1270N/Aimport javax.servlet.http.HttpServletRequest;
1270N/Aimport org.junit.AfterClass;
1270N/Aimport org.junit.BeforeClass;
1270N/Aimport org.junit.Test;
1270N/Aimport org.opensolaris.opengrok.history.HistoryGuru;
1270N/Aimport org.opensolaris.opengrok.util.TestRepository;
1270N/A
1271N/Aimport static org.junit.Assert.*;
1271N/A
1270N/A/**
1270N/A * Unit tests for the {@code PageConfig} class.
1270N/A */
1270N/Apublic class PageConfigTest {
1270N/A private static TestRepository repository = new TestRepository();
1270N/A
1270N/A @BeforeClass
1270N/A public static void setUpClass() throws Exception {
1270N/A repository = new TestRepository();
1270N/A repository.create(
1270N/A HistoryGuru.class.getResourceAsStream("repositories.zip"));
1270N/A HistoryGuru.getInstance().addRepositories(repository.getSourceRoot());
1270N/A }
1270N/A
1270N/A @AfterClass
1270N/A public static void tearDownClass() throws Exception {
1270N/A repository.destroy();
1270N/A repository = null;
1270N/A }
1270N/A
1270N/A @Test
1270N/A public void canProcess() {
1270N/A // Expect no redirection (that is, empty string is returned) for a
1270N/A // file that exists.
1270N/A assertCanProcess("", "/source", "/xref", "/mercurial/main.c");
1270N/A assertCanProcess("", "/source", "/history", "/mercurial/main.c");
1270N/A
1270N/A // Expect directories without trailing slash to get a trailing slash
1270N/A // appended.
1270N/A assertCanProcess("/source/xref/mercurial/",
1270N/A "/source", "/xref", "/mercurial");
1270N/A assertCanProcess("/source/history/mercurial/",
1270N/A "/source", "/history", "/mercurial");
1270N/A
1270N/A // Expect no redirection (that is, empty string is returned) if the
1270N/A // directories already have a trailing slash.
1270N/A assertCanProcess("", "/source", "/xref", "/mercurial/");
1270N/A assertCanProcess("", "/source", "/history", "/mercurial/");
1270N/A
1270N/A // Expect null if the file or directory doesn't exist.
1270N/A assertCanProcess(null, "/source", "/xref", "/mercurial/xyz");
1270N/A assertCanProcess(null, "/source", "/history", "/mercurial/xyz");
1270N/A assertCanProcess(null, "/source", "/xref", "/mercurial/xyz/");
1270N/A assertCanProcess(null, "/source", "/history", "/mercurial/xyz/");
1270N/A }
1270N/A
1270N/A /**
1270N/A * Assert that {@code canProcess()} returns the expected value for the
1270N/A * specified path.
1270N/A *
1270N/A * @param expected the expected return value
1270N/A * @param context the context path
1270N/A * @param servlet the servlet path
1270N/A * @param pathInfo the path info
1270N/A */
1270N/A private void assertCanProcess(
1270N/A String expected, String context, String servlet, String pathInfo) {
1270N/A PageConfig config =
1270N/A PageConfig.get(createRequest(context, servlet, pathInfo));
1270N/A assertEquals(expected, config.canProcess());
1270N/A }
1270N/A
1270N/A /**
1270N/A * Create a request with the specified path elements.
1270N/A *
1270N/A * @param contextPath the context path
1270N/A * @param servletPath the path of the servlet
1270N/A * @param pathInfo the path info
1270N/A * @return a servlet request for the specified path
1270N/A */
1270N/A private static HttpServletRequest createRequest(
1270N/A final String contextPath, final String servletPath,
1270N/A final String pathInfo)
1270N/A {
1270N/A return new DummyHttpServletRequest() {
1270N/A @Override
1270N/A public String getContextPath() {
1270N/A return contextPath;
1270N/A }
1270N/A @Override
1270N/A public String getServletPath() {
1270N/A return servletPath;
1270N/A }
1270N/A @Override
1270N/A public String getPathInfo() {
1270N/A return pathInfo;
1270N/A }
1270N/A };
1270N/A }
1270N/A}