001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.procedure2.util;
019
020import static org.junit.Assert.assertEquals;
021
022import org.apache.hadoop.hbase.HBaseClassTestRule;
023import org.apache.hadoop.hbase.testclassification.MasterTests;
024import org.apache.hadoop.hbase.testclassification.SmallTests;
025import org.junit.ClassRule;
026import org.junit.Test;
027import org.junit.experimental.categories.Category;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031@Category({MasterTests.class, SmallTests.class})
032public class TestDelayedUtil {
033
034  @ClassRule
035  public static final HBaseClassTestRule CLASS_RULE =
036      HBaseClassTestRule.forClass(TestDelayedUtil.class);
037
038  private static final Logger LOG = LoggerFactory.getLogger(TestDelayedUtil.class);
039
040  @Test
041  public void testDelayedContainerEquals() {
042    Object o1 = new Object();
043    Object o2 = new Object();
044    ZeroDelayContainer<Long> lnull = new ZeroDelayContainer(null);
045    ZeroDelayContainer<Long> l10a = new ZeroDelayContainer<>(10L);
046    ZeroDelayContainer<Long> l10b = new ZeroDelayContainer(10L);
047    ZeroDelayContainer<Long> l15 = new ZeroDelayContainer(15L);
048    ZeroDelayContainer<Object> onull = new ZeroDelayContainer<>(null);
049    ZeroDelayContainer<Object> o1ca = new ZeroDelayContainer<>(o1);
050    ZeroDelayContainer<Object> o1cb = new ZeroDelayContainer<>(o1);
051    ZeroDelayContainer<Object> o2c = new ZeroDelayContainer<>(o2);
052
053    ZeroDelayContainer[] items = new ZeroDelayContainer[] {
054      lnull, l10a, l10b, l15, onull, o1ca, o1cb, o2c,
055    };
056
057    assertContainersEquals(lnull, items, lnull, onull);
058    assertContainersEquals(l10a, items, l10a, l10b);
059    assertContainersEquals(l10b, items, l10a, l10b);
060    assertContainersEquals(l15, items, l15);
061    assertContainersEquals(onull, items, lnull, onull);
062    assertContainersEquals(o1ca, items, o1ca, o1cb);
063    assertContainersEquals(o1cb, items, o1ca, o1cb);
064    assertContainersEquals(o2c, items, o2c);
065  }
066
067  private void assertContainersEquals(final ZeroDelayContainer src,
068      final ZeroDelayContainer[] items, final ZeroDelayContainer... matches) {
069    for (int i = 0; i < items.length; ++i) {
070      boolean shouldMatch = false;
071      for (int j = 0; j < matches.length; ++j) {
072        if (items[i] == matches[j]) {
073          shouldMatch = true;
074          break;
075        }
076      }
077      boolean isMatching = src.equals(items[i]);
078      assertEquals(src.getObject() + " unexpectedly match " + items[i].getObject(),
079        shouldMatch, isMatching);
080    }
081  }
082
083  private static class ZeroDelayContainer<T> extends DelayedUtil.DelayedContainer<T> {
084    public ZeroDelayContainer(final T object) {
085      super(object);
086    }
087
088    @Override
089    public long getTimeout() {
090      return 0;
091    }
092  }
093}