Skip to content

Commit a89db89

Browse files
committed
Polishing
1 parent 3d0fffa commit a89db89

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

spring-webmvc/src/test/java/org/springframework/web/servlet/tags/AbstractTagTests.java

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,12 @@
1616

1717
package org.springframework.web.servlet.tags;
1818

19+
import jakarta.el.ELContext;
20+
import jakarta.servlet.ServletContext;
21+
import jakarta.servlet.http.HttpServletRequest;
22+
import jakarta.servlet.http.HttpServletResponse;
23+
24+
import org.springframework.lang.Nullable;
1925
import org.springframework.web.context.WebApplicationContext;
2026
import org.springframework.web.servlet.DispatcherServlet;
2127
import org.springframework.web.servlet.LocaleResolver;
@@ -59,11 +65,32 @@ protected MockPageContext createPageContext() {
5965
sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
6066
}
6167

62-
return new MockPageContext(sc, request, response);
68+
return new ExtendedMockPageContext(sc, request, response);
6369
}
6470

6571
protected boolean inDispatcherServlet() {
6672
return true;
6773
}
6874

75+
76+
protected static class ExtendedMockPageContext extends MockPageContext {
77+
78+
private ELContext elContext;
79+
80+
public ExtendedMockPageContext(@Nullable ServletContext servletContext, @Nullable HttpServletRequest request,
81+
@Nullable HttpServletResponse response) {
82+
83+
super(servletContext, request, response);
84+
}
85+
86+
@Override
87+
public ELContext getELContext() {
88+
return this.elContext;
89+
}
90+
91+
public void setELContext(ELContext elContext) {
92+
this.elContext = elContext;
93+
}
94+
}
95+
6996
}

spring-webmvc/src/test/java/org/springframework/web/servlet/tags/EvalTagTests.java

+9-10
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,11 @@
3939
import org.springframework.web.testfixture.servlet.MockPageContext;
4040

4141
import static org.assertj.core.api.Assertions.assertThat;
42+
import static org.mockito.ArgumentMatchers.any;
4243
import static org.mockito.ArgumentMatchers.eq;
4344
import static org.mockito.ArgumentMatchers.isNull;
44-
import static org.mockito.ArgumentMatchers.same;
4545
import static org.mockito.BDDMockito.given;
4646
import static org.mockito.Mockito.mock;
47-
import static org.mockito.Mockito.spy;
4847

4948
/**
5049
* @author Keith Donald
@@ -60,15 +59,9 @@ class EvalTagTests extends AbstractTagTests {
6059
void setup() {
6160
LocaleContextHolder.setDefaultLocale(Locale.UK);
6261

63-
context = spy(createPageContext());
64-
ELContext elContext = mock();
65-
ELResolver elResolver = mock();
66-
given(elResolver.getValue(same(elContext), isNull(), eq("pageContext"))).willReturn(context);
67-
given(elContext.getELResolver()).willReturn(elResolver);
68-
given(context.getELContext()).willReturn(elContext);
69-
7062
FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean();
7163
factory.afterPropertiesSet();
64+
context = createPageContext();
7265
context.getRequest().setAttribute("org.springframework.core.convert.ConversionService", factory.getObject());
7366
context.getRequest().setAttribute("bean", new Bean());
7467

@@ -198,12 +191,18 @@ void mapAccess() throws Exception {
198191

199192
@Test
200193
void resolveImplicitVariable() throws Exception {
194+
ELContext elContext = mock();
195+
ELResolver elResolver = mock();
196+
given(elContext.getELResolver()).willReturn(elResolver);
197+
given(elResolver.getValue(any(ELContext.class), isNull(), eq("pageContext"))).willReturn(context);
198+
((ExtendedMockPageContext) context).setELContext(elContext);
199+
201200
tag.setExpression("pageContext.getClass().getSimpleName()");
202201
int action = tag.doStartTag();
203202
assertThat(action).isEqualTo(Tag.EVAL_BODY_INCLUDE);
204203
action = tag.doEndTag();
205204
assertThat(action).isEqualTo(Tag.EVAL_PAGE);
206-
assertThat(((MockHttpServletResponse) context.getResponse()).getContentAsString()).isEqualTo("MockPageContext");
205+
assertThat(((MockHttpServletResponse) context.getResponse()).getContentAsString()).isEqualTo("ExtendedMockPageContext");
207206
}
208207

209208

0 commit comments

Comments
 (0)