Debian Codegraph @

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