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.ipc;
019
020import static org.hamcrest.CoreMatchers.is;
021import static org.junit.Assert.assertEquals;
022import static org.junit.Assert.assertThat;
023
024import java.net.InetSocketAddress;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.testclassification.ClientTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.apache.log4j.Appender;
030import org.apache.log4j.Level;
031import org.apache.log4j.LogManager;
032import org.apache.log4j.spi.LoggingEvent;
033import org.junit.After;
034import org.junit.Before;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038import org.junit.runner.RunWith;
039import org.mockito.ArgumentCaptor;
040import org.mockito.Captor;
041import org.mockito.Mock;
042import org.mockito.Mockito;
043import org.mockito.runners.MockitoJUnitRunner;
044
045@RunWith(MockitoJUnitRunner.class)
046@Category({ ClientTests.class, SmallTests.class })
047public class TestFailedServersLog {
048
049  @ClassRule
050  public static final HBaseClassTestRule CLASS_RULE =
051      HBaseClassTestRule.forClass(TestFailedServersLog.class);
052
053  static final int TEST_PORT = 9999;
054  private InetSocketAddress addr;
055
056  @Mock
057  private Appender mockAppender;
058
059  @Captor
060  private ArgumentCaptor captorLoggingEvent;
061
062  @Before
063  public void setup() {
064    LogManager.getRootLogger().addAppender(mockAppender);
065  }
066
067  @After
068  public void teardown() {
069    LogManager.getRootLogger().removeAppender(mockAppender);
070  }
071
072  @Test
073  public void testAddToFailedServersLogging() {
074    Throwable nullException = new NullPointerException();
075
076    FailedServers fs = new FailedServers(new Configuration());
077    addr = new InetSocketAddress(TEST_PORT);
078
079    fs.addToFailedServers(addr, nullException);
080
081    Mockito.verify(mockAppender).doAppend((LoggingEvent) captorLoggingEvent.capture());
082    LoggingEvent loggingEvent = (LoggingEvent) captorLoggingEvent.getValue();
083    assertThat(loggingEvent.getLevel(), is(Level.DEBUG));
084    assertEquals("Added failed server with address " + addr.toString() + " to list caused by "
085        + nullException.toString(),
086      loggingEvent.getRenderedMessage());
087  }
088
089}