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.hamcrest.MatcherAssert.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@Category(SmallTests.class)
032public class TestFieldValue {
033
034  @ClassRule
035  public static final HBaseClassTestRule CLASS_RULE =
036    HBaseClassTestRule.forClass(TestFieldValue.class);
037
038  @Test
039  public void testParseAndAsSomethingMethod() {
040    // String
041    FieldValue stringFieldValue = new FieldValue("aaa", FieldValueType.STRING);
042    assertThat(stringFieldValue.asString(), is("aaa"));
043
044    try {
045      new FieldValue(1, FieldValueType.STRING);
046      fail();
047    } catch (IllegalArgumentException ignored) {
048    }
049
050    // Integer
051    FieldValue integerFieldValue = new FieldValue(100, FieldValueType.INTEGER);
052    assertThat(integerFieldValue.asInt(), is(100));
053
054    integerFieldValue = new FieldValue("100", FieldValueType.INTEGER);
055    assertThat(integerFieldValue.asInt(), is(100));
056
057    try {
058      new FieldValue("aaa", FieldValueType.INTEGER);
059      fail();
060    } catch (IllegalArgumentException ignored) {
061    }
062
063    // Long
064    FieldValue longFieldValue = new FieldValue(100L, FieldValueType.LONG);
065    assertThat(longFieldValue.asLong(), is(100L));
066
067    longFieldValue = new FieldValue("100", FieldValueType.LONG);
068    assertThat(longFieldValue.asLong(), is(100L));
069
070    try {
071      new FieldValue("aaa", FieldValueType.LONG);
072      fail();
073    } catch (IllegalArgumentException ignored) {
074    }
075
076    try {
077      new FieldValue(100, FieldValueType.LONG);
078      fail();
079    } catch (IllegalArgumentException ignored) {
080    }
081
082    // Float
083    FieldValue floatFieldValue = new FieldValue(1.0f, FieldValueType.FLOAT);
084    assertThat(floatFieldValue.asFloat(), is(1.0f));
085
086    floatFieldValue = new FieldValue("1", FieldValueType.FLOAT);
087    assertThat(floatFieldValue.asFloat(), is(1.0f));
088
089    try {
090      new FieldValue("aaa", FieldValueType.FLOAT);
091      fail();
092    } catch (IllegalArgumentException ignored) {
093    }
094
095    try {
096      new FieldValue(1, FieldValueType.FLOAT);
097      fail();
098    } catch (IllegalArgumentException ignored) {
099    }
100
101    // Size
102    FieldValue sizeFieldValue =
103      new FieldValue(new Size(100, Size.Unit.MEGABYTE), FieldValueType.SIZE);
104    assertThat(sizeFieldValue.asString(), is("100.0MB"));
105    assertThat(sizeFieldValue.asSize(), is(new Size(100, Size.Unit.MEGABYTE)));
106
107    sizeFieldValue = new FieldValue("100MB", FieldValueType.SIZE);
108    assertThat(sizeFieldValue.asString(), is("100.0MB"));
109    assertThat(sizeFieldValue.asSize(), is(new Size(100, Size.Unit.MEGABYTE)));
110
111    try {
112      new FieldValue("100", FieldValueType.SIZE);
113      fail();
114    } catch (IllegalArgumentException ignored) {
115    }
116
117    try {
118      new FieldValue(100, FieldValueType.SIZE);
119      fail();
120    } catch (IllegalArgumentException ignored) {
121    }
122
123    // Percent
124    FieldValue percentFieldValue = new FieldValue(100f, FieldValueType.PERCENT);
125    assertThat(percentFieldValue.asString(), is("100.00%"));
126    assertThat(percentFieldValue.asFloat(), is(100f));
127
128    percentFieldValue = new FieldValue("100%", FieldValueType.PERCENT);
129    assertThat(percentFieldValue.asString(), is("100.00%"));
130    assertThat(percentFieldValue.asFloat(), is(100f));
131
132    percentFieldValue = new FieldValue("100", FieldValueType.PERCENT);
133    assertThat(percentFieldValue.asString(), is("100.00%"));
134    assertThat(percentFieldValue.asFloat(), is(100f));
135
136    try {
137      new FieldValue(100, FieldValueType.PERCENT);
138      fail();
139    } catch (IllegalArgumentException ignored) {
140    }
141  }
142
143  @Test
144  public void testCompareTo() {
145    // String
146    FieldValue stringAFieldValue = new FieldValue("a", FieldValueType.STRING);
147    FieldValue stringAFieldValue2 = new FieldValue("a", FieldValueType.STRING);
148    FieldValue stringBFieldValue = new FieldValue("b", FieldValueType.STRING);
149    FieldValue stringCapitalAFieldValue = new FieldValue("A", FieldValueType.STRING);
150
151    assertThat(stringAFieldValue.compareTo(stringAFieldValue2), is(0));
152    assertThat(stringBFieldValue.compareTo(stringAFieldValue), is(1));
153    assertThat(stringAFieldValue.compareTo(stringBFieldValue), is(-1));
154    assertThat(stringAFieldValue.compareTo(stringCapitalAFieldValue), is(32));
155
156    // Integer
157    FieldValue integer1FieldValue = new FieldValue(1, FieldValueType.INTEGER);
158    FieldValue integer1FieldValue2 = new FieldValue(1, FieldValueType.INTEGER);
159    FieldValue integer2FieldValue = new FieldValue(2, FieldValueType.INTEGER);
160
161    assertThat(integer1FieldValue.compareTo(integer1FieldValue2), is(0));
162    assertThat(integer2FieldValue.compareTo(integer1FieldValue), is(1));
163    assertThat(integer1FieldValue.compareTo(integer2FieldValue), is(-1));
164
165    // Long
166    FieldValue long1FieldValue = new FieldValue(1L, FieldValueType.LONG);
167    FieldValue long1FieldValue2 = new FieldValue(1L, FieldValueType.LONG);
168    FieldValue long2FieldValue = new FieldValue(2L, FieldValueType.LONG);
169
170    assertThat(long1FieldValue.compareTo(long1FieldValue2), is(0));
171    assertThat(long2FieldValue.compareTo(long1FieldValue), is(1));
172    assertThat(long1FieldValue.compareTo(long2FieldValue), is(-1));
173
174    // Float
175    FieldValue float1FieldValue = new FieldValue(1.0f, FieldValueType.FLOAT);
176    FieldValue float1FieldValue2 = new FieldValue(1.0f, FieldValueType.FLOAT);
177    FieldValue float2FieldValue = new FieldValue(2.0f, FieldValueType.FLOAT);
178
179    assertThat(float1FieldValue.compareTo(float1FieldValue2), is(0));
180    assertThat(float2FieldValue.compareTo(float1FieldValue), is(1));
181    assertThat(float1FieldValue.compareTo(float2FieldValue), is(-1));
182
183    // Size
184    FieldValue size100MBFieldValue =
185      new FieldValue(new Size(100, Size.Unit.MEGABYTE), FieldValueType.SIZE);
186    FieldValue size100MBFieldValue2 =
187      new FieldValue(new Size(100, Size.Unit.MEGABYTE), FieldValueType.SIZE);
188    FieldValue size200MBFieldValue =
189      new FieldValue(new Size(200, Size.Unit.MEGABYTE), FieldValueType.SIZE);
190
191    assertThat(size100MBFieldValue.compareTo(size100MBFieldValue2), is(0));
192    assertThat(size200MBFieldValue.compareTo(size100MBFieldValue), is(1));
193    assertThat(size100MBFieldValue.compareTo(size200MBFieldValue), is(-1));
194
195    // Percent
196    FieldValue percent50FieldValue = new FieldValue(50.0f, FieldValueType.PERCENT);
197    FieldValue percent50FieldValue2 = new FieldValue(50.0f, FieldValueType.PERCENT);
198    FieldValue percent100FieldValue = new FieldValue(100.0f, FieldValueType.PERCENT);
199
200    assertThat(percent50FieldValue.compareTo(percent50FieldValue2), is(0));
201    assertThat(percent100FieldValue.compareTo(percent50FieldValue), is(1));
202    assertThat(percent50FieldValue.compareTo(percent100FieldValue), is(-1));
203  }
204
205  @Test
206  public void testPlus() {
207    // String
208    FieldValue stringFieldValue = new FieldValue("a", FieldValueType.STRING);
209    FieldValue stringFieldValue2 = new FieldValue("b", FieldValueType.STRING);
210    assertThat(stringFieldValue.plus(stringFieldValue2).asString(), is("ab"));
211
212    // Integer
213    FieldValue integerFieldValue = new FieldValue(1, FieldValueType.INTEGER);
214    FieldValue integerFieldValue2 = new FieldValue(2, FieldValueType.INTEGER);
215    assertThat(integerFieldValue.plus(integerFieldValue2).asInt(), is(3));
216
217    // Long
218    FieldValue longFieldValue = new FieldValue(1L, FieldValueType.LONG);
219    FieldValue longFieldValue2 = new FieldValue(2L, FieldValueType.LONG);
220    assertThat(longFieldValue.plus(longFieldValue2).asLong(), is(3L));
221
222    // Float
223    FieldValue floatFieldValue = new FieldValue(1.2f, FieldValueType.FLOAT);
224    FieldValue floatFieldValue2 = new FieldValue(2.2f, FieldValueType.FLOAT);
225    assertThat(floatFieldValue.plus(floatFieldValue2).asFloat(), is(3.4f));
226
227    // Size
228    FieldValue sizeFieldValue =
229      new FieldValue(new Size(100, Size.Unit.MEGABYTE), FieldValueType.SIZE);
230    FieldValue sizeFieldValue2 =
231      new FieldValue(new Size(200, Size.Unit.MEGABYTE), FieldValueType.SIZE);
232    assertThat(sizeFieldValue.plus(sizeFieldValue2).asString(), is("300.0MB"));
233    assertThat(sizeFieldValue.plus(sizeFieldValue2).asSize(),
234      is(new Size(300, Size.Unit.MEGABYTE)));
235
236    // Percent
237    FieldValue percentFieldValue = new FieldValue(30f, FieldValueType.PERCENT);
238    FieldValue percentFieldValue2 = new FieldValue(60f, FieldValueType.PERCENT);
239    assertThat(percentFieldValue.plus(percentFieldValue2).asString(), is("90.00%"));
240    assertThat(percentFieldValue.plus(percentFieldValue2).asFloat(), is(90f));
241  }
242
243  @Test
244  public void testCompareToIgnoreCase() {
245    FieldValue stringAFieldValue = new FieldValue("a", FieldValueType.STRING);
246    FieldValue stringCapitalAFieldValue = new FieldValue("A", FieldValueType.STRING);
247    FieldValue stringCapitalBFieldValue = new FieldValue("B", FieldValueType.STRING);
248
249    assertThat(stringAFieldValue.compareToIgnoreCase(stringCapitalAFieldValue), is(0));
250    assertThat(stringCapitalBFieldValue.compareToIgnoreCase(stringAFieldValue), is(1));
251    assertThat(stringAFieldValue.compareToIgnoreCase(stringCapitalBFieldValue), is(-1));
252  }
253
254  @Test
255  public void testOptimizeSize() {
256    FieldValue sizeFieldValue = new FieldValue(new Size(1, Size.Unit.BYTE), FieldValueType.SIZE);
257    assertThat(sizeFieldValue.asString(), is("1.0B"));
258
259    sizeFieldValue = new FieldValue(new Size(1024, Size.Unit.BYTE), FieldValueType.SIZE);
260    assertThat(sizeFieldValue.asString(), is("1.0KB"));
261
262    sizeFieldValue = new FieldValue(new Size(2 * 1024, Size.Unit.BYTE), FieldValueType.SIZE);
263    assertThat(sizeFieldValue.asString(), is("2.0KB"));
264
265    sizeFieldValue = new FieldValue(new Size(2 * 1024, Size.Unit.KILOBYTE), FieldValueType.SIZE);
266    assertThat(sizeFieldValue.asString(), is("2.0MB"));
267
268    sizeFieldValue = new FieldValue(new Size(1024 * 1024, Size.Unit.KILOBYTE), FieldValueType.SIZE);
269    assertThat(sizeFieldValue.asString(), is("1.0GB"));
270
271    sizeFieldValue =
272      new FieldValue(new Size(2 * 1024 * 1024, Size.Unit.MEGABYTE), FieldValueType.SIZE);
273    assertThat(sizeFieldValue.asString(), is("2.0TB"));
274
275    sizeFieldValue = new FieldValue(new Size(2 * 1024, Size.Unit.TERABYTE), FieldValueType.SIZE);
276    assertThat(sizeFieldValue.asString(), is("2.0PB"));
277
278    sizeFieldValue = new FieldValue(new Size(1024 * 1024, Size.Unit.TERABYTE), FieldValueType.SIZE);
279    assertThat(sizeFieldValue.asString(), is("1024.0PB"));
280
281    sizeFieldValue = new FieldValue(new Size(1, Size.Unit.PETABYTE), FieldValueType.SIZE);
282    assertThat(sizeFieldValue.asString(), is("1.0PB"));
283
284    sizeFieldValue = new FieldValue(new Size(1024, Size.Unit.PETABYTE), FieldValueType.SIZE);
285    assertThat(sizeFieldValue.asString(), is("1024.0PB"));
286  }
287}