Apply 'instanceof pattern matching' in spring-test and Servlet mocks

This commit is contained in:
Sam Brannen
2022-12-09 11:49:48 -05:00
parent aae46263cc
commit 485c80fcf3
25 changed files with 124 additions and 116 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2022 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.
@@ -69,8 +69,8 @@ public class MockBodyContent extends BodyContent {
}
private static JspWriter adaptJspWriter(@Nullable Writer targetWriter, @Nullable HttpServletResponse response) {
if (targetWriter instanceof JspWriter) {
return (JspWriter) targetWriter;
if (targetWriter instanceof JspWriter jspWriter) {
return jspWriter;
}
else {
return new MockJspWriter(response, targetWriter);

View File

@@ -167,11 +167,11 @@ public class MockHttpSession implements HttpSession {
if (value != null) {
Object oldValue = this.attributes.put(name, value);
if (value != oldValue) {
if (oldValue instanceof HttpSessionBindingListener) {
((HttpSessionBindingListener) oldValue).valueUnbound(new HttpSessionBindingEvent(this, name, oldValue));
if (oldValue instanceof HttpSessionBindingListener listener) {
listener.valueUnbound(new HttpSessionBindingEvent(this, name, oldValue));
}
if (value instanceof HttpSessionBindingListener) {
((HttpSessionBindingListener) value).valueBound(new HttpSessionBindingEvent(this, name, value));
if (value instanceof HttpSessionBindingListener listener) {
listener.valueBound(new HttpSessionBindingEvent(this, name, value));
}
}
}
@@ -185,8 +185,8 @@ public class MockHttpSession implements HttpSession {
assertIsValid();
Assert.notNull(name, "Attribute name must not be null");
Object value = this.attributes.remove(name);
if (value instanceof HttpSessionBindingListener) {
((HttpSessionBindingListener) value).valueUnbound(new HttpSessionBindingEvent(this, name, value));
if (value instanceof HttpSessionBindingListener listener) {
listener.valueUnbound(new HttpSessionBindingEvent(this, name, value));
}
}
@@ -199,8 +199,8 @@ public class MockHttpSession implements HttpSession {
String name = entry.getKey();
Object value = entry.getValue();
it.remove();
if (value instanceof HttpSessionBindingListener) {
((HttpSessionBindingListener) value).valueUnbound(new HttpSessionBindingEvent(this, name, value));
if (value instanceof HttpSessionBindingListener listener) {
listener.valueUnbound(new HttpSessionBindingEvent(this, name, value));
}
}
}
@@ -251,14 +251,14 @@ public class MockHttpSession implements HttpSession {
String name = entry.getKey();
Object value = entry.getValue();
it.remove();
if (value instanceof Serializable) {
state.put(name, (Serializable) value);
if (value instanceof Serializable serializable) {
state.put(name, serializable);
}
else {
// Not serializable... Servlet containers usually automatically
// unbind the attribute in this case.
if (value instanceof HttpSessionBindingListener) {
((HttpSessionBindingListener) value).valueUnbound(new HttpSessionBindingEvent(this, name, value));
if (value instanceof HttpSessionBindingListener listener) {
listener.valueUnbound(new HttpSessionBindingEvent(this, name, value));
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2022 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.
@@ -65,8 +65,8 @@ public class MockJspWriter extends JspWriter {
public MockJspWriter(@Nullable HttpServletResponse response, @Nullable Writer targetWriter) {
super(DEFAULT_BUFFER, true);
this.response = (response != null ? response : new MockHttpServletResponse());
if (targetWriter instanceof PrintWriter) {
this.targetWriter = (PrintWriter) targetWriter;
if (targetWriter instanceof PrintWriter printWriter) {
this.targetWriter = printWriter;
}
else if (targetWriter != null) {
this.targetWriter = new PrintWriter(targetWriter);

View File

@@ -342,13 +342,17 @@ public class MockPageContext extends PageContext {
}
public byte[] getContentAsByteArray() {
Assert.state(this.response instanceof MockHttpServletResponse, "MockHttpServletResponse required");
return ((MockHttpServletResponse) this.response).getContentAsByteArray();
if (this.response instanceof MockHttpServletResponse mockResponse) {
return mockResponse.getContentAsByteArray();
}
throw new IllegalStateException("MockHttpServletResponse is required");
}
public String getContentAsString() throws UnsupportedEncodingException {
Assert.state(this.response instanceof MockHttpServletResponse, "MockHttpServletResponse required");
return ((MockHttpServletResponse) this.response).getContentAsString();
if (this.response instanceof MockHttpServletResponse mockResponse) {
return mockResponse.getContentAsString();
}
throw new IllegalStateException("MockHttpServletResponse is required");
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2022 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.
@@ -78,11 +78,11 @@ public class MockRequestDispatcher implements RequestDispatcher {
* {@link HttpServletResponseWrapper} decorators if necessary.
*/
protected MockHttpServletResponse getMockHttpServletResponse(ServletResponse response) {
if (response instanceof MockHttpServletResponse) {
return (MockHttpServletResponse) response;
if (response instanceof MockHttpServletResponse mockResponse) {
return mockResponse;
}
if (response instanceof HttpServletResponseWrapper) {
return getMockHttpServletResponse(((HttpServletResponseWrapper) response).getResponse());
if (response instanceof HttpServletResponseWrapper wrapper) {
return getMockHttpServletResponse(wrapper.getResponse());
}
throw new IllegalArgumentException("MockRequestDispatcher requires MockHttpServletResponse");
}