| 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.internal.functions; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.lang.reflect.Method; |
| 19 |
import java.util.List; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.RxJavaTest; |
| 24 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 25 |
import io.reactivex.rxjava3.functions.*; |
| 26 |
import io.reactivex.rxjava3.internal.functions.Functions.*; |
| 27 |
import io.reactivex.rxjava3.internal.util.ExceptionHelper; |
| 28 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 29 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 30 |
|
| 31 |
public class FunctionsTest extends RxJavaTest { |
| 32 |
@Test |
| 33 |
public void utilityClass() { |
| 34 |
TestHelper.checkUtilityClass(Functions.class); |
| 35 |
} |
| 36 |
|
| 37 |
@SuppressWarnings("unchecked") |
| 38 |
@Test |
| 39 |
public void hashSetCallableEnum() { |
| 40 |
// inlined TestHelper.checkEnum due to access restrictions |
| 41 |
try { |
| 42 |
Method m = Functions.HashSetSupplier.class.getMethod("values"); |
| 43 |
m.setAccessible(true); |
| 44 |
Method e = Functions.HashSetSupplier.class.getMethod("valueOf", String.class); |
| 45 |
e.setAccessible(true); |
| 46 |
|
| 47 |
for (Enum<HashSetSupplier> o : (Enum<HashSetSupplier>[])m.invoke(null)) { |
| 48 |
assertSame(o, e.invoke(null, o.name())); |
| 49 |
} |
| 50 |
|
| 51 |
} catch (Throwable ex) { |
| 52 |
throw ExceptionHelper.wrapOrThrow(ex); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
@SuppressWarnings("unchecked") |
| 57 |
@Test |
| 58 |
public void naturalComparatorEnum() { |
| 59 |
// inlined TestHelper.checkEnum due to access restrictions |
| 60 |
try { |
| 61 |
Method m = Functions.NaturalComparator.class.getMethod("values"); |
| 62 |
m.setAccessible(true); |
| 63 |
Method e = Functions.NaturalComparator.class.getMethod("valueOf", String.class); |
| 64 |
e.setAccessible(true); |
| 65 |
|
| 66 |
for (Enum<NaturalComparator> o : (Enum<NaturalComparator>[])m.invoke(null)) { |
| 67 |
assertSame(o, e.invoke(null, o.name())); |
| 68 |
} |
| 69 |
|
| 70 |
} catch (Throwable ex) { |
| 71 |
throw ExceptionHelper.wrapOrThrow(ex); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
@Test |
| 76 |
public void booleanSupplierPredicateReverse() throws Throwable { |
| 77 |
BooleanSupplier s = new BooleanSupplier() { |
| 78 |
@Override |
| 79 |
public boolean getAsBoolean() throws Exception { |
| 80 |
return false; |
| 81 |
} |
| 82 |
}; |
| 83 |
|
| 84 |
assertTrue(Functions.predicateReverseFor(s).test(1)); |
| 85 |
|
| 86 |
s = new BooleanSupplier() { |
| 87 |
@Override |
| 88 |
public boolean getAsBoolean() throws Exception { |
| 89 |
return true; |
| 90 |
} |
| 91 |
}; |
| 92 |
|
| 93 |
assertFalse(Functions.predicateReverseFor(s).test(1)); |
| 94 |
} |
| 95 |
|
| 96 |
@Test(expected = IllegalArgumentException.class) |
| 97 |
public void toFunction2() throws Throwable { |
| 98 |
Functions.toFunction(new BiFunction<Integer, Integer, Integer>() { |
| 99 |
@Override |
| 100 |
public Integer apply(Integer t1, Integer t2) throws Exception { |
| 101 |
return null; |
| 102 |
} |
| 103 |
}).apply(new Object[20]); |
| 104 |
} |
| 105 |
|
| 106 |
@Test(expected = IllegalArgumentException.class) |
| 107 |
public void toFunction3() throws Throwable { |
| 108 |
Functions.toFunction(new Function3<Integer, Integer, Integer, Integer>() { |
| 109 |
@Override |
| 110 |
public Integer apply(Integer t1, Integer t2, Integer t3) throws Exception { |
| 111 |
return null; |
| 112 |
} |
| 113 |
}).apply(new Object[20]); |
| 114 |
} |
| 115 |
|
| 116 |
@Test(expected = IllegalArgumentException.class) |
| 117 |
public void toFunction4() throws Throwable { |
| 118 |
Functions.toFunction(new Function4<Integer, Integer, Integer, Integer, Integer>() { |
| 119 |
@Override |
| 120 |
public Integer apply(Integer t1, Integer t2, Integer t3, Integer t4) throws Exception { |
| 121 |
return null; |
| 122 |
} |
| 123 |
}).apply(new Object[20]); |
| 124 |
} |
| 125 |
|
| 126 |
@Test(expected = IllegalArgumentException.class) |
| 127 |
public void toFunction5() throws Throwable { |
| 128 |
Functions.toFunction(new Function5<Integer, Integer, Integer, Integer, Integer, Integer>() { |
| 129 |
@Override |
| 130 |
public Integer apply(Integer t1, Integer t2, Integer t3, Integer t4, Integer t5) throws Exception { |
| 131 |
return null; |
| 132 |
} |
| 133 |
}).apply(new Object[20]); |
| 134 |
} |
| 135 |
|
| 136 |
@Test(expected = IllegalArgumentException.class) |
| 137 |
public void toFunction6() throws Throwable { |
| 138 |
Functions.toFunction(new Function6<Integer, Integer, Integer, Integer, Integer, Integer, Integer>() { |
| 139 |
@Override |
| 140 |
public Integer apply(Integer t1, Integer t2, Integer t3, Integer t4, Integer t5, Integer t6) throws Exception { |
| 141 |
return null; |
| 142 |
} |
| 143 |
}).apply(new Object[20]); |
| 144 |
} |
| 145 |
|
| 146 |
@Test(expected = IllegalArgumentException.class) |
| 147 |
public void toFunction7() throws Throwable { |
| 148 |
Functions.toFunction(new Function7<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer>() { |
| 149 |
@Override |
| 150 |
public Integer apply(Integer t1, Integer t2, Integer t3, Integer t4, Integer t5, Integer t6, Integer t7) throws Exception { |
| 151 |
return null; |
| 152 |
} |
| 153 |
}).apply(new Object[20]); |
| 154 |
} |
| 155 |
|
| 156 |
@Test(expected = IllegalArgumentException.class) |
| 157 |
public void toFunction8() throws Throwable { |
| 158 |
Functions.toFunction(new Function8<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer>() { |
| 159 |
@Override |
| 160 |
public Integer apply(Integer t1, Integer t2, Integer t3, Integer t4, Integer t5, Integer t6, Integer t7, Integer t8) throws Exception { |
| 161 |
return null; |
| 162 |
} |
| 163 |
}).apply(new Object[20]); |
| 164 |
} |
| 165 |
|
| 166 |
@Test(expected = IllegalArgumentException.class) |
| 167 |
public void toFunction9() throws Throwable { |
| 168 |
Functions.toFunction(new Function9<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer>() { |
| 169 |
@Override |
| 170 |
public Integer apply(Integer t1, Integer t2, Integer t3, Integer t4, Integer t5, Integer t6, Integer t7, Integer t8, Integer t9) throws Exception { |
| 171 |
return null; |
| 172 |
} |
| 173 |
}).apply(new Object[20]); |
| 174 |
} |
| 175 |
|
| 176 |
@Test |
| 177 |
public void identityFunctionToString() { |
| 178 |
assertEquals("IdentityFunction", Functions.identity().toString()); |
| 179 |
} |
| 180 |
|
| 181 |
@Test |
| 182 |
public void emptyActionToString() { |
| 183 |
assertEquals("EmptyAction", Functions.EMPTY_ACTION.toString()); |
| 184 |
} |
| 185 |
|
| 186 |
@Test |
| 187 |
public void emptyRunnableToString() { |
| 188 |
assertEquals("EmptyRunnable", Functions.EMPTY_RUNNABLE.toString()); |
| 189 |
} |
| 190 |
|
| 191 |
@Test |
| 192 |
public void emptyConsumerToString() { |
| 193 |
assertEquals("EmptyConsumer", Functions.EMPTY_CONSUMER.toString()); |
| 194 |
} |
| 195 |
|
| 196 |
@Test |
| 197 |
public void errorConsumerEmpty() throws Throwable { |
| 198 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 199 |
try { |
| 200 |
Functions.ERROR_CONSUMER.accept(new TestException()); |
| 201 |
|
| 202 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 203 |
assertEquals(errors.toString(), 1, errors.size()); |
| 204 |
} finally { |
| 205 |
RxJavaPlugins.reset(); |
| 206 |
} |
| 207 |
} |
| 208 |
} |