Remove some try/catch blocks from test methods

pull/4/merge
Dave Syer 12 years ago
parent 4b1c58d736
commit 7bf3e35249

@ -41,6 +41,6 @@ public interface EmbeddedServletContainerFactory {
* @see EmbeddedServletContainer#stop() * @see EmbeddedServletContainer#stop()
*/ */
EmbeddedServletContainer getEmbdeddedServletContainer( EmbeddedServletContainer getEmbdeddedServletContainer(
ServletContextInitializer... initializers); //TODO(6/14/2013) Fix name of method ServletContextInitializer... initializers);
} }

@ -15,15 +15,12 @@
*/ */
package org.springframework.bootstrap.autoconfigure.web; package org.springframework.bootstrap.autoconfigure.web;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
import javax.servlet.MultipartConfigElement; import javax.servlet.MultipartConfigElement;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.junit.rules.ExpectedException;
import org.springframework.bootstrap.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; import org.springframework.bootstrap.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
import org.springframework.bootstrap.context.embedded.jetty.JettyEmbeddedServletContainerFactory; import org.springframework.bootstrap.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
import org.springframework.bootstrap.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; import org.springframework.bootstrap.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
@ -38,40 +35,45 @@ import org.springframework.web.multipart.support.StandardServletMultipartResolve
import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
/** /**
* A series of embedded unit tests, based on an empty configuration, no multipart * A series of embedded unit tests, based on an empty configuration, no multipart
* configuration, and a multipart configuration, with both Jetty and Tomcat. * configuration, and a multipart configuration, with both Jetty and Tomcat.
* *
* @author Greg Turnquist * @author Greg Turnquist
* @author Dave Syer
*/ */
public class MultipartAutoConfigurationTests { public class MultipartAutoConfigurationTests {
private AnnotationConfigEmbeddedWebApplicationContext context; private AnnotationConfigEmbeddedWebApplicationContext context;
@Rule
public ExpectedException exception = ExpectedException.none();
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
@Test @Test
public void containerWithNothing() { public void containerWithNothing() {
this.context = new AnnotationConfigEmbeddedWebApplicationContext( this.context = new AnnotationConfigEmbeddedWebApplicationContext(
ContainerWithNothing.class, ContainerWithNothing.class,
EmbeddedServletContainerAutoConfiguration.class, EmbeddedServletContainerAutoConfiguration.class,
MultipartAutoConfiguration.class); MultipartAutoConfiguration.class);
try { DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class);
DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class); assertNull(servlet.getMultipartResolver());
assertNull(servlet.getMultipartResolver()); assertEquals(0,
try { this.context.getBeansOfType(StandardServletMultipartResolver.class)
this.context.getBean(StandardServletMultipartResolver.class); .size());
fail("Expected to receive a " + NoSuchBeanDefinitionException.class); assertEquals(0, this.context.getBeansOfType(MultipartResolver.class).size());
} catch (NoSuchBeanDefinitionException e) {
}
try {
this.context.getBean(MultipartResolver.class);
fail("Expected to receive a " + NoSuchBeanDefinitionException.class);
} catch (NoSuchBeanDefinitionException e) {
}
} finally {
this.context.close();
}
} }
@Configuration @Configuration
public static class ContainerWithNothing { public static class ContainerWithNothing {
} }
@ -82,23 +84,13 @@ public class MultipartAutoConfigurationTests {
ContainerWithNoMultipartJetty.class, ContainerWithNoMultipartJetty.class,
EmbeddedServletContainerAutoConfiguration.class, EmbeddedServletContainerAutoConfiguration.class,
MultipartAutoConfiguration.class); MultipartAutoConfiguration.class);
try { DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class);
DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class); assertNull(servlet.getMultipartResolver());
assertNull(servlet.getMultipartResolver()); assertEquals(0,
try { this.context.getBeansOfType(StandardServletMultipartResolver.class)
this.context.getBean(StandardServletMultipartResolver.class); .size());
fail("Expected to receive a " + NoSuchBeanDefinitionException.class); assertEquals(0, this.context.getBeansOfType(MultipartResolver.class).size());
} catch (NoSuchBeanDefinitionException e) { verifyServletWorks();
}
try {
this.context.getBean(MultipartResolver.class);
fail("Expected to receive a " + NoSuchBeanDefinitionException.class);
} catch (NoSuchBeanDefinitionException e) {
}
verifyServletWorks();
} finally {
this.context.close();
}
} }
@Configuration @Configuration
@ -107,11 +99,12 @@ public class MultipartAutoConfigurationTests {
JettyEmbeddedServletContainerFactory containerFactory() { JettyEmbeddedServletContainerFactory containerFactory() {
return new JettyEmbeddedServletContainerFactory(); return new JettyEmbeddedServletContainerFactory();
} }
@Bean @Bean
WebController controller() { WebController controller() {
return new WebController(); return new WebController();
} }
} }
@Test @Test
public void containerWithNoMultipartTomcatConfiguration() { public void containerWithNoMultipartTomcatConfiguration() {
@ -119,23 +112,13 @@ public class MultipartAutoConfigurationTests {
ContainerWithNoMultipartTomcat.class, ContainerWithNoMultipartTomcat.class,
EmbeddedServletContainerAutoConfiguration.class, EmbeddedServletContainerAutoConfiguration.class,
MultipartAutoConfiguration.class); MultipartAutoConfiguration.class);
try { DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class);
DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class); assertNull(servlet.getMultipartResolver());
assertNull(servlet.getMultipartResolver()); assertEquals(0,
try { this.context.getBeansOfType(StandardServletMultipartResolver.class)
this.context.getBean(StandardServletMultipartResolver.class); .size());
fail("Expected to receive a " + NoSuchBeanDefinitionException.class); assertEquals(0, this.context.getBeansOfType(MultipartResolver.class).size());
} catch (NoSuchBeanDefinitionException e) { verifyServletWorks();
}
try {
this.context.getBean(MultipartResolver.class);
fail("Expected to receive a " + NoSuchBeanDefinitionException.class);
} catch (NoSuchBeanDefinitionException e) {
}
verifyServletWorks();
} finally {
this.context.close();
}
} }
@Configuration @Configuration
@ -144,11 +127,12 @@ public class MultipartAutoConfigurationTests {
TomcatEmbeddedServletContainerFactory containerFactory() { TomcatEmbeddedServletContainerFactory containerFactory() {
return new TomcatEmbeddedServletContainerFactory(); return new TomcatEmbeddedServletContainerFactory();
} }
@Bean @Bean
WebController controller() { WebController controller() {
return new WebController(); return new WebController();
} }
} }
@Test @Test
public void containerWithAutomatedMultipartJettyConfiguration() { public void containerWithAutomatedMultipartJettyConfiguration() {
@ -156,15 +140,10 @@ public class MultipartAutoConfigurationTests {
ContainerWithEverythingJetty.class, ContainerWithEverythingJetty.class,
EmbeddedServletContainerAutoConfiguration.class, EmbeddedServletContainerAutoConfiguration.class,
MultipartAutoConfiguration.class); MultipartAutoConfiguration.class);
try { this.context.getBean(MultipartConfigElement.class);
this.context.getBean(MultipartConfigElement.class); assertSame(this.context.getBean(DispatcherServlet.class).getMultipartResolver(),
assertSame( this.context.getBean(StandardServletMultipartResolver.class));
this.context.getBean(DispatcherServlet.class).getMultipartResolver(), verifyServletWorks();
this.context.getBean(StandardServletMultipartResolver.class));
verifyServletWorks();
} finally {
this.context.close();
}
} }
@Configuration @Configuration
@ -173,10 +152,12 @@ public class MultipartAutoConfigurationTests {
MultipartConfigElement multipartConfigElement() { MultipartConfigElement multipartConfigElement() {
return new MultipartConfigElement(""); return new MultipartConfigElement("");
} }
@Bean @Bean
JettyEmbeddedServletContainerFactory containerFactory() { JettyEmbeddedServletContainerFactory containerFactory() {
return new JettyEmbeddedServletContainerFactory(); return new JettyEmbeddedServletContainerFactory();
} }
@Bean @Bean
WebController webController() { WebController webController() {
return new WebController(); return new WebController();
@ -189,15 +170,10 @@ public class MultipartAutoConfigurationTests {
ContainerWithEverythingTomcat.class, ContainerWithEverythingTomcat.class,
EmbeddedServletContainerAutoConfiguration.class, EmbeddedServletContainerAutoConfiguration.class,
MultipartAutoConfiguration.class); MultipartAutoConfiguration.class);
try { this.context.getBean(MultipartConfigElement.class);
this.context.getBean(MultipartConfigElement.class); assertSame(this.context.getBean(DispatcherServlet.class).getMultipartResolver(),
assertSame( this.context.getBean(StandardServletMultipartResolver.class));
this.context.getBean(DispatcherServlet.class).getMultipartResolver(), verifyServletWorks();
this.context.getBean(StandardServletMultipartResolver.class));
verifyServletWorks();
} finally {
this.context.close();
}
} }
@Configuration @Configuration
@ -207,10 +183,12 @@ public class MultipartAutoConfigurationTests {
MultipartConfigElement multipartConfigElement() { MultipartConfigElement multipartConfigElement() {
return new MultipartConfigElement(""); return new MultipartConfigElement("");
} }
@Bean @Bean
TomcatEmbeddedServletContainerFactory containerFactory() { TomcatEmbeddedServletContainerFactory containerFactory() {
return new TomcatEmbeddedServletContainerFactory(); return new TomcatEmbeddedServletContainerFactory();
} }
@Bean @Bean
WebController webController() { WebController webController() {
return new WebController(); return new WebController();
@ -220,15 +198,16 @@ public class MultipartAutoConfigurationTests {
@Controller @Controller
public static class WebController { public static class WebController {
@RequestMapping("/") @RequestMapping("/")
public @ResponseBody String index() { public @ResponseBody
String index() {
return "Hello"; return "Hello";
} }
} }
private void verifyServletWorks() { private void verifyServletWorks() {
RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();
assertEquals(restTemplate.getForObject("http://localhost:8080/", String.class), "Hello"); assertEquals(restTemplate.getForObject("http://localhost:8080/", String.class),
"Hello");
} }
} }

Loading…
Cancel
Save