View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional information regarding
4    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
8    * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
9    * for the specific language governing permissions and limitations under the License.
10   */
11  
12  package org.apache.hadoop.hbase.ipc;
13  
14  import static org.hamcrest.CoreMatchers.is;
15  import static org.junit.Assert.assertEquals;
16  import static org.junit.Assert.assertThat;
17  
18  import java.net.InetSocketAddress;
19  
20  import org.apache.hadoop.conf.Configuration;
21  import org.apache.hadoop.hbase.testclassification.ClientTests;
22  import org.apache.hadoop.hbase.testclassification.SmallTests;
23  import org.apache.log4j.Appender;
24  import org.apache.log4j.Level;
25  import org.apache.log4j.LogManager;
26  import org.apache.log4j.spi.LoggingEvent;
27  import org.junit.After;
28  import org.junit.Before;
29  import org.junit.Test;
30  import org.junit.experimental.categories.Category;
31  import org.junit.runner.RunWith;
32  import org.mockito.ArgumentCaptor;
33  import org.mockito.Captor;
34  import org.mockito.Mock;
35  import org.mockito.Mockito;
36  import org.mockito.runners.MockitoJUnitRunner;
37  
38  @RunWith(MockitoJUnitRunner.class)
39  @Category({ ClientTests.class, SmallTests.class })
40  public class TestFailedServersLog {
41    static final int TEST_PORT = 9999;
42    private InetSocketAddress addr;
43  
44    @Mock
45    private Appender mockAppender;
46  
47    @Captor
48    private ArgumentCaptor captorLoggingEvent;
49  
50    @Before
51    public void setup() {
52      LogManager.getRootLogger().addAppender(mockAppender);
53    }
54  
55    @After
56    public void teardown() {
57      LogManager.getRootLogger().removeAppender(mockAppender);
58    }
59  
60    @Test
61    public void testAddToFailedServersLogging() {
62      Throwable nullException = new NullPointerException();
63  
64      FailedServers fs = new FailedServers(new Configuration());
65      addr = new InetSocketAddress(TEST_PORT);
66  
67      fs.addToFailedServers(addr, nullException);
68  
69      Mockito.verify(mockAppender).doAppend((LoggingEvent) captorLoggingEvent.capture());
70      LoggingEvent loggingEvent = (LoggingEvent) captorLoggingEvent.getValue();
71      assertThat(loggingEvent.getLevel(), is(Level.DEBUG));
72      assertEquals("Added failed server with address " + addr.toString() + " to list caused by "
73          + nullException.toString(),
74        loggingEvent.getRenderedMessage());
75    }
76  
77  }