* iagotomas@gmail.com

* iagotomas@gmail.com

* iagotomas@gmail.com

* iagotomas@gmail.com

* iagotomas@gmail.com

* - Added dependency rx-java 1.1.1
- Added rx stringobservable tests to demo usage

* iagotomas@gmail.com
This commit is contained in:
Iago Tomas 2018-02-24 10:10:13 +01:00 committed by Grzegorz Piwowarek
parent 9371794370
commit 2aa56c3ffc
2 changed files with 151 additions and 0 deletions

View File

@ -31,6 +31,12 @@
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>io.reactivex</groupId>
<artifactId>rxjava-string</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

View File

@ -0,0 +1,145 @@
package com.baeldung.rxjava.operators;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import org.junit.Assert;
import org.junit.Test;
import rx.Observable;
import rx.observables.StringObservable;
import rx.observers.TestSubscriber;
public class RxStringOperatorsTest
{
@Test
public void givenStringObservable_whenFromInputStream_ThenSuccessfull()
{
//given
ByteArrayInputStream is = new ByteArrayInputStream("Lorem ipsum loream, Lorem ipsum lore".getBytes(StandardCharsets.UTF_8));
TestSubscriber<String> subscriber = TestSubscriber.create();
// when
StringObservable.decode(StringObservable.from(is), StandardCharsets.UTF_8)
.subscribe(subscriber);
// then
subscriber.assertCompleted();
subscriber.assertNoErrors();
subscriber.assertValueCount(1);
subscriber.assertValues("Lorem ipsum loream, Lorem ipsum lore");
}
@Test
public void givenStringObservable_whenEncodingString_ThenSuccessfullObtainingByteStream()
{
//given
Observable<String> sourceObservable = Observable.just("Lorem ipsum loream");
TestSubscriber<byte[]> subscriber = TestSubscriber.create();
// when
StringObservable.encode(sourceObservable, StandardCharsets.UTF_8)
.subscribe(subscriber);
// then
subscriber.assertCompleted();
subscriber.assertNoErrors();
subscriber.assertValueCount(1);
subscriber.getOnNextEvents()
.stream()
.forEach(bytes -> Assert.assertTrue(Arrays.equals(bytes, "Lorem ipsum loream".getBytes(StandardCharsets.UTF_8))));
}
@Test
public void givenStringObservable_whenConcatenatingStrings_ThenSuccessfullObtainingSingleString()
{
//given
Observable<String> sourceObservable = Observable.just("Lorem ipsum loream","Lorem ipsum lore");
TestSubscriber<String> subscriber = TestSubscriber.create();
// when
StringObservable.stringConcat(sourceObservable)
.subscribe(subscriber);
// then
subscriber.assertCompleted();
subscriber.assertNoErrors();
subscriber.assertValueCount(1);
subscriber.assertValues("Lorem ipsum loreamLorem ipsum lore");
}
@Test
public void givenStringObservable_whenDecodingByteArray_ThenSuccessfullObtainingStringStream()
{
//given
Observable<byte[]> sourceObservable = Observable.just("Lorem ipsum loream".getBytes(StandardCharsets.UTF_8));
TestSubscriber<String> subscriber = TestSubscriber.create();
// when
StringObservable.decode(sourceObservable, StandardCharsets.UTF_8)
.subscribe(subscriber);
// then
subscriber.assertCompleted();
subscriber.assertNoErrors();
subscriber.assertValueCount(1);
subscriber.assertValues("Lorem ipsum loream");
}
@Test
public void givenStringObservable_whenStringSplitted_ThenSuccessfullObtainingStringsStream()
{
//given
Observable<String> sourceObservable = Observable.just("Lorem ipsum loream,Lorem ipsum lore");
TestSubscriber<String> subscriber = TestSubscriber.create();
// when
StringObservable.split(sourceObservable,",")
.subscribe(subscriber);
// then
subscriber.assertCompleted();
subscriber.assertNoErrors();
subscriber.assertValueCount(2);
subscriber.assertValues("Lorem ipsum loream", "Lorem ipsum lore");
}
@Test
public void givenStringObservable_whenSplittingByLine_ThenSuccessfullObtainingStringsStream() {
//given
Observable<String> sourceObservable = Observable.just("Lorem ipsum loream\nLorem ipsum lore");
TestSubscriber<String> subscriber = TestSubscriber.create();
// when
StringObservable.byLine(sourceObservable)
.subscribe(subscriber);
// then
subscriber.assertCompleted();
subscriber.assertNoErrors();
subscriber.assertValueCount(2);
subscriber.assertValues("Lorem ipsum loream", "Lorem ipsum lore");
}
@Test
public void givenStringObservable_whenJoiningStrings_ThenSuccessfullObtainingSingleString() {
//given
Observable<String> sourceObservable = Observable.just("Lorem ipsum loream","Lorem ipsum lore");
TestSubscriber<String> subscriber = TestSubscriber.create();
// when
StringObservable.join(sourceObservable,",")
.subscribe(subscriber);
// then
subscriber.assertCompleted();
subscriber.assertNoErrors();
subscriber.assertValueCount(1);
subscriber.assertValues("Lorem ipsum loream,Lorem ipsum lore");
}
}