Migrate JUnit 3 tests to JUnit 4

This commit migrates all remaining tests from JUnit 3 to JUnit 4, with
the exception of Spring's legacy JUnit 3.8 based testing framework that
is still in use in the spring-orm module.

Issue: SPR-13514
This commit is contained in:
Sam Brannen
2015-09-26 00:10:58 +02:00
parent 1580288815
commit d5ee787e1e
213 changed files with 4879 additions and 3939 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 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.
@@ -32,9 +32,10 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import junit.framework.TestCase;
import org.aopalliance.intercept.MethodInvocation;
import org.junit.Test;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
@@ -46,17 +47,21 @@ import org.springframework.remoting.support.RemoteInvocationResult;
import org.springframework.tests.sample.beans.ITestBean;
import org.springframework.tests.sample.beans.TestBean;
import static org.junit.Assert.*;
/**
* @author Juergen Hoeller
* @since 09.08.2004
*/
public class HttpInvokerTests extends TestCase {
public class HttpInvokerTests {
public void testHttpInvokerProxyFactoryBeanAndServiceExporter() throws Throwable {
@Test
public void httpInvokerProxyFactoryBeanAndServiceExporter() throws Throwable {
doTestHttpInvokerProxyFactoryBeanAndServiceExporter(false);
}
public void testHttpInvokerProxyFactoryBeanAndServiceExporterWithExplicitClassLoader() throws Throwable {
@Test
public void httpInvokerProxyFactoryBeanAndServiceExporterWithExplicitClassLoader() throws Throwable {
doTestHttpInvokerProxyFactoryBeanAndServiceExporter(true);
}
@@ -126,7 +131,8 @@ public class HttpInvokerTests extends TestCase {
}
}
public void testHttpInvokerProxyFactoryBeanAndServiceExporterWithIOException() throws Exception {
@Test
public void httpInvokerProxyFactoryBeanAndServiceExporterWithIOException() throws Exception {
TestBean target = new TestBean("myname", 99);
final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
@@ -158,7 +164,8 @@ public class HttpInvokerTests extends TestCase {
}
}
public void testHttpInvokerProxyFactoryBeanAndServiceExporterWithGzipCompression() throws Throwable {
@Test
public void httpInvokerProxyFactoryBeanAndServiceExporterWithGzipCompression() throws Throwable {
TestBean target = new TestBean("myname", 99);
final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter() {
@@ -242,7 +249,8 @@ public class HttpInvokerTests extends TestCase {
}
}
public void testHttpInvokerProxyFactoryBeanAndServiceExporterWithWrappedInvocations() throws Throwable {
@Test
public void httpInvokerProxyFactoryBeanAndServiceExporterWithWrappedInvocations() throws Throwable {
TestBean target = new TestBean("myname", 99);
final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter() {
@@ -321,7 +329,8 @@ public class HttpInvokerTests extends TestCase {
}
}
public void testHttpInvokerProxyFactoryBeanAndServiceExporterWithInvocationAttributes() throws Exception {
@Test
public void httpInvokerProxyFactoryBeanAndServiceExporterWithInvocationAttributes() throws Exception {
TestBean target = new TestBean("myname", 99);
final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
@@ -383,7 +392,8 @@ public class HttpInvokerTests extends TestCase {
assertEquals(99, proxy.getAge());
}
public void testHttpInvokerProxyFactoryBeanAndServiceExporterWithCustomInvocationObject() throws Exception {
@Test
public void httpInvokerProxyFactoryBeanAndServiceExporterWithCustomInvocationObject() throws Exception {
TestBean target = new TestBean("myname", 99);
final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
@@ -434,7 +444,8 @@ public class HttpInvokerTests extends TestCase {
assertEquals(99, proxy.getAge());
}
public void testHttpInvokerWithSpecialLocalMethods() throws Exception {
@Test
public void httpInvokerWithSpecialLocalMethods() throws Exception {
String serviceUrl = "http://myurl";
HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
pfb.setServiceInterface(ITestBean.class);

View File

@@ -100,7 +100,7 @@ public class WebRequestDataBinderIntegrationTests {
@Test
public void testPartsBinding() {
public void partsBinding() {
PartsBean bean = new PartsBean();
partsServlet.setBean(bean);
@@ -117,7 +117,7 @@ public class WebRequestDataBinderIntegrationTests {
}
@Test
public void testPartListBinding() {
public void partListBinding() {
PartListBean bean = new PartListBean();
partListServlet.setBean(bean);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 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.
@@ -18,18 +18,21 @@ package org.springframework.web.context.request;
import javax.servlet.ServletRequestEvent;
import junit.framework.TestCase;
import org.junit.Test;
import org.springframework.core.task.MockRunnable;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockServletContext;
import static org.junit.Assert.*;
/**
* @author Juergen Hoeller
*/
public class RequestContextListenerTests extends TestCase {
public class RequestContextListenerTests {
public void testRequestContextListenerWithSameThread() {
@Test
public void requestContextListenerWithSameThread() {
RequestContextListener listener = new RequestContextListener();
MockServletContext context = new MockServletContext();
MockHttpServletRequest request = new MockHttpServletRequest(context);
@@ -49,7 +52,8 @@ public class RequestContextListenerTests extends TestCase {
assertTrue(runnable.wasExecuted());
}
public void testRequestContextListenerWithSameThreadAndAttributesGone() {
@Test
public void requestContextListenerWithSameThreadAndAttributesGone() {
RequestContextListener listener = new RequestContextListener();
MockServletContext context = new MockServletContext();
MockHttpServletRequest request = new MockHttpServletRequest(context);
@@ -70,7 +74,8 @@ public class RequestContextListenerTests extends TestCase {
assertTrue(runnable.wasExecuted());
}
public void testRequestContextListenerWithDifferentThread() {
@Test
public void requestContextListenerWithDifferentThread() {
final RequestContextListener listener = new RequestContextListener();
final MockServletContext context = new MockServletContext();
final MockHttpServletRequest request = new MockHttpServletRequest(context);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 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.
@@ -16,7 +16,9 @@
package org.springframework.web.context.request;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanCurrentlyInCreationException;
@@ -29,18 +31,22 @@ import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.tests.sample.beans.DerivedTestBean;
import org.springframework.tests.sample.beans.TestBean;
import static org.junit.Assert.*;
/**
* @author Rob Harrop
* @author Juergen Hoeller
* @author Mark Fisher
* @author Sam Brannen
* @see SessionScopeTests
*/
public class RequestScopeTests extends TestCase {
public class RequestScopeTests {
private DefaultListableBeanFactory beanFactory;
private final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
@Override
protected void setUp() throws Exception {
this.beanFactory = new DefaultListableBeanFactory();
@Before
public void setUp() throws Exception {
this.beanFactory.registerScope("request", new RequestScope());
this.beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver());
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory);
@@ -48,63 +54,57 @@ public class RequestScopeTests extends TestCase {
this.beanFactory.preInstantiateSingletons();
}
public void testGetFromScope() throws Exception {
@After
public void resetRequestAttributes() {
RequestContextHolder.setRequestAttributes(null);
}
@Test
public void getFromScope() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setContextPath("/path");
RequestAttributes requestAttributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(requestAttributes);
try {
String name = "requestScopedObject";
assertNull(request.getAttribute(name));
TestBean bean = (TestBean) this.beanFactory.getBean(name);
assertEquals("/path", bean.getName());
assertSame(bean, request.getAttribute(name));
assertSame(bean, this.beanFactory.getBean(name));
}
finally {
RequestContextHolder.setRequestAttributes(null);
}
String name = "requestScopedObject";
assertNull(request.getAttribute(name));
TestBean bean = (TestBean) this.beanFactory.getBean(name);
assertEquals("/path", bean.getName());
assertSame(bean, request.getAttribute(name));
assertSame(bean, this.beanFactory.getBean(name));
}
public void testDestructionAtRequestCompletion() throws Exception {
@Test
public void destructionAtRequestCompletion() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(requestAttributes);
try {
String name = "requestScopedDisposableObject";
assertNull(request.getAttribute(name));
DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name);
assertSame(bean, request.getAttribute(name));
assertSame(bean, this.beanFactory.getBean(name));
String name = "requestScopedDisposableObject";
assertNull(request.getAttribute(name));
DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name);
assertSame(bean, request.getAttribute(name));
assertSame(bean, this.beanFactory.getBean(name));
requestAttributes.requestCompleted();
assertTrue(bean.wasDestroyed());
}
finally {
RequestContextHolder.setRequestAttributes(null);
}
requestAttributes.requestCompleted();
assertTrue(bean.wasDestroyed());
}
public void testGetFromFactoryBeanInScope() throws Exception {
@Test
public void getFromFactoryBeanInScope() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
RequestAttributes requestAttributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(requestAttributes);
try {
String name = "requestScopedFactoryBean";
assertNull(request.getAttribute(name));
TestBean bean = (TestBean) this.beanFactory.getBean(name);
assertTrue(request.getAttribute(name) instanceof FactoryBean);
assertSame(bean, this.beanFactory.getBean(name));
}
finally {
RequestContextHolder.setRequestAttributes(null);
}
String name = "requestScopedFactoryBean";
assertNull(request.getAttribute(name));
TestBean bean = (TestBean) this.beanFactory.getBean(name);
assertTrue(request.getAttribute(name) instanceof FactoryBean);
assertSame(bean, this.beanFactory.getBean(name));
}
public void testCircleLeadsToException() throws Exception {
@Test
public void circleLeadsToException() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
RequestAttributes requestAttributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(requestAttributes);
@@ -112,65 +112,54 @@ public class RequestScopeTests extends TestCase {
try {
String name = "requestScopedObjectCircle1";
assertNull(request.getAttribute(name));
this.beanFactory.getBean(name);
fail("Should have thrown BeanCreationException");
}
catch (BeanCreationException ex) {
// expected
assertTrue(ex.contains(BeanCurrentlyInCreationException.class));
}
finally {
RequestContextHolder.setRequestAttributes(null);
}
}
public void testInnerBeanInheritsContainingBeanScopeByDefault() {
@Test
public void innerBeanInheritsContainingBeanScopeByDefault() {
MockHttpServletRequest request = new MockHttpServletRequest();
ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(requestAttributes);
try {
String outerBeanName = "requestScopedOuterBean";
assertNull(request.getAttribute(outerBeanName));
TestBean outer1 = (TestBean) this.beanFactory.getBean(outerBeanName);
assertNotNull(request.getAttribute(outerBeanName));
TestBean inner1 = (TestBean) outer1.getSpouse();
assertSame(outer1, this.beanFactory.getBean(outerBeanName));
requestAttributes.requestCompleted();
assertTrue(outer1.wasDestroyed());
assertTrue(inner1.wasDestroyed());
request = new MockHttpServletRequest();
requestAttributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(requestAttributes);
TestBean outer2 = (TestBean) this.beanFactory.getBean(outerBeanName);
assertNotSame(outer1, outer2);
assertNotSame(inner1, outer2.getSpouse());
}
finally {
RequestContextHolder.setRequestAttributes(null);
}
String outerBeanName = "requestScopedOuterBean";
assertNull(request.getAttribute(outerBeanName));
TestBean outer1 = (TestBean) this.beanFactory.getBean(outerBeanName);
assertNotNull(request.getAttribute(outerBeanName));
TestBean inner1 = (TestBean) outer1.getSpouse();
assertSame(outer1, this.beanFactory.getBean(outerBeanName));
requestAttributes.requestCompleted();
assertTrue(outer1.wasDestroyed());
assertTrue(inner1.wasDestroyed());
request = new MockHttpServletRequest();
requestAttributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(requestAttributes);
TestBean outer2 = (TestBean) this.beanFactory.getBean(outerBeanName);
assertNotSame(outer1, outer2);
assertNotSame(inner1, outer2.getSpouse());
}
public void testRequestScopedInnerBeanDestroyedWhileContainedBySingleton() throws Exception {
@Test
public void requestScopedInnerBeanDestroyedWhileContainedBySingleton() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(requestAttributes);
try {
String outerBeanName = "singletonOuterBean";
TestBean outer1 = (TestBean) this.beanFactory.getBean(outerBeanName);
assertNull(request.getAttribute(outerBeanName));
TestBean inner1 = (TestBean) outer1.getSpouse();
TestBean outer2 = (TestBean) this.beanFactory.getBean(outerBeanName);
assertSame(outer1, outer2);
assertSame(inner1, outer2.getSpouse());
requestAttributes.requestCompleted();
assertTrue(inner1.wasDestroyed());
assertFalse(outer1.wasDestroyed());
}
finally {
RequestContextHolder.setRequestAttributes(null);
}
String outerBeanName = "singletonOuterBean";
TestBean outer1 = (TestBean) this.beanFactory.getBean(outerBeanName);
assertNull(request.getAttribute(outerBeanName));
TestBean inner1 = (TestBean) outer1.getSpouse();
TestBean outer2 = (TestBean) this.beanFactory.getBean(outerBeanName);
assertSame(outer1, outer2);
assertSame(inner1, outer2.getSpouse());
requestAttributes.requestCompleted();
assertTrue(inner1.wasDestroyed());
assertFalse(outer1.wasDestroyed());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 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.
@@ -18,7 +18,9 @@ package org.springframework.web.context.request;
import java.io.Serializable;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
@@ -32,74 +34,78 @@ import org.springframework.tests.sample.beans.DerivedTestBean;
import org.springframework.tests.sample.beans.TestBean;
import org.springframework.util.SerializationTestUtils;
import static org.junit.Assert.*;
/**
* @author Rob Harrop
* @author Juergen Hoeller
* @author Sam Brannen
* @see RequestScopeTests
*/
public class SessionScopeTests extends TestCase {
public class SessionScopeTests {
private DefaultListableBeanFactory beanFactory;
private final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
@Override
protected void setUp() throws Exception {
this.beanFactory = new DefaultListableBeanFactory();
@Before
public void setUp() throws Exception {
this.beanFactory.registerScope("session", new SessionScope());
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory);
reader.loadBeanDefinitions(new ClassPathResource("sessionScopeTests.xml", getClass()));
}
public void testGetFromScope() throws Exception {
@After
public void resetRequestAttributes() {
RequestContextHolder.setRequestAttributes(null);
}
@Test
public void getFromScope() throws Exception {
MockHttpSession session = new MockHttpSession();
MockHttpServletRequest request = new MockHttpServletRequest();
request.setSession(session);
ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(requestAttributes);
try {
String name = "sessionScopedObject";
assertNull(session.getAttribute(name));
TestBean bean = (TestBean) this.beanFactory.getBean(name);
assertEquals(session.getAttribute(name), bean);
assertSame(bean, this.beanFactory.getBean(name));
}
finally {
RequestContextHolder.setRequestAttributes(null);
}
String name = "sessionScopedObject";
assertNull(session.getAttribute(name));
TestBean bean = (TestBean) this.beanFactory.getBean(name);
assertEquals(session.getAttribute(name), bean);
assertSame(bean, this.beanFactory.getBean(name));
}
public void testDestructionAtSessionTermination() throws Exception {
@Test
public void destructionAtSessionTermination() throws Exception {
MockHttpSession session = new MockHttpSession();
MockHttpServletRequest request = new MockHttpServletRequest();
request.setSession(session);
ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(requestAttributes);
try {
String name = "sessionScopedDisposableObject";
assertNull(session.getAttribute(name));
DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name);
assertEquals(session.getAttribute(name), bean);
assertSame(bean, this.beanFactory.getBean(name));
String name = "sessionScopedDisposableObject";
assertNull(session.getAttribute(name));
DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name);
assertEquals(session.getAttribute(name), bean);
assertSame(bean, this.beanFactory.getBean(name));
requestAttributes.requestCompleted();
session.invalidate();
assertTrue(bean.wasDestroyed());
}
finally {
RequestContextHolder.setRequestAttributes(null);
}
requestAttributes.requestCompleted();
session.invalidate();
assertTrue(bean.wasDestroyed());
}
public void testDestructionWithSessionSerialization() throws Exception {
@Test
public void destructionWithSessionSerialization() throws Exception {
doTestDestructionWithSessionSerialization(false);
}
public void testDestructionWithSessionSerializationAndBeanPostProcessor() throws Exception {
@Test
public void destructionWithSessionSerializationAndBeanPostProcessor() throws Exception {
this.beanFactory.addBeanPostProcessor(new CustomDestructionAwareBeanPostProcessor());
doTestDestructionWithSessionSerialization(false);
}
public void testDestructionWithSessionSerializationAndSerializableBeanPostProcessor() throws Exception {
@Test
public void destructionWithSessionSerializationAndSerializableBeanPostProcessor() throws Exception {
this.beanFactory.addBeanPostProcessor(new CustomSerializableDestructionAwareBeanPostProcessor());
doTestDestructionWithSessionSerialization(true);
}
@@ -113,20 +119,15 @@ public class SessionScopeTests extends TestCase {
ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(requestAttributes);
try {
String name = "sessionScopedDisposableObject";
assertNull(session.getAttribute(name));
DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name);
assertEquals(session.getAttribute(name), bean);
assertSame(bean, this.beanFactory.getBean(name));
String name = "sessionScopedDisposableObject";
assertNull(session.getAttribute(name));
DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name);
assertEquals(session.getAttribute(name), bean);
assertSame(bean, this.beanFactory.getBean(name));
requestAttributes.requestCompleted();
serializedState = session.serializeState();
assertFalse(bean.wasDestroyed());
}
finally {
RequestContextHolder.setRequestAttributes(null);
}
requestAttributes.requestCompleted();
serializedState = session.serializeState();
assertFalse(bean.wasDestroyed());
serializedState = (Serializable) SerializationTestUtils.serializeAndDeserialize(serializedState);
@@ -137,26 +138,21 @@ public class SessionScopeTests extends TestCase {
requestAttributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(requestAttributes);
try {
String name = "sessionScopedDisposableObject";
assertNotNull(session.getAttribute(name));
DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name);
assertEquals(session.getAttribute(name), bean);
assertSame(bean, this.beanFactory.getBean(name));
name = "sessionScopedDisposableObject";
assertNotNull(session.getAttribute(name));
bean = (DerivedTestBean) this.beanFactory.getBean(name);
assertEquals(session.getAttribute(name), bean);
assertSame(bean, this.beanFactory.getBean(name));
requestAttributes.requestCompleted();
session.invalidate();
assertTrue(bean.wasDestroyed());
requestAttributes.requestCompleted();
session.invalidate();
assertTrue(bean.wasDestroyed());
if (beanNameReset) {
assertNull(bean.getBeanName());
}
else {
assertNotNull(bean.getBeanName());
}
if (beanNameReset) {
assertNull(bean.getBeanName());
}
finally {
RequestContextHolder.setRequestAttributes(null);
else {
assertNotNull(bean.getBeanName());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 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.
@@ -20,7 +20,7 @@ import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import junit.framework.TestCase;
import org.junit.Test;
import org.springframework.mock.web.test.MockFilterConfig;
import org.springframework.mock.web.test.MockHttpServletResponse;
@@ -33,14 +33,15 @@ import static org.mockito.BDDMockito.*;
* @author Rick Evans
* @author Juergen Hoeller
*/
public class CharacterEncodingFilterTests extends TestCase {
public class CharacterEncodingFilterTests {
private static final String FILTER_NAME = "boot";
private static final String ENCODING = "UTF-8";
public void testForceAlwaysSetsEncoding() throws Exception {
@Test
public void forceEncodingAlwaysSetsEncoding() throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class);
request.setCharacterEncoding(ENCODING);
given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
@@ -61,7 +62,8 @@ public class CharacterEncodingFilterTests extends TestCase {
verify(filterChain).doFilter(request, response);
}
public void testEncodingIfEmptyAndNotForced() throws Exception {
@Test
public void encodingIfEmptyAndNotForced() throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class);
given(request.getCharacterEncoding()).willReturn(null);
given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
@@ -83,7 +85,8 @@ public class CharacterEncodingFilterTests extends TestCase {
verify(filterChain).doFilter(request, response);
}
public void testDoesNowtIfEncodingIsNotEmptyAndNotForced() throws Exception {
@Test
public void doesNowtIfEncodingIsNotEmptyAndNotForced() throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class);
given(request.getCharacterEncoding()).willReturn(ENCODING);
given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
@@ -103,7 +106,8 @@ public class CharacterEncodingFilterTests extends TestCase {
verify(filterChain).doFilter(request, response);
}
public void testWithBeanInitialization() throws Exception {
@Test
public void withBeanInitialization() throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class);
given(request.getCharacterEncoding()).willReturn(null);
given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
@@ -125,7 +129,8 @@ public class CharacterEncodingFilterTests extends TestCase {
verify(filterChain).doFilter(request, response);
}
public void testWithIncompleteInitialization() throws Exception {
@Test
public void withIncompleteInitialization() throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class);
given(request.getCharacterEncoding()).willReturn(null);
given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
@@ -144,4 +149,5 @@ public class CharacterEncodingFilterTests extends TestCase {
verify(request).removeAttribute(CharacterEncodingFilter.class.getName() + OncePerRequestFilter.ALREADY_FILTERED_SUFFIX);
verify(filterChain).doFilter(request, response);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 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.
@@ -17,12 +17,13 @@
package org.springframework.web.filter;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import junit.framework.TestCase;
import org.junit.Test;
import org.springframework.mock.web.test.MockFilterConfig;
import org.springframework.mock.web.test.MockHttpServletRequest;
@@ -31,21 +32,25 @@ import org.springframework.mock.web.test.MockServletContext;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import static org.junit.Assert.*;
/**
* @author Rod Johnson
* @author Juergen Hoeller
*/
public class RequestContextFilterTests extends TestCase {
public class RequestContextFilterTests {
public void testHappyPath() throws Exception {
@Test
public void happyPath() throws Exception {
testFilterInvocation(null);
}
public void testWithException() throws Exception {
@Test
public void withException() throws Exception {
testFilterInvocation(new ServletException());
}
public void testFilterInvocation(final ServletException sex) throws Exception {
private void testFilterInvocation(final ServletException sex) throws Exception {
final MockHttpServletRequest req = new MockHttpServletRequest();
req.setAttribute("myAttr", "myValue");
final MockHttpServletResponse resp = new MockHttpServletResponse();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2006 the original author or authors.
* Copyright 2002-2015 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.
@@ -19,37 +19,35 @@ package org.springframework.web.jsf;
import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;
import junit.framework.TestCase;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.StaticListableBeanFactory;
import static org.junit.Assert.*;
/**
* @author Colin Sampaleanu
* @author Juergen Hoeller
*/
public class DelegatingNavigationHandlerTests extends TestCase {
public class DelegatingNavigationHandlerTests {
private MockFacesContext facesContext;
private StaticListableBeanFactory beanFactory;
private TestNavigationHandler origNavHandler;
private DelegatingNavigationHandlerProxy delNavHandler;
private final MockFacesContext facesContext = new MockFacesContext();
@Override
protected void setUp() {
facesContext = new MockFacesContext();
beanFactory = new StaticListableBeanFactory();
origNavHandler = new TestNavigationHandler();
private final StaticListableBeanFactory beanFactory = new StaticListableBeanFactory();
delNavHandler = new DelegatingNavigationHandlerProxy(origNavHandler) {
@Override
protected BeanFactory getBeanFactory(FacesContext facesContext) {
return beanFactory;
}
};
}
private final TestNavigationHandler origNavHandler = new TestNavigationHandler();
public void testHandleNavigationWithoutDecoration() {
private final DelegatingNavigationHandlerProxy delNavHandler = new DelegatingNavigationHandlerProxy(origNavHandler) {
@Override
protected BeanFactory getBeanFactory(FacesContext facesContext) {
return beanFactory;
}
};
@Test
public void handleNavigationWithoutDecoration() {
TestNavigationHandler targetHandler = new TestNavigationHandler();
beanFactory.addBean("jsfNavigationHandler", targetHandler);
@@ -58,7 +56,8 @@ public class DelegatingNavigationHandlerTests extends TestCase {
assertEquals("myViewId", targetHandler.lastOutcome);
}
public void testHandleNavigationWithDecoration() {
@Test
public void handleNavigationWithDecoration() {
TestDecoratingNavigationHandler targetHandler = new TestDecoratingNavigationHandler();
beanFactory.addBean("jsfNavigationHandler", targetHandler);
@@ -72,7 +71,7 @@ public class DelegatingNavigationHandlerTests extends TestCase {
}
public static class TestNavigationHandler extends NavigationHandler {
static class TestNavigationHandler extends NavigationHandler {
private String lastFromAction;
private String lastOutcome;
@@ -85,7 +84,7 @@ public class DelegatingNavigationHandlerTests extends TestCase {
}
public static class TestDecoratingNavigationHandler extends DecoratingNavigationHandler {
static class TestDecoratingNavigationHandler extends DecoratingNavigationHandler {
private String lastFromAction;
private String lastOutcome;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 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.
@@ -21,36 +21,33 @@ import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import junit.framework.TestCase;
import org.junit.Test;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.support.StaticListableBeanFactory;
import static org.junit.Assert.*;
/**
* @author Colin Sampaleanu
* @author Juergen Hoeller
*/
public class DelegatingPhaseListenerTests extends TestCase {
public class DelegatingPhaseListenerTests {
private MockFacesContext facesContext;
private StaticListableBeanFactory beanFactory;
private DelegatingPhaseListenerMulticaster delPhaseListener;
private final MockFacesContext facesContext = new MockFacesContext();
private final StaticListableBeanFactory beanFactory = new StaticListableBeanFactory();
@Override
@SuppressWarnings("serial")
protected void setUp() {
facesContext = new MockFacesContext();
beanFactory = new StaticListableBeanFactory();
private final DelegatingPhaseListenerMulticaster delPhaseListener = new DelegatingPhaseListenerMulticaster() {
@Override
protected ListableBeanFactory getBeanFactory(FacesContext facesContext) {
return beanFactory;
}
};
delPhaseListener = new DelegatingPhaseListenerMulticaster() {
@Override
protected ListableBeanFactory getBeanFactory(FacesContext facesContext) {
return beanFactory;
}
};
}
public void testBeforeAndAfterPhaseWithSingleTarget() {
@Test
public void beforeAndAfterPhaseWithSingleTarget() {
TestListener target = new TestListener();
beanFactory.addBean("testListener", target);
@@ -64,7 +61,8 @@ public class DelegatingPhaseListenerTests extends TestCase {
assertTrue(target.afterCalled);
}
public void testBeforeAndAfterPhaseWithMultipleTargets() {
@Test
public void beforeAndAfterPhaseWithMultipleTargets() {
TestListener target1 = new TestListener();
TestListener target2 = new TestListener();
beanFactory.addBean("testListener1", target1);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 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.
@@ -18,33 +18,37 @@ package org.springframework.web.multipart.support;
import java.io.IOException;
import junit.framework.TestCase;
import org.junit.Test;
import org.springframework.web.multipart.MultipartFile;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
/**
* @author Rick Evans
* @author Sam Brannen
*/
public final class ByteArrayMultipartFileEditorTests extends TestCase {
public final class ByteArrayMultipartFileEditorTests {
public void testSetValueAsByteArray() throws Exception {
ByteArrayMultipartFileEditor editor = new ByteArrayMultipartFileEditor();
private final ByteArrayMultipartFileEditor editor = new ByteArrayMultipartFileEditor();
@Test
public void setValueAsByteArray() throws Exception {
String expectedValue = "Shumwere, shumhow, a shuck ish washing you. - Drunken Far Side";
editor.setValue(expectedValue.getBytes());
assertEquals(expectedValue, editor.getAsText());
}
public void testSetValueAsString() throws Exception {
ByteArrayMultipartFileEditor editor = new ByteArrayMultipartFileEditor();
@Test
public void setValueAsString() throws Exception {
String expectedValue = "'Green Wing' - classic British comedy";
editor.setValue(expectedValue);
assertEquals(expectedValue, editor.getAsText());
}
public void testSetValueAsCustomObjectInvokesToString() throws Exception {
ByteArrayMultipartFileEditor editor = new ByteArrayMultipartFileEditor();
@Test
public void setValueAsCustomObjectInvokesToString() throws Exception {
final String expectedValue = "'Green Wing' - classic British comedy";
Object object = new Object() {
@Override
@@ -52,35 +56,31 @@ public final class ByteArrayMultipartFileEditorTests extends TestCase {
return expectedValue;
}
};
editor.setValue(object);
assertEquals(expectedValue, editor.getAsText());
}
public void testSetValueAsNullGetsBackEmptyString() throws Exception {
ByteArrayMultipartFileEditor editor = new ByteArrayMultipartFileEditor();
@Test
public void setValueAsNullGetsBackEmptyString() throws Exception {
editor.setValue(null);
assertEquals("", editor.getAsText());
}
public void testSetValueAsMultipartFile() throws Exception {
@Test
public void setValueAsMultipartFile() throws Exception {
String expectedValue = "That is comforting to know";
ByteArrayMultipartFileEditor editor = new ByteArrayMultipartFileEditor();
MultipartFile file = mock(MultipartFile.class);
given(file.getBytes()).willReturn(expectedValue.getBytes());
editor.setValue(file);
assertEquals(expectedValue, editor.getAsText());
}
public void testSetValueAsMultipartFileWithBadBytes() throws Exception {
ByteArrayMultipartFileEditor editor = new ByteArrayMultipartFileEditor();
@Test(expected = IllegalArgumentException.class)
public void setValueAsMultipartFileWithBadBytes() throws Exception {
MultipartFile file = mock(MultipartFile.class);
given(file.getBytes()).willThrow(new IOException());
try {
editor.setValue(file);
fail("Must have thrown an IllegalArgumentException: IOException thrown when reading MultipartFile bytes");
}
catch (IllegalArgumentException expected) {
}
editor.setValue(file);
}
}