Add GeodeAssertions to apache-geode-extensions.

GeodeAssertions contains various assertions to assert different aspects and characteristics of Geode objects.
This commit is contained in:
John Blum
2020-03-20 09:45:56 -07:00
parent 09fff0fb3c
commit d0ef5e6973

View File

@@ -0,0 +1,93 @@
/*
* Copyright 2020 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.geode.util;
import org.apache.geode.cache.GemFireCache;
import org.apache.geode.cache.Region;
import org.apache.geode.distributed.internal.InternalDistributedSystem;
import org.apache.geode.internal.cache.AbstractRegion;
import org.apache.geode.internal.cache.GemFireCacheImpl;
/**
* Abstract utility class containing different assertions for Apache Geode objects, such as a {@link GemFireCache}
* or {@link Region}, and so on.
*
* @author John Blum
* @see org.apache.geode.cache.GemFireCache
* @see org.apache.geode.cache.Region
* @since 1.3.0
*/
@SuppressWarnings("unused")
public abstract class GeodeAssertions {
public static <T> AssertThat<T> assertThat(T obj) {
return () -> obj;
}
private static void assertIsInstanceOf(Object target, Class<?> type) {
if (!type.isInstance(target)) {
throw new AssertionError(String.format("[%1$s] is not an instance of [%2$s]",
nullSafeTypeName(target), nullSafeTypeName(type)));
}
}
private static void assertIsNotInstanceOf(Object target, Class<?> type) {
if (type.isInstance(target)) {
throw new AssertionError(String.format("[%1%s] is an instance of [%2$s]",
nullSafeTypeName(target), nullSafeTypeName(type)));
}
}
private static Class<?> nullSafeType(Object obj) {
return obj != null ? obj.getClass() : null;
}
private static String nullSafeTypeName(Class<?> type) {
return type != null ? type.getName() : null;
}
public static String nullSafeTypeName(Object obj) {
return nullSafeTypeName(nullSafeType(obj));
}
@FunctionalInterface
public interface AssertThat<T> {
T getSubject();
default void isInstanceOfInternalDistributedSystem() {
assertIsInstanceOf(getSubject(), InternalDistributedSystem.class);
}
default void isInstanceOfGemFireCacheImpl() {
assertIsInstanceOf(getSubject(), GemFireCacheImpl.class);
}
default void isNotInstanceOfAbstractRegion() {
assertIsNotInstanceOf(getSubject(), AbstractRegion.class);
}
default void isNotInstanceOfInternalDistributedSystem() {
assertIsNotInstanceOf(getSubject(), InternalDistributedSystem.class);
}
default void isNotInstanceOfGemFireCacheImpl() {
assertIsNotInstanceOf(getSubject(), GemFireCacheImpl.class);
}
}
}