Package de.odysseus.calyxo.base.misc

Source Code of de.odysseus.calyxo.base.misc.ContextAccessorTest

/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* 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 de.odysseus.calyxo.base.misc;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.jsp.el.ELException;

import de.odysseus.calyxo.base.test.TestModuleGroup;
import de.odysseus.calyxo.base.test.TestModuleContext;
import de.odysseus.calyxo.base.test.TestPageContext;
import de.odysseus.calyxo.base.test.TestRequest;

import junit.framework.TestCase;

/**
*
* @author Christoph Beck
*/
public class ContextAccessorTest extends TestCase {

  private TestPageContext pageContext;
  private TestModuleContext module;
  private TestRequest request = new TestRequest() {
    public String getContextPath() {
      return "/test";
    }
    public String getScheme() {
      return "http";
    }
    public String getServerName() {
      return "localhost";
    }
    public int getServerPort() {
      return 8081;
    }
  };

  /**
   * Constructor for I18nViewTest.
   * @param arg0
   */
  public ContextAccessorTest(String arg0) {
    super(arg0);
  }

  protected void setUp() throws ServletException {
    pageContext = new TestPageContext(request);
    // configure test module
    module = new TestModuleContext("test", pageContext.getServletContext());
    // configure module container
    ServletContext context = request.getSession().getServletContext();
    TestModuleGroup group = TestModuleGroup.getInstance(context);
    group.add(module);
    // select module
    group.setTestModuleContext(request, module);
  }

  protected Object evaluate(Class type, String expression) throws ELException {
    return pageContext.getExpressionEvaluator().evaluate(
      expression,
      type,
      pageContext.getVariableResolver(),
      null
    );
  }

  public void testPath() throws ELException {
    ContextAccessor accessor = new ContextAccessor(module);
    request.setAttribute("context", accessor.get(request));
    String path = request.getContextPath();
    assertEquals(path, evaluate(String.class, "${context.path}"));
  }

  public void testUrl() throws ELException {
    ContextAccessor accessor = new ContextAccessor(module);
    request.setAttribute("context", accessor.get(request));
    String url =
      request.getScheme() +
      "://" +
      request.getServerName() +
      ":" +
      request.getServerPort() +
      request.getContextPath();
    assertEquals(url, evaluate(String.class, "${context.home}"));
  }

  public static void main(String[] args) {
    junit.textui.TestRunner.run(ContextAccessorTest.class);
  }

}
TOP

Related Classes of de.odysseus.calyxo.base.misc.ContextAccessorTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.