/*
* 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.http.HttpServletRequest;
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 junit.framework.TestCase;
/**
*
* @author Christoph Beck
*/
public class ModuleAccessorTest extends TestCase {
private TestPageContext pageContext;
private TestModuleContext module1, module2;
/**
* Constructor for I18nViewTest.
* @param arg0
*/
public ModuleAccessorTest(String arg0) {
super(arg0);
}
protected void setUp() throws ServletException {
pageContext = new TestPageContext();
HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
// configure module container
ServletContext context = request.getSession().getServletContext();
TestModuleGroup group = TestModuleGroup.getInstance(context);
// configure test modules
module1 = new TestModuleContext("test1", pageContext.getServletContext());
group.add(module1);
module2 = new TestModuleContext("test2", pageContext.getServletContext());
group.add(module2);
group.setTestModuleContext(request, module1);
}
protected Object evaluate(Class type, String expression) throws ELException {
return pageContext.getExpressionEvaluator().evaluate(
expression,
type,
pageContext.getVariableResolver(),
null
);
}
public void testName() throws ELException {
HttpServletRequest request =
(HttpServletRequest)pageContext.getRequest();
ModuleAccessor accessor = new ModuleAccessor(module1);
request.setAttribute("module", accessor.get(request));
assertEquals(module1.getName(), evaluate(String.class, "${module.name}"));
}
public void testPath() throws ELException {
HttpServletRequest request =
(HttpServletRequest)pageContext.getRequest();
ModuleAccessor accessor = new ModuleAccessor(module1);
request.setAttribute("module", accessor.get(request));
assertEquals(module1.getPath("action"), evaluate(String.class, "${module.path['action']}"));
}
public void testForName() throws ELException {
HttpServletRequest request =
(HttpServletRequest)pageContext.getRequest();
ModuleAccessor accessor = new ModuleAccessor(module1);
request.setAttribute("module", accessor.get(request));
String expr1 = "${module.forName['" + module1.getName() + "'].name}";
assertSame(module1.getName(), evaluate(String.class, expr1));
String expr2 = "${module.forName['" + module2.getName() + "'].name}";
assertSame(module2.getName(), evaluate(String.class, expr2));
}
public static void main(String[] args) {
junit.textui.TestRunner.run(ModuleAccessorTest.class);
}
}