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
1466N/A @SuppressWarnings("javadoc")
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
1466N/A @SuppressWarnings("javadoc")
1270N/A @AfterClass
1466N/A public static void tearDownClass() {
1270N/A repository.destroy();
1270N/A repository = null;
1270N/A }
1270N/A
1466N/A @SuppressWarnings({ "javadoc", "static-method" })
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.
1419N/A assertCanProcess("", "/source", Prefix.XREF_P, "/mercurial/main.c");
1419N/A assertCanProcess("", "/source", Prefix.HIST_L, "/mercurial/main.c");
1466N/A assertCanProcess("", "/source", Prefix.DIFF_P, "/mercurial/main.c");
1270N/A
1270N/A // Expect directories without trailing slash to get a trailing slash
1270N/A // appended.
1419N/A assertCanProcess("/source" + Prefix.XREF_P + "/mercurial/",
1419N/A "/source", Prefix.XREF_P, "/mercurial");
1466N/A // but not for diffs or pathes with history - pass through
1466N/A assertCanProcess("", "/source", Prefix.HIST_L, "/mercurial");
1466N/A assertCanProcess("", "/source", Prefix.DIFF_P, "/mercurial");
1466N/A
1466N/A // if there is no repo, don't pass
1466N/A assertCanProcess(null, "/source", Prefix.XREF_P, "/mercurial1");
1466N/A assertCanProcess(null, "/source", Prefix.HIST_L, "/mercurial1");
1466N/A // but diff takes care by itself - so always pass
1466N/A assertCanProcess("", "/source", Prefix.DIFF_P, "/mercurial1");
1270N/A
1270N/A // Expect no redirection (that is, empty string is returned) if the
1270N/A // directories already have a trailing slash.
1419N/A assertCanProcess("", "/source", Prefix.XREF_P, "/mercurial/");
1419N/A assertCanProcess("", "/source", Prefix.HIST_L, "/mercurial/");
1270N/A
1466N/A // if the file or directory doesn't exist, don't pass.
1419N/A assertCanProcess(null, "/source", Prefix.XREF_P, "/mercurial/xyz");
1466N/A assertCanProcess(null, "/source", Prefix.XREF_P, "/mercurial/xyz/main.c");
1466N/A // but for history it might be a previously existing and now deleted path
1466N/A // so let the servelt determine, what to do
1466N/A assertCanProcess("", "/source", Prefix.HIST_L, "/mercurial/xyz");
1466N/A assertCanProcess("", "/source", Prefix.HIST_L, "/mercurial/xyz/nomain.c");
1466N/A assertCanProcess("", "/source", Prefix.HIST_L, "/mercurial/xyz/");
1466N/A // but diff takes care by itself - so always pass
1466N/A assertCanProcess("", "/source", Prefix.DIFF_P, "/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 */
1466N/A private static void assertCanProcess(String expected, String context,
1466N/A Prefix servlet, String pathInfo)
1466N/A {
1466N/A PageConfig config = PageConfig
1466N/A .get(createRequest(context, servlet.toString(), 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}