Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Apr 10, 2024
1 parent 3d85ec2 commit bed3689
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.Optional;

import jakarta.servlet.http.Part;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -54,6 +53,10 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.InstanceOfAssertFactories.LIST;
import static org.assertj.core.api.InstanceOfAssertFactories.array;
import static org.assertj.core.api.InstanceOfAssertFactories.optional;
import static org.assertj.core.api.InstanceOfAssertFactories.type;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.springframework.web.testfixture.method.MvcAnnotationPredicates.requestParam;
Expand Down Expand Up @@ -84,6 +87,7 @@ void setup() {

// Expose request to the current thread (for SpEL expressions)
RequestContextHolder.setRequestAttributes(webRequest);
context.close();
}

@Test
Expand Down Expand Up @@ -169,9 +173,7 @@ void resolveString() throws Exception {

MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class);
Object result = resolver.resolveArgument(param, null, webRequest, null);
boolean condition = result instanceof String;
assertThat(condition).isTrue();
assertThat(result).as("Invalid result").isEqualTo(expected);
assertThat(result).isEqualTo(expected);
}

@Test
Expand All @@ -181,9 +183,7 @@ void resolveStringArray() throws Exception {

MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class);
Object result = resolver.resolveArgument(param, null, webRequest, null);
boolean condition = result instanceof String[];
assertThat(condition).isTrue();
assertThat((String[]) result).as("Invalid result").isEqualTo(expected);
assertThat(result).asInstanceOf(array(String[].class)).containsExactly(expected);
}

@Test // gh-32577
Expand All @@ -194,9 +194,7 @@ void resolveStringArrayWithEmptyArraySuffix() throws Exception {

MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class);
Object result = resolver.resolveArgument(param, null, webRequest, null);
boolean condition = result instanceof String[];
assertThat(condition).isTrue();
assertThat((String[]) result).isEqualTo(expected);
assertThat(result).asInstanceOf(array(String[].class)).containsExactly(expected);
}

@Test
Expand All @@ -208,9 +206,7 @@ void resolveMultipartFile() throws Exception {

MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile.class);
Object result = resolver.resolveArgument(param, null, webRequest, null);
boolean condition = result instanceof MultipartFile;
assertThat(condition).isTrue();
assertThat(result).as("Invalid result").isEqualTo(expected);
assertThat(result).asInstanceOf(type(MultipartFile.class)).isEqualTo(expected);
}

@Test
Expand All @@ -226,9 +222,7 @@ void resolveMultipartFileList() throws Exception {
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
Object result = resolver.resolveArgument(param, null, webRequest, null);

boolean condition = result instanceof List;
assertThat(condition).isTrue();
assertThat(result).isEqualTo(Arrays.asList(expected1, expected2));
assertThat(result).asInstanceOf(LIST).containsExactly(expected1, expected2);
}

@Test
Expand All @@ -255,12 +249,7 @@ void resolveMultipartFileArray() throws Exception {
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile[].class);
Object result = resolver.resolveArgument(param, null, webRequest, null);

boolean condition = result instanceof MultipartFile[];
assertThat(condition).isTrue();
MultipartFile[] parts = (MultipartFile[]) result;
assertThat(parts).hasSize(2);
assertThat(expected1).isEqualTo(parts[0]);
assertThat(expected2).isEqualTo(parts[1]);
assertThat(result).asInstanceOf(array(MultipartFile[].class)).containsExactly(expected1, expected2);
}

@Test
Expand All @@ -286,9 +275,7 @@ void resolvePart() throws Exception {
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Part.class);
Object result = resolver.resolveArgument(param, null, webRequest, null);

boolean condition = result instanceof Part;
assertThat(condition).isTrue();
assertThat(result).as("Invalid result").isEqualTo(expected);
assertThat(result).asInstanceOf(type(Part.class)).isEqualTo(expected);
}

@Test
Expand All @@ -306,9 +293,7 @@ void resolvePartList() throws Exception {
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, Part.class);
Object result = resolver.resolveArgument(param, null, webRequest, null);

boolean condition = result instanceof List;
assertThat(condition).isTrue();
assertThat(result).isEqualTo(Arrays.asList(expected1, expected2));
assertThat(result).asInstanceOf(LIST).containsExactly(expected1, expected2);
}

@Test
Expand Down Expand Up @@ -339,12 +324,7 @@ void resolvePartArray() throws Exception {
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Part[].class);
Object result = resolver.resolveArgument(param, null, webRequest, null);

boolean condition = result instanceof Part[];
assertThat(condition).isTrue();
Part[] parts = (Part[]) result;
assertThat(parts).hasSize(2);
assertThat(expected1).isEqualTo(parts[0]);
assertThat(expected2).isEqualTo(parts[1]);
assertThat(result).asInstanceOf(array(Part[].class)).containsExactly(expected1, expected2);
}

@Test
Expand All @@ -369,9 +349,7 @@ void resolveMultipartFileNotAnnot() throws Exception {

MethodParameter param = this.testMethod.annotNotPresent().arg(MultipartFile.class);
Object result = resolver.resolveArgument(param, null, webRequest, null);
boolean condition = result instanceof MultipartFile;
assertThat(condition).isTrue();
assertThat(result).as("Invalid result").isEqualTo(expected);
assertThat(result).asInstanceOf(type(MultipartFile.class)).isEqualTo(expected);
}

@Test
Expand All @@ -387,9 +365,7 @@ void resolveMultipartFileListNotannot() throws Exception {
.annotNotPresent(RequestParam.class).arg(List.class, MultipartFile.class);

Object result = resolver.resolveArgument(param, null, webRequest, null);
boolean condition = result instanceof List;
assertThat(condition).isTrue();
assertThat(result).isEqualTo(Arrays.asList(expected1, expected2));
assertThat(result).asInstanceOf(LIST).containsExactly(expected1, expected2);
}

@Test
Expand All @@ -411,8 +387,7 @@ public void isMultipartRequestHttpPut() throws Exception {
.annotNotPresent(RequestParam.class).arg(List.class, MultipartFile.class);

Object actual = resolver.resolveArgument(param, null, webRequest, null);
assertThat(actual).isInstanceOf(List.class).asInstanceOf(InstanceOfAssertFactories.LIST)
.containsExactly(expected);
assertThat(actual).asInstanceOf(LIST).containsExactly(expected);
}

@Test
Expand Down Expand Up @@ -443,18 +418,14 @@ void resolvePartNotAnnot() throws Exception {

MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(Part.class);
Object result = resolver.resolveArgument(param, null, webRequest, null);
boolean condition = result instanceof Part;
assertThat(condition).isTrue();
assertThat(result).as("Invalid result").isEqualTo(expected);
assertThat(result).asInstanceOf(type(Part.class)).isEqualTo(expected);
}

@Test
void resolveDefaultValue() throws Exception {
MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class);
Object result = resolver.resolveArgument(param, null, webRequest, null);
boolean condition = result instanceof String;
assertThat(condition).isTrue();
assertThat(result).as("Invalid result").isEqualTo("bar");
assertThat(result).isEqualTo("bar");
}

@Test
Expand Down Expand Up @@ -529,8 +500,6 @@ void resolveSimpleTypeParam() throws Exception {
MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
Object result = resolver.resolveArgument(param, null, webRequest, null);

boolean condition = result instanceof String;
assertThat(condition).isTrue();
assertThat(result).isEqualTo("plainValue");
}

Expand Down Expand Up @@ -678,9 +647,7 @@ void resolveOptionalMultipartFile() throws Exception {
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);

boolean condition = result instanceof Optional;
assertThat(condition).isTrue();
assertThat(((Optional<?>) result).get()).as("Invalid result").isEqualTo(expected);
assertThat(result).asInstanceOf(optional(MultipartFile.class)).contains(expected);
}

@Test
Expand Down Expand Up @@ -724,8 +691,7 @@ void resolveNameFromSystemPropertyThroughPlaceholder() throws Exception {
try {
MethodParameter param = this.testMethod.annot(requestParam().name("${systemProperty}")).arg(Integer.class);
Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
boolean condition = result instanceof Integer;
assertThat(condition).isTrue();
assertThat(result).isInstanceOf(Integer.class);
}
finally {
System.clearProperty("systemProperty");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.web.reactive.result.method.annotation;


import java.time.Duration;
import java.util.Map;
import java.util.Optional;
Expand All @@ -39,6 +40,8 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.InstanceOfAssertFactories.array;
import static org.assertj.core.api.InstanceOfAssertFactories.optional;
import static org.springframework.core.ResolvableType.forClassWithGenerics;
import static org.springframework.web.testfixture.method.MvcAnnotationPredicates.requestParam;

Expand All @@ -58,7 +61,6 @@ class RequestParamMethodArgumentResolverTests {

@BeforeEach
void setup() throws Exception {

ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
this.resolver = new RequestParamMethodArgumentResolver(null, adapterRegistry, true);

Expand All @@ -70,7 +72,6 @@ void setup() throws Exception {

@Test
void supportsParameter() {

MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class);
assertThat(this.resolver.supportsParameter(param)).isTrue();

Expand All @@ -91,7 +92,6 @@ void supportsParameter() {

param = this.testMethod.annot(requestParam().notRequired()).arg(String.class);
assertThat(this.resolver.supportsParameter(param)).isTrue();

}

@Test
Expand Down Expand Up @@ -125,19 +125,15 @@ void resolveStringArray() {
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class);
MockServerHttpRequest request = MockServerHttpRequest.get("/path?name=foo&name=bar").build();
Object result = resolve(param, MockServerWebExchange.from(request));
boolean condition = result instanceof String[];
assertThat(condition).isTrue();
assertThat((String[]) result).isEqualTo(new String[] {"foo", "bar"});
assertThat(result).asInstanceOf(array(String[].class)).containsExactly("foo", "bar");
}

@Test // gh-32577
void resolveStringArrayWithEmptyArraySuffix() {
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class);
MockServerHttpRequest request = MockServerHttpRequest.get("/path?name[]=foo&name[]=bar").build();
Object result = resolve(param, MockServerWebExchange.from(request));
boolean condition = result instanceof String[];
assertThat(condition).isTrue();
assertThat((String[]) result).isEqualTo(new String[] {"foo", "bar"});
assertThat(result).asInstanceOf(array(String[].class)).containsExactly("foo", "bar");
}

@Test
Expand Down Expand Up @@ -214,10 +210,7 @@ void resolveOptionalParamValue() {
exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path?name=123"));
result = resolve(param, exchange);

assertThat(result.getClass()).isEqualTo(Optional.class);
Optional<?> value = (Optional<?>) result;
assertThat(value).isPresent();
assertThat(value.get()).isEqualTo(123);
assertThat(result).asInstanceOf(optional(Integer.class)).contains(123);
}


Expand Down

0 comments on commit bed3689

Please sign in to comment.