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.util;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertNotNull;
023import static org.junit.jupiter.api.Assertions.assertTrue;
024import static org.mockito.Mockito.mock;
025import static org.mockito.Mockito.verify;
026import static org.mockito.Mockito.when;
027
028import org.apache.hadoop.hbase.testclassification.MiscTests;
029import org.apache.hadoop.hbase.testclassification.SmallTests;
030import org.junit.jupiter.api.Tag;
031import org.junit.jupiter.api.Test;
032
033@Tag(MiscTests.TAG)
034@Tag(SmallTests.TAG)
035public class TestEnvironmentEdgeManager {
036
037  @Test
038  public void testManageSingleton() {
039    EnvironmentEdgeManager.reset();
040    EnvironmentEdge edge = EnvironmentEdgeManager.getDelegate();
041    assertNotNull(edge);
042    assertTrue(edge instanceof DefaultEnvironmentEdge);
043    EnvironmentEdgeManager.reset();
044    EnvironmentEdge edge2 = EnvironmentEdgeManager.getDelegate();
045    assertFalse(edge == edge2);
046    IncrementingEnvironmentEdge newEdge = new IncrementingEnvironmentEdge();
047    EnvironmentEdgeManager.injectEdge(newEdge);
048    assertEquals(newEdge, EnvironmentEdgeManager.getDelegate());
049
050    // injecting null will result in default being assigned.
051    EnvironmentEdgeManager.injectEdge(null);
052    EnvironmentEdge nullResult = EnvironmentEdgeManager.getDelegate();
053    assertTrue(nullResult instanceof DefaultEnvironmentEdge);
054  }
055
056  @Test
057  public void testCurrentTimeInMillis() {
058    EnvironmentEdge mock = mock(EnvironmentEdge.class);
059    EnvironmentEdgeManager.injectEdge(mock);
060    long expectation = 3456;
061    when(mock.currentTime()).thenReturn(expectation);
062    long result = EnvironmentEdgeManager.currentTime();
063    verify(mock).currentTime();
064    assertEquals(expectation, result);
065  }
066}