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 24 25

import org.junit.Test;
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;

P
Phillip Webb 已提交
34 35 36 37 38
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.springframework.core.ResolvableType.forClass;
39 40 41 42

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

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

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


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

	@Override
55
	@Test
56 57 58 59 60 61
	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));
62 63
	}

64 65

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

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

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

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

109
}