View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import static org.mockito.Matchers.*;
22  import static org.mockito.Mockito.*;
23  
24  import org.apache.hadoop.hbase.regionserver.compactions.CompactionContext;
25  import org.apache.hadoop.hbase.regionserver.compactions.CompactionRequest;
26  import org.apache.hadoop.hbase.security.User;
27  import org.mockito.invocation.InvocationOnMock;
28  import org.mockito.stubbing.Answer;
29  
30  /**
31   * This class is a helper that allows to create a partially-implemented, stateful mocks of
32   * Store. It contains a bunch of blank methods, and answers redirecting to these.
33   */
34  public class StatefulStoreMockMaker {
35    // Add and expand the methods and answers as needed.
36    public CompactionContext selectCompaction() { return null; }
37    public void cancelCompaction(Object originalContext) {}
38    public int getPriority() { return 0; }
39  
40    private class SelectAnswer implements Answer<CompactionContext> {
41      public CompactionContext answer(InvocationOnMock invocation) throws Throwable {
42        return selectCompaction();
43      }
44    }
45    private class PriorityAnswer implements Answer<Integer> {
46      public Integer answer(InvocationOnMock invocation) throws Throwable {
47        return getPriority();
48      }
49    }
50    private class CancelAnswer implements Answer<Object> {
51      public CompactionContext answer(InvocationOnMock invocation) throws Throwable {
52        cancelCompaction(invocation.getArguments()[0]); return null;
53      }
54    }
55  
56    public Store createStoreMock(String name) throws Exception {
57      Store store = mock(Store.class, name);
58      when(store.requestCompaction(
59          anyInt(), isNull(CompactionRequest.class))).then(new SelectAnswer());
60      when(store.requestCompaction(
61        anyInt(), isNull(CompactionRequest.class), any(User.class))).then(new SelectAnswer());
62      when(store.getCompactPriority()).then(new PriorityAnswer());
63      doAnswer(new CancelAnswer()).when(
64          store).cancelRequestedCompaction(any(CompactionContext.class));
65      return store;
66    }
67  }