From 3580141a3c456d02a5550485b942f469d7addda5 Mon Sep 17 00:00:00 2001 From: Mercy Ma Date: Thu, 16 Jan 2025 20:32:30 +0800 Subject: [PATCH] Polish #33 : Add testing coverage --- .../event/RegistrationDeregisteredEvent.java | 10 ++-- .../registry/event/RegistrationEvent.java | 49 +++++++++++++++++-- .../RegistrationPreDeregisteredEvent.java | 12 ++--- .../event/RegistrationPreRegisteredEvent.java | 12 ++--- .../event/RegistrationRegisteredEvent.java | 12 ++--- .../ServiceRegistryAutoConfigurationTest.java | 29 +++++++++++ 6 files changed, 93 insertions(+), 31 deletions(-) diff --git a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationDeregisteredEvent.java b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationDeregisteredEvent.java index 294583c..898b039 100644 --- a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationDeregisteredEvent.java +++ b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationDeregisteredEvent.java @@ -19,6 +19,8 @@ import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.ServiceRegistry; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.DEREGISTERED; + /** * The after-{@link ServiceRegistry#deregister(Registration) deregister} event. * @@ -33,12 +35,8 @@ public RegistrationDeregisteredEvent(ServiceRegistry registry, Reg } @Override - public boolean isRegistered() { - return false; + public Type getType() { + return DEREGISTERED; } - @Override - public boolean isDeregistered() { - return true; - } } diff --git a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationEvent.java b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationEvent.java index d51d26a..b8e1b26 100644 --- a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationEvent.java +++ b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationEvent.java @@ -22,6 +22,11 @@ import org.springframework.lang.NonNull; import org.springframework.util.Assert; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.DEREGISTERED; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.PRE_DEREGISTERED; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.PRE_REGISTERED; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.REGISTERED; + /** * The Spring event for {@link ServiceRegistry} * @@ -80,7 +85,7 @@ public ServiceRegistry getRegistry() { * @return true if pre-registered */ public final boolean isPreRegistered() { - return !isRegistered(); + return getType() == PRE_REGISTERED; } /** @@ -89,7 +94,9 @@ public final boolean isPreRegistered() { * * @return true if registered */ - public abstract boolean isRegistered(); + public final boolean isRegistered() { + return getType() == REGISTERED; + } /** * Current event is raised before the {@link #getRegistration() registration} is @@ -98,7 +105,7 @@ public final boolean isPreRegistered() { * @return true if pre-deregistered */ public final boolean isPreDeregistered() { - return !isDeregistered(); + return getType() == PRE_DEREGISTERED; } /** @@ -107,6 +114,40 @@ public final boolean isPreDeregistered() { * * @return true if deregistered */ - public abstract boolean isDeregistered(); + public final boolean isDeregistered() { + return getType() == DEREGISTERED; + } + + /** + * Get the {@link Type} of the {@link RegistrationEvent} + * + * @return non-null + */ + public abstract Type getType(); + /** + * The {@link Type} of the {@link RegistrationEvent} + */ + public static enum Type { + + /** + * The {@link RegistrationPreRegisteredEvent} + */ + PRE_REGISTERED, + + /** + * The {@link RegistrationRegisteredEvent} + */ + REGISTERED, + + /** + * The {@link RegistrationPreDeregisteredEvent} + */ + PRE_DEREGISTERED, + + /** + * The {@link RegistrationDeregisteredEvent} + */ + DEREGISTERED + } } diff --git a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationPreDeregisteredEvent.java b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationPreDeregisteredEvent.java index 6e19475..892bdf0 100644 --- a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationPreDeregisteredEvent.java +++ b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationPreDeregisteredEvent.java @@ -19,6 +19,9 @@ import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.ServiceRegistry; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.DEREGISTERED; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.PRE_DEREGISTERED; + /** * The before-{@link ServiceRegistry#deregister(Registration) deregister} event. * @@ -33,12 +36,7 @@ public RegistrationPreDeregisteredEvent(ServiceRegistry registry, } @Override - public boolean isRegistered() { - return false; - } - - @Override - public boolean isDeregistered() { - return false; + public Type getType() { + return PRE_DEREGISTERED; } } \ No newline at end of file diff --git a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationPreRegisteredEvent.java b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationPreRegisteredEvent.java index 2e0d192..44501ed 100644 --- a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationPreRegisteredEvent.java +++ b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationPreRegisteredEvent.java @@ -19,6 +19,9 @@ import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.ServiceRegistry; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.PRE_DEREGISTERED; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.PRE_REGISTERED; + /** * The before-{@link ServiceRegistry#register(Registration) register} event. * @@ -33,12 +36,7 @@ public RegistrationPreRegisteredEvent(ServiceRegistry registry, Re } @Override - public boolean isRegistered() { - return false; - } - - @Override - public boolean isDeregistered() { - return false; + public Type getType() { + return PRE_REGISTERED; } } diff --git a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationRegisteredEvent.java b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationRegisteredEvent.java index cc20ed1..5fdb3ac 100644 --- a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationRegisteredEvent.java +++ b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationRegisteredEvent.java @@ -19,6 +19,9 @@ import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.ServiceRegistry; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.PRE_REGISTERED; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.REGISTERED; + /** * The after-{@link ServiceRegistry#register(Registration) register} event. * @@ -33,12 +36,7 @@ public RegistrationRegisteredEvent(ServiceRegistry registry, Regis } @Override - public boolean isRegistered() { - return true; - } - - @Override - public boolean isDeregistered() { - return false; + public Type getType() { + return REGISTERED; } } \ No newline at end of file diff --git a/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/ServiceRegistryAutoConfigurationTest.java b/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/ServiceRegistryAutoConfigurationTest.java index a519332..2f23cd9 100644 --- a/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/ServiceRegistryAutoConfigurationTest.java +++ b/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/ServiceRegistryAutoConfigurationTest.java @@ -18,8 +18,15 @@ import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.EnableAspectJAutoProxy; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.DEREGISTERED; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.PRE_DEREGISTERED; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.PRE_REGISTERED; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.REGISTERED; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.springframework.aop.support.AopUtils.getTargetClass; /** @@ -85,18 +92,38 @@ private void onApplicationEvent(ApplicationEvent event) { private void onRegistrationPreRegisteredEvent(RegistrationPreRegisteredEvent event) { assertRegistrationEvent(event); + assertTrue(event.isPreRegistered()); + assertFalse(event.isRegistered()); + assertFalse(event.isPreDeregistered()); + assertFalse(event.isDeregistered()); + assertEquals(PRE_REGISTERED, event.getType()); } private void onRegistrationRegisteredEvent(RegistrationRegisteredEvent event) { assertRegistrationEvent(event); + assertFalse(event.isPreRegistered()); + assertTrue(event.isRegistered()); + assertFalse(event.isPreDeregistered()); + assertFalse(event.isDeregistered()); + assertEquals(REGISTERED, event.getType()); } private void onRegistrationPreDeregisteredEvent(RegistrationPreDeregisteredEvent event) { assertRegistrationEvent(event); + assertFalse(event.isPreRegistered()); + assertFalse(event.isRegistered()); + assertTrue(event.isPreDeregistered()); + assertFalse(event.isDeregistered()); + assertEquals(PRE_DEREGISTERED, event.getType()); } private void onRegistrationDeregisteredEvent(RegistrationDeregisteredEvent event) { assertRegistrationEvent(event); + assertFalse(event.isPreRegistered()); + assertFalse(event.isRegistered()); + assertFalse(event.isPreDeregistered()); + assertTrue(event.isDeregistered()); + assertEquals(DEREGISTERED, event.getType()); } private void assertRegistrationEvent(RegistrationEvent event) { @@ -104,6 +131,8 @@ private void assertRegistrationEvent(RegistrationEvent event) { assertEquals(this.registration, registration); assertSame(this.registration, registration); assertSame(getTargetClass(this.serviceRegistry), getTargetClass(event.getRegistry())); + assertNotNull(event.getSource()); + assertNotNull(event.getType()); count++; } } \ No newline at end of file