SimpleResultHandlerTests.java 2.9 KB
Newer Older
1
/*
2
 * Copyright 2002-2016 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

17
package org.springframework.web.reactive.result;
18

19
import java.util.List;
20 21
import java.util.concurrent.CompletableFuture;

22
import org.junit.Before;
23 24
import org.junit.Test;
import org.reactivestreams.Publisher;
25
import reactor.core.publisher.Flux;
26
import rx.Observable;
27

28
import org.springframework.core.MethodParameter;
29
import org.springframework.core.ResolvableType;
30
import org.springframework.web.reactive.HandlerResult;
31

32
import static org.junit.Assert.assertEquals;
33

34
/**
R
Rossen Stoyanchev 已提交
35
 * Unit tests for {@link SimpleResultHandler}.
36
 * @author Sebastien Deleuze
R
Rossen Stoyanchev 已提交
37
 * @author Rossen Stoyanchev
38
 */
39
public class SimpleResultHandlerTests {
40

41
	private SimpleResultHandler resultHandler;
42

43 44 45

	@Before
	public void setUp() throws Exception {
R
Rossen Stoyanchev 已提交
46
		this.resultHandler = new SimpleResultHandler();
47
	}
R
Rossen Stoyanchev 已提交
48

49

50
	@Test
51
	public void supports() throws NoSuchMethodException {
52 53 54 55 56 57 58 59
		testSupports(ResolvableType.forClass(void.class), true);
		testSupports(ResolvableType.forClassWithGenerics(Publisher.class, Void.class), true);
		testSupports(ResolvableType.forClassWithGenerics(Flux.class, Void.class), true);
		testSupports(ResolvableType.forClassWithGenerics(Observable.class, Void.class), true);
		testSupports(ResolvableType.forClassWithGenerics(CompletableFuture.class, Void.class), true);

		testSupports(ResolvableType.forClass(String.class), false);
		testSupports(ResolvableType.forClassWithGenerics(Publisher.class, String.class), false);
60 61
	}

62 63 64 65 66
	@Test
	public void supportsUsesGenericTypeInformation() throws Exception {
		testSupports(ResolvableType.forClassWithGenerics(List.class, Void.class), false);
	}

67
	private void testSupports(ResolvableType type, boolean result) {
R
Rossen Stoyanchev 已提交
68
		MethodParameter param = ResolvableMethod.onClass(TestController.class).returning(type).resolveReturnType();
69 70
		HandlerResult handlerResult = new HandlerResult(new TestController(), null, param);
		assertEquals(result, this.resultHandler.supports(handlerResult));
71 72 73 74 75 76
	}


	@SuppressWarnings("unused")
	private static class TestController {

77 78 79 80 81
		public void voidReturn() { }

		public Publisher<String> publisherString() { return null; }

		public Flux<Void> flux() { return null; }
82

83
		public Observable<Void> observable() { return null; }
84

85
		public CompletableFuture<Void> completableFuture() { return null; }
86

87
		public String string() { return null; }
88

89
		public Publisher<Void> publisher() { return null; }
90

91 92
		public List<Void> list() { return null; }

93 94 95
	}

}