|
| 1 | +// @formatter:off |
| 2 | +/** |
| 3 | +* |
| 4 | +* This file is part of streaming pool (http://www.streamingpool.org). |
| 5 | +* |
| 6 | +* Copyright (c) 2017-present, CERN. All rights reserved. |
| 7 | +* |
| 8 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +* you may not use this file except in compliance with the License. |
| 10 | +* You may obtain a copy of the License at |
| 11 | +* |
| 12 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +* |
| 14 | +* Unless required by applicable law or agreed to in writing, software |
| 15 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +* See the License for the specific language governing permissions and |
| 18 | +* limitations under the License. |
| 19 | +* |
| 20 | +*/ |
| 21 | +// @formatter:on |
| 22 | + |
| 23 | +package org.tensorics.core.util.names; |
| 24 | + |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | + |
| 27 | +import java.util.function.Function; |
| 28 | + |
| 29 | +import org.junit.Test; |
| 30 | +import org.tensorics.core.util.chains.Chains; |
| 31 | + |
| 32 | +public class NamesTest { |
| 33 | + |
| 34 | + @Test |
| 35 | + public void nonOverriddenStringReturnsNull() { |
| 36 | + Function<Object, String> chain = Chains.chainFor(String.class).or(Names::fromOverriddenToString).orElseNull(); |
| 37 | + String name = chain.apply(new EmptyTestClass()); |
| 38 | + assertThat(name).isNull(); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void chainOverriddenToStringWithSimpleClassNameReturnsSimpleClassName() { |
| 43 | + Function<Object, String> chain = Chains.chainFor(String.class).or(Names::fromOverriddenToString) |
| 44 | + .or(Names::fromSimpleClassName).orElseNull(); |
| 45 | + String name = chain.apply(new EmptyTestClass()); |
| 46 | + assertThat(name).isEqualTo("EmptyTestClass"); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void anyToStringWillNotBeCalledWhenSimpleClassNameBefore() { |
| 51 | + Function<Object, String> chain = Chains.chainFor(String.class).or(Names::fromOverriddenToString) |
| 52 | + .or(Names::fromSimpleClassName).or(Names::fromToString).orElseNull(); |
| 53 | + String name = chain.apply(new EmptyTestClass()); |
| 54 | + assertThat(name).isEqualTo("EmptyTestClass"); |
| 55 | + } |
| 56 | + |
| 57 | + private static final class EmptyTestClass { |
| 58 | + /* empty on purpose */ |
| 59 | + } |
| 60 | +} |
0 commit comments