ResourceDecoderTests.java 2.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright 2002-2016 the original author or authors.
 *
 * 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.core.codec;
18 19

import java.io.IOException;
20
import java.util.Collections;
21 22 23

import org.junit.Test;
import reactor.core.publisher.Flux;
24
import reactor.test.subscriber.ScriptedSubscriber;
25 26 27 28 29

import org.springframework.core.ResolvableType;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
30
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
31
import org.springframework.core.io.buffer.DataBuffer;
32
import org.springframework.util.MimeTypeUtils;
33 34
import org.springframework.util.StreamUtils;

35 36 37
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
38 39 40 41

/**
 * @author Arjen Poutsma
 */
42
public class ResourceDecoderTests extends AbstractDataBufferAllocatingTestCase {
43 44 45 46 47

	private final ResourceDecoder decoder = new ResourceDecoder();

	@Test
	public void canDecode() throws Exception {
48
		assertTrue(this.decoder.canDecode(ResolvableType.forClass(InputStreamResource.class),
49
				MimeTypeUtils.TEXT_PLAIN));
50
		assertTrue(this.decoder.canDecode(ResolvableType.forClass(ByteArrayResource.class),
51
				MimeTypeUtils.TEXT_PLAIN));
52
		assertTrue(this.decoder.canDecode(ResolvableType.forClass(Resource.class),
53
				MimeTypeUtils.TEXT_PLAIN));
54
		assertTrue(this.decoder.canDecode(ResolvableType.forClass(InputStreamResource.class),
55
				MimeTypeUtils.APPLICATION_JSON));
56 57 58 59 60 61 62 63
	}

	@Test
	public void decode() throws Exception {
		DataBuffer fooBuffer = stringBuffer("foo");
		DataBuffer barBuffer = stringBuffer("bar");
		Flux<DataBuffer> source = Flux.just(fooBuffer, barBuffer);

64
		Flux<Resource> result = this.decoder
65
				.decode(source, ResolvableType.forClass(Resource.class), null, Collections.emptyMap());
66

67 68
		ScriptedSubscriber.<Resource>create()
				.consumeNextWith(resource -> {
69
					try {
70
						byte[] bytes = StreamUtils.copyToByteArray(resource.getInputStream());
71 72 73 74 75
						assertEquals("foobar", new String(bytes));
					}
					catch (IOException e) {
						fail(e.getMessage());
					}
76 77 78
				})
				.expectComplete()
				.verify(result);
79 80 81
	}

}