From 452b124ff62537f39c6eea6d5fb197dfe45b628a Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Mon, 28 Sep 2015 19:33:49 +0200 Subject: [PATCH] Polish FreeMarker tests and suppress warnings --- .../view/freemarker/FreeMarkerMacroTests.java | 4 +- .../view/freemarker/FreeMarkerViewTests.java | 57 ++++++++++--------- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerMacroTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerMacroTests.java index 7c4b635afe..36593225a9 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerMacroTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerMacroTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 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. @@ -27,6 +27,7 @@ import freemarker.template.Configuration; import freemarker.template.SimpleHash; import freemarker.template.Template; import freemarker.template.TemplateException; + import org.junit.Before; import org.junit.Test; @@ -91,6 +92,7 @@ public class FreeMarkerMacroTests { public void testExposeSpringMacroHelpers() throws Exception { FreeMarkerView fv = new FreeMarkerView() { @Override + @SuppressWarnings("rawtypes") protected void processTemplate(Template template, SimpleHash fmModel, HttpServletResponse response) throws TemplateException { Map model = fmModel.toMap(); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerViewTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerViewTests.java index 330399afcb..fe3c4fb3e9 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerViewTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerViewTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 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. @@ -25,11 +25,9 @@ import java.util.Locale; import java.util.Map; import javax.servlet.http.HttpServletResponse; -import freemarker.ext.servlet.AllHttpScopesHashModel; -import freemarker.template.Configuration; -import freemarker.template.Template; -import freemarker.template.TemplateException; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.springframework.context.ApplicationContextException; import org.springframework.mock.web.test.MockHttpServletRequest; @@ -44,17 +42,27 @@ import org.springframework.web.servlet.view.AbstractView; import org.springframework.web.servlet.view.InternalResourceView; import org.springframework.web.servlet.view.RedirectView; +import freemarker.ext.servlet.AllHttpScopesHashModel; +import freemarker.template.Configuration; +import freemarker.template.Template; +import freemarker.template.TemplateException; + +import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; import static org.mockito.BDDMockito.*; /** * @author Juergen Hoeller + * @author Sam Brannen * @since 14.03.2004 */ public class FreeMarkerViewTests { + @Rule + public final ExpectedException exception = ExpectedException.none(); + @Test - public void testNoFreeMarkerConfig() throws Exception { + public void noFreeMarkerConfig() throws Exception { FreeMarkerView fv = new FreeMarkerView(); WebApplicationContext wac = mock(WebApplicationContext.class); @@ -62,32 +70,23 @@ public class FreeMarkerViewTests { given(wac.getServletContext()).willReturn(new MockServletContext()); fv.setUrl("anythingButNull"); - try { - fv.setApplicationContext(wac); - fv.afterPropertiesSet(); - fail("Should have thrown BeanDefinitionStoreException"); - } - catch (ApplicationContextException ex) { - // Check there's a helpful error message - assertTrue(ex.getMessage().contains("FreeMarkerConfig")); - } + + exception.expect(ApplicationContextException.class); + exception.expectMessage(containsString("FreeMarkerConfig")); + fv.setApplicationContext(wac); } @Test - public void testNoTemplateName() throws Exception { + public void noTemplateName() throws Exception { FreeMarkerView fv = new FreeMarkerView(); - try { - fv.afterPropertiesSet(); - fail("Should have thrown IllegalArgumentException"); - } - catch (IllegalArgumentException ex) { - // Check there's a helpful error message - assertTrue(ex.getMessage().contains("url")); - } + + exception.expect(IllegalArgumentException.class); + exception.expectMessage(containsString("url")); + fv.afterPropertiesSet(); } @Test - public void testValidTemplateName() throws Exception { + public void validTemplateName() throws Exception { FreeMarkerView fv = new FreeMarkerView(); WebApplicationContext wac = mock(WebApplicationContext.class); @@ -117,7 +116,7 @@ public class FreeMarkerViewTests { } @Test - public void testKeepExistingContentType() throws Exception { + public void keepExistingContentType() throws Exception { FreeMarkerView fv = new FreeMarkerView(); WebApplicationContext wac = mock(WebApplicationContext.class); @@ -148,7 +147,7 @@ public class FreeMarkerViewTests { } @Test - public void testFreeMarkerViewResolver() throws Exception { + public void freeMarkerViewResolver() throws Exception { FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); configurer.setConfiguration(new TestConfiguration()); @@ -181,6 +180,10 @@ public class FreeMarkerViewTests { private class TestConfiguration extends Configuration { + TestConfiguration() { + super(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS); + } + @Override public Template getTemplate(String name, final Locale locale) throws IOException { if (name.equals("templateName") || name.equals("prefix_test_suffix")) {