Polish FreeMarker tests and suppress warnings

This commit is contained in:
Sam Brannen 2015-09-28 19:33:49 +02:00
parent 58c2990794
commit 452b124ff6
2 changed files with 33 additions and 28 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.SimpleHash;
import freemarker.template.Template; import freemarker.template.Template;
import freemarker.template.TemplateException; import freemarker.template.TemplateException;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -91,6 +92,7 @@ public class FreeMarkerMacroTests {
public void testExposeSpringMacroHelpers() throws Exception { public void testExposeSpringMacroHelpers() throws Exception {
FreeMarkerView fv = new FreeMarkerView() { FreeMarkerView fv = new FreeMarkerView() {
@Override @Override
@SuppressWarnings("rawtypes")
protected void processTemplate(Template template, SimpleHash fmModel, HttpServletResponse response) protected void processTemplate(Template template, SimpleHash fmModel, HttpServletResponse response)
throws TemplateException { throws TemplateException {
Map model = fmModel.toMap(); Map model = fmModel.toMap();

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 java.util.Map;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import freemarker.ext.servlet.AllHttpScopesHashModel; import org.junit.Rule;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.context.ApplicationContextException; import org.springframework.context.ApplicationContextException;
import org.springframework.mock.web.test.MockHttpServletRequest; 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.InternalResourceView;
import org.springframework.web.servlet.view.RedirectView; 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.junit.Assert.*;
import static org.mockito.BDDMockito.*; import static org.mockito.BDDMockito.*;
/** /**
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Sam Brannen
* @since 14.03.2004 * @since 14.03.2004
*/ */
public class FreeMarkerViewTests { public class FreeMarkerViewTests {
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test @Test
public void testNoFreeMarkerConfig() throws Exception { public void noFreeMarkerConfig() throws Exception {
FreeMarkerView fv = new FreeMarkerView(); FreeMarkerView fv = new FreeMarkerView();
WebApplicationContext wac = mock(WebApplicationContext.class); WebApplicationContext wac = mock(WebApplicationContext.class);
@ -62,32 +70,23 @@ public class FreeMarkerViewTests {
given(wac.getServletContext()).willReturn(new MockServletContext()); given(wac.getServletContext()).willReturn(new MockServletContext());
fv.setUrl("anythingButNull"); fv.setUrl("anythingButNull");
try {
exception.expect(ApplicationContextException.class);
exception.expectMessage(containsString("FreeMarkerConfig"));
fv.setApplicationContext(wac); 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"));
}
} }
@Test @Test
public void testNoTemplateName() throws Exception { public void noTemplateName() throws Exception {
FreeMarkerView fv = new FreeMarkerView(); FreeMarkerView fv = new FreeMarkerView();
try {
exception.expect(IllegalArgumentException.class);
exception.expectMessage(containsString("url"));
fv.afterPropertiesSet(); fv.afterPropertiesSet();
fail("Should have thrown IllegalArgumentException");
}
catch (IllegalArgumentException ex) {
// Check there's a helpful error message
assertTrue(ex.getMessage().contains("url"));
}
} }
@Test @Test
public void testValidTemplateName() throws Exception { public void validTemplateName() throws Exception {
FreeMarkerView fv = new FreeMarkerView(); FreeMarkerView fv = new FreeMarkerView();
WebApplicationContext wac = mock(WebApplicationContext.class); WebApplicationContext wac = mock(WebApplicationContext.class);
@ -117,7 +116,7 @@ public class FreeMarkerViewTests {
} }
@Test @Test
public void testKeepExistingContentType() throws Exception { public void keepExistingContentType() throws Exception {
FreeMarkerView fv = new FreeMarkerView(); FreeMarkerView fv = new FreeMarkerView();
WebApplicationContext wac = mock(WebApplicationContext.class); WebApplicationContext wac = mock(WebApplicationContext.class);
@ -148,7 +147,7 @@ public class FreeMarkerViewTests {
} }
@Test @Test
public void testFreeMarkerViewResolver() throws Exception { public void freeMarkerViewResolver() throws Exception {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setConfiguration(new TestConfiguration()); configurer.setConfiguration(new TestConfiguration());
@ -181,6 +180,10 @@ public class FreeMarkerViewTests {
private class TestConfiguration extends Configuration { private class TestConfiguration extends Configuration {
TestConfiguration() {
super(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
}
@Override @Override
public Template getTemplate(String name, final Locale locale) throws IOException { public Template getTemplate(String name, final Locale locale) throws IOException {
if (name.equals("templateName") || name.equals("prefix_test_suffix")) { if (name.equals("templateName") || name.equals("prefix_test_suffix")) {