Skip to content

Commit

Permalink
Merge branch '6.2.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Dec 14, 2024
2 parents 34f7402 + a89db89 commit 30d249c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -59,11 +65,32 @@ protected MockPageContext createPageContext() {
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;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@
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
Expand All @@ -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());

Expand Down Expand Up @@ -198,12 +191,18 @@ void mapAccess() throws Exception {

@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");
}


Expand Down

0 comments on commit 30d249c

Please sign in to comment.