001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 *     http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.gbif.utils;
015
016import java.util.ArrayList;
017import java.util.Collection;
018import java.util.Collections;
019
020import org.junit.jupiter.api.Test;
021
022import static org.junit.jupiter.api.Assertions.assertEquals;
023import static org.junit.jupiter.api.Assertions.assertNull;
024
025public class ObjectUtilsTest {
026
027  @Test
028  public void testCoalesce() {
029    assertNull(ObjectUtils.coalesce());
030    assertNull(ObjectUtils.coalesce((Integer) null));
031    assertNull(ObjectUtils.coalesce(null, (Integer) null));
032
033    assertEquals((Integer) 13, ObjectUtils.coalesce(null, 13));
034    assertEquals((Integer) 13, ObjectUtils.coalesce(null, 13, 14));
035    assertEquals((Integer) 13, ObjectUtils.coalesce(13, 15));
036  }
037
038  @Test
039  public void testCoalesce1() {
040    assertNull(ObjectUtils.coalesce((Collection) null));
041    assertNull(ObjectUtils.coalesce(new ArrayList<>()));
042    assertNull(ObjectUtils.coalesce(newArrayList(null, null)));
043
044    assertEquals((Integer) 13, ObjectUtils.coalesce(newArrayList(null, null, 13)));
045    assertEquals((Integer) 13, ObjectUtils.coalesce(newArrayList(null, null, 13, 14)));
046    assertEquals((Integer) 13, ObjectUtils.coalesce(newArrayList(13, null)));
047  }
048
049  public ArrayList<Integer> newArrayList(Integer... elements) {
050    ArrayList<Integer> list = new ArrayList<>(elements.length);
051    Collections.addAll(list, elements);
052    return list;
053  }
054}