Polishing
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
* Copyright 2002-2024 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,6 +16,12 @@
|
||||
|
||||
package org.springframework.web.servlet.tags;
|
||||
|
||||
import jakarta.el.ELContext;
|
||||
import jakarta.servlet.ServletContext;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
import org.springframework.web.servlet.LocaleResolver;
|
||||
@@ -59,11 +65,32 @@ public abstract class AbstractTagTests {
|
||||
sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
|
||||
}
|
||||
|
||||
return new MockPageContext(sc, request, response);
|
||||
return new ExtendedMockPageContext(sc, request, response);
|
||||
}
|
||||
|
||||
protected boolean inDispatcherServlet() {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
protected static class ExtendedMockPageContext extends MockPageContext {
|
||||
|
||||
private ELContext elContext;
|
||||
|
||||
public ExtendedMockPageContext(@Nullable ServletContext servletContext, @Nullable HttpServletRequest request,
|
||||
@Nullable HttpServletResponse response) {
|
||||
|
||||
super(servletContext, request, response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ELContext getELContext() {
|
||||
return this.elContext;
|
||||
}
|
||||
|
||||
public void setELContext(ELContext elContext) {
|
||||
this.elContext = elContext;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -39,12 +39,11 @@ import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
|
||||
import org.springframework.web.testfixture.servlet.MockPageContext;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.ArgumentMatchers.same;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
/**
|
||||
* @author Keith Donald
|
||||
@@ -60,15 +59,9 @@ class EvalTagTests extends AbstractTagTests {
|
||||
void setup() {
|
||||
LocaleContextHolder.setDefaultLocale(Locale.UK);
|
||||
|
||||
context = spy(createPageContext());
|
||||
ELContext elContext = mock();
|
||||
ELResolver elResolver = mock();
|
||||
given(elResolver.getValue(same(elContext), isNull(), eq("pageContext"))).willReturn(context);
|
||||
given(elContext.getELResolver()).willReturn(elResolver);
|
||||
given(context.getELContext()).willReturn(elContext);
|
||||
|
||||
FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean();
|
||||
factory.afterPropertiesSet();
|
||||
context = createPageContext();
|
||||
context.getRequest().setAttribute("org.springframework.core.convert.ConversionService", factory.getObject());
|
||||
context.getRequest().setAttribute("bean", new Bean());
|
||||
|
||||
@@ -198,12 +191,18 @@ class EvalTagTests extends AbstractTagTests {
|
||||
|
||||
@Test
|
||||
void resolveImplicitVariable() throws Exception {
|
||||
ELContext elContext = mock();
|
||||
ELResolver elResolver = mock();
|
||||
given(elContext.getELResolver()).willReturn(elResolver);
|
||||
given(elResolver.getValue(any(ELContext.class), isNull(), eq("pageContext"))).willReturn(context);
|
||||
((ExtendedMockPageContext) context).setELContext(elContext);
|
||||
|
||||
tag.setExpression("pageContext.getClass().getSimpleName()");
|
||||
int action = tag.doStartTag();
|
||||
assertThat(action).isEqualTo(Tag.EVAL_BODY_INCLUDE);
|
||||
action = tag.doEndTag();
|
||||
assertThat(action).isEqualTo(Tag.EVAL_PAGE);
|
||||
assertThat(((MockHttpServletResponse) context.getResponse()).getContentAsString()).isEqualTo("MockPageContext");
|
||||
assertThat(((MockHttpServletResponse) context.getResponse()).getContentAsString()).isEqualTo("ExtendedMockPageContext");
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user