mirror of https://github.com/longtai-cn/hippo4j
* feat(infra): add AbstractSubjectCenter test class #1338 1. add SubjectCenter test class 2. add remove observer method 3. add get size for Observers method Co-authored-by: rnmb<19276209+rnmb@users.noreply.github.com> * feat(infra): add AbstractSubjectCenter test class 1. add SubjectCenter test class 2. rollback SubjectCenter method Co-authored-by: rnmb<19276209+rnmb@users.noreply.github.com>pull/1344/head
parent
f46da2b3d3
commit
bd046b9271
@ -0,0 +1,146 @@
|
||||
package cn.hippo4j.common.extension.design;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class AbstractSubjectCenterTest {
|
||||
|
||||
|
||||
private Map<String, List<Observer>> OBSERVERS_MAP ;
|
||||
|
||||
private SubjectNotifyListener subjectNotifyListener;
|
||||
|
||||
@Before
|
||||
public void setUp() throws NoSuchFieldException, IllegalAccessException {
|
||||
subjectNotifyListener = new SubjectNotifyListener();
|
||||
|
||||
Field field = AbstractSubjectCenter.class.getDeclaredField("OBSERVERS_MAP");
|
||||
field.setAccessible(true);
|
||||
OBSERVERS_MAP = (Map<String, List<Observer>>) field.get(AbstractSubjectCenter.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* test register listener
|
||||
*/
|
||||
@Test
|
||||
public void testDefaultRegister() {
|
||||
AbstractSubjectCenter.register(subjectNotifyListener);
|
||||
List<Observer> list = OBSERVERS_MAP.get(AbstractSubjectCenter.SubjectType.SPRING_CONTENT_REFRESHED.name());
|
||||
Assert.assertNotNull(list);
|
||||
Assert.assertEquals(1, list.size());
|
||||
Assert.assertSame(subjectNotifyListener, list.get(0));
|
||||
OBSERVERS_MAP.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* test register listener
|
||||
*/
|
||||
@Test
|
||||
public void testSubjectTypeEnumRegister() {
|
||||
AbstractSubjectCenter.register(AbstractSubjectCenter.SubjectType.THREAD_POOL_DYNAMIC_REFRESH, subjectNotifyListener);
|
||||
List<Observer> list = OBSERVERS_MAP.get(AbstractSubjectCenter.SubjectType.THREAD_POOL_DYNAMIC_REFRESH.name());
|
||||
Assert.assertNotNull(list);
|
||||
Assert.assertEquals(1, list.size());
|
||||
Assert.assertSame(subjectNotifyListener, list.get(0));
|
||||
OBSERVERS_MAP.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* test register listener
|
||||
*/
|
||||
@Test
|
||||
public void testSubjectTypeNameRegister() {
|
||||
AbstractSubjectCenter.register(AbstractSubjectCenter.SubjectType.THREAD_POOL_DYNAMIC_REFRESH.name(), subjectNotifyListener);
|
||||
List<Observer> list = OBSERVERS_MAP.get(AbstractSubjectCenter.SubjectType.THREAD_POOL_DYNAMIC_REFRESH.name());
|
||||
Assert.assertNotNull(list);
|
||||
Assert.assertEquals(1, list.size());
|
||||
Assert.assertSame(subjectNotifyListener, list.get(0));
|
||||
OBSERVERS_MAP.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* test remove listener
|
||||
*/
|
||||
@Test
|
||||
public void testDefaultRemoveListener() {
|
||||
AbstractSubjectCenter.register(subjectNotifyListener);
|
||||
List<Observer> list = OBSERVERS_MAP.get(AbstractSubjectCenter.SubjectType.SPRING_CONTENT_REFRESHED.name());
|
||||
Assert.assertNotNull(list);
|
||||
Assert.assertEquals(1, list.size());
|
||||
Assert.assertSame(subjectNotifyListener, list.get(0));
|
||||
|
||||
AbstractSubjectCenter.remove(subjectNotifyListener);
|
||||
Assert.assertEquals(0, list.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* test remove listener
|
||||
*/
|
||||
@Test
|
||||
public void testRemoveSubjectTypeNameListener() {
|
||||
AbstractSubjectCenter.register(AbstractSubjectCenter.SubjectType.THREAD_POOL_DYNAMIC_REFRESH, subjectNotifyListener);
|
||||
List<Observer> list = OBSERVERS_MAP.get(AbstractSubjectCenter.SubjectType.THREAD_POOL_DYNAMIC_REFRESH.name());
|
||||
Assert.assertNotNull(list);
|
||||
Assert.assertEquals(1, list.size());
|
||||
Assert.assertSame(subjectNotifyListener, list.get(0));
|
||||
|
||||
AbstractSubjectCenter.remove(AbstractSubjectCenter.SubjectType.THREAD_POOL_DYNAMIC_REFRESH.name(), subjectNotifyListener);
|
||||
Assert.assertEquals(0, list.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* test notify
|
||||
*/
|
||||
@Test
|
||||
public void testNotifyBySubjectType() {
|
||||
AbstractSubjectCenter.register(subjectNotifyListener);
|
||||
List<Observer> list = OBSERVERS_MAP.get(AbstractSubjectCenter.SubjectType.SPRING_CONTENT_REFRESHED.name());
|
||||
Assert.assertNotNull(list);
|
||||
|
||||
NotifyMessage notifyMessage = new NotifyMessage();
|
||||
Assert.assertEquals(0, notifyMessage.getCount().get());
|
||||
AbstractSubjectCenter.notify(AbstractSubjectCenter.SubjectType.SPRING_CONTENT_REFRESHED, () -> notifyMessage);
|
||||
Assert.assertEquals(1, notifyMessage.getCount().get());
|
||||
OBSERVERS_MAP.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* test notify
|
||||
*/
|
||||
@Test
|
||||
public void testNotifyBySubjectTypeName() {
|
||||
AbstractSubjectCenter.register(subjectNotifyListener);
|
||||
List<Observer> list = OBSERVERS_MAP.get(AbstractSubjectCenter.SubjectType.SPRING_CONTENT_REFRESHED.name());
|
||||
Assert.assertNotNull(list);
|
||||
|
||||
NotifyMessage notifyMessage = new NotifyMessage();
|
||||
Assert.assertEquals(0, notifyMessage.getCount().get());
|
||||
AbstractSubjectCenter.notify(AbstractSubjectCenter.SubjectType.SPRING_CONTENT_REFRESHED.name(), () -> notifyMessage);
|
||||
Assert.assertEquals(1, notifyMessage.getCount().get());
|
||||
OBSERVERS_MAP.clear();
|
||||
}
|
||||
|
||||
@Getter
|
||||
private static final class NotifyMessage {
|
||||
private final AtomicInteger count = new AtomicInteger(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subject Response Listener
|
||||
*/
|
||||
private static final class SubjectNotifyListener implements Observer<NotifyMessage>{
|
||||
|
||||
@Override
|
||||
public void accept(ObserverMessage<NotifyMessage> observerMessage) {
|
||||
observerMessage.message().getCount().incrementAndGet();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue