mirror of
https://github.com/alibaba/testable-mock.git
synced 2026-08-19 09:43:29 +08:00
refine code
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package com.alibaba.demo.lambda;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* @author jim
|
||||
*/
|
||||
public interface BaseDemo {
|
||||
default void blackHole(Object... ignore) {}
|
||||
|
||||
default void consumesRun(Runnable r) {
|
||||
r.run();
|
||||
}
|
||||
|
||||
default void consumesFunction0(Runnable f) {
|
||||
f.run();
|
||||
}
|
||||
|
||||
default <T> void consumesFunction1(Consumer<T> f) {
|
||||
f.accept(null);
|
||||
}
|
||||
|
||||
default <T, R> void consumesFunction2(Function<T, R> f) {
|
||||
f.apply(null);
|
||||
}
|
||||
|
||||
default <T1, T2, R> void consumesFunction3(BiFunction<T1, T2, R> f) {
|
||||
f.apply(null, null);
|
||||
}
|
||||
|
||||
default <T> void consumesSupplier(Supplier<T> supplier) {
|
||||
supplier.get();
|
||||
}
|
||||
|
||||
default <T, R> void consumesTwoFunction2(Function<T, R> f1, Function<T, R> f2) {
|
||||
f1.apply(null);
|
||||
f2.apply(null);
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package com.alibaba.demo.lambda;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* @author jim
|
||||
*/
|
||||
public class Fzo {
|
||||
|
||||
public void objectStaticMethodReference() {
|
||||
Boolean aBoolean = zz().get();
|
||||
System.out.println(aBoolean);
|
||||
}
|
||||
|
||||
public Optional<Boolean> zz() {
|
||||
List<List<Boolean>> zz = new ArrayList<>();
|
||||
List<Boolean> f = new ArrayList<>();
|
||||
f.add(false);
|
||||
f.add(false);
|
||||
f.add(false);
|
||||
zz.add(f);
|
||||
return zz.stream()
|
||||
.flatMap(Collection::stream)
|
||||
.reduce(Boolean::logicalAnd);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package com.alibaba.demo.lambda;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author jim
|
||||
*/
|
||||
public class InvokeInterfaceDemo implements BaseDemo {
|
||||
|
||||
public void collectionInterfaceDefaultOrStatic() {
|
||||
blackHole(collectionStreamTest());
|
||||
blackHole(arraysStreamTest());
|
||||
}
|
||||
|
||||
public void interfaceDefault() {
|
||||
ILambda l = new LambdaFoo();
|
||||
consumesRun(l::run);
|
||||
consumesFunction1(l::function1);
|
||||
}
|
||||
|
||||
public void interfaceStatic() {
|
||||
consumesRun(ILambda::staticRun);
|
||||
consumesFunction1(ILambda::staticFunction1);
|
||||
}
|
||||
|
||||
public Object collectionStreamTest() {
|
||||
List<List<String>> testList = new ArrayList<>();
|
||||
List<String> fooList = new ArrayList<>();
|
||||
testList.add(fooList);
|
||||
return testList.stream()
|
||||
//.flatMap(v -> v.stream())
|
||||
.flatMap(Collection::stream)
|
||||
.map(Double::valueOf)
|
||||
.map(BigDecimal::new)
|
||||
.reduce(BigDecimal::add);
|
||||
}
|
||||
|
||||
public Object arraysStreamTest() {
|
||||
List<String[]> zz = new ArrayList<>();
|
||||
zz.add(new String[]{"1", "2", "3"});
|
||||
return zz.stream()
|
||||
.flatMap(Arrays::stream)
|
||||
.map(Double::valueOf)
|
||||
.map(BigDecimal::new)
|
||||
.reduce(BigDecimal::add);
|
||||
}
|
||||
|
||||
public void objectStaticMethodReference() {
|
||||
List<Boolean> f = new ArrayList<>();
|
||||
f.add(false);
|
||||
f.add(false);
|
||||
f.add(false);
|
||||
blackHole(f.stream()
|
||||
.reduce(Boolean::logicalAnd)
|
||||
.get()
|
||||
);
|
||||
}
|
||||
|
||||
public interface ILambda {
|
||||
default void run() {
|
||||
|
||||
}
|
||||
|
||||
default void function1(String s) {
|
||||
|
||||
}
|
||||
|
||||
static void staticRun() {
|
||||
|
||||
}
|
||||
|
||||
static void staticFunction1(String s) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static class LambdaFoo implements ILambda {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ import java.util.function.Supplier;
|
||||
* @author jimca
|
||||
*/
|
||||
@SuppressWarnings({"WrapperTypeMayBePrimitive", "ResultOfMethodCallIgnored", "MismatchedReadAndWriteOfArray", "unused"})
|
||||
public class ExternalLambdaDemo {
|
||||
public class InvokeVirtualDemo {
|
||||
|
||||
public void string1() {
|
||||
String s = "";
|
||||
@@ -197,8 +197,8 @@ public class ExternalLambdaDemo {
|
||||
}
|
||||
|
||||
public void function3() {
|
||||
ExternalLambdaDemo externalLambdaDemo = new ExternalLambdaDemo();
|
||||
consumesFunction3(externalLambdaDemo::f3);
|
||||
InvokeVirtualDemo invokeVirtualDemo = new InvokeVirtualDemo();
|
||||
consumesFunction3(invokeVirtualDemo::f3);
|
||||
}
|
||||
|
||||
public Boolean f3(String s1, Long l) {
|
||||
@@ -10,50 +10,15 @@ import java.util.stream.Collectors;
|
||||
/**
|
||||
* @author jim
|
||||
*/
|
||||
public class StaticInstanceReference {
|
||||
public class StaticInstanceReference implements BaseDemo {
|
||||
|
||||
private static final StaticClassA STATIC_CLASS_A = new StaticClassA();
|
||||
|
||||
public void staticMethodReference() {
|
||||
//StaticClassA a = new StaticClassA();
|
||||
//consumesRun(a::doIt);
|
||||
consumesRun(STATIC_CLASS_A::doIt);
|
||||
consumesFunction1(STATIC_CLASS_A::function1);
|
||||
consumesFunction2(STATIC_CLASS_A::function2);
|
||||
consumesFunction3(STATIC_CLASS_A::function3);
|
||||
|
||||
}
|
||||
|
||||
public void collectionInterfaceDefaultOrStatic() {
|
||||
blackHole(invokeInterfaceTest());
|
||||
blackHole(interfaceStaticMethodTest());
|
||||
}
|
||||
|
||||
public void interfaceDefault() {
|
||||
ILambda l = new LambdaFoo();
|
||||
consumesRun(l::run);
|
||||
consumesFunction1(l::function1);
|
||||
}
|
||||
|
||||
public void interfaceStatic() {
|
||||
consumesRun(ILambda::staticRun);
|
||||
consumesFunction1(ILambda::staticFunction1);
|
||||
}
|
||||
|
||||
private void consumesRun(Runnable r) {
|
||||
r.run();
|
||||
}
|
||||
|
||||
private <T> void consumesFunction1(Consumer<T> r) {
|
||||
r.accept(null);
|
||||
}
|
||||
|
||||
private <T, R> void consumesFunction2(Function<T, R> r) {
|
||||
r.apply(null);
|
||||
}
|
||||
|
||||
private <T1, T2, R> void consumesFunction3(BiFunction<T1, T2, R> r) {
|
||||
r.apply(null, null);
|
||||
}
|
||||
|
||||
public static class StaticClassA {
|
||||
@@ -72,77 +37,4 @@ public class StaticInstanceReference {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
public static class XBean {
|
||||
private Long id;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
|
||||
public void foo() {
|
||||
List<XBean> testList = Collections.emptyList();
|
||||
//noinspection RedundantOperationOnEmptyContainer
|
||||
List<Long> response = testList.stream().map(XBean::getId).distinct().collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public Object invokeInterfaceTest() {
|
||||
List<List<String>> zz = new ArrayList<>();
|
||||
zz.add(new ArrayList<>());
|
||||
return zz.stream()
|
||||
//.flatMap(v -> v.stream())
|
||||
.flatMap(Collection::stream)
|
||||
.map(Double::valueOf)
|
||||
.map(BigDecimal::new)
|
||||
.reduce(BigDecimal::add);
|
||||
}
|
||||
public Object interfaceStaticMethodTest() {
|
||||
List<String[]> zz = new ArrayList<>();
|
||||
zz.add(new String[]{"1"});
|
||||
return zz.stream()
|
||||
.flatMap(Arrays::stream)
|
||||
.map(Double::valueOf)
|
||||
.map(BigDecimal::new)
|
||||
.reduce(BigDecimal::add);
|
||||
}
|
||||
|
||||
public void objectStaticMethodReference() {
|
||||
List<Boolean> f = new ArrayList<>();
|
||||
f.add(false);
|
||||
f.add(false);
|
||||
f.add(false);
|
||||
blackHole(f.stream()
|
||||
.reduce(Boolean::logicalAnd)
|
||||
.get()
|
||||
);
|
||||
}
|
||||
|
||||
private void blackHole(Object... ignore) {}
|
||||
|
||||
public interface ILambda {
|
||||
default void run() {
|
||||
|
||||
}
|
||||
|
||||
default void function1(String s) {
|
||||
|
||||
}
|
||||
|
||||
static void staticRun() {
|
||||
|
||||
}
|
||||
|
||||
static void staticFunction1(String s) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static class LambdaFoo implements ILambda {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
package com.alibaba.demo.lambda;
|
||||
|
||||
import com.alibaba.testable.core.annotation.MockDiagnose;
|
||||
import com.alibaba.testable.core.annotation.MockInvoke;
|
||||
import com.alibaba.testable.core.model.LogLevel;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* @author jim
|
||||
*/
|
||||
public class FzoTest {
|
||||
Fzo f = new Fzo();
|
||||
@MockDiagnose(LogLevel.VERBOSE)
|
||||
public static class Mock {
|
||||
@MockInvoke(targetClass = Double.class, targetMethod = "hashCode")
|
||||
private int cc() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@MockInvoke(targetClass = Collection.class, targetMethod = "stream")
|
||||
<E> Stream<E> mockStream() {
|
||||
List<E> l = new ArrayList<>();
|
||||
l.add((E)Boolean.TRUE);
|
||||
l.add((E)Boolean.TRUE);
|
||||
l.add((E)Boolean.TRUE);
|
||||
System.out.println("mockStreammockStreammockStreammockStreammockStreammockStreammockStream");
|
||||
return l.stream();
|
||||
}
|
||||
|
||||
@MockInvoke(targetClass = Boolean.class, targetMethod = "logicalAnd")
|
||||
public static boolean mockLogicalAnd(boolean a, boolean b) {
|
||||
System.out.println("ZPPPPPPPPPPPPPPPPPPPPPP");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMockInterfaceStatic() {
|
||||
f.objectStaticMethodReference();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.alibaba.demo.lambda;
|
||||
|
||||
import com.alibaba.testable.core.annotation.MockDiagnose;
|
||||
import com.alibaba.testable.core.annotation.MockInvoke;
|
||||
import com.alibaba.testable.core.model.LogLevel;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static com.alibaba.testable.core.matcher.InvocationVerifier.verifyInvoked;
|
||||
|
||||
|
||||
/**
|
||||
* @author jim
|
||||
*/
|
||||
public class InvokeInterfaceDemoTest {
|
||||
|
||||
private final InvokeInterfaceDemo instance = new InvokeInterfaceDemo();
|
||||
|
||||
@MockDiagnose(LogLevel.VERBOSE)
|
||||
public static class Mock {
|
||||
|
||||
@MockInvoke(targetClass = InvokeInterfaceDemo.ILambda.class, targetMethod = "run")
|
||||
private void mockILambdaRun() {
|
||||
}
|
||||
|
||||
@MockInvoke(targetClass = InvokeInterfaceDemo.ILambda.class, targetMethod = "function1")
|
||||
private void mockILambdaFunction1(String s) {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@MockInvoke(targetClass = Collection.class, targetMethod = "stream")
|
||||
<E> Stream<E> mockStream() {
|
||||
List<E> fooList = new ArrayList<>();
|
||||
fooList.add((E) "123");
|
||||
fooList.add((E) "456");
|
||||
return fooList.stream();
|
||||
}
|
||||
|
||||
@MockInvoke(targetClass = Boolean.class, targetMethod = "logicalAnd")
|
||||
public static boolean mockLogicalAnd(boolean a, boolean b) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMockCollectionStream() {
|
||||
instance.collectionInterfaceDefaultOrStatic();
|
||||
verifyInvoked("mockStream").withTimes(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMockInterfaceDefault() {
|
||||
instance.interfaceDefault();
|
||||
verifyInvoked("mockILambdaRun").withTimes(1);
|
||||
verifyInvoked("mockILambdaFunction1").withTimes(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMockObjectStaticMethodReference() {
|
||||
instance.objectStaticMethodReference();
|
||||
verifyInvoked("mockLogicalAnd").withTimes(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMockInterfaceStatic() {
|
||||
instance.interfaceStatic();
|
||||
}
|
||||
}
|
||||
@@ -8,8 +8,8 @@ import static com.alibaba.testable.core.matcher.InvocationVerifier.verifyInvoked
|
||||
/**
|
||||
* @author zcbbpo
|
||||
*/
|
||||
public class ExternalLambdaDemoTest {
|
||||
private final ExternalLambdaDemo lambdaDemo = new ExternalLambdaDemo();
|
||||
public class InvokeVirtualDemoTest {
|
||||
private final InvokeVirtualDemo lambdaDemo = new InvokeVirtualDemo();
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public static class Mock {
|
||||
@@ -34,7 +34,7 @@ public class ExternalLambdaDemoTest {
|
||||
return "";
|
||||
}
|
||||
|
||||
@MockInvoke(targetClass = ExternalLambdaDemo.class, targetMethod = "f3")
|
||||
@MockInvoke(targetClass = InvokeVirtualDemo.class, targetMethod = "f3")
|
||||
public Boolean mockF3(String s1, Long l) {
|
||||
return true;
|
||||
}
|
||||
@@ -5,9 +5,6 @@ import com.alibaba.testable.core.annotation.MockInvoke;
|
||||
import com.alibaba.testable.core.model.LogLevel;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static com.alibaba.testable.core.matcher.InvocationVerifier.verifyInvoked;
|
||||
|
||||
|
||||
@@ -23,23 +20,6 @@ public class StaticInstanceReferenceTest {
|
||||
@MockInvoke(targetClass = StaticInstanceReference.StaticClassA.class, targetMethod = "doIt")
|
||||
private void mockDoIt() {
|
||||
}
|
||||
|
||||
@MockInvoke(targetClass = StaticInstanceReference.ILambda.class, targetMethod = "run")
|
||||
private void mockILambdaRun() {
|
||||
}
|
||||
|
||||
@MockInvoke(targetClass = StaticInstanceReference.ILambda.class, targetMethod = "function1")
|
||||
private void mockILambdaFunction1(String s) {
|
||||
}
|
||||
@MockInvoke(targetClass = Collection.class, targetMethod = "stream")
|
||||
<E> Stream<E> mockStream() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@MockInvoke(targetClass = Boolean.class, targetMethod = "logicalAnd")
|
||||
public static boolean mockLogicalAnd(boolean a, boolean b) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -47,28 +27,4 @@ public class StaticInstanceReferenceTest {
|
||||
instance.staticMethodReference();
|
||||
verifyInvoked("mockDoIt").withTimes(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMockCollectionStream() {
|
||||
instance.collectionInterfaceDefaultOrStatic();
|
||||
verifyInvoked("mockStream").withTimes(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMockInterfaceDefault() {
|
||||
instance.interfaceDefault();
|
||||
verifyInvoked("mockILambdaRun").withTimes(1);
|
||||
verifyInvoked("mockILambdaFunction1").withTimes(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMockObjectStaticMethodReference() {
|
||||
instance.objectStaticMethodReference();
|
||||
verifyInvoked("mockLogicalAnd").withTimes(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMockInterfaceStatic() {
|
||||
instance.interfaceStatic();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user