Move spring-web-reactive classes to spring-core
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.springframework.core.codec;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.test.TestSubscriber;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
public class ByteBufferDecoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
|
||||
private final ByteBufferDecoder decoder = new ByteBufferDecoder();
|
||||
|
||||
@Test
|
||||
public void canDecode() {
|
||||
assertTrue(this.decoder.canDecode(ResolvableType.forClass(ByteBuffer.class), MimeTypeUtils.TEXT_PLAIN));
|
||||
assertFalse(this.decoder.canDecode(ResolvableType.forClass(Integer.class), MimeTypeUtils.TEXT_PLAIN));
|
||||
assertTrue(this.decoder.canDecode(ResolvableType.forClass(ByteBuffer.class), MimeTypeUtils.APPLICATION_JSON));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decode() {
|
||||
DataBuffer fooBuffer = stringBuffer("foo");
|
||||
DataBuffer barBuffer = stringBuffer("bar");
|
||||
Flux<DataBuffer> source = Flux.just(fooBuffer, barBuffer);
|
||||
Flux<ByteBuffer> output = this.decoder.decode(source,
|
||||
ResolvableType.forClassWithGenerics(Publisher.class, ByteBuffer.class),
|
||||
null);
|
||||
TestSubscriber
|
||||
.subscribe(output)
|
||||
.assertNoError()
|
||||
.assertComplete()
|
||||
.assertValues(ByteBuffer.wrap("foo".getBytes()), ByteBuffer.wrap("bar".getBytes()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.springframework.core.codec;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.test.TestSubscriber;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
public class ByteBufferEncoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
|
||||
private ByteBufferEncoder encoder;
|
||||
|
||||
@Before
|
||||
public void createEncoder() {
|
||||
this.encoder = new ByteBufferEncoder();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canEncode() {
|
||||
assertTrue(this.encoder.canEncode(ResolvableType.forClass(ByteBuffer.class), MimeTypeUtils.TEXT_PLAIN));
|
||||
assertFalse(this.encoder.canEncode(ResolvableType.forClass(Integer.class), MimeTypeUtils.TEXT_PLAIN));
|
||||
assertTrue(this.encoder.canEncode(ResolvableType.forClass(ByteBuffer.class), MimeTypeUtils.APPLICATION_JSON));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encode() {
|
||||
byte[] fooBytes = "foo".getBytes(StandardCharsets.UTF_8);
|
||||
byte[] barBytes = "bar".getBytes(StandardCharsets.UTF_8);
|
||||
Flux<ByteBuffer> source =
|
||||
Flux.just(ByteBuffer.wrap(fooBytes), ByteBuffer.wrap(barBytes));
|
||||
|
||||
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory,
|
||||
ResolvableType.forClassWithGenerics(Publisher.class, ByteBuffer.class),
|
||||
null);
|
||||
TestSubscriber
|
||||
.subscribe(output)
|
||||
.assertValuesWith(b -> {
|
||||
byte[] buf = new byte[3];
|
||||
b.read(buf);
|
||||
assertArrayEquals(fooBytes, buf);
|
||||
}, b -> {
|
||||
byte[] buf = new byte[3];
|
||||
b.read(buf);
|
||||
assertArrayEquals(barBytes, buf);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.springframework.core.codec;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.test.TestSubscriber;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class ResourceDecoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
|
||||
private final ResourceDecoder decoder = new ResourceDecoder();
|
||||
|
||||
@Test
|
||||
public void canDecode() throws Exception {
|
||||
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));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decode() throws Exception {
|
||||
DataBuffer fooBuffer = stringBuffer("foo");
|
||||
DataBuffer barBuffer = stringBuffer("bar");
|
||||
Flux<DataBuffer> source = Flux.just(fooBuffer, barBuffer);
|
||||
|
||||
Flux<Resource> result = this.decoder
|
||||
.decode(source, ResolvableType.forClass(Resource.class), null);
|
||||
|
||||
TestSubscriber
|
||||
.subscribe(result)
|
||||
.assertNoError()
|
||||
.assertComplete()
|
||||
.assertValuesWith(resource -> {
|
||||
try {
|
||||
byte[] bytes =
|
||||
StreamUtils.copyToByteArray(resource.getInputStream());
|
||||
assertEquals("foobar", new String(bytes));
|
||||
}
|
||||
catch (IOException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.springframework.core.codec;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.core.test.TestSubscriber;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class ResourceEncoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
|
||||
private final ResourceEncoder encoder = new ResourceEncoder();
|
||||
|
||||
@Test
|
||||
public void canEncode() throws Exception {
|
||||
assertTrue(this.encoder.canEncode(
|
||||
ResolvableType.forClass(InputStreamResource.class), MimeTypeUtils.TEXT_PLAIN));
|
||||
assertTrue(this.encoder.canEncode(
|
||||
ResolvableType.forClass(ByteArrayResource.class), MimeTypeUtils.TEXT_PLAIN));
|
||||
assertTrue(this.encoder.canEncode(
|
||||
ResolvableType.forClass(Resource.class), MimeTypeUtils.TEXT_PLAIN));
|
||||
assertTrue(this.encoder.canEncode(
|
||||
ResolvableType.forClass(InputStreamResource.class), MimeTypeUtils.APPLICATION_JSON));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encode() throws Exception {
|
||||
String s = "foo";
|
||||
Resource resource = new ByteArrayResource(s.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
Mono<Resource> source = Mono.just(resource);
|
||||
|
||||
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory,
|
||||
ResolvableType.forClass(Resource.class),
|
||||
null);
|
||||
|
||||
TestSubscriber
|
||||
.subscribe(output)
|
||||
.assertNoError()
|
||||
.assertComplete()
|
||||
.assertValuesWith(stringConsumer(s));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.springframework.core.codec;
|
||||
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.core.test.TestSubscriber;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Sebastien Deleuze
|
||||
* @author Brian Clozel
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class StringDecoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
|
||||
private StringDecoder decoder = new StringDecoder();
|
||||
|
||||
|
||||
@Test
|
||||
public void canDecode() {
|
||||
assertTrue(this.decoder.canDecode(ResolvableType.forClass(String.class), MimeTypeUtils.TEXT_PLAIN));
|
||||
assertTrue(this.decoder.canDecode(ResolvableType.forClass(String.class), MimeTypeUtils.TEXT_HTML));
|
||||
assertTrue(this.decoder.canDecode(ResolvableType.forClass(String.class), MimeTypeUtils.APPLICATION_JSON));
|
||||
assertFalse(this.decoder.canDecode(ResolvableType.forClass(Integer.class), MimeTypeUtils.TEXT_PLAIN));
|
||||
assertFalse(this.decoder.canDecode(ResolvableType.forClass(Object.class), MimeTypeUtils.APPLICATION_JSON));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decode() throws InterruptedException {
|
||||
this.decoder = new StringDecoder(false);
|
||||
Flux<DataBuffer> source = Flux.just(stringBuffer("foo"), stringBuffer("bar"), stringBuffer("baz"));
|
||||
Flux<String> output = this.decoder.decode(source, ResolvableType.forClass(String.class), null);
|
||||
|
||||
TestSubscriber.subscribe(output)
|
||||
.assertNoError()
|
||||
.assertComplete()
|
||||
.assertValues("foo", "bar", "baz");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodeNewLine() throws InterruptedException {
|
||||
DataBuffer fooBar = stringBuffer("\nfoo\r\nbar\r");
|
||||
DataBuffer baz = stringBuffer("\nbaz");
|
||||
Flux<DataBuffer> source = Flux.just(fooBar, baz);
|
||||
Flux<String> output = decoder.decode(source, ResolvableType.forClass(String.class), null);
|
||||
|
||||
TestSubscriber.subscribe(output)
|
||||
.assertNoError()
|
||||
.assertComplete().assertValues("\n", "foo\r", "\n", "bar\r", "\n", "baz");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodeEmptyFlux() throws InterruptedException {
|
||||
Flux<DataBuffer> source = Flux.empty();
|
||||
Flux<String> output = this.decoder.decode(source, ResolvableType.forClass(String.class), null);
|
||||
|
||||
TestSubscriber.subscribe(output)
|
||||
.assertNoError()
|
||||
.assertComplete()
|
||||
.assertNoValues();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodeEmptyString() throws InterruptedException {
|
||||
Flux<DataBuffer> source = Flux.just(stringBuffer(""));
|
||||
Flux<String> output = this.decoder.decode(source, ResolvableType.forClass(String.class), null);
|
||||
|
||||
TestSubscriber.subscribe(output).assertValues("");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodeToMono() throws InterruptedException {
|
||||
this.decoder = new StringDecoder(false);
|
||||
Flux<DataBuffer> source = Flux.just(stringBuffer("foo"), stringBuffer("bar"), stringBuffer("baz"));
|
||||
Mono<String> output = this.decoder.decodeToMono(source, ResolvableType.forClass(String.class), null);
|
||||
|
||||
TestSubscriber.subscribe(output)
|
||||
.assertNoError()
|
||||
.assertComplete()
|
||||
.assertValues("foobarbaz");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodeToMonoWithEmptyFlux() throws InterruptedException {
|
||||
Flux<DataBuffer> source = Flux.empty();
|
||||
Mono<String> output = this.decoder.decodeToMono(source, ResolvableType.forClass(String.class), null);
|
||||
|
||||
TestSubscriber.subscribe(output)
|
||||
.assertNoError()
|
||||
.assertComplete()
|
||||
.assertNoValues();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.springframework.core.codec;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.test.TestSubscriber;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
|
||||
import org.springframework.core.io.buffer.support.DataBufferUtils;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class StringEncoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
|
||||
private StringEncoder encoder;
|
||||
|
||||
@Before
|
||||
public void createEncoder() {
|
||||
this.encoder = new StringEncoder();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canWrite() {
|
||||
assertTrue(this.encoder.canEncode(ResolvableType.forClass(String.class), MimeTypeUtils.TEXT_PLAIN));
|
||||
assertFalse(this.encoder.canEncode(ResolvableType.forClass(Integer.class), MimeTypeUtils.TEXT_PLAIN));
|
||||
assertFalse(this.encoder.canEncode(ResolvableType.forClass(String.class), MimeTypeUtils.APPLICATION_JSON));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void write() throws InterruptedException {
|
||||
Flux<String> output = Flux.from(
|
||||
this.encoder.encode(Flux.just("foo"), this.bufferFactory, null, null))
|
||||
.map(chunk -> {
|
||||
byte[] b = new byte[chunk.readableByteCount()];
|
||||
chunk.read(b);
|
||||
DataBufferUtils.release(chunk);
|
||||
return new String(b, StandardCharsets.UTF_8);
|
||||
});
|
||||
TestSubscriber
|
||||
.subscribe(output)
|
||||
.assertValues("foo");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ReactorToRxJava1Converter}.
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class MonoToCompletableFutureConverterTests {
|
||||
|
||||
private GenericConversionService conversionService;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
this.conversionService = new GenericConversionService();
|
||||
this.conversionService.addConverter(new MonoToCompletableFutureConverter());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canConvert() throws Exception {
|
||||
assertTrue(this.conversionService.canConvert(Mono.class, CompletableFuture.class));
|
||||
assertTrue(this.conversionService.canConvert(CompletableFuture.class, Mono.class));
|
||||
|
||||
assertFalse(this.conversionService.canConvert(Flux.class, CompletableFuture.class));
|
||||
assertFalse(this.conversionService.canConvert(CompletableFuture.class, Flux.class));
|
||||
|
||||
assertFalse(this.conversionService.canConvert(Publisher.class, CompletableFuture.class));
|
||||
assertFalse(this.conversionService.canConvert(CompletableFuture.class, Publisher.class));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import rx.Completable;
|
||||
import rx.Observable;
|
||||
import rx.Single;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ReactorToRxJava1Converter}.
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class ReactorToRxJava1ConverterTests {
|
||||
|
||||
private GenericConversionService conversionService;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
this.conversionService = new GenericConversionService();
|
||||
this.conversionService.addConverter(new ReactorToRxJava1Converter());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canConvert() throws Exception {
|
||||
assertTrue(this.conversionService.canConvert(Flux.class, Observable.class));
|
||||
assertTrue(this.conversionService.canConvert(Observable.class, Flux.class));
|
||||
|
||||
assertTrue(this.conversionService.canConvert(Mono.class, Single.class));
|
||||
assertTrue(this.conversionService.canConvert(Single.class, Mono.class));
|
||||
|
||||
assertTrue(this.conversionService.canConvert(Mono.class, Completable.class));
|
||||
assertTrue(this.conversionService.canConvert(Completable.class, Mono.class));
|
||||
|
||||
assertFalse(this.conversionService.canConvert(Flux.class, Single.class));
|
||||
assertFalse(this.conversionService.canConvert(Single.class, Flux.class));
|
||||
|
||||
assertFalse(this.conversionService.canConvert(Flux.class, Completable.class));
|
||||
assertFalse(this.conversionService.canConvert(Completable.class, Flux.class));
|
||||
|
||||
assertFalse(this.conversionService.canConvert(Mono.class, Observable.class));
|
||||
assertFalse(this.conversionService.canConvert(Observable.class, Mono.class));
|
||||
|
||||
assertFalse(this.conversionService.canConvert(Publisher.class, Observable.class));
|
||||
assertFalse(this.conversionService.canConvert(Observable.class, Publisher.class));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.springframework.core.io.buffer;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import io.netty.buffer.PooledByteBufAllocator;
|
||||
import io.netty.buffer.UnpooledByteBufAllocator;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
import org.springframework.core.io.buffer.support.DataBufferTestUtils;
|
||||
import org.springframework.core.io.buffer.support.DataBufferUtils;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public abstract class AbstractDataBufferAllocatingTestCase {
|
||||
|
||||
@Parameterized.Parameter
|
||||
public DataBufferFactory bufferFactory;
|
||||
|
||||
@Parameterized.Parameters(name = "{0}")
|
||||
public static Object[][] dataBufferFactories() {
|
||||
return new Object[][]{
|
||||
{new NettyDataBufferFactory(new UnpooledByteBufAllocator(true))},
|
||||
{new NettyDataBufferFactory(new UnpooledByteBufAllocator(false))},
|
||||
{new NettyDataBufferFactory(new PooledByteBufAllocator(true))},
|
||||
{new NettyDataBufferFactory(new PooledByteBufAllocator(false))},
|
||||
{new DefaultDataBufferFactory(true)},
|
||||
{new DefaultDataBufferFactory(false)}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
protected DataBuffer createDataBuffer(int capacity) {
|
||||
return this.bufferFactory.allocateBuffer(capacity);
|
||||
}
|
||||
|
||||
protected DataBuffer stringBuffer(String value) {
|
||||
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
|
||||
DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length);
|
||||
buffer.write(bytes);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
protected void release(DataBuffer... buffers) {
|
||||
Arrays.stream(buffers).forEach(DataBufferUtils::release);
|
||||
}
|
||||
|
||||
protected Consumer<DataBuffer> stringConsumer(String expected) {
|
||||
return dataBuffer -> {
|
||||
String value =
|
||||
DataBufferTestUtils.dumpString(dataBuffer, StandardCharsets.UTF_8);
|
||||
assertEquals(expected, value);
|
||||
DataBufferUtils.release(dataBuffer);
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,260 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.springframework.core.io.buffer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
|
||||
|
||||
@Test
|
||||
public void writeAndRead() {
|
||||
|
||||
DataBuffer buffer = createDataBuffer(5);
|
||||
buffer.write(new byte[]{'a', 'b', 'c'});
|
||||
|
||||
int ch = buffer.read();
|
||||
assertEquals('a', ch);
|
||||
|
||||
buffer.write((byte) 'd');
|
||||
buffer.write((byte) 'e');
|
||||
|
||||
byte[] result = new byte[4];
|
||||
buffer.read(result);
|
||||
|
||||
assertArrayEquals(new byte[]{'b', 'c', 'd', 'e'}, result);
|
||||
|
||||
release(buffer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void inputStream() throws IOException {
|
||||
byte[] data = new byte[]{'a', 'b', 'c', 'd', 'e'};
|
||||
|
||||
DataBuffer buffer = createDataBuffer(4);
|
||||
buffer.write(data);
|
||||
|
||||
buffer.read(); // readIndex++
|
||||
|
||||
InputStream inputStream = buffer.asInputStream();
|
||||
|
||||
int available = inputStream.available();
|
||||
assertEquals(4, available);
|
||||
|
||||
int result = inputStream.read();
|
||||
assertEquals('b', result);
|
||||
|
||||
available = inputStream.available();
|
||||
assertEquals(3, available);
|
||||
|
||||
byte[] bytes = new byte[2];
|
||||
int len = inputStream.read(bytes);
|
||||
assertEquals(2, len);
|
||||
assertArrayEquals(new byte[]{'c', 'd'}, bytes);
|
||||
|
||||
Arrays.fill(bytes, (byte) 0);
|
||||
len = inputStream.read(bytes);
|
||||
assertEquals(1, len);
|
||||
assertArrayEquals(new byte[]{'e', (byte) 0}, bytes);
|
||||
|
||||
release(buffer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void outputStream() throws IOException {
|
||||
DataBuffer buffer = createDataBuffer(4);
|
||||
buffer.write((byte) 'a');
|
||||
|
||||
OutputStream outputStream = buffer.asOutputStream();
|
||||
outputStream.write(new byte[]{'b', 'c', 'd'});
|
||||
|
||||
buffer.write((byte) 'e');
|
||||
|
||||
byte[] bytes = new byte[5];
|
||||
buffer.read(bytes);
|
||||
assertArrayEquals(new byte[]{'a', 'b', 'c', 'd', 'e'}, bytes);
|
||||
|
||||
release(buffer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expand() {
|
||||
DataBuffer buffer = createDataBuffer(1);
|
||||
buffer.write((byte) 'a');
|
||||
buffer.write((byte) 'b');
|
||||
|
||||
byte[] result = new byte[2];
|
||||
buffer.read(result);
|
||||
assertArrayEquals(new byte[]{'a', 'b'}, result);
|
||||
|
||||
buffer.write(new byte[]{'c', 'd'});
|
||||
|
||||
result = new byte[2];
|
||||
buffer.read(result);
|
||||
assertArrayEquals(new byte[]{'c', 'd'}, result);
|
||||
|
||||
release(buffer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeByteBuffer() {
|
||||
DataBuffer buffer1 = createDataBuffer(1);
|
||||
buffer1.write((byte) 'a');
|
||||
ByteBuffer buffer2 = createByteBuffer(2);
|
||||
buffer2.put((byte) 'b');
|
||||
buffer2.flip();
|
||||
ByteBuffer buffer3 = createByteBuffer(3);
|
||||
buffer3.put((byte) 'c');
|
||||
buffer3.flip();
|
||||
|
||||
buffer1.write(buffer2, buffer3);
|
||||
buffer1.write((byte) 'd'); // make sure the write index is correctly set
|
||||
|
||||
assertEquals(4, buffer1.readableByteCount());
|
||||
byte[] result = new byte[4];
|
||||
buffer1.read(result);
|
||||
|
||||
assertArrayEquals(new byte[]{'a', 'b', 'c', 'd'}, result);
|
||||
|
||||
release(buffer1);
|
||||
}
|
||||
|
||||
private ByteBuffer createByteBuffer(int capacity) {
|
||||
return ByteBuffer.allocate(capacity);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeDataBuffer() {
|
||||
DataBuffer buffer1 = createDataBuffer(1);
|
||||
buffer1.write((byte) 'a');
|
||||
DataBuffer buffer2 = createDataBuffer(2);
|
||||
buffer2.write((byte) 'b');
|
||||
DataBuffer buffer3 = createDataBuffer(3);
|
||||
buffer3.write((byte) 'c');
|
||||
|
||||
buffer1.write(buffer2, buffer3);
|
||||
buffer1.write((byte) 'd'); // make sure the write index is correctly set
|
||||
|
||||
assertEquals(4, buffer1.readableByteCount());
|
||||
byte[] result = new byte[4];
|
||||
buffer1.read(result);
|
||||
|
||||
assertArrayEquals(new byte[]{'a', 'b', 'c', 'd'}, result);
|
||||
|
||||
release(buffer1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asByteBuffer() {
|
||||
DataBuffer buffer = createDataBuffer(4);
|
||||
buffer.write(new byte[]{'a', 'b', 'c'});
|
||||
buffer.read(); // skip a
|
||||
|
||||
ByteBuffer result = buffer.asByteBuffer();
|
||||
|
||||
buffer.write((byte) 'd');
|
||||
assertEquals(2, result.remaining());
|
||||
byte[] resultBytes = new byte[2];
|
||||
buffer.read(resultBytes);
|
||||
assertArrayEquals(new byte[]{'b', 'c'}, resultBytes);
|
||||
|
||||
release(buffer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void indexOf() {
|
||||
DataBuffer buffer = createDataBuffer(3);
|
||||
buffer.write(new byte[]{'a', 'b', 'c'});
|
||||
|
||||
int result = buffer.indexOf(b -> b == 'c', 0);
|
||||
assertEquals(2, result);
|
||||
|
||||
result = buffer.indexOf(b -> b == 'c', Integer.MIN_VALUE);
|
||||
assertEquals(2, result);
|
||||
|
||||
result = buffer.indexOf(b -> b == 'c', Integer.MAX_VALUE);
|
||||
assertEquals(-1, result);
|
||||
|
||||
result = buffer.indexOf(b -> b == 'z', 0);
|
||||
assertEquals(-1, result);
|
||||
|
||||
release(buffer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lastIndexOf() {
|
||||
DataBuffer buffer = createDataBuffer(3);
|
||||
buffer.write(new byte[]{'a', 'b', 'c'});
|
||||
|
||||
int result = buffer.lastIndexOf(b -> b == 'b', 3);
|
||||
assertEquals(1, result);
|
||||
|
||||
result = buffer.lastIndexOf(b -> b == 'b', Integer.MAX_VALUE);
|
||||
assertEquals(1, result);
|
||||
|
||||
result = buffer.lastIndexOf(b -> b == 'b', Integer.MIN_VALUE);
|
||||
assertEquals(-1, result);
|
||||
|
||||
result = buffer.lastIndexOf(b -> b == 'z', 0);
|
||||
assertEquals(-1, result);
|
||||
|
||||
release(buffer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void slice() {
|
||||
DataBuffer buffer = createDataBuffer(3);
|
||||
buffer.write(new byte[]{'a', 'b'});
|
||||
|
||||
DataBuffer slice = buffer.slice(1, 2);
|
||||
assertEquals(2, slice.readableByteCount());
|
||||
try {
|
||||
slice.write((byte) 0);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
}
|
||||
catch (Exception ignored) {
|
||||
}
|
||||
buffer.write((byte) 'c');
|
||||
|
||||
assertEquals(3, buffer.readableByteCount());
|
||||
byte[] result = new byte[3];
|
||||
buffer.read(result);
|
||||
|
||||
assertArrayEquals(new byte[]{'a', 'b', 'c'}, result);
|
||||
|
||||
assertEquals(2, slice.readableByteCount());
|
||||
result = new byte[2];
|
||||
slice.read(result);
|
||||
|
||||
assertArrayEquals(new byte[]{'b', 'c'}, result);
|
||||
|
||||
|
||||
release(buffer);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.springframework.core.io.buffer;
|
||||
|
||||
import io.netty.buffer.PooledByteBufAllocator;
|
||||
import io.netty.buffer.UnpooledByteBufAllocator;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class PooledDataBufferTests {
|
||||
|
||||
@Parameterized.Parameter
|
||||
public DataBufferFactory dataBufferFactory;
|
||||
|
||||
@Parameterized.Parameters(name = "{0}")
|
||||
public static Object[][] buffers() {
|
||||
|
||||
return new Object[][]{
|
||||
{new NettyDataBufferFactory(new UnpooledByteBufAllocator(true))},
|
||||
{new NettyDataBufferFactory(new UnpooledByteBufAllocator(false))},
|
||||
{new NettyDataBufferFactory(new PooledByteBufAllocator(true))},
|
||||
{new NettyDataBufferFactory(new PooledByteBufAllocator(false))}};
|
||||
}
|
||||
|
||||
private PooledDataBuffer createDataBuffer(int capacity) {
|
||||
return (PooledDataBuffer) dataBufferFactory.allocateBuffer(capacity);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retainAndRelease() {
|
||||
PooledDataBuffer buffer = createDataBuffer(1);
|
||||
buffer.write((byte) 'a');
|
||||
|
||||
buffer.retain();
|
||||
boolean result = buffer.release();
|
||||
assertFalse(result);
|
||||
result = buffer.release();
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void tooManyReleases() {
|
||||
PooledDataBuffer buffer = createDataBuffer(1);
|
||||
buffer.write((byte) 'a');
|
||||
|
||||
buffer.release();
|
||||
buffer.release();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.springframework.core.io.buffer.support;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Utility class for working with {@link DataBuffer}s in tests.
|
||||
*
|
||||
* <p>Note that this class is in the {@code test} tree of the project: the methods
|
||||
* contained herein are not suitable for production code bases.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public abstract class DataBufferTestUtils {
|
||||
|
||||
/**
|
||||
* Dumps all the bytes in the given data buffer, and returns them as a byte array.
|
||||
*
|
||||
* <p>Note that this method reads the entire buffer into the heap, which might
|
||||
* consume a lot of memory.
|
||||
* @param buffer the data buffer to dump the bytes of
|
||||
* @return the bytes in the given data buffer
|
||||
*/
|
||||
public static byte[] dumpBytes(DataBuffer buffer) {
|
||||
Assert.notNull(buffer, "'buffer' must not be null");
|
||||
|
||||
byte[] bytes = new byte[buffer.readableByteCount()];
|
||||
buffer.read(bytes);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dumps all the bytes in the given data buffer, and returns them as a string.
|
||||
*
|
||||
* <p>Note that this method reads the entire buffer into the heap, which might
|
||||
* consume a lot of memory.
|
||||
* @param buffer the data buffer to dump the string contents of
|
||||
* @param charset the charset of the data
|
||||
* @return the string representation of the given data buffer
|
||||
*/
|
||||
public static String dumpString(DataBuffer buffer, Charset charset) {
|
||||
Assert.notNull(charset, "'charset' must not be null");
|
||||
|
||||
byte[] bytes = dumpBytes(buffer);
|
||||
return new String(bytes, charset);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.springframework.core.io.buffer.support;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class DataBufferTestUtilsTests extends AbstractDataBufferAllocatingTestCase {
|
||||
|
||||
@Test
|
||||
public void dumpBytes() {
|
||||
DataBuffer buffer = this.bufferFactory.allocateBuffer(4);
|
||||
byte[] source = {'a', 'b', 'c', 'd'};
|
||||
buffer.write(source);
|
||||
|
||||
byte[] result = DataBufferTestUtils.dumpBytes(buffer);
|
||||
|
||||
assertArrayEquals(source, result);
|
||||
|
||||
release(buffer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dumpString() {
|
||||
DataBuffer buffer = this.bufferFactory.allocateBuffer(4);
|
||||
String source = "abcd";
|
||||
buffer.write(source.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
String result = DataBufferTestUtils.dumpString(buffer, StandardCharsets.UTF_8);
|
||||
|
||||
assertEquals(source, result);
|
||||
|
||||
release(buffer);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.springframework.core.io.buffer.support;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.test.TestSubscriber;
|
||||
|
||||
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
|
||||
|
||||
@Test
|
||||
public void readChannel() throws Exception {
|
||||
URI uri = DataBufferUtilsTests.class.getResource("DataBufferUtilsTests.txt")
|
||||
.toURI();
|
||||
FileChannel channel = FileChannel.open(Paths.get(uri), StandardOpenOption.READ);
|
||||
|
||||
Flux<DataBuffer> flux = DataBufferUtils.read(channel, this.bufferFactory, 4);
|
||||
|
||||
TestSubscriber
|
||||
.subscribe(flux)
|
||||
.assertNoError()
|
||||
.assertComplete()
|
||||
.assertValuesWith(
|
||||
stringConsumer("foo\n"), stringConsumer("bar\n"),
|
||||
stringConsumer("baz\n"), stringConsumer("qux\n"));
|
||||
|
||||
assertFalse(channel.isOpen());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readUnalignedChannel() throws Exception {
|
||||
URI uri = DataBufferUtilsTests.class.getResource("DataBufferUtilsTests.txt")
|
||||
.toURI();
|
||||
FileChannel channel = FileChannel.open(Paths.get(uri), StandardOpenOption.READ);
|
||||
|
||||
Flux<DataBuffer> flux = DataBufferUtils.read(channel, this.bufferFactory, 3);
|
||||
|
||||
TestSubscriber
|
||||
.subscribe(flux)
|
||||
.assertNoError()
|
||||
.assertComplete()
|
||||
.assertValuesWith(
|
||||
stringConsumer("foo"), stringConsumer("\nba"),
|
||||
stringConsumer("r\nb"), stringConsumer("az\n"),
|
||||
stringConsumer("qux"), stringConsumer("\n"));
|
||||
|
||||
assertFalse(channel.isOpen());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readInputStream() {
|
||||
InputStream is = DataBufferUtilsTests.class
|
||||
.getResourceAsStream("DataBufferUtilsTests.txt");
|
||||
|
||||
Flux<DataBuffer> flux = DataBufferUtils.read(is, this.bufferFactory, 4);
|
||||
|
||||
TestSubscriber
|
||||
.subscribe(flux)
|
||||
.assertNoError()
|
||||
.assertComplete()
|
||||
.assertValuesWith(
|
||||
stringConsumer("foo\n"), stringConsumer("bar\n"),
|
||||
stringConsumer("baz\n"), stringConsumer("qux\n"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void takeUntilByteCount() {
|
||||
DataBuffer foo = stringBuffer("foo");
|
||||
DataBuffer bar = stringBuffer("bar");
|
||||
DataBuffer baz = stringBuffer("baz");
|
||||
Flux<DataBuffer> flux = Flux.just(foo, bar, baz);
|
||||
|
||||
Flux<DataBuffer> result = DataBufferUtils.takeUntilByteCount(flux, 5L);
|
||||
|
||||
TestSubscriber
|
||||
.subscribe(result)
|
||||
.assertNoError()
|
||||
.assertComplete()
|
||||
.assertValuesWith(stringConsumer("foo"), stringConsumer("ba"));
|
||||
|
||||
release(baz);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.springframework.util.xml;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.xml.stream.XMLEventReader;
|
||||
import javax.xml.stream.XMLEventWriter;
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLOutputFactory;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.stream.events.XMLEvent;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class ListBasedXMLEventReaderTests {
|
||||
|
||||
private final XMLInputFactory inputFactory = XMLInputFactory.newFactory();
|
||||
|
||||
private final XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
|
||||
|
||||
@Test
|
||||
public void standard() throws Exception {
|
||||
String xml = "<foo><bar>baz</bar></foo>";
|
||||
List<XMLEvent> events = readEvents(xml);
|
||||
|
||||
ListBasedXMLEventReader reader = new ListBasedXMLEventReader(events);
|
||||
|
||||
StringWriter resultWriter = new StringWriter();
|
||||
XMLEventWriter writer = this.outputFactory.createXMLEventWriter(resultWriter);
|
||||
writer.add(reader);
|
||||
|
||||
assertXMLEqual(xml, resultWriter.toString());
|
||||
}
|
||||
|
||||
private List<XMLEvent> readEvents(String xml) throws XMLStreamException {
|
||||
XMLEventReader reader =
|
||||
this.inputFactory.createXMLEventReader(new StringReader(xml));
|
||||
List<XMLEvent> events = new ArrayList<>();
|
||||
while (reader.hasNext()) {
|
||||
events.add(reader.nextEvent());
|
||||
}
|
||||
return events;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user