From 57993d871ea137247004c68780f52dbe6dae7df2 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 3 Mar 2010 16:26:47 +0000 Subject: [PATCH] WebApplicationObjectSupport's initServletContext will be called only once in any scenario (SPR-6914) --- .../view/freemarker/FreeMarkerViewTests.java | 6 +++--- .../view/velocity/VelocityViewTests.java | 14 +++++--------- .../support/WebApplicationObjectSupport.java | 19 ++++++++++--------- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerViewTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerViewTests.java index 9ec250a8e2b..a74e2f04071 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerViewTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerViewTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 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. @@ -110,7 +110,7 @@ public class FreeMarkerViewTests { wac.getParentBeanFactory(); wmc.setReturnValue(null); wac.getServletContext(); - wmc.setReturnValue(sc, 5); + wmc.setReturnValue(sc, 2); wmc.replay(); fv.setUrl("templateName"); @@ -147,7 +147,7 @@ public class FreeMarkerViewTests { wac.getParentBeanFactory(); wmc.setReturnValue(null); wac.getServletContext(); - wmc.setReturnValue(sc, 5); + wmc.setReturnValue(sc, 2); wmc.replay(); fv.setUrl("templateName"); diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/VelocityViewTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/VelocityViewTests.java index 105e0e54b56..a4fb650257f 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/VelocityViewTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/VelocityViewTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 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,16 +16,10 @@ package org.springframework.web.servlet.view.velocity; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - import java.io.IOException; import java.util.HashMap; import java.util.Locale; import java.util.Map; - import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -38,7 +32,9 @@ import org.apache.velocity.tools.generic.DateTool; import org.apache.velocity.tools.generic.MathTool; import org.apache.velocity.tools.generic.NumberTool; import org.easymock.MockControl; +import static org.junit.Assert.*; import org.junit.Test; + import org.springframework.context.ApplicationContextException; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; @@ -145,7 +141,7 @@ public class VelocityViewTests { wac.getParentBeanFactory(); wmc.setReturnValue(null); wac.getServletContext(); - wmc.setReturnValue(sc, 4); + wmc.setReturnValue(sc, 3); wmc.replay(); HttpServletRequest request = new MockHttpServletRequest(); @@ -201,7 +197,7 @@ public class VelocityViewTests { wac.getParentBeanFactory(); wmc.setReturnValue(null); wac.getServletContext(); - wmc.setReturnValue(sc, 4); + wmc.setReturnValue(sc, 3); wmc.replay(); HttpServletRequest request = new MockHttpServletRequest(); diff --git a/org.springframework.web/src/main/java/org/springframework/web/context/support/WebApplicationObjectSupport.java b/org.springframework.web/src/main/java/org/springframework/web/context/support/WebApplicationObjectSupport.java index affe765ad0c..0a69d90d6a8 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/context/support/WebApplicationObjectSupport.java +++ b/org.springframework.web/src/main/java/org/springframework/web/context/support/WebApplicationObjectSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2010 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,7 +17,6 @@ package org.springframework.web.context.support; import java.io.File; - import javax.servlet.ServletContext; import org.springframework.context.ApplicationContext; @@ -42,9 +41,11 @@ public abstract class WebApplicationObjectSupport extends ApplicationObjectSuppo public final void setServletContext(ServletContext servletContext) { - this.servletContext = servletContext; - if (servletContext != null) { - initServletContext(servletContext); + if (servletContext != this.servletContext) { + this.servletContext = servletContext; + if (servletContext != null) { + initServletContext(servletContext); + } } } @@ -69,10 +70,10 @@ public abstract class WebApplicationObjectSupport extends ApplicationObjectSuppo @Override protected void initApplicationContext(ApplicationContext context) { super.initApplicationContext(context); - if (context instanceof WebApplicationContext) { - ServletContext servletContext = ((WebApplicationContext) context).getServletContext(); - if (servletContext != null) { - initServletContext(servletContext); + if (this.servletContext == null && context instanceof WebApplicationContext) { + this.servletContext = ((WebApplicationContext) context).getServletContext(); + if (this.servletContext != null) { + initServletContext(this.servletContext); } } }