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 20 21 22 23 24 25 26 27

import java.io.IOException;

import org.junit.Test;
import reactor.core.publisher.Flux;

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

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

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

	private final ResourceDecoder decoder = new ResourceDecoder();

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

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

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

66 67 68 69 70
		TestSubscriber
				.subscribe(result)
				.assertNoError()
				.assertComplete()
				.assertValuesWith(resource -> {
71 72 73 74 75 76 77 78 79 80 81 82 83
					try {
						byte[] bytes =
								StreamUtils.copyToByteArray(resource.getInputStream());
						assertEquals("foobar", new String(bytes));
					}
					catch (IOException e) {
						fail(e.getMessage());
					}
				});

	}

}