Debian Codegraph @

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

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
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.rxjava3.flowable;
15
16 import static org.junit.Assert.assertEquals;
17
18 import java.util.*;
19
20 import org.junit.Test;
21 import org.reactivestreams.Publisher;
22
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.flowables.GroupedFlowable;
25 import io.reactivex.rxjava3.functions.*;
26 import io.reactivex.rxjava3.testsupport.TestSubscriberEx;
27
28 /**
29 * Test super/extends of generics.
30 *
31 * See https://github.com/Netflix/RxJava/pull/331
32 */
33 public class FlowableCovarianceTest extends RxJavaTest {
34
35 /**
36 * This won't compile if super/extends isn't done correctly on generics.
37 */
38 @Test
39 public void covarianceOfFrom() {
40 Flowable.<Movie> just(new HorrorMovie());
41 Flowable.<Movie> fromIterable(new ArrayList<HorrorMovie>());
42 // Observable.<HorrorMovie>from(new Movie()); // may not compile
43 }
44
45 @Test
46 public void sortedList() {
47 Comparator<Media> sortFunction = new Comparator<Media>() {
48 @Override
49 public int compare(Media t1, Media t2) {
50 return 1;
51 }
52 };
53
54 // this one would work without the covariance generics
55 Flowable<Media> f = Flowable.just(new Movie(), new TVSeason(), new Album());
56 f.toSortedList(sortFunction);
57
58 // this one would NOT work without the covariance generics
59 Flowable<Movie> f2 = Flowable.just(new Movie(), new ActionMovie(), new HorrorMovie());
60 f2.toSortedList(sortFunction);
61 }
62
63 @Test
64 public void groupByCompose() {
65 Flowable<Movie> movies = Flowable.just(new HorrorMovie(), new ActionMovie(), new Movie());
66 TestSubscriberEx<String> ts = new TestSubscriberEx<>();
67
68 movies
69 .groupBy(new Function<Movie, Object>() {
70 @Override
71 public Object apply(Movie v) {
72 return v.getClass();
73 }
74 })
75 .doOnNext(new Consumer<GroupedFlowable<Object, Movie>>() {
76 @Override
77 public void accept(GroupedFlowable<Object, Movie> g) {
78 System.out.println(g.getKey());
79 }
80 })
81 .flatMap(new Function<GroupedFlowable<Object, Movie>, Publisher<String>>() {
82 @Override
83 public Publisher<String> apply(GroupedFlowable<Object, Movie> g) {
84 return g
85 .doOnNext(new Consumer<Movie>() {
86 @Override
87 public void accept(Movie v) {
88 System.out.println(v);
89 }
90 })
91 .compose(new FlowableTransformer<Movie, Movie>() {
92 @Override
93 public Publisher<Movie> apply(Flowable<Movie> m) {
94 return m.concatWith(Flowable.just(new ActionMovie()));
95 }
96 }
97 )
98 .map(new Function<Object, String>() {
99 @Override
100 public String apply(Object v) {
101 return v.toString();
102 }
103 });
104 }
105 })
106 .subscribe(ts);
107 ts.assertTerminated();
108 ts.assertNoErrors();
109 // System.out.println(ts.getOnNextEvents());
110 assertEquals(6, ts.values().size());
111 }
112
113 @SuppressWarnings("unused")
114 @Test
115 public void covarianceOfCompose() {
116 Flowable<HorrorMovie> movie = Flowable.just(new HorrorMovie());
117 Flowable<Movie> movie2 = movie.compose(new FlowableTransformer<HorrorMovie, Movie>() {
118 @Override
119 public Publisher<Movie> apply(Flowable<HorrorMovie> t) {
120 return Flowable.just(new Movie());
121 }
122 });
123 }
124
125 @SuppressWarnings("unused")
126 @Test
127 public void covarianceOfCompose2() {
128 Flowable<Movie> movie = Flowable.<Movie> just(new HorrorMovie());
129 Flowable<HorrorMovie> movie2 = movie.compose(new FlowableTransformer<Movie, HorrorMovie>() {
130 @Override
131 public Publisher<HorrorMovie> apply(Flowable<Movie> t) {
132 return Flowable.just(new HorrorMovie());
133 }
134 });
135 }
136
137 @SuppressWarnings("unused")
138 @Test
139 public void covarianceOfCompose3() {
140 Flowable<Movie> movie = Flowable.<Movie>just(new HorrorMovie());
141 Flowable<HorrorMovie> movie2 = movie.compose(new FlowableTransformer<Movie, HorrorMovie>() {
142 @Override
143 public Publisher<HorrorMovie> apply(Flowable<Movie> t) {
144 return Flowable.just(new HorrorMovie()).map(new Function<HorrorMovie, HorrorMovie>() {
145 @Override
146 public HorrorMovie apply(HorrorMovie v) {
147 return v;
148 }
149 });
150 }
151 }
152 );
153 }
154
155 @SuppressWarnings("unused")
156 @Test
157 public void covarianceOfCompose4() {
158 Flowable<HorrorMovie> movie = Flowable.just(new HorrorMovie());
159 Flowable<HorrorMovie> movie2 = movie.compose(new FlowableTransformer<HorrorMovie, HorrorMovie>() {
160 @Override
161 public Publisher<HorrorMovie> apply(Flowable<HorrorMovie> t1) {
162 return t1.map(new Function<HorrorMovie, HorrorMovie>() {
163 @Override
164 public HorrorMovie apply(HorrorMovie v) {
165 return v;
166 }
167 });
168 }
169 });
170 }
171
172 @Test
173 public void composeWithDeltaLogic() {
174 List<Movie> list1 = Arrays.asList(new Movie(), new HorrorMovie(), new ActionMovie());
175 List<Movie> list2 = Arrays.asList(new ActionMovie(), new Movie(), new HorrorMovie(), new ActionMovie());
176 Flowable<List<Movie>> movies = Flowable.just(list1, list2);
177 movies.compose(deltaTransformer);
178 }
179
180 static Function<List<List<Movie>>, Flowable<Movie>> calculateDelta = new Function<List<List<Movie>>, Flowable<Movie>>() {
181 @Override
182 public Flowable<Movie> apply(List<List<Movie>> listOfLists) {
183 if (listOfLists.size() == 1) {
184 return Flowable.fromIterable(listOfLists.get(0));
185 } else {
186 // diff the two
187 List<Movie> newList = listOfLists.get(1);
188 List<Movie> oldList = new ArrayList<>(listOfLists.get(0));
189
190 Set<Movie> delta = new LinkedHashSet<>();
191 delta.addAll(newList);
192 // remove all that match in old
193 delta.removeAll(oldList);
194
195 // filter oldList to those that aren't in the newList
196 oldList.removeAll(newList);
197
198 // for all left in the oldList we'll create DROP events
199 for (@SuppressWarnings("unused") Movie old : oldList) {
200 delta.add(new Movie());
201 }
202
203 return Flowable.fromIterable(delta);
204 }
205 }
206 };
207
208 static FlowableTransformer<List<Movie>, Movie> deltaTransformer = new FlowableTransformer<List<Movie>, Movie>() {
209 @Override
210 public Publisher<Movie> apply(Flowable<List<Movie>> movieList) {
211 return movieList
212 .startWithItem(new ArrayList<>())
213 .buffer(2, 1)
214 .skip(1)
215 .flatMap(calculateDelta);
216 }
217 };
218
219 /*
220 * Most tests are moved into their applicable classes such as [Operator]Tests.java
221 */
222
223 static class Media {
224 }
225
226 static class Movie extends Media {
227 }
228
229 static class HorrorMovie extends Movie {
230 }
231
232 static class ActionMovie extends Movie {
233 }
234
235 static class Album extends Media {
236 }
237
238 static class TVSeason extends Media {
239 }
240
241 static class Rating {
242 }
243
244 static class CoolRating extends Rating {
245 }
246
247 static class Result {
248 }
249
250 static class ExtendedResult extends Result {
251 }
252 }