|
| 1 | +/** |
| 2 | + * Copyright 2013 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package rx.operators; |
| 17 | + |
| 18 | +import static org.mockito.Matchers.isA; |
| 19 | +import static org.mockito.Mockito.inOrder; |
| 20 | +import static org.mockito.Mockito.mock; |
| 21 | +import static org.mockito.Mockito.times; |
| 22 | + |
| 23 | +import org.junit.Test; |
| 24 | +import org.mockito.InOrder; |
| 25 | + |
| 26 | +import rx.Observable; |
| 27 | +import rx.Observer; |
| 28 | +import rx.util.functions.Func2; |
| 29 | + |
| 30 | +public class OperationSequenceEqualTests { |
| 31 | + |
| 32 | + @Test |
| 33 | + public void test1() { |
| 34 | + Observable<Boolean> observable = Observable.sequenceEqual( |
| 35 | + Observable.from("one", "two", "three"), |
| 36 | + Observable.from("one", "two", "three")); |
| 37 | + verifyResult(observable, true); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void test2() { |
| 42 | + Observable<Boolean> observable = Observable.sequenceEqual( |
| 43 | + Observable.from("one", "two", "three"), |
| 44 | + Observable.from("one", "two", "three", "four")); |
| 45 | + verifyResult(observable, false); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void test3() { |
| 50 | + Observable<Boolean> observable = Observable.sequenceEqual( |
| 51 | + Observable.from("one", "two", "three", "four"), |
| 52 | + Observable.from("one", "two", "three")); |
| 53 | + verifyResult(observable, false); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testWithError1() { |
| 58 | + Observable<Boolean> observable = Observable.sequenceEqual( |
| 59 | + Observable.concat(Observable.from("one"), |
| 60 | + Observable.<String> error(new TestException())), |
| 61 | + Observable.from("one", "two", "three")); |
| 62 | + verifyError(observable); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testWithError2() { |
| 67 | + Observable<Boolean> observable = Observable.sequenceEqual( |
| 68 | + Observable.from("one", "two", "three"), |
| 69 | + Observable.concat(Observable.from("one"), |
| 70 | + Observable.<String> error(new TestException()))); |
| 71 | + verifyError(observable); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testWithError3() { |
| 76 | + Observable<Boolean> observable = Observable.sequenceEqual( |
| 77 | + Observable.concat(Observable.from("one"), |
| 78 | + Observable.<String> error(new TestException())), |
| 79 | + Observable.concat(Observable.from("one"), |
| 80 | + Observable.<String> error(new TestException()))); |
| 81 | + verifyError(observable); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testWithEmpty1() { |
| 86 | + Observable<Boolean> observable = Observable.sequenceEqual( |
| 87 | + Observable.<String> empty(), |
| 88 | + Observable.from("one", "two", "three")); |
| 89 | + verifyResult(observable, false); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testWithEmpty2() { |
| 94 | + Observable<Boolean> observable = Observable.sequenceEqual( |
| 95 | + Observable.from("one", "two", "three"), |
| 96 | + Observable.<String> empty()); |
| 97 | + verifyResult(observable, false); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testWithEmpty3() { |
| 102 | + Observable<Boolean> observable = Observable.sequenceEqual( |
| 103 | + Observable.<String> empty(), Observable.<String> empty()); |
| 104 | + verifyResult(observable, true); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void testWithNull1() { |
| 109 | + Observable<Boolean> observable = Observable.sequenceEqual( |
| 110 | + Observable.from((String) null), Observable.from("one")); |
| 111 | + verifyResult(observable, false); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testWithNull2() { |
| 116 | + Observable<Boolean> observable = Observable.sequenceEqual( |
| 117 | + Observable.from((String) null), Observable.from((String) null)); |
| 118 | + verifyResult(observable, true); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testWithEqualityError() { |
| 123 | + Observable<Boolean> observable = Observable.sequenceEqual( |
| 124 | + Observable.from("one"), Observable.from("one"), |
| 125 | + new Func2<String, String, Boolean>() { |
| 126 | + @Override |
| 127 | + public Boolean call(String t1, String t2) { |
| 128 | + throw new TestException(); |
| 129 | + } |
| 130 | + }); |
| 131 | + verifyError(observable); |
| 132 | + } |
| 133 | + |
| 134 | + private void verifyResult(Observable<Boolean> observable, boolean result) { |
| 135 | + @SuppressWarnings("unchecked") |
| 136 | + Observer<Boolean> observer = mock(Observer.class); |
| 137 | + observable.subscribe(observer); |
| 138 | + |
| 139 | + InOrder inOrder = inOrder(observer); |
| 140 | + inOrder.verify(observer, times(1)).onNext(result); |
| 141 | + inOrder.verify(observer).onCompleted(); |
| 142 | + inOrder.verifyNoMoreInteractions(); |
| 143 | + } |
| 144 | + |
| 145 | + private void verifyError(Observable<Boolean> observable) { |
| 146 | + @SuppressWarnings("unchecked") |
| 147 | + Observer<Boolean> observer = mock(Observer.class); |
| 148 | + observable.subscribe(observer); |
| 149 | + |
| 150 | + InOrder inOrder = inOrder(observer); |
| 151 | + inOrder.verify(observer, times(1)).onError(isA(TestException.class)); |
| 152 | + inOrder.verifyNoMoreInteractions(); |
| 153 | + } |
| 154 | + |
| 155 | + private class TestException extends RuntimeException { |
| 156 | + private static final long serialVersionUID = 1L; |
| 157 | + } |
| 158 | +} |
0 commit comments