View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.hbtop.screen.top;
19  
20  import static org.hamcrest.CoreMatchers.is;
21  import static org.junit.Assert.assertThat;
22  import static org.mockito.Mockito.any;
23  import static org.mockito.Mockito.eq;
24  import static org.mockito.Mockito.inOrder;
25  import static org.mockito.Mockito.verify;
26  import static org.mockito.Mockito.when;
27  
28  import java.util.ArrayList;
29  import java.util.List;
30  import org.apache.hadoop.hbase.hbtop.screen.ScreenView;
31  import org.apache.hadoop.hbase.testclassification.SmallTests;
32  import org.junit.Before;
33  import org.junit.Test;
34  import org.junit.experimental.categories.Category;
35  import org.junit.runner.RunWith;
36  import org.mockito.InOrder;
37  import org.mockito.Mock;
38  import org.mockito.runners.MockitoJUnitRunner;
39  
40  @Category(SmallTests.class)
41  @RunWith(MockitoJUnitRunner.class)
42  public class TestInputModeScreenPresenter {
43  
44    private static final String TEST_INPUT_MESSAGE = "test input message";
45  
46    @Mock
47    private InputModeScreenView inputModeScreenView;
48  
49    @Mock
50    private TopScreenView topScreenView;
51  
52    @Mock
53    private InputModeScreenPresenter.ResultListener resultListener;
54  
55    private InputModeScreenPresenter inputModeScreenPresenter;
56  
57    @Before
58    public void setup() {
59      List<String> histories = new ArrayList<>();
60      histories.add("history1");
61      histories.add("history2");
62  
63      inputModeScreenPresenter = new InputModeScreenPresenter(inputModeScreenView,
64        TEST_INPUT_MESSAGE, histories, resultListener);
65    }
66  
67    @Test
68    public void testInit() {
69      inputModeScreenPresenter.init();
70  
71      verify(inputModeScreenView).showInput(eq(TEST_INPUT_MESSAGE), eq(""), eq(0));
72    }
73  
74    @Test
75    public void testCharacter() {
76      inputModeScreenPresenter.character('a');
77      inputModeScreenPresenter.character('b');
78      inputModeScreenPresenter.character('c');
79  
80      InOrder inOrder = inOrder(inputModeScreenView);
81      inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("a"), eq(1));
82      inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("ab"), eq(2));
83      inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("abc"), eq(3));
84    }
85  
86    @Test
87    public void testArrowLeftAndRight() {
88      inputModeScreenPresenter.character('a');
89      inputModeScreenPresenter.character('b');
90      inputModeScreenPresenter.character('c');
91      inputModeScreenPresenter.arrowLeft();
92      inputModeScreenPresenter.arrowLeft();
93      inputModeScreenPresenter.arrowLeft();
94      inputModeScreenPresenter.arrowLeft();
95      inputModeScreenPresenter.arrowRight();
96      inputModeScreenPresenter.arrowRight();
97      inputModeScreenPresenter.arrowRight();
98      inputModeScreenPresenter.arrowRight();
99  
100     InOrder inOrder = inOrder(inputModeScreenView);
101     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("a"), eq(1));
102     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("ab"), eq(2));
103     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("abc"), eq(3));
104     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("abc"), eq(2));
105     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("abc"), eq(1));
106     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("abc"), eq(0));
107     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("abc"), eq(1));
108     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("abc"), eq(2));
109     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("abc"), eq(3));
110   }
111 
112   @Test
113   public void testHomeAndEnd() {
114     inputModeScreenPresenter.character('a');
115     inputModeScreenPresenter.character('b');
116     inputModeScreenPresenter.character('c');
117     inputModeScreenPresenter.home();
118     inputModeScreenPresenter.home();
119     inputModeScreenPresenter.end();
120     inputModeScreenPresenter.end();
121 
122     InOrder inOrder = inOrder(inputModeScreenView);
123     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("a"), eq(1));
124     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("ab"), eq(2));
125     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("abc"), eq(3));
126     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("abc"), eq(0));
127     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("abc"), eq(3));
128   }
129 
130   @Test
131   public void testBackspace() {
132     inputModeScreenPresenter.character('a');
133     inputModeScreenPresenter.character('b');
134     inputModeScreenPresenter.character('c');
135     inputModeScreenPresenter.backspace();
136     inputModeScreenPresenter.backspace();
137     inputModeScreenPresenter.backspace();
138     inputModeScreenPresenter.backspace();
139 
140     InOrder inOrder = inOrder(inputModeScreenView);
141     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("a"), eq(1));
142     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("ab"), eq(2));
143     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("abc"), eq(3));
144     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("ab"), eq(2));
145     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("a"), eq(1));
146     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq(""), eq(0));
147   }
148 
149   @Test
150   public void testDelete() {
151     inputModeScreenPresenter.character('a');
152     inputModeScreenPresenter.character('b');
153     inputModeScreenPresenter.character('c');
154     inputModeScreenPresenter.delete();
155     inputModeScreenPresenter.arrowLeft();
156     inputModeScreenPresenter.delete();
157 
158     InOrder inOrder = inOrder(inputModeScreenView);
159     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("a"), eq(1));
160     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("ab"), eq(2));
161     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("abc"), eq(3));
162     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("abc"), eq(2));
163     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("ab"), eq(2));
164   }
165 
166   @Test
167   public void testHistories() {
168     inputModeScreenPresenter.character('a');
169     inputModeScreenPresenter.character('b');
170     inputModeScreenPresenter.character('c');
171     inputModeScreenPresenter.arrowUp();
172     inputModeScreenPresenter.arrowUp();
173     inputModeScreenPresenter.arrowUp();
174     inputModeScreenPresenter.arrowDown();
175     inputModeScreenPresenter.arrowDown();
176     inputModeScreenPresenter.arrowDown();
177 
178     InOrder inOrder = inOrder(inputModeScreenView);
179     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("a"), eq(1));
180     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("ab"), eq(2));
181     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("abc"), eq(3));
182     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("history2"), eq(8));
183     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("history1"), eq(8));
184     inOrder.verify(inputModeScreenView).showInput(any(String.class), eq("history2"), eq(8));
185   }
186 
187   @Test
188   public void testReturnToTopScreen() {
189     when(resultListener.apply(any(String.class))).thenReturn(topScreenView);
190 
191     inputModeScreenPresenter.character('a');
192     inputModeScreenPresenter.character('b');
193     inputModeScreenPresenter.character('c');
194 
195     assertThat(inputModeScreenPresenter.returnToNextScreen(), is((ScreenView) topScreenView));
196     verify(resultListener).apply(eq("abc"));
197   }
198 }