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.mockito.invocation.InvocationOnMock;
27  import org.mockito.stubbing.Answer;
28  
29  /**
30   * This class is a helper that allows to create a partially-implemented, stateful mocks of
31   * Store. It contains a bunch of blank methods, and answers redirecting to these.
32   */
33  public class StatefulStoreMockMaker {
34    // Add and expand the methods and answers as needed.
35    public CompactionContext selectCompaction() { return null; }
36    public void cancelCompaction(Object originalContext) {}
37    public int getPriority() { return 0; }
38  
39    private class SelectAnswer implements Answer<CompactionContext> {
40      public CompactionContext answer(InvocationOnMock invocation) throws Throwable {
41        return selectCompaction();
42      }
43    }
44    private class PriorityAnswer implements Answer<Integer> {
45      public Integer answer(InvocationOnMock invocation) throws Throwable {
46        return getPriority();
47      }
48    }
49    private class CancelAnswer implements Answer<Object> {
50      public CompactionContext answer(InvocationOnMock invocation) throws Throwable {
51        cancelCompaction(invocation.getArguments()[0]); return null;
52      }
53    }
54  
55    public Store createStoreMock(String name) throws Exception {
56      Store store = mock(Store.class, name);
57      when(store.requestCompaction(
58          anyInt(), isNull(CompactionRequest.class))).then(new SelectAnswer());
59      when(store.getCompactPriority()).then(new PriorityAnswer());
60      doAnswer(new CancelAnswer()).when(
61          store).cancelRequestedCompaction(any(CompactionContext.class));
62      return store;
63    }
64  }