1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| public class LambdaUtilTest {
@Test public void getMethodNameTest() { final SerFunction<MyTeacher, String> lambda = MyTeacher::getAge; final String methodName = LambdaUtil.getMethodName(lambda); Assertions.assertEquals("getAge", methodName); }
@Test public void getFieldNameTest() { final SerFunction<MyTeacher, String> lambda = MyTeacher::getAge; final String fieldName = LambdaUtil.getFieldName(lambda); Assertions.assertEquals("age", fieldName); }
@Test public void resolveTest() { Stream.<Runnable>of(() -> { final SerSupplier<MyTeacher> lambda = MyTeacher::new; final LambdaInfo lambdaInfo = LambdaUtil.resolve(lambda); Assertions.assertEquals(0, lambdaInfo.getParameterTypes().length); Assertions.assertEquals(MyTeacher.class, lambdaInfo.getReturnType()); }, () -> { final SerFunction<Integer, MyTeacher[]> lambda = MyTeacher[]::new; final LambdaInfo lambdaInfo = LambdaUtil.resolve(lambda); Assertions.assertEquals(int.class, lambdaInfo.getParameterTypes()[0]); Assertions.assertEquals(MyTeacher[].class, lambdaInfo.getReturnType()); }).forEach(Runnable::run); }
@Test public void getRealClassTest() { final MyTeacher myTeacher = new MyTeacher(); final SerFunction<MyTeacher, String> lambda = MyTeacher::getAge; Assertions.assertEquals(MyTeacher.class, LambdaUtil.getRealClass(lambda)); }
@Test public void getterTest() { final Bean bean = new Bean(); bean.setId(2L);
final Function<Bean, Long> getId = LambdaUtil.buildGetter(MethodUtil.getMethod(Bean.class, "getId")); final Function<Bean, Long> getId2 = LambdaUtil.buildGetter(Bean.class, Bean.Fields.id);
Assertions.assertEquals(getId, getId2); Assertions.assertEquals(bean.getId(), getId.apply(bean)); }
@Test public void setterTest() { final Bean bean = new Bean(); bean.setId(2L); bean.setFlag(false);
final BiConsumer<Bean, Long> setId = LambdaUtil.buildSetter(MethodUtil.getMethod(Bean.class, "setId", Long.class)); final BiConsumer<Bean, Long> setId2 = LambdaUtil.buildSetter(Bean.class, Bean.Fields.id); Assertions.assertEquals(setId, setId2);
setId.accept(bean, 3L); Assertions.assertEquals(3L, (long) bean.getId()); }
@Test @EnabledForJreRange(max = JRE.JAVA_8) void buildSetterWithPrimitiveTest() { final Bean bean = new Bean(); bean.setId(2L); bean.setFlag(false);
final BiConsumer<Bean, Object> setter = LambdaUtil.buildSetter(Bean.class, "flag"); setter.accept(bean, Boolean.TRUE); Assertions.assertTrue(bean.isFlag()); }
@Test void getInvokeMethodTest() { Method invokeMethod = LambdaUtil.getInvokeMethod(Predicate.class); Assertions.assertEquals("test", invokeMethod.getName());
invokeMethod = LambdaUtil.getInvokeMethod(Consumer.class); Assertions.assertEquals("accept", invokeMethod.getName());
invokeMethod = LambdaUtil.getInvokeMethod(Runnable.class); Assertions.assertEquals("run", invokeMethod.getName());
invokeMethod = LambdaUtil.getInvokeMethod(SerFunction.class); Assertions.assertEquals("applying", invokeMethod.getName());
invokeMethod = LambdaUtil.getInvokeMethod(Function.class); Assertions.assertEquals("apply", invokeMethod.getName()); }
@Test void getInvokeMethodErrorTest() { Assertions.assertThrows(IllegalArgumentException.class, () -> { LambdaUtil.getInvokeMethod(LambdaUtilTest.class); }); } }
|