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 org.apache.hadoop.conf.Configuration;
19  import org.apache.hadoop.hbase.net.Address;
20  import org.apache.hadoop.hbase.testclassification.ClientTests;
21  import org.apache.hadoop.hbase.testclassification.SmallTests;
22  import org.apache.log4j.Appender;
23  import org.apache.log4j.Level;
24  import org.apache.log4j.LogManager;
25  import org.apache.log4j.spi.LoggingEvent;
26  import org.junit.After;
27  import org.junit.Before;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  import org.junit.runner.RunWith;
31  import org.mockito.ArgumentCaptor;
32  import org.mockito.Captor;
33  import org.mockito.Mock;
34  import org.mockito.Mockito;
35  import org.mockito.runners.MockitoJUnitRunner;
36  
37  @RunWith(MockitoJUnitRunner.class)
38  @Category({ ClientTests.class, SmallTests.class })
39  public class TestFailedServersLog {
40    static final int TEST_PORT = 9999;
41    private Address addr;
42  
43    @Mock
44    private Appender mockAppender;
45  
46    @Captor
47    private ArgumentCaptor captorLoggingEvent;
48  
49    @Before
50    public void setup() {
51      LogManager.getRootLogger().addAppender(mockAppender);
52    }
53  
54    @After
55    public void teardown() {
56      LogManager.getRootLogger().removeAppender(mockAppender);
57    }
58  
59    @Test
60    public void testAddToFailedServersLogging() {
61      Throwable nullException = new NullPointerException();
62  
63      FailedServers fs = new FailedServers(new Configuration());
64      addr = Address.fromParts("localhost", TEST_PORT);
65  
66      fs.addToFailedServers(addr, nullException);
67  
68      Mockito.verify(mockAppender).doAppend((LoggingEvent) captorLoggingEvent.capture());
69      LoggingEvent loggingEvent = (LoggingEvent) captorLoggingEvent.getValue();
70      assertThat(loggingEvent.getLevel(), is(Level.DEBUG));
71      assertEquals("Added failed server with address " + addr + " to list caused by "
72          + nullException.toString(),
73        loggingEvent.getRenderedMessage());
74    }
75  
76  }