|
| 1 | +package org.apache.flink.streaming.connectors.kafka.testutils; |
| 2 | + |
| 3 | + |
| 4 | +import org.apache.flink.api.common.serialization.DeserializationSchema; |
| 5 | +import org.apache.flink.api.common.serialization.SerializationSchema; |
| 6 | +import org.apache.flink.util.InstantiationUtil; |
| 7 | +import org.apache.flink.util.UserCodeClassLoader; |
| 8 | +import org.junit.Before; |
| 9 | +import org.mockito.*; |
| 10 | + |
| 11 | +import java.util.concurrent.Callable; |
| 12 | + |
| 13 | +import static org.junit.Assert.assertEquals; |
| 14 | +import static org.mockito.ArgumentMatchers.*; |
| 15 | +import static org.mockito.Mockito.when; |
| 16 | + |
| 17 | +public class SerializationTestBase { |
| 18 | + @Mock |
| 19 | + protected DeserializationSchema.InitializationContext deserializationContext; |
| 20 | + @Mock |
| 21 | + protected SerializationSchema.InitializationContext serializationContext; |
| 22 | + @Mock |
| 23 | + protected UserCodeClassLoader userCodeClassLoader; |
| 24 | + @Mock |
| 25 | + protected ClassLoader classLoader; |
| 26 | + @Captor |
| 27 | + private ArgumentCaptor<ClassLoader> classLoaderCaptor; |
| 28 | + |
| 29 | + @Before |
| 30 | + public void setUp() { |
| 31 | + when(userCodeClassLoader.asClassLoader()).thenReturn(classLoader); |
| 32 | + setupContext(); |
| 33 | + } |
| 34 | + |
| 35 | + protected void setupContext() { |
| 36 | + } |
| 37 | + |
| 38 | + protected void testUserClassLoaderIsUsedWhen(Callable<Object> callable, Object instance) throws Exception { |
| 39 | + try (MockedStatic<InstantiationUtil> mocked = Mockito.mockStatic(InstantiationUtil.class)) { |
| 40 | + |
| 41 | + mocked.when(() -> InstantiationUtil.instantiate( |
| 42 | + anyString(), |
| 43 | + notNull(), |
| 44 | + any(ClassLoader.class) |
| 45 | + )).thenReturn(instance); |
| 46 | + |
| 47 | + callable.call(); |
| 48 | + |
| 49 | + mocked.verify(() -> InstantiationUtil.instantiate( |
| 50 | + anyString(), |
| 51 | + notNull(), |
| 52 | + classLoaderCaptor.capture() |
| 53 | + )); |
| 54 | + |
| 55 | + assertEquals(classLoader, classLoaderCaptor.getValue()); |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments