001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.hbtop.field;
019
020import static org.hamcrest.CoreMatchers.is;
021import static org.junit.Assert.assertThat;
022import static org.junit.Assert.fail;
023
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.Size;
026import org.apache.hadoop.hbase.testclassification.SmallTests;
027import org.junit.ClassRule;
028import org.junit.Test;
029import org.junit.experimental.categories.Category;
030
031
032@Category(SmallTests.class)
033public class TestFieldValue {
034
035  @ClassRule
036  public static final HBaseClassTestRule CLASS_RULE =
037    HBaseClassTestRule.forClass(TestFieldValue.class);
038
039  @Test
040  public void testParseAndAsSomethingMethod() {
041    // String
042    FieldValue stringFieldValue = new FieldValue("aaa", FieldValueType.STRING);
043    assertThat(stringFieldValue.asString(), is("aaa"));
044
045    try {
046      new FieldValue(1, FieldValueType.STRING);
047      fail();
048    } catch (IllegalArgumentException ignored) {
049    }
050
051    // Integer
052    FieldValue integerFieldValue = new FieldValue(100, FieldValueType.INTEGER);
053    assertThat(integerFieldValue.asInt(), is(100));
054
055    integerFieldValue = new FieldValue("100", FieldValueType.INTEGER);
056    assertThat(integerFieldValue.asInt(), is(100));
057
058    try {
059      new FieldValue("aaa", FieldValueType.INTEGER);
060      fail();
061    } catch (IllegalArgumentException ignored) {
062    }
063
064    // Long
065    FieldValue longFieldValue = new FieldValue(100L, FieldValueType.LONG);
066    assertThat(longFieldValue.asLong(), is(100L));
067
068    longFieldValue = new FieldValue("100", FieldValueType.LONG);
069    assertThat(longFieldValue.asLong(), is(100L));
070
071    try {
072      new FieldValue("aaa", FieldValueType.LONG);
073      fail();
074    } catch (IllegalArgumentException ignored) {
075    }
076
077    try {
078      new FieldValue(100, FieldValueType.LONG);
079      fail();
080    } catch (IllegalArgumentException ignored) {
081    }
082
083    // Float
084    FieldValue floatFieldValue = new FieldValue(1.0f, FieldValueType.FLOAT);
085    assertThat(floatFieldValue.asFloat(), is(1.0f));
086
087    floatFieldValue = new FieldValue("1", FieldValueType.FLOAT);
088    assertThat(floatFieldValue.asFloat(), is(1.0f));
089
090    try {
091      new FieldValue("aaa", FieldValueType.FLOAT);
092      fail();
093    } catch (IllegalArgumentException ignored) {
094    }
095
096    try {
097      new FieldValue(1, FieldValueType.FLOAT);
098      fail();
099    } catch (IllegalArgumentException ignored) {
100    }
101
102    // Size
103    FieldValue sizeFieldValue =
104      new FieldValue(new Size(100, Size.Unit.MEGABYTE), FieldValueType.SIZE);
105    assertThat(sizeFieldValue.asString(), is("100.0MB"));
106    assertThat(sizeFieldValue.asSize(), is(new Size(100, Size.Unit.MEGABYTE)));
107
108    sizeFieldValue = new FieldValue("100MB", FieldValueType.SIZE);
109    assertThat(sizeFieldValue.asString(), is("100.0MB"));
110    assertThat(sizeFieldValue.asSize(), is(new Size(100, Size.Unit.MEGABYTE)));
111
112    try {
113      new FieldValue("100", FieldValueType.SIZE);
114      fail();
115    } catch (IllegalArgumentException ignored) {
116    }
117
118    try {
119      new FieldValue(100, FieldValueType.SIZE);
120      fail();
121    } catch (IllegalArgumentException ignored) {
122    }
123
124    // Percent
125    FieldValue percentFieldValue =
126      new FieldValue(100f, FieldValueType.PERCENT);
127    assertThat(percentFieldValue.asString(), is("100.00%"));
128    assertThat(percentFieldValue.asFloat(), is(100f));
129
130    percentFieldValue = new FieldValue("100%", FieldValueType.PERCENT);
131    assertThat(percentFieldValue.asString(), is("100.00%"));
132    assertThat(percentFieldValue.asFloat(), is(100f));
133
134    percentFieldValue = new FieldValue("100", FieldValueType.PERCENT);
135    assertThat(percentFieldValue.asString(), is("100.00%"));
136    assertThat(percentFieldValue.asFloat(), is(100f));
137
138    try {
139      new FieldValue(100, FieldValueType.PERCENT);
140      fail();
141    } catch (IllegalArgumentException ignored) {
142    }
143  }
144
145  @Test
146  public void testCompareTo() {
147    // String
148    FieldValue stringAFieldValue = new FieldValue("a", FieldValueType.STRING);
149    FieldValue stringAFieldValue2 = new FieldValue("a", FieldValueType.STRING);
150    FieldValue stringBFieldValue = new FieldValue("b", FieldValueType.STRING);
151    FieldValue stringCapitalAFieldValue = new FieldValue("A", FieldValueType.STRING);
152
153    assertThat(stringAFieldValue.compareTo(stringAFieldValue2), is(0));
154    assertThat(stringBFieldValue.compareTo(stringAFieldValue), is(1));
155    assertThat(stringAFieldValue.compareTo(stringBFieldValue), is(-1));
156    assertThat(stringAFieldValue.compareTo(stringCapitalAFieldValue), is(32));
157
158    // Integer
159    FieldValue integer1FieldValue = new FieldValue(1, FieldValueType.INTEGER);
160    FieldValue integer1FieldValue2 = new FieldValue(1, FieldValueType.INTEGER);
161    FieldValue integer2FieldValue = new FieldValue(2, FieldValueType.INTEGER);
162
163    assertThat(integer1FieldValue.compareTo(integer1FieldValue2), is(0));
164    assertThat(integer2FieldValue.compareTo(integer1FieldValue), is(1));
165    assertThat(integer1FieldValue.compareTo(integer2FieldValue), is(-1));
166
167    // Long
168    FieldValue long1FieldValue = new FieldValue(1L, FieldValueType.LONG);
169    FieldValue long1FieldValue2 = new FieldValue(1L, FieldValueType.LONG);
170    FieldValue long2FieldValue = new FieldValue(2L, FieldValueType.LONG);
171
172    assertThat(long1FieldValue.compareTo(long1FieldValue2), is(0));
173    assertThat(long2FieldValue.compareTo(long1FieldValue), is(1));
174    assertThat(long1FieldValue.compareTo(long2FieldValue), is(-1));
175
176    // Float
177    FieldValue float1FieldValue = new FieldValue(1.0f, FieldValueType.FLOAT);
178    FieldValue float1FieldValue2 = new FieldValue(1.0f, FieldValueType.FLOAT);
179    FieldValue float2FieldValue = new FieldValue(2.0f, FieldValueType.FLOAT);
180
181    assertThat(float1FieldValue.compareTo(float1FieldValue2), is(0));
182    assertThat(float2FieldValue.compareTo(float1FieldValue), is(1));
183    assertThat(float1FieldValue.compareTo(float2FieldValue), is(-1));
184
185    // Size
186    FieldValue size100MBFieldValue =
187      new FieldValue(new Size(100, Size.Unit.MEGABYTE), FieldValueType.SIZE);
188    FieldValue size100MBFieldValue2 =
189      new FieldValue(new Size(100, Size.Unit.MEGABYTE), FieldValueType.SIZE);
190    FieldValue size200MBFieldValue =
191      new FieldValue(new Size(200, Size.Unit.MEGABYTE), FieldValueType.SIZE);
192
193    assertThat(size100MBFieldValue.compareTo(size100MBFieldValue2), is(0));
194    assertThat(size200MBFieldValue.compareTo(size100MBFieldValue), is(1));
195    assertThat(size100MBFieldValue.compareTo(size200MBFieldValue), is(-1));
196
197    // Percent
198    FieldValue percent50FieldValue = new FieldValue(50.0f, FieldValueType.PERCENT);
199    FieldValue percent50FieldValue2 = new FieldValue(50.0f, FieldValueType.PERCENT);
200    FieldValue percent100FieldValue = new FieldValue(100.0f, FieldValueType.PERCENT);
201
202    assertThat(percent50FieldValue.compareTo(percent50FieldValue2), is(0));
203    assertThat(percent100FieldValue.compareTo(percent50FieldValue), is(1));
204    assertThat(percent50FieldValue.compareTo(percent100FieldValue), is(-1));
205  }
206
207  @Test
208  public void testPlus() {
209    // String
210    FieldValue stringFieldValue = new FieldValue("a", FieldValueType.STRING);
211    FieldValue stringFieldValue2 = new FieldValue("b", FieldValueType.STRING);
212    assertThat(stringFieldValue.plus(stringFieldValue2).asString(), is("ab"));
213
214    // Integer
215    FieldValue integerFieldValue = new FieldValue(1, FieldValueType.INTEGER);
216    FieldValue integerFieldValue2 = new FieldValue(2, FieldValueType.INTEGER);
217    assertThat(integerFieldValue.plus(integerFieldValue2).asInt(), is(3));
218
219    // Long
220    FieldValue longFieldValue = new FieldValue(1L, FieldValueType.LONG);
221    FieldValue longFieldValue2 = new FieldValue(2L, FieldValueType.LONG);
222    assertThat(longFieldValue.plus(longFieldValue2).asLong(), is(3L));
223
224    // Float
225    FieldValue floatFieldValue = new FieldValue(1.2f, FieldValueType.FLOAT);
226    FieldValue floatFieldValue2 = new FieldValue(2.2f, FieldValueType.FLOAT);
227    assertThat(floatFieldValue.plus(floatFieldValue2).asFloat(), is(3.4f));
228
229    // Size
230    FieldValue sizeFieldValue =
231      new FieldValue(new Size(100, Size.Unit.MEGABYTE), FieldValueType.SIZE);
232    FieldValue sizeFieldValue2 =
233      new FieldValue(new Size(200, Size.Unit.MEGABYTE), FieldValueType.SIZE);
234    assertThat(sizeFieldValue.plus(sizeFieldValue2).asString(), is("300.0MB"));
235    assertThat(sizeFieldValue.plus(sizeFieldValue2).asSize(),
236      is(new Size(300, Size.Unit.MEGABYTE)));
237
238    // Percent
239    FieldValue percentFieldValue = new FieldValue(30f, FieldValueType.PERCENT);
240    FieldValue percentFieldValue2 = new FieldValue(60f, FieldValueType.PERCENT);
241    assertThat(percentFieldValue.plus(percentFieldValue2).asString(), is("90.00%"));
242    assertThat(percentFieldValue.plus(percentFieldValue2).asFloat(), is(90f));
243  }
244
245  @Test
246  public void testCompareToIgnoreCase() {
247    FieldValue stringAFieldValue = new FieldValue("a", FieldValueType.STRING);
248    FieldValue stringCapitalAFieldValue = new FieldValue("A", FieldValueType.STRING);
249    FieldValue stringCapitalBFieldValue = new FieldValue("B", FieldValueType.STRING);
250
251    assertThat(stringAFieldValue.compareToIgnoreCase(stringCapitalAFieldValue), is(0));
252    assertThat(stringCapitalBFieldValue.compareToIgnoreCase(stringAFieldValue), is(1));
253    assertThat(stringAFieldValue.compareToIgnoreCase(stringCapitalBFieldValue), is(-1));
254  }
255
256  @Test
257  public void testOptimizeSize() {
258    FieldValue sizeFieldValue =
259      new FieldValue(new Size(1, Size.Unit.BYTE), FieldValueType.SIZE);
260    assertThat(sizeFieldValue.asString(), is("1.0B"));
261
262    sizeFieldValue =
263      new FieldValue(new Size(1024, Size.Unit.BYTE), FieldValueType.SIZE);
264    assertThat(sizeFieldValue.asString(), is("1.0KB"));
265
266    sizeFieldValue =
267      new FieldValue(new Size(2 * 1024, Size.Unit.BYTE), FieldValueType.SIZE);
268    assertThat(sizeFieldValue.asString(), is("2.0KB"));
269
270    sizeFieldValue =
271      new FieldValue(new Size(2 * 1024, Size.Unit.KILOBYTE), FieldValueType.SIZE);
272    assertThat(sizeFieldValue.asString(), is("2.0MB"));
273
274    sizeFieldValue =
275      new FieldValue(new Size(1024 * 1024, Size.Unit.KILOBYTE), FieldValueType.SIZE);
276    assertThat(sizeFieldValue.asString(), is("1.0GB"));
277
278    sizeFieldValue =
279      new FieldValue(new Size(2 * 1024 * 1024, Size.Unit.MEGABYTE), FieldValueType.SIZE);
280    assertThat(sizeFieldValue.asString(), is("2.0TB"));
281
282    sizeFieldValue =
283      new FieldValue(new Size(2 * 1024, Size.Unit.TERABYTE), FieldValueType.SIZE);
284    assertThat(sizeFieldValue.asString(), is("2.0PB"));
285
286    sizeFieldValue =
287      new FieldValue(new Size(1024 * 1024, Size.Unit.TERABYTE), FieldValueType.SIZE);
288    assertThat(sizeFieldValue.asString(), is("1024.0PB"));
289
290    sizeFieldValue =
291      new FieldValue(new Size(1, Size.Unit.PETABYTE), FieldValueType.SIZE);
292    assertThat(sizeFieldValue.asString(), is("1.0PB"));
293
294    sizeFieldValue =
295      new FieldValue(new Size(1024, Size.Unit.PETABYTE), FieldValueType.SIZE);
296    assertThat(sizeFieldValue.asString(), is("1024.0PB"));
297  }
298}