diff --git a/cdi/src/test/java/.gitkeep b/cdi/src/test/java/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/cdi/src/test/java/com/qwlabs/cdi/partition/PartitionInterceptorTest.java b/cdi/src/test/java/com/qwlabs/cdi/partition/PartitionInterceptorTest.java new file mode 100644 index 0000000..88f8518 --- /dev/null +++ b/cdi/src/test/java/com/qwlabs/cdi/partition/PartitionInterceptorTest.java @@ -0,0 +1,52 @@ +package com.qwlabs.cdi.partition; + + +import jakarta.interceptor.InvocationContext; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.Collection; +import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class PartitionInterceptorTest { + private PartitionInterceptor interceptor; + + @BeforeEach + void setUp() { + interceptor = new PartitionInterceptor(); + } + + @Test + void should_call_twice(@Mock InvocationContext context) throws Exception { + TestClass target = new TestClass(); + var method = target.getClass().getMethod("testMethod", String.class, Collection.class); + var parameters1 = "org"; + var parameters2 = List.of("1", "2", "3"); + when(context.getTarget()).thenReturn(target); + when(context.getMethod()).thenReturn(method); + when(context.getParameters()).thenReturn(new Object[]{parameters1, parameters2}); + when(context.proceed()).thenReturn(List.of("r1", "r2"), List.of("r3", "r4")); + + var result = interceptor.intercept(context); + + assertThat(result, is(List.of("r1", "r2", "r3", "r4"))); + verify(context, times(2)).proceed(); + } + + private static class TestClass { + @Partition(size = 2) + public List testMethod(String orgId, @PartitionBy Collection ids) { + return List.of("1"); + } + } +} diff --git a/cdi/src/test/java/com/qwlabs/cdi/partition/PartitionResultTest.java b/cdi/src/test/java/com/qwlabs/cdi/partition/PartitionResultTest.java new file mode 100644 index 0000000..dbab56d --- /dev/null +++ b/cdi/src/test/java/com/qwlabs/cdi/partition/PartitionResultTest.java @@ -0,0 +1,70 @@ +package com.qwlabs.cdi.partition; + +import com.qwlabs.exceptions.CodeException; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Stream; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.assertThrows; + + +class PartitionResultTest { + @Test + void should_return_stream() { + Stream data = Stream.of(Stream.of("1"), Stream.of("2")); + + var result = PartitionResult.get(data, Stream.class); + + assertThat(result.toList(), is(List.of("1", "2"))); + } + + @Test + void should_return_set() { + Stream data = Stream.of(Set.of("1"), Set.of("2")); + + var result = PartitionResult.get(data, Set.class); + + assertThat(result, is(Set.of("1", "2"))); + } + + @Test + void should_return_list() { + Stream data = Stream.of(List.of("1"), List.of("2")); + + var result = PartitionResult.get(data, List.class); + + assertThat(result, is(List.of("1", "2"))); + } + + @Test + void should_return_integer() { + Stream data = Stream.of(1, 2); + + var result = PartitionResult.get(data, Integer.class); + + assertThat(result, is(3)); + } + + @Test + void should_return_long() { + Stream data = Stream.of(1L, 2L); + + var result = PartitionResult.get(data, Long.class); + + assertThat(result, is(3L)); + } + + @Test + void should_exception() { + Stream data = Stream.of(Map.of("a", "b")); + + var exception = assertThrows(CodeException.class, () -> PartitionResult.get(data, Map.class)); + + assertThat(exception.getMessage(), is("代码错误: PartitionResult can not support type java.util.Map")); + } +}