|
| 1 | +package rx; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import rx.util.functions.Action1; |
| 6 | +import rx.util.functions.Func2; |
| 7 | + |
| 8 | +/** |
| 9 | + * Test super/extends of generics. |
| 10 | + * |
| 11 | + * See https://github.com/Netflix/RxJava/pull/331 |
| 12 | + */ |
| 13 | +public class CovarianceTest { |
| 14 | + |
| 15 | + /** |
| 16 | + * This won't compile if super/extends isn't done correctly on generics |
| 17 | + */ |
| 18 | + @Test |
| 19 | + public void testCovarianceOfZip() { |
| 20 | + Observable<HorrorMovie> horrors = Observable.from(new HorrorMovie()); |
| 21 | + Observable<CoolRating> ratings = Observable.from(new CoolRating()); |
| 22 | + |
| 23 | + Func2<Media, Rating, ExtendedResult> combine = new Func2<Media, Rating, ExtendedResult>() { |
| 24 | + @Override |
| 25 | + public ExtendedResult call(Media m, Rating r) { |
| 26 | + return new ExtendedResult(); |
| 27 | + } |
| 28 | + }; |
| 29 | + |
| 30 | + Observable.<Result, Movie, CoolRating> zip(horrors, ratings, combine).toBlockingObservable().forEach(new Action1<Result>() { |
| 31 | + |
| 32 | + @Override |
| 33 | + public void call(Result t1) { |
| 34 | + System.out.println("Result: " + t1); |
| 35 | + } |
| 36 | + |
| 37 | + }); |
| 38 | + |
| 39 | + Observable.<Result, Movie, CoolRating> zip(horrors, ratings, combine).toBlockingObservable().forEach(new Action1<Result>() { |
| 40 | + |
| 41 | + @Override |
| 42 | + public void call(Result t1) { |
| 43 | + System.out.println("Result: " + t1); |
| 44 | + } |
| 45 | + |
| 46 | + }); |
| 47 | + |
| 48 | + Observable.<ExtendedResult, Media, Rating> zip(horrors, ratings, combine).toBlockingObservable().forEach(new Action1<ExtendedResult>() { |
| 49 | + |
| 50 | + @Override |
| 51 | + public void call(ExtendedResult t1) { |
| 52 | + System.out.println("Result: " + t1); |
| 53 | + } |
| 54 | + |
| 55 | + }); |
| 56 | + |
| 57 | + Observable.<Result, Media, Rating> zip(horrors, ratings, combine).toBlockingObservable().forEach(new Action1<Result>() { |
| 58 | + |
| 59 | + @Override |
| 60 | + public void call(Result t1) { |
| 61 | + System.out.println("Result: " + t1); |
| 62 | + } |
| 63 | + |
| 64 | + }); |
| 65 | + |
| 66 | + Observable.<ExtendedResult, Media, Rating> zip(horrors, ratings, combine).toBlockingObservable().forEach(new Action1<Result>() { |
| 67 | + |
| 68 | + @Override |
| 69 | + public void call(Result t1) { |
| 70 | + System.out.println("Result: " + t1); |
| 71 | + } |
| 72 | + |
| 73 | + }); |
| 74 | + |
| 75 | + Observable.<Result, Movie, CoolRating> zip(horrors, ratings, combine); |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + static class Media { |
| 80 | + } |
| 81 | + |
| 82 | + static class Movie extends Media { |
| 83 | + } |
| 84 | + |
| 85 | + static class HorrorMovie extends Movie { |
| 86 | + } |
| 87 | + |
| 88 | + static class Rating { |
| 89 | + } |
| 90 | + |
| 91 | + static class CoolRating extends Rating { |
| 92 | + } |
| 93 | + |
| 94 | + static class Result { |
| 95 | + } |
| 96 | + |
| 97 | + static class ExtendedResult extends Result { |
| 98 | + } |
| 99 | +} |
0 commit comments