Add the o.s.b.d.g.core.utils.ObjectUtils utility class containing various operations on Objects.

This commit is contained in:
John Blum
2018-05-21 22:49:04 -07:00
parent 1160d67dee
commit f4d843b14e
2 changed files with 204 additions and 0 deletions

View File

@@ -0,0 +1,80 @@
/*
* Copyright 2018 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
*
* http://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.boot.data.geode.core.util;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newRuntimeException;
import org.junit.Test;
/**
* Unit tests for {@link ObjectUtils}.
*
* @author John Blum
* @see org.junit.Test
* @since 1.0.0
*/
public class ObjectUtilsUnitTests {
@Test
public void doOperationSafelyReturnsResult() {
assertThat(ObjectUtils.doOperationSafely(() -> "test")).isEqualTo("test");
}
@Test
public void doOperationSafelyReturnsDefaultValue() {
assertThat(ObjectUtils.doOperationSafely(() -> { throw newRuntimeException("test"); },
"default value")).isEqualTo("default value");
}
@Test(expected = IllegalStateException.class)
public void doOperationSafelyThrowsIllegalStateException() {
try {
ObjectUtils.doOperationSafely(() -> { throw newRuntimeException("test"); }, null);
}
catch (IllegalStateException expected) {
assertThat(expected).hasMessage("Failed to execute operation");
assertThat(expected).hasCauseInstanceOf(RuntimeException.class);
assertThat(expected.getCause()).hasMessage("test");
assertThat(expected.getCause()).hasNoCause();
throw expected;
}
}
@Test
public void returnValueThrowOnNullWithNonNullValueReturnsValue() {
assertThat(ObjectUtils.returnValueThrowOnNull("test")).isEqualTo("test");
}
@Test(expected = RuntimeException.class)
public void returnValueThrowOnNullWithNullValueThrowsException() {
try {
ObjectUtils.returnValueThrowOnNull(null, newRuntimeException("test"));
}
catch (RuntimeException expected) {
assertThat(expected).hasMessage("test");
assertThat(expected).hasNoCause();
throw expected;
}
}
}