ResourceDecoderTests.java 3.4 KB
Newer Older
1
/*
2
 * Copyright 2002-2019 the original author or authors.
3 4 5 6 7
 *
 * 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
 *
S
Spring Operator 已提交
8
 *      https://www.apache.org/licenses/LICENSE-2.0
9 10 11 12 13 14 15 16
 *
 * 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.core.codec;
18 19

import java.io.IOException;
20
import java.nio.charset.StandardCharsets;
21
import java.util.Collections;
22

23
import org.junit.jupiter.api.Test;
24 25
import reactor.core.publisher.Flux;

26
import org.springframework.core.ResolvableType;
27 28 29 30
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.buffer.DataBuffer;
31
import org.springframework.util.MimeTypeUtils;
32 33
import org.springframework.util.StreamUtils;

34
import static org.assertj.core.api.Assertions.assertThat;
P
Phillip Webb 已提交
35
import static org.springframework.core.ResolvableType.forClass;
36 37 38 39

/**
 * @author Arjen Poutsma
 */
40
class ResourceDecoderTests extends AbstractDecoderTestCase<ResourceDecoder> {
41

42
	private final byte[] fooBytes = "foo".getBytes(StandardCharsets.UTF_8);
43

44 45 46
	private final byte[] barBytes = "bar".getBytes(StandardCharsets.UTF_8);


47
	ResourceDecoderTests() {
48 49 50 51
		super(new ResourceDecoder());
	}

	@Override
52
	@Test
53
	public void canDecode() {
54 55 56 57 58
		assertThat(this.decoder.canDecode(forClass(InputStreamResource.class), MimeTypeUtils.TEXT_PLAIN)).isTrue();
		assertThat(this.decoder.canDecode(forClass(ByteArrayResource.class), MimeTypeUtils.TEXT_PLAIN)).isTrue();
		assertThat(this.decoder.canDecode(forClass(Resource.class), MimeTypeUtils.TEXT_PLAIN)).isTrue();
		assertThat(this.decoder.canDecode(forClass(InputStreamResource.class), MimeTypeUtils.APPLICATION_JSON)).isTrue();
		assertThat(this.decoder.canDecode(forClass(Object.class), MimeTypeUtils.APPLICATION_JSON)).isFalse();
59 60
	}

61 62

	@Override
63
	@Test
64
	public void decode() {
65
		Flux<DataBuffer> input = Flux.concat(dataBuffer(this.fooBytes), dataBuffer(this.barBytes));
66

67
		testDecodeAll(input, Resource.class, step -> step
68
				.consumeNextWith(resource -> {
69
					try {
70
						byte[] bytes = StreamUtils.copyToByteArray(resource.getInputStream());
71
						assertThat(new String(bytes)).isEqualTo("foobar");
72
					}
73 74
					catch (IOException ex) {
						throw new AssertionError(ex.getMessage(), ex);
75
					}
76 77
				})
				.expectComplete()
78
				.verify());
79 80
	}

81
	@Override
82
	public void decodeToMono() {
83 84 85 86
		Flux<DataBuffer> input = Flux.concat(
				dataBuffer(this.fooBytes),
				dataBuffer(this.barBytes));

87 88 89 90 91 92
		testDecodeToMonoAll(input, ResolvableType.forClass(Resource.class),
				step -> step
						.consumeNextWith(value -> {
							Resource resource = (Resource) value;
							try {
								byte[] bytes = StreamUtils.copyToByteArray(resource.getInputStream());
93 94
								assertThat(new String(bytes)).isEqualTo("foobar");
								assertThat(resource.getFilename()).isEqualTo("testFile");
95
							}
96 97
							catch (IOException ex) {
								throw new AssertionError(ex.getMessage(), ex);
98 99 100 101 102 103
							}
						})
						.expectComplete()
						.verify(),
				null,
				Collections.singletonMap(ResourceDecoder.FILENAME_HINT, "testFile"));
104 105
	}

106
}