ResourceDecoderTests.java 3.7 KB
Newer Older
1
/*
2
 * Copyright 2002-2018 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 21
import java.nio.charset.StandardCharsets;
import java.util.Map;
22 23

import org.junit.Test;
24
import org.reactivestreams.Publisher;
25
import reactor.core.publisher.Flux;
26
import reactor.test.StepVerifier;
27

28
import org.springframework.core.ResolvableType;
29 30 31 32
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;
33 34
import org.springframework.lang.Nullable;
import org.springframework.util.MimeType;
35
import org.springframework.util.MimeTypeUtils;
36 37
import org.springframework.util.StreamUtils;

38
import static org.junit.Assert.*;
39
import static org.springframework.core.ResolvableType.forClass;
40 41 42 43

/**
 * @author Arjen Poutsma
 */
44
public class ResourceDecoderTests extends AbstractDecoderTestCase<ResourceDecoder> {
45

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

48 49 50 51 52 53 54 55
	private final byte[] barBytes = "bar".getBytes(StandardCharsets.UTF_8);


	public ResourceDecoderTests() {
		super(new ResourceDecoder());
	}

	@Override
56
	@Test
57 58 59 60 61 62
	public void canDecode() {
		assertTrue(this.decoder.canDecode(forClass(InputStreamResource.class), MimeTypeUtils.TEXT_PLAIN));
		assertTrue(this.decoder.canDecode(forClass(ByteArrayResource.class), MimeTypeUtils.TEXT_PLAIN));
		assertTrue(this.decoder.canDecode(forClass(Resource.class), MimeTypeUtils.TEXT_PLAIN));
		assertTrue(this.decoder.canDecode(forClass(InputStreamResource.class), MimeTypeUtils.APPLICATION_JSON));
		assertFalse(this.decoder.canDecode(forClass(Object.class), MimeTypeUtils.APPLICATION_JSON));
63 64
	}

65 66

	@Override
67
	@Test
68
	public void decode() {
69 70 71
		Flux<DataBuffer> input = Flux.concat(
				dataBuffer(this.fooBytes),
				dataBuffer(this.barBytes));
72

73
		testDecodeAll(input, Resource.class, step -> step
74
				.consumeNextWith(resource -> {
75
					try {
76
						byte[] bytes = StreamUtils.copyToByteArray(resource.getInputStream());
77 78 79 80 81
						assertEquals("foobar", new String(bytes));
					}
					catch (IOException e) {
						fail(e.getMessage());
					}
82 83
				})
				.expectComplete()
84
				.verify());
85 86
	}

87 88 89
	@Override
	protected void testDecodeError(Publisher<DataBuffer> input, ResolvableType outputType,
			@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
90

91 92 93
		input = Flux.concat(
				Flux.from(input).take(1),
				Flux.error(new InputException()));
94

95
		Flux<Resource> result = this.decoder.decode(input, outputType, mimeType, hints);
96 97

		StepVerifier.create(result)
98
				.expectError(InputException.class)
99 100 101
				.verify();
	}

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	@Override
	public void decodeToMono() throws Exception {
		Flux<DataBuffer> input = Flux.concat(
				dataBuffer(this.fooBytes),
				dataBuffer(this.barBytes));

		testDecodeToMonoAll(input, Resource.class, step -> step
				.consumeNextWith(resource -> {
					try {
						byte[] bytes = StreamUtils.copyToByteArray(resource.getInputStream());
						assertEquals("foobar", new String(bytes));
					}
					catch (IOException e) {
						fail(e.getMessage());
					}
				})
				.expectComplete()
				.verify());
	}

122
}