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.field;
19  
20  import static org.hamcrest.CoreMatchers.is;
21  import static org.junit.Assert.assertThat;
22  import static org.junit.Assert.fail;
23  
24  import org.apache.hadoop.hbase.testclassification.SmallTests;
25  import org.junit.Test;
26  import org.junit.experimental.categories.Category;
27  
28  @Category(SmallTests.class)
29  public class TestFieldValue {
30  
31    @Test
32    public void testParseAndAsSomethingMethod() {
33      // String
34      FieldValue stringFieldValue = new FieldValue("aaa", FieldValueType.STRING);
35      assertThat(stringFieldValue.asString(), is("aaa"));
36  
37      try {
38        new FieldValue(1, FieldValueType.STRING);
39        fail();
40      } catch (IllegalArgumentException ignored) {
41      }
42  
43      // Integer
44      FieldValue integerFieldValue = new FieldValue(100, FieldValueType.INTEGER);
45      assertThat(integerFieldValue.asInt(), is(100));
46  
47      integerFieldValue = new FieldValue("100", FieldValueType.INTEGER);
48      assertThat(integerFieldValue.asInt(), is(100));
49  
50      try {
51        new FieldValue("aaa", FieldValueType.INTEGER);
52        fail();
53      } catch (IllegalArgumentException ignored) {
54      }
55  
56      // Long
57      FieldValue longFieldValue = new FieldValue(100L, FieldValueType.LONG);
58      assertThat(longFieldValue.asLong(), is(100L));
59  
60      longFieldValue = new FieldValue("100", FieldValueType.LONG);
61      assertThat(longFieldValue.asLong(), is(100L));
62  
63      try {
64        new FieldValue("aaa", FieldValueType.LONG);
65        fail();
66      } catch (IllegalArgumentException ignored) {
67      }
68  
69      try {
70        new FieldValue(100, FieldValueType.LONG);
71        fail();
72      } catch (IllegalArgumentException ignored) {
73      }
74  
75      // Float
76      FieldValue floatFieldValue = new FieldValue(1.0f, FieldValueType.FLOAT);
77      assertThat(floatFieldValue.asFloat(), is(1.0f));
78  
79      floatFieldValue = new FieldValue("1", FieldValueType.FLOAT);
80      assertThat(floatFieldValue.asFloat(), is(1.0f));
81  
82      try {
83        new FieldValue("aaa", FieldValueType.FLOAT);
84        fail();
85      } catch (IllegalArgumentException ignored) {
86      }
87  
88      try {
89        new FieldValue(1, FieldValueType.FLOAT);
90        fail();
91      } catch (IllegalArgumentException ignored) {
92      }
93  
94      // Size
95      FieldValue sizeFieldValue =
96        new FieldValue(new Size(100, Size.Unit.MEGABYTE), FieldValueType.SIZE);
97      assertThat(sizeFieldValue.asString(), is("100.0MB"));
98      assertThat(sizeFieldValue.asSize(), is(new Size(100, Size.Unit.MEGABYTE)));
99  
100     sizeFieldValue = new FieldValue("100MB", FieldValueType.SIZE);
101     assertThat(sizeFieldValue.asString(), is("100.0MB"));
102     assertThat(sizeFieldValue.asSize(), is(new Size(100, Size.Unit.MEGABYTE)));
103 
104     try {
105       new FieldValue("100", FieldValueType.SIZE);
106       fail();
107     } catch (IllegalArgumentException ignored) {
108     }
109 
110     try {
111       new FieldValue(100, FieldValueType.SIZE);
112       fail();
113     } catch (IllegalArgumentException ignored) {
114     }
115 
116     // Percent
117     FieldValue percentFieldValue =
118       new FieldValue(100f, FieldValueType.PERCENT);
119     assertThat(percentFieldValue.asString(), is("100.00%"));
120     assertThat(percentFieldValue.asFloat(), is(100f));
121 
122     percentFieldValue = new FieldValue("100%", FieldValueType.PERCENT);
123     assertThat(percentFieldValue.asString(), is("100.00%"));
124     assertThat(percentFieldValue.asFloat(), is(100f));
125 
126     percentFieldValue = new FieldValue("100", FieldValueType.PERCENT);
127     assertThat(percentFieldValue.asString(), is("100.00%"));
128     assertThat(percentFieldValue.asFloat(), is(100f));
129 
130     try {
131       new FieldValue(100, FieldValueType.PERCENT);
132       fail();
133     } catch (IllegalArgumentException ignored) {
134     }
135   }
136 
137   @Test
138   public void testCompareTo() {
139     // String
140     FieldValue stringAFieldValue = new FieldValue("a", FieldValueType.STRING);
141     FieldValue stringAFieldValue2 = new FieldValue("a", FieldValueType.STRING);
142     FieldValue stringBFieldValue = new FieldValue("b", FieldValueType.STRING);
143     FieldValue stringCapitalAFieldValue = new FieldValue("A", FieldValueType.STRING);
144 
145     assertThat(stringAFieldValue.compareTo(stringAFieldValue2), is(0));
146     assertThat(stringBFieldValue.compareTo(stringAFieldValue), is(1));
147     assertThat(stringAFieldValue.compareTo(stringBFieldValue), is(-1));
148     assertThat(stringAFieldValue.compareTo(stringCapitalAFieldValue), is(32));
149 
150     // Integer
151     FieldValue integer1FieldValue = new FieldValue(1, FieldValueType.INTEGER);
152     FieldValue integer1FieldValue2 = new FieldValue(1, FieldValueType.INTEGER);
153     FieldValue integer2FieldValue = new FieldValue(2, FieldValueType.INTEGER);
154 
155     assertThat(integer1FieldValue.compareTo(integer1FieldValue2), is(0));
156     assertThat(integer2FieldValue.compareTo(integer1FieldValue), is(1));
157     assertThat(integer1FieldValue.compareTo(integer2FieldValue), is(-1));
158 
159     // Long
160     FieldValue long1FieldValue = new FieldValue(1L, FieldValueType.LONG);
161     FieldValue long1FieldValue2 = new FieldValue(1L, FieldValueType.LONG);
162     FieldValue long2FieldValue = new FieldValue(2L, FieldValueType.LONG);
163 
164     assertThat(long1FieldValue.compareTo(long1FieldValue2), is(0));
165     assertThat(long2FieldValue.compareTo(long1FieldValue), is(1));
166     assertThat(long1FieldValue.compareTo(long2FieldValue), is(-1));
167 
168     // Float
169     FieldValue float1FieldValue = new FieldValue(1.0f, FieldValueType.FLOAT);
170     FieldValue float1FieldValue2 = new FieldValue(1.0f, FieldValueType.FLOAT);
171     FieldValue float2FieldValue = new FieldValue(2.0f, FieldValueType.FLOAT);
172 
173     assertThat(float1FieldValue.compareTo(float1FieldValue2), is(0));
174     assertThat(float2FieldValue.compareTo(float1FieldValue), is(1));
175     assertThat(float1FieldValue.compareTo(float2FieldValue), is(-1));
176 
177     // Size
178     FieldValue size100MBFieldValue =
179       new FieldValue(new Size(100, Size.Unit.MEGABYTE), FieldValueType.SIZE);
180     FieldValue size100MBFieldValue2 =
181       new FieldValue(new Size(100, Size.Unit.MEGABYTE), FieldValueType.SIZE);
182     FieldValue size200MBFieldValue =
183       new FieldValue(new Size(200, Size.Unit.MEGABYTE), FieldValueType.SIZE);
184 
185     assertThat(size100MBFieldValue.compareTo(size100MBFieldValue2), is(0));
186     assertThat(size200MBFieldValue.compareTo(size100MBFieldValue), is(1));
187     assertThat(size100MBFieldValue.compareTo(size200MBFieldValue), is(-1));
188 
189     // Percent
190     FieldValue percent50FieldValue = new FieldValue(50.0f, FieldValueType.PERCENT);
191     FieldValue percent50FieldValue2 = new FieldValue(50.0f, FieldValueType.PERCENT);
192     FieldValue percent100FieldValue = new FieldValue(100.0f, FieldValueType.PERCENT);
193 
194     assertThat(percent50FieldValue.compareTo(percent50FieldValue2), is(0));
195     assertThat(percent100FieldValue.compareTo(percent50FieldValue), is(1));
196     assertThat(percent50FieldValue.compareTo(percent100FieldValue), is(-1));
197   }
198 
199   @Test
200   public void testPlus() {
201     // String
202     FieldValue stringFieldValue = new FieldValue("a", FieldValueType.STRING);
203     FieldValue stringFieldValue2 = new FieldValue("b", FieldValueType.STRING);
204     assertThat(stringFieldValue.plus(stringFieldValue2).asString(), is("ab"));
205 
206     // Integer
207     FieldValue integerFieldValue = new FieldValue(1, FieldValueType.INTEGER);
208     FieldValue integerFieldValue2 = new FieldValue(2, FieldValueType.INTEGER);
209     assertThat(integerFieldValue.plus(integerFieldValue2).asInt(), is(3));
210 
211     // Long
212     FieldValue longFieldValue = new FieldValue(1L, FieldValueType.LONG);
213     FieldValue longFieldValue2 = new FieldValue(2L, FieldValueType.LONG);
214     assertThat(longFieldValue.plus(longFieldValue2).asLong(), is(3L));
215 
216     // Float
217     FieldValue floatFieldValue = new FieldValue(1.2f, FieldValueType.FLOAT);
218     FieldValue floatFieldValue2 = new FieldValue(2.2f, FieldValueType.FLOAT);
219     assertThat(floatFieldValue.plus(floatFieldValue2).asFloat(), is(3.4f));
220 
221     // Size
222     FieldValue sizeFieldValue =
223       new FieldValue(new Size(100, Size.Unit.MEGABYTE), FieldValueType.SIZE);
224     FieldValue sizeFieldValue2 =
225       new FieldValue(new Size(200, Size.Unit.MEGABYTE), FieldValueType.SIZE);
226     assertThat(sizeFieldValue.plus(sizeFieldValue2).asString(), is("300.0MB"));
227     assertThat(sizeFieldValue.plus(sizeFieldValue2).asSize(),
228       is(new Size(300, Size.Unit.MEGABYTE)));
229 
230     // Percent
231     FieldValue percentFieldValue = new FieldValue(30f, FieldValueType.PERCENT);
232     FieldValue percentFieldValue2 = new FieldValue(60f, FieldValueType.PERCENT);
233     assertThat(percentFieldValue.plus(percentFieldValue2).asString(), is("90.00%"));
234     assertThat(percentFieldValue.plus(percentFieldValue2).asFloat(), is(90f));
235   }
236 
237   @Test
238   public void testCompareToIgnoreCase() {
239     FieldValue stringAFieldValue = new FieldValue("a", FieldValueType.STRING);
240     FieldValue stringCapitalAFieldValue = new FieldValue("A", FieldValueType.STRING);
241     FieldValue stringCapitalBFieldValue = new FieldValue("B", FieldValueType.STRING);
242 
243     assertThat(stringAFieldValue.compareToIgnoreCase(stringCapitalAFieldValue), is(0));
244     assertThat(stringCapitalBFieldValue.compareToIgnoreCase(stringAFieldValue), is(1));
245     assertThat(stringAFieldValue.compareToIgnoreCase(stringCapitalBFieldValue), is(-1));
246   }
247 
248   @Test
249   public void testOptimizeSize() {
250     FieldValue sizeFieldValue =
251       new FieldValue(new Size(1, Size.Unit.BYTE), FieldValueType.SIZE);
252     assertThat(sizeFieldValue.asString(), is("1.0B"));
253 
254     sizeFieldValue =
255       new FieldValue(new Size(1024, Size.Unit.BYTE), FieldValueType.SIZE);
256     assertThat(sizeFieldValue.asString(), is("1.0KB"));
257 
258     sizeFieldValue =
259       new FieldValue(new Size(2 * 1024, Size.Unit.BYTE), FieldValueType.SIZE);
260     assertThat(sizeFieldValue.asString(), is("2.0KB"));
261 
262     sizeFieldValue =
263       new FieldValue(new Size(2 * 1024, Size.Unit.KILOBYTE), FieldValueType.SIZE);
264     assertThat(sizeFieldValue.asString(), is("2.0MB"));
265 
266     sizeFieldValue =
267       new FieldValue(new Size(1024 * 1024, Size.Unit.KILOBYTE), FieldValueType.SIZE);
268     assertThat(sizeFieldValue.asString(), is("1.0GB"));
269 
270     sizeFieldValue =
271       new FieldValue(new Size(2 * 1024 * 1024, Size.Unit.MEGABYTE), FieldValueType.SIZE);
272     assertThat(sizeFieldValue.asString(), is("2.0TB"));
273 
274     sizeFieldValue =
275       new FieldValue(new Size(2 * 1024, Size.Unit.TERABYTE), FieldValueType.SIZE);
276     assertThat(sizeFieldValue.asString(), is("2.0PB"));
277 
278     sizeFieldValue =
279       new FieldValue(new Size(1024 * 1024, Size.Unit.TERABYTE), FieldValueType.SIZE);
280     assertThat(sizeFieldValue.asString(), is("1024.0PB"));
281 
282     sizeFieldValue =
283       new FieldValue(new Size(1, Size.Unit.PETABYTE), FieldValueType.SIZE);
284     assertThat(sizeFieldValue.asString(), is("1.0PB"));
285 
286     sizeFieldValue =
287       new FieldValue(new Size(1024, Size.Unit.PETABYTE), FieldValueType.SIZE);
288     assertThat(sizeFieldValue.asString(), is("1024.0PB"));
289   }
290 }