diff --git a/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/mock/test/context/event/TestContextEventType.java b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/mock/test/context/event/TestContextEventType.java new file mode 100644 index 0000000..8f0117b --- /dev/null +++ b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/mock/test/context/event/TestContextEventType.java @@ -0,0 +1,91 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ +package org.springframework.data.gemfire.tests.mock.test.context.event; + +import org.springframework.lang.NonNull; +import org.springframework.lang.Nullable; +import org.springframework.test.context.TestContext; +import org.springframework.test.context.event.AfterTestClassEvent; +import org.springframework.test.context.event.AfterTestExecutionEvent; +import org.springframework.test.context.event.AfterTestMethodEvent; +import org.springframework.test.context.event.BeforeTestClassEvent; +import org.springframework.test.context.event.BeforeTestExecutionEvent; +import org.springframework.test.context.event.BeforeTestMethodEvent; +import org.springframework.test.context.event.PrepareTestInstanceEvent; +import org.springframework.test.context.event.TestContextEvent; +import org.springframework.util.Assert; + +/** + * An enumeration of {@link TestContextEvent} types in the Spring {@link TestContext} test framework. + * + * @author John Blum + * @see org.springframework.test.context.TestContext + * @see org.springframework.test.context.event.TestContextEvent + * @since 0.0.16 + */ +@SuppressWarnings("unused") +public enum TestContextEventType { + + BEFORE_TEST_CLASS(BeforeTestClassEvent.class), + PREPARE_TEST_INSTANCE(PrepareTestInstanceEvent.class), + BEFORE_TEST_METHOD(BeforeTestMethodEvent.class), + BEFORE_TEST_EXECUTION(BeforeTestExecutionEvent.class), + AFTER_TEST_EXECUTION(AfterTestExecutionEvent.class), + AFTER_TEST_METHOD(AfterTestMethodEvent.class), + AFTER_TEST_CLASS(AfterTestClassEvent.class); + + + public static @Nullable TestContextEventType from(@Nullable TestContextEvent event) { + + for (TestContextEventType eventType : values()) { + if (eventType.getTestContextEventType().isInstance(event)) { + return eventType; + } + } + + return null; + } + + private final Class eventType; + + /** + * Constructs a new instance of the {@link TestContextEventType} enumeration creating a new enumerated value + * initialized with a {@link TestContextEvent} {@link Class type}. + * + * @param eventType {@link TestContextEvent} {@link Class type} on which this enumerated type is based. + * @throws IllegalArgumentException if the {@link TestContextEvent} {@link Class type} is {@literal null}. + * @see org.springframework.test.context.event.TestContextEvent + * @see java.lang.Class + */ + TestContextEventType(Class eventType) { + + Assert.notNull(eventType, + "The Class type of the TestContextEvent for this enumerated value must not be null"); + + this.eventType = eventType; + } + + /** + * Returns the {@link TestContextEvent} {@link Class type} on which this enumerated type is based. + * + * @return the {@link TestContextEvent} {@link Class type} on which this enumerated type is based. + * @see org.springframework.test.context.event.TestContextEvent + * @see java.lang.Class + */ + public @NonNull Class getTestContextEventType() { + return eventType; + } +} diff --git a/spring-data-geode-test/src/test/java/org/springframework/data/gemfire/tests/mock/test/context/event/TestContextEventTypeUnitTest.java b/spring-data-geode-test/src/test/java/org/springframework/data/gemfire/tests/mock/test/context/event/TestContextEventTypeUnitTest.java new file mode 100644 index 0000000..846e791 --- /dev/null +++ b/spring-data-geode-test/src/test/java/org/springframework/data/gemfire/tests/mock/test/context/event/TestContextEventTypeUnitTest.java @@ -0,0 +1,78 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ +package org.springframework.data.gemfire.tests.mock.test.context.event; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +import java.util.Arrays; +import java.util.List; + +import org.junit.Test; + +import org.springframework.test.context.event.AfterTestClassEvent; +import org.springframework.test.context.event.AfterTestExecutionEvent; +import org.springframework.test.context.event.AfterTestMethodEvent; +import org.springframework.test.context.event.BeforeTestClassEvent; +import org.springframework.test.context.event.BeforeTestExecutionEvent; +import org.springframework.test.context.event.BeforeTestMethodEvent; +import org.springframework.test.context.event.PrepareTestInstanceEvent; +import org.springframework.test.context.event.TestContextEvent; + +/** + * Unit Tests for {@link TestContextEventType}. + * + * @author John Blum + * @see org.junit.Test + * @see org.mockito.Mockito + * @see org.springframework.data.gemfire.tests.mock.test.context.event.TestContextEventType + * @see org.springframework.test.context.event.TestContextEvent + * @since 0.0.16 + */ +public class TestContextEventTypeUnitTest { + + @Test + public void fromTestContextEventToEnum() { + + List events = Arrays.asList( + mock(BeforeTestClassEvent.class), + mock(PrepareTestInstanceEvent.class), + mock(BeforeTestMethodEvent.class), + mock(BeforeTestExecutionEvent.class), + mock(AfterTestExecutionEvent.class), + mock(AfterTestMethodEvent.class), + mock(AfterTestClassEvent.class) + ); + + events.forEach(event -> { + + TestContextEventType eventType = TestContextEventType.from(event); + + assertThat(eventType).isNotNull(); + assertThat(eventType.getTestContextEventType()).isAssignableFrom(event.getClass()); + }); + } + + @Test + public void fromTestContextEventReturnsNull() { + assertThat(TestContextEventType.from(mock(TestContextEvent.class))).isNull(); + } + + @Test + public void fromNullIsNullSafeAndReturnsNull() { + assertThat(TestContextEventType.from(null)).isNull(); + } +}