Skip to content

Commit 4562e69

Browse files
authored
2.x: add javadoc and unit test to the recently changed XTransformers (#4674)
* 2.x: add javadoc and unit test to the recently changed XTransformer * Use uppercase F
1 parent c68077b commit 4562e69

File tree

7 files changed

+230
-10
lines changed

7 files changed

+230
-10
lines changed

src/main/java/io/reactivex/CompletableTransformer.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,12 @@
1818
* Completable fluently.
1919
*/
2020
public interface CompletableTransformer {
21-
CompletableSource apply(Completable completable) throws Exception;
21+
/**
22+
* Applies a function to the upstream Completable and returns a CompletableSource.
23+
* @param upstream the upstream Completable instance
24+
* @return the transformed CompletableSource instance
25+
* @throws Exception in case the transformation throws, checked exceptions will be wrapped
26+
* into a RuntimeException
27+
*/
28+
CompletableSource apply(Completable upstream) throws Exception;
2229
}

src/main/java/io/reactivex/FlowableTransformer.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,13 @@
2222
* @param <Downstream> the downstream value type
2323
*/
2424
public interface FlowableTransformer<Upstream, Downstream> {
25-
Publisher<? extends Downstream> apply(Flowable<Upstream> flowable) throws Exception;
25+
/**
26+
* Applies a function to the upstream Flowable and returns a Publisher with
27+
* optionally different element type.
28+
* @param upstream the upstream Flowable instance
29+
* @return the transformed Publisher instance
30+
* @throws Exception in case the transformation throws, checked exceptions will be wrapped
31+
* into a RuntimeException
32+
*/
33+
Publisher<Downstream> apply(Flowable<Upstream> upstream) throws Exception;
2634
}

src/main/java/io/reactivex/MaybeTransformer.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,13 @@
2020
* @param <Downstream> the downstream value type
2121
*/
2222
public interface MaybeTransformer<Upstream, Downstream> {
23-
MaybeSource<Downstream> apply(Maybe<Upstream> maybe) throws Exception;
23+
/**
24+
* Applies a function to the upstream Maybe and returns a MaybeSource with
25+
* optionally different element type.
26+
* @param upstream the upstream Maybe instance
27+
* @return the transformed MaybeSource instance
28+
* @throws Exception in case the transformation throws, checked exceptions will be wrapped
29+
* into a RuntimeException
30+
*/
31+
MaybeSource<Downstream> apply(Maybe<Upstream> upstream) throws Exception;
2432
}

src/main/java/io/reactivex/ObservableTransformer.java

+8
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,13 @@
2020
* @param <Downstream> the downstream value type
2121
*/
2222
public interface ObservableTransformer<Upstream, Downstream> {
23+
/**
24+
* Applies a function to the upstream Observable and returns an ObservableSource with
25+
* optionally different element type.
26+
* @param upstream the upstream Observable instance
27+
* @return the transformed ObservableSource instance
28+
* @throws Exception in case the transformation throws, checked exceptions will be wrapped
29+
* into a RuntimeException
30+
*/
2331
ObservableSource<Downstream> apply(Observable<Upstream> upstream) throws Exception;
2432
}

src/main/java/io/reactivex/SingleTransformer.java

+8
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,13 @@
2020
* @param <Downstream> the downstream value type
2121
*/
2222
public interface SingleTransformer<Upstream, Downstream> {
23+
/**
24+
* Applies a function to the upstream Single and returns a SingleSource with
25+
* optionally different element type.
26+
* @param upstream the upstream Single instance
27+
* @return the transformed SingleSource instance
28+
* @throws Exception in case the transformation throws, checked exceptions will be wrapped
29+
* into a RuntimeException
30+
*/
2331
SingleSource<Downstream> apply(Single<Upstream> upstream) throws Exception;
2432
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/**
2+
* Copyright 2016 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5+
* compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is
10+
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11+
* the License for the specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package io.reactivex;
15+
16+
import static org.junit.Assert.*;
17+
18+
import java.io.IOException;
19+
20+
import org.junit.Test;
21+
import org.reactivestreams.Publisher;
22+
23+
import io.reactivex.exceptions.TestException;
24+
25+
public class TransformerTest {
26+
27+
@Test
28+
public void flowableTransformerThrows() {
29+
try {
30+
Flowable.just(1).compose(new FlowableTransformer<Integer, Integer>() {
31+
@Override
32+
public Publisher<Integer> apply(Flowable<Integer> v) throws Exception {
33+
throw new TestException("Forced failure");
34+
}
35+
});
36+
fail("Should have thrown!");
37+
} catch (TestException ex) {
38+
assertEquals("Forced failure", ex.getMessage());
39+
}
40+
}
41+
42+
@Test
43+
public void flowableTransformerThrowsChecked() {
44+
try {
45+
Flowable.just(1).compose(new FlowableTransformer<Integer, Integer>() {
46+
@Override
47+
public Publisher<Integer> apply(Flowable<Integer> v) throws Exception {
48+
throw new IOException("Forced failure");
49+
}
50+
});
51+
fail("Should have thrown!");
52+
} catch (RuntimeException ex) {
53+
assertTrue(ex.toString(), ex.getCause() instanceof IOException);
54+
assertEquals("Forced failure", ex.getCause().getMessage());
55+
}
56+
}
57+
58+
@Test
59+
public void observableTransformerThrows() {
60+
try {
61+
Observable.just(1).compose(new ObservableTransformer<Integer, Integer>() {
62+
@Override
63+
public Observable<Integer> apply(Observable<Integer> v) throws Exception {
64+
throw new TestException("Forced failure");
65+
}
66+
});
67+
fail("Should have thrown!");
68+
} catch (TestException ex) {
69+
assertEquals("Forced failure", ex.getMessage());
70+
}
71+
}
72+
73+
@Test
74+
public void observableTransformerThrowsChecked() {
75+
try {
76+
Observable.just(1).compose(new ObservableTransformer<Integer, Integer>() {
77+
@Override
78+
public Observable<Integer> apply(Observable<Integer> v) throws Exception {
79+
throw new IOException("Forced failure");
80+
}
81+
});
82+
fail("Should have thrown!");
83+
} catch (RuntimeException ex) {
84+
assertTrue(ex.toString(), ex.getCause() instanceof IOException);
85+
assertEquals("Forced failure", ex.getCause().getMessage());
86+
}
87+
}
88+
89+
@Test
90+
public void singleTransformerThrows() {
91+
try {
92+
Single.just(1).compose(new SingleTransformer<Integer, Integer>() {
93+
@Override
94+
public Single<Integer> apply(Single<Integer> v) throws Exception {
95+
throw new TestException("Forced failure");
96+
}
97+
});
98+
fail("Should have thrown!");
99+
} catch (TestException ex) {
100+
assertEquals("Forced failure", ex.getMessage());
101+
}
102+
}
103+
104+
@Test
105+
public void singleTransformerThrowsChecked() {
106+
try {
107+
Single.just(1).compose(new SingleTransformer<Integer, Integer>() {
108+
@Override
109+
public Single<Integer> apply(Single<Integer> v) throws Exception {
110+
throw new IOException("Forced failure");
111+
}
112+
});
113+
fail("Should have thrown!");
114+
} catch (RuntimeException ex) {
115+
assertTrue(ex.toString(), ex.getCause() instanceof IOException);
116+
assertEquals("Forced failure", ex.getCause().getMessage());
117+
}
118+
}
119+
120+
@Test
121+
public void maybeTransformerThrows() {
122+
try {
123+
Maybe.just(1).compose(new MaybeTransformer<Integer, Integer>() {
124+
@Override
125+
public Maybe<Integer> apply(Maybe<Integer> v) throws Exception {
126+
throw new TestException("Forced failure");
127+
}
128+
});
129+
fail("Should have thrown!");
130+
} catch (TestException ex) {
131+
assertEquals("Forced failure", ex.getMessage());
132+
}
133+
}
134+
135+
@Test
136+
public void maybeTransformerThrowsChecked() {
137+
try {
138+
Maybe.just(1).compose(new MaybeTransformer<Integer, Integer>() {
139+
@Override
140+
public Maybe<Integer> apply(Maybe<Integer> v) throws Exception {
141+
throw new IOException("Forced failure");
142+
}
143+
});
144+
fail("Should have thrown!");
145+
} catch (RuntimeException ex) {
146+
assertTrue(ex.toString(), ex.getCause() instanceof IOException);
147+
assertEquals("Forced failure", ex.getCause().getMessage());
148+
}
149+
}
150+
151+
@Test
152+
public void completabeTransformerThrows() {
153+
try {
154+
Completable.complete().compose(new CompletableTransformer() {
155+
@Override
156+
public Completable apply(Completable v) throws Exception {
157+
throw new TestException("Forced failure");
158+
}
159+
});
160+
fail("Should have thrown!");
161+
} catch (TestException ex) {
162+
assertEquals("Forced failure", ex.getMessage());
163+
}
164+
}
165+
166+
@Test
167+
public void completabeTransformerThrowsChecked() {
168+
try {
169+
Completable.complete().compose(new CompletableTransformer() {
170+
@Override
171+
public Completable apply(Completable v) throws Exception {
172+
throw new IOException("Forced failure");
173+
}
174+
});
175+
fail("Should have thrown!");
176+
} catch (RuntimeException ex) {
177+
assertTrue(ex.toString(), ex.getCause() instanceof IOException);
178+
assertEquals("Forced failure", ex.getCause().getMessage());
179+
}
180+
}
181+
}

src/test/java/io/reactivex/flowable/FlowableCovarianceTest.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ public void accept(Movie v) {
9292
System.out.println(v);
9393
}
9494
})
95-
.compose(new FlowableTransformer<Movie, Object>() {
95+
.compose(new FlowableTransformer<Movie, Movie>() {
9696
@Override
97-
public Publisher<? extends Object> apply(Flowable<Movie> m) {
97+
public Publisher<Movie> apply(Flowable<Movie> m) {
9898
return m.concatWith(Flowable.just(new ActionMovie()));
9999
}
100100
}
@@ -120,7 +120,7 @@ public void testCovarianceOfCompose() {
120120
Flowable<HorrorMovie> movie = Flowable.just(new HorrorMovie());
121121
Flowable<Movie> movie2 = movie.compose(new FlowableTransformer<HorrorMovie, Movie>() {
122122
@Override
123-
public Publisher<? extends Movie> apply(Flowable<HorrorMovie> t) {
123+
public Publisher<Movie> apply(Flowable<HorrorMovie> t) {
124124
return Flowable.just(new Movie());
125125
}
126126
});
@@ -132,7 +132,7 @@ public void testCovarianceOfCompose2() {
132132
Flowable<Movie> movie = Flowable.<Movie> just(new HorrorMovie());
133133
Flowable<HorrorMovie> movie2 = movie.compose(new FlowableTransformer<Movie, HorrorMovie>() {
134134
@Override
135-
public Publisher<? extends HorrorMovie> apply(Flowable<Movie> t) {
135+
public Publisher<HorrorMovie> apply(Flowable<Movie> t) {
136136
return Flowable.just(new HorrorMovie());
137137
}
138138
});
@@ -144,7 +144,7 @@ public void testCovarianceOfCompose3() {
144144
Flowable<Movie> movie = Flowable.<Movie>just(new HorrorMovie());
145145
Flowable<HorrorMovie> movie2 = movie.compose(new FlowableTransformer<Movie, HorrorMovie>() {
146146
@Override
147-
public Publisher<? extends HorrorMovie> apply(Flowable<Movie> t) {
147+
public Publisher<HorrorMovie> apply(Flowable<Movie> t) {
148148
return Flowable.just(new HorrorMovie()).map(new Function<HorrorMovie, HorrorMovie>() {
149149
@Override
150150
public HorrorMovie apply(HorrorMovie v) {
@@ -162,7 +162,7 @@ public void testCovarianceOfCompose4() {
162162
Flowable<HorrorMovie> movie = Flowable.just(new HorrorMovie());
163163
Flowable<HorrorMovie> movie2 = movie.compose(new FlowableTransformer<HorrorMovie, HorrorMovie>() {
164164
@Override
165-
public Publisher<? extends HorrorMovie> apply(Flowable<HorrorMovie> t1) {
165+
public Publisher<HorrorMovie> apply(Flowable<HorrorMovie> t1) {
166166
return t1.map(new Function<HorrorMovie, HorrorMovie>() {
167167
@Override
168168
public HorrorMovie apply(HorrorMovie v) {
@@ -211,7 +211,7 @@ public Flowable<Movie> apply(List<List<Movie>> listOfLists) {
211211

212212
static FlowableTransformer<List<Movie>, Movie> deltaTransformer = new FlowableTransformer<List<Movie>, Movie>() {
213213
@Override
214-
public Publisher<? extends Movie> apply(Flowable<List<Movie>> movieList) {
214+
public Publisher<Movie> apply(Flowable<List<Movie>> movieList) {
215215
return movieList
216216
.startWith(new ArrayList<Movie>())
217217
.buffer(2, 1)

0 commit comments

Comments
 (0)