ResponseBodyResultHandlerTests.java 2.8 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.method.annotation;
18 19 20 21 22 23

import java.util.Collections;

import org.junit.Test;
import org.reactivestreams.Publisher;

24
import org.springframework.core.ResolvableType;
25
import org.springframework.core.codec.support.StringEncoder;
26
import org.springframework.core.convert.support.DefaultConversionService;
A
Arjen Poutsma 已提交
27
import org.springframework.http.converter.reactive.CodecHttpMessageConverter;
28
import org.springframework.ui.ExtendedModelMap;
29 30
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
31
import org.springframework.web.reactive.HandlerResult;
32

33 34 35
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

36 37 38 39 40
/**
 * @author Sebastien Deleuze
 */
public class ResponseBodyResultHandlerTests {

41

42 43
	@Test
	public void supports() throws NoSuchMethodException {
44
		ResponseBodyResultHandler handler = new ResponseBodyResultHandler(Collections.singletonList(
A
Arjen Poutsma 已提交
45
				new CodecHttpMessageConverter<String>(new StringEncoder(), null)),
46
				new DefaultConversionService());
47 48
		TestController controller = new TestController();

49
		HandlerMethod hm = new HandlerMethod(controller,TestController.class.getMethod("notAnnotated"));
50
		ResolvableType type = ResolvableType.forMethodParameter(hm.getReturnType());
51
		assertFalse(handler.supports(new HandlerResult(hm, null, type, new ExtendedModelMap())));
52

53
		hm = new HandlerMethod(controller, TestController.class.getMethod("publisherString"));
54
		type = ResolvableType.forMethodParameter(hm.getReturnType());
55
		assertTrue(handler.supports(new HandlerResult(hm, null, type, new ExtendedModelMap())));
56

57
		hm = new HandlerMethod(controller, TestController.class.getMethod("publisherVoid"));
58
		type = ResolvableType.forMethodParameter(hm.getReturnType());
59
		assertTrue(handler.supports(new HandlerResult(hm, null, type, new ExtendedModelMap())));
60 61 62
	}


63
	@SuppressWarnings("unused")
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
	private static class TestController {

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

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

		@ResponseBody
		public Publisher<Void> publisherVoid() {
			return null;
		}
	}

}