Moved tests from testsuite to web.servlet
This commit is contained in:
@@ -1,90 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2006 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.web.bind;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.validation.ObjectError;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 02.05.2003
|
||||
*/
|
||||
public class EscapedErrorsTests extends TestCase {
|
||||
|
||||
public void testEscapedErrors() {
|
||||
TestBean tb = new TestBean();
|
||||
tb.setName("empty &");
|
||||
|
||||
Errors errors = new EscapedErrors(new BindException(tb, "tb"));
|
||||
errors.rejectValue("name", "NAME_EMPTY &", null, "message: &");
|
||||
errors.rejectValue("age", "AGE_NOT_SET <tag>", null, "message: <tag>");
|
||||
errors.rejectValue("age", "AGE_NOT_32 <tag>", null, "message: <tag>");
|
||||
errors.reject("GENERAL_ERROR \" '", null, "message: \" '");
|
||||
|
||||
assertTrue("Correct errors flag", errors.hasErrors());
|
||||
assertTrue("Correct number of errors", errors.getErrorCount() == 4);
|
||||
assertTrue("Correct object name", "tb".equals(errors.getObjectName()));
|
||||
|
||||
assertTrue("Correct global errors flag", errors.hasGlobalErrors());
|
||||
assertTrue("Correct number of global errors", errors.getGlobalErrorCount() == 1);
|
||||
ObjectError globalError = errors.getGlobalError();
|
||||
assertTrue("Global error message escaped", "message: " '".equals(globalError.getDefaultMessage()));
|
||||
assertTrue("Global error code not escaped", "GENERAL_ERROR \" '".equals(globalError.getCode()));
|
||||
ObjectError globalErrorInList = (ObjectError) errors.getGlobalErrors().get(0);
|
||||
assertTrue("Same global error in list", globalError.getDefaultMessage().equals(globalErrorInList.getDefaultMessage()));
|
||||
ObjectError globalErrorInAllList = (ObjectError) errors.getAllErrors().get(3);
|
||||
assertTrue("Same global error in list", globalError.getDefaultMessage().equals(globalErrorInAllList.getDefaultMessage()));
|
||||
|
||||
assertTrue("Correct field errors flag", errors.hasFieldErrors());
|
||||
assertTrue("Correct number of field errors", errors.getFieldErrorCount() == 3);
|
||||
assertTrue("Correct number of field errors in list", errors.getFieldErrors().size() == 3);
|
||||
FieldError fieldError = errors.getFieldError();
|
||||
assertTrue("Field error code not escaped", "NAME_EMPTY &".equals(fieldError.getCode()));
|
||||
assertTrue("Field value escaped", "empty &".equals(errors.getFieldValue("name")));
|
||||
FieldError fieldErrorInList = (FieldError) errors.getFieldErrors().get(0);
|
||||
assertTrue("Same field error in list", fieldError.getDefaultMessage().equals(fieldErrorInList.getDefaultMessage()));
|
||||
|
||||
assertTrue("Correct name errors flag", errors.hasFieldErrors("name"));
|
||||
assertTrue("Correct number of name errors", errors.getFieldErrorCount("name") == 1);
|
||||
assertTrue("Correct number of name errors in list", errors.getFieldErrors("name").size() == 1);
|
||||
FieldError nameError = errors.getFieldError("name");
|
||||
assertTrue("Name error message escaped", "message: &".equals(nameError.getDefaultMessage()));
|
||||
assertTrue("Name error code not escaped", "NAME_EMPTY &".equals(nameError.getCode()));
|
||||
assertTrue("Name value escaped", "empty &".equals(errors.getFieldValue("name")));
|
||||
FieldError nameErrorInList = (FieldError) errors.getFieldErrors("name").get(0);
|
||||
assertTrue("Same name error in list", nameError.getDefaultMessage().equals(nameErrorInList.getDefaultMessage()));
|
||||
|
||||
assertTrue("Correct age errors flag", errors.hasFieldErrors("age"));
|
||||
assertTrue("Correct number of age errors", errors.getFieldErrorCount("age") == 2);
|
||||
assertTrue("Correct number of age errors in list", errors.getFieldErrors("age").size() == 2);
|
||||
FieldError ageError = errors.getFieldError("age");
|
||||
assertTrue("Age error message escaped", "message: <tag>".equals(ageError.getDefaultMessage()));
|
||||
assertTrue("Age error code not escaped", "AGE_NOT_SET <tag>".equals(ageError.getCode()));
|
||||
assertTrue("Age value not escaped", (new Integer(0)).equals(errors.getFieldValue("age")));
|
||||
FieldError ageErrorInList = (FieldError) errors.getFieldErrors("age").get(0);
|
||||
assertTrue("Same name error in list", ageError.getDefaultMessage().equals(ageErrorInList.getDefaultMessage()));
|
||||
FieldError ageError2 = (FieldError) errors.getFieldErrors("age").get(1);
|
||||
assertTrue("Age error 2 message escaped", "message: <tag>".equals(ageError2.getDefaultMessage()));
|
||||
assertTrue("Age error 2 code not escaped", "AGE_NOT_32 <tag>".equals(ageError2.getCode()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2006 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.web.jsf;
|
||||
|
||||
import javax.faces.application.NavigationHandler;
|
||||
import javax.faces.context.FacesContext;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.support.StaticListableBeanFactory;
|
||||
|
||||
/**
|
||||
* @author Colin Sampaleanu
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class DelegatingNavigationHandlerTests extends TestCase {
|
||||
|
||||
private MockFacesContext facesContext;
|
||||
private StaticListableBeanFactory beanFactory;
|
||||
private TestNavigationHandler origNavHandler;
|
||||
private DelegatingNavigationHandlerProxy delNavHandler;
|
||||
|
||||
protected void setUp() {
|
||||
facesContext = new MockFacesContext();
|
||||
beanFactory = new StaticListableBeanFactory();
|
||||
origNavHandler = new TestNavigationHandler();
|
||||
|
||||
delNavHandler = new DelegatingNavigationHandlerProxy(origNavHandler) {
|
||||
protected BeanFactory getBeanFactory(FacesContext facesContext) {
|
||||
return beanFactory;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void testHandleNavigationWithoutDecoration() {
|
||||
TestNavigationHandler targetHandler = new TestNavigationHandler();
|
||||
beanFactory.addBean("jsfNavigationHandler", targetHandler);
|
||||
|
||||
delNavHandler.handleNavigation(facesContext, "fromAction", "myViewId");
|
||||
assertEquals(targetHandler.lastFromAction, "fromAction");
|
||||
assertEquals(targetHandler.lastOutcome, "myViewId");
|
||||
}
|
||||
|
||||
public void testHandleNavigationWithDecoration() {
|
||||
TestDecoratingNavigationHandler targetHandler = new TestDecoratingNavigationHandler();
|
||||
beanFactory.addBean("jsfNavigationHandler", targetHandler);
|
||||
|
||||
delNavHandler.handleNavigation(facesContext, "fromAction", "myViewId");
|
||||
assertEquals(targetHandler.lastFromAction, "fromAction");
|
||||
assertEquals(targetHandler.lastOutcome, "myViewId");
|
||||
|
||||
// Original handler must have been invoked as well...
|
||||
assertEquals(origNavHandler.lastFromAction, "fromAction");
|
||||
assertEquals(origNavHandler.lastOutcome, "myViewId");
|
||||
}
|
||||
|
||||
|
||||
public static class TestNavigationHandler extends NavigationHandler {
|
||||
|
||||
private String lastFromAction;
|
||||
private String lastOutcome;
|
||||
|
||||
public void handleNavigation(FacesContext facesContext, String fromAction, String outcome) {
|
||||
lastFromAction = fromAction;
|
||||
lastOutcome = outcome;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class TestDecoratingNavigationHandler extends DecoratingNavigationHandler {
|
||||
|
||||
private String lastFromAction;
|
||||
private String lastOutcome;
|
||||
|
||||
public void handleNavigation(
|
||||
FacesContext facesContext, String fromAction, String outcome, NavigationHandler originalNavigationHandler) {
|
||||
lastFromAction = fromAction;
|
||||
lastOutcome = outcome;
|
||||
if (originalNavigationHandler != null) {
|
||||
originalNavigationHandler.handleNavigation(facesContext, fromAction, outcome);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,101 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2006 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.web.jsf;
|
||||
|
||||
import javax.faces.context.FacesContext;
|
||||
import javax.faces.event.PhaseEvent;
|
||||
import javax.faces.event.PhaseId;
|
||||
import javax.faces.event.PhaseListener;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.beans.factory.ListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.StaticListableBeanFactory;
|
||||
|
||||
/**
|
||||
* @author Colin Sampaleanu
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class DelegatingPhaseListenerTests extends TestCase {
|
||||
|
||||
private MockFacesContext facesContext;
|
||||
private StaticListableBeanFactory beanFactory;
|
||||
private DelegatingPhaseListenerMulticaster delPhaseListener;
|
||||
|
||||
protected void setUp() {
|
||||
facesContext = new MockFacesContext();
|
||||
beanFactory = new StaticListableBeanFactory();
|
||||
|
||||
delPhaseListener = new DelegatingPhaseListenerMulticaster() {
|
||||
protected ListableBeanFactory getBeanFactory(FacesContext facesContext) {
|
||||
return beanFactory;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void testBeforeAndAfterPhaseWithSingleTarget() {
|
||||
TestListener target = new TestListener();
|
||||
beanFactory.addBean("testListener", target);
|
||||
|
||||
assertEquals(delPhaseListener.getPhaseId(), PhaseId.ANY_PHASE);
|
||||
PhaseEvent event = new PhaseEvent(facesContext, PhaseId.INVOKE_APPLICATION, new MockLifecycle());
|
||||
|
||||
delPhaseListener.beforePhase(event);
|
||||
assertTrue(target.beforeCalled);
|
||||
|
||||
delPhaseListener.afterPhase(event);
|
||||
assertTrue(target.afterCalled);
|
||||
}
|
||||
|
||||
public void testBeforeAndAfterPhaseWithMultipleTargets() {
|
||||
TestListener target1 = new TestListener();
|
||||
TestListener target2 = new TestListener();
|
||||
beanFactory.addBean("testListener1", target1);
|
||||
beanFactory.addBean("testListener2", target2);
|
||||
|
||||
assertEquals(delPhaseListener.getPhaseId(), PhaseId.ANY_PHASE);
|
||||
PhaseEvent event = new PhaseEvent(facesContext, PhaseId.INVOKE_APPLICATION, new MockLifecycle());
|
||||
|
||||
delPhaseListener.beforePhase(event);
|
||||
assertTrue(target1.beforeCalled);
|
||||
assertTrue(target2.beforeCalled);
|
||||
|
||||
delPhaseListener.afterPhase(event);
|
||||
assertTrue(target1.afterCalled);
|
||||
assertTrue(target2.afterCalled);
|
||||
}
|
||||
|
||||
|
||||
public static class TestListener implements PhaseListener {
|
||||
|
||||
boolean beforeCalled = false;
|
||||
boolean afterCalled = false;
|
||||
|
||||
public PhaseId getPhaseId() {
|
||||
return PhaseId.ANY_PHASE;
|
||||
}
|
||||
|
||||
public void beforePhase(PhaseEvent arg0) {
|
||||
beforeCalled = true;
|
||||
}
|
||||
|
||||
public void afterPhase(PhaseEvent arg0) {
|
||||
afterCalled = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.web.jsf;
|
||||
|
||||
import javax.faces.context.FacesContext;
|
||||
import javax.faces.el.EvaluationException;
|
||||
import javax.faces.el.VariableResolver;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 02.08.2004
|
||||
*/
|
||||
public class DelegatingVariableResolverTests extends TestCase {
|
||||
|
||||
public void testDelegatingVariableResolver() {
|
||||
final StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.registerSingleton("bean1", TestBean.class, null);
|
||||
wac.registerSingleton("var1", TestBean.class, null);
|
||||
wac.refresh();
|
||||
TestBean bean1 = (TestBean) wac.getBean("bean1");
|
||||
|
||||
// We need to override the getWebApplicationContext method here:
|
||||
// FacesContext and ExternalContext are hard to mock.
|
||||
DelegatingVariableResolver resolver = new DelegatingVariableResolver(new OriginalVariableResolver()) {
|
||||
protected WebApplicationContext getWebApplicationContext(FacesContext facesContext) {
|
||||
return wac;
|
||||
}
|
||||
};
|
||||
assertEquals(bean1, resolver.resolveVariable(null, "bean1"));
|
||||
assertEquals("val1", resolver.resolveVariable(null, "var1"));
|
||||
}
|
||||
|
||||
public void testSpringBeanVariableResolver() {
|
||||
final StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.registerSingleton("bean1", TestBean.class, null);
|
||||
wac.registerSingleton("var1", TestBean.class, null);
|
||||
wac.refresh();
|
||||
TestBean bean1 = (TestBean) wac.getBean("bean1");
|
||||
TestBean var1 = (TestBean) wac.getBean("var1");
|
||||
|
||||
// We need to override the getWebApplicationContext method here:
|
||||
// FacesContext and ExternalContext are hard to mock.
|
||||
SpringBeanVariableResolver resolver = new SpringBeanVariableResolver(new OriginalVariableResolver()) {
|
||||
protected WebApplicationContext getWebApplicationContext(FacesContext facesContext) {
|
||||
return wac;
|
||||
}
|
||||
};
|
||||
assertEquals(bean1, resolver.resolveVariable(null, "bean1"));
|
||||
assertEquals(var1, resolver.resolveVariable(null, "var1"));
|
||||
}
|
||||
|
||||
|
||||
private static class OriginalVariableResolver extends VariableResolver {
|
||||
|
||||
public Object resolveVariable(FacesContext facesContext, String name) throws EvaluationException {
|
||||
if ("var1".equals(name)) {
|
||||
return "val1";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user