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 =
054      new ZeroDelayContainer[] { lnull, l10a, l10b, l15, onull, o1ca, o1cb, o2c, };
055
056    assertContainersEquals(lnull, items, lnull, onull);
057    assertContainersEquals(l10a, items, l10a, l10b);
058    assertContainersEquals(l10b, items, l10a, l10b);
059    assertContainersEquals(l15, items, l15);
060    assertContainersEquals(onull, items, lnull, onull);
061    assertContainersEquals(o1ca, items, o1ca, o1cb);
062    assertContainersEquals(o1cb, items, o1ca, o1cb);
063    assertContainersEquals(o2c, items, o2c);
064  }
065
066  private void assertContainersEquals(final ZeroDelayContainer src,
067    final ZeroDelayContainer[] items, final ZeroDelayContainer... matches) {
068    for (int i = 0; i < items.length; ++i) {
069      boolean shouldMatch = false;
070      for (int j = 0; j < matches.length; ++j) {
071        if (items[i] == matches[j]) {
072          shouldMatch = true;
073          break;
074        }
075      }
076      boolean isMatching = src.equals(items[i]);
077      assertEquals(src.getObject() + " unexpectedly match " + items[i].getObject(), shouldMatch,
078        isMatching);
079    }
080  }
081
082  private static class ZeroDelayContainer<T> extends DelayedUtil.DelayedContainer<T> {
083    public ZeroDelayContainer(final T object) {
084      super(object);
085    }
086
087    @Override
088    public long getTimeout() {
089      return 0;
090    }
091  }
092}