Skip to content

Commit

Permalink
HBASE-23626 Reduced number of Checkstyle violations in tests in hbase…
Browse files Browse the repository at this point in the history
…-common

Signed-off-by: Viraj Jasani <[email protected]>
  • Loading branch information
HorizonNet committed Dec 29, 2019
1 parent 19d5d4b commit a1d5e81
Show file tree
Hide file tree
Showing 10 changed files with 354 additions and 335 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.apache.hadoop.hbase.net;

import static org.junit.Assert.assertEquals;

import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.testclassification.MiscTests;
Expand All @@ -25,8 +26,6 @@
import org.junit.Test;
import org.junit.experimental.categories.Category;

import static org.junit.Assert.assertEquals;

@Category({ MiscTests.class, SmallTests.class })
public class TestAddress {
@ClassRule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@
@RunWith(Parameterized.class)
@Category({MiscTests.class, SmallTests.class})
public class TestStruct {

@ClassRule
public static final HBaseClassTestRule CLASS_RULE =
HBaseClassTestRule.forClass(TestStruct.class);

@Parameterized.Parameter(value = 0)
@Parameterized.Parameter()
public Struct generic;

@SuppressWarnings("rawtypes")
Expand Down Expand Up @@ -88,16 +87,21 @@ public static Collection<Object[]> params() {
return Arrays.asList(params);
}

static final Comparator<byte[]> NULL_SAFE_BYTES_COMPARATOR =
new Comparator<byte[]>() {
@Override
public int compare(byte[] o1, byte[] o2) {
if (o1 == o2) return 0;
if (null == o1) return -1;
if (null == o2) return 1;
return Bytes.compareTo(o1, o2);
}
};
static final Comparator<byte[]> NULL_SAFE_BYTES_COMPARATOR = (o1, o2) -> {
if (o1 == o2) {
return 0;
}

if (null == o1) {
return -1;
}

if (null == o2) {
return 1;
}

return Bytes.compareTo(o1, o2);
};

/**
* A simple object to serialize.
Expand Down Expand Up @@ -134,7 +138,7 @@ public int compareTo(Pojo1 o) {
if (cmp != 0) {
return cmp;
}
cmp = Integer.valueOf(intFieldAsc).compareTo(Integer.valueOf(o.intFieldAsc));
cmp = Integer.compare(intFieldAsc, o.intFieldAsc);
if (cmp != 0) {
return cmp;
}
Expand Down Expand Up @@ -173,13 +177,10 @@ public boolean equals(Object obj) {
return false;
}
if (stringFieldAsc == null) {
if (other.stringFieldAsc != null) {
return false;
}
} else if (!stringFieldAsc.equals(other.stringFieldAsc)) {
return false;
return other.stringFieldAsc == null;
} else {
return stringFieldAsc.equals(other.stringFieldAsc);
}
return true;
}
}

Expand Down Expand Up @@ -225,16 +226,17 @@ public int compareTo(Pojo2 o) {
if (cmp != 0) {
return cmp;
}

if (null == stringFieldDsc) {
cmp = 1;
}
else if (null == o.stringFieldDsc) {
} else if (null == o.stringFieldDsc) {
cmp = -1;
}
else if (stringFieldDsc.equals(o.stringFieldDsc)) {
} else if (stringFieldDsc.equals(o.stringFieldDsc)) {
cmp = 0;
} else {
cmp = -stringFieldDsc.compareTo(o.stringFieldDsc);
}
else cmp = -stringFieldDsc.compareTo(o.stringFieldDsc);

if (cmp != 0) {
return cmp;
}
Expand Down Expand Up @@ -274,56 +276,58 @@ public boolean equals(Object obj) {
return false;
}
if (stringFieldDsc == null) {
if (other.stringFieldDsc != null) {
return false;
}
} else if (!stringFieldDsc.equals(other.stringFieldDsc)) {
return false;
return other.stringFieldDsc == null;
} else {
return stringFieldDsc.equals(other.stringFieldDsc);
}
return true;
}
}

/**
* A custom data type implementation specialized for {@link Pojo1}.
*/
private static class SpecializedPojo1Type1 implements DataType<Pojo1> {

private static final RawStringTerminated stringField = new RawStringTerminated("/");
private static final RawInteger intField = new RawInteger();
private static final RawDouble doubleField = new RawDouble();

/**
* The {@link Struct} equivalent of this type.
*/
public static Struct GENERIC =
new StructBuilder().add(stringField)
.add(intField)
.add(doubleField)
.toStruct();
public static Struct GENERIC = new StructBuilder().add(stringField).add(intField)
.add(doubleField).toStruct();

@Override
public boolean isOrderPreserving() { return true; }
public boolean isOrderPreserving() {
return true;
}

@Override
public Order getOrder() { return null; }
public Order getOrder() {
return null;
}

@Override
public boolean isNullable() { return false; }
public boolean isNullable() {
return false;
}

@Override
public boolean isSkippable() { return true; }
public boolean isSkippable() {
return true;
}

@Override
public int encodedLength(Pojo1 val) {
return
stringField.encodedLength(val.stringFieldAsc) +
return stringField.encodedLength(val.stringFieldAsc) +
intField.encodedLength(val.intFieldAsc) +
doubleField.encodedLength(val.doubleFieldAsc);
}

@Override
public Class<Pojo1> encodedClass() { return Pojo1.class; }
public Class<Pojo1> encodedClass() {
return Pojo1.class;
}

@Override
public int skip(PositionedByteRange src) {
Expand Down Expand Up @@ -355,7 +359,6 @@ public int encode(PositionedByteRange dst, Pojo1 val) {
* A custom data type implementation specialized for {@link Pojo2}.
*/
private static class SpecializedPojo2Type1 implements DataType<Pojo2> {

private static RawBytesTerminated byteField1 = new RawBytesTerminated("/");
private static RawBytesTerminated byteField2 =
new RawBytesTerminated(Order.DESCENDING, "/");
Expand All @@ -366,36 +369,41 @@ private static class SpecializedPojo2Type1 implements DataType<Pojo2> {
/**
* The {@link Struct} equivalent of this type.
*/
public static Struct GENERIC =
new StructBuilder().add(byteField1)
.add(byteField2)
.add(stringField)
.add(byteField3)
.toStruct();
public static Struct GENERIC = new StructBuilder().add(byteField1).add(byteField2)
.add(stringField).add(byteField3).toStruct();

@Override
public boolean isOrderPreserving() { return true; }
public boolean isOrderPreserving() {
return true;
}

@Override
public Order getOrder() { return null; }
public Order getOrder() {
return null;
}

@Override
public boolean isNullable() { return false; }
public boolean isNullable() {
return false;
}

@Override
public boolean isSkippable() { return true; }
public boolean isSkippable() {
return true;
}

@Override
public int encodedLength(Pojo2 val) {
return
byteField1.encodedLength(val.byteField1Asc) +
return byteField1.encodedLength(val.byteField1Asc) +
byteField2.encodedLength(val.byteField2Dsc) +
stringField.encodedLength(val.stringFieldDsc) +
byteField3.encodedLength(val.byteField3Dsc);
}

@Override
public Class<Pojo2> encodedClass() { return Pojo2.class; }
public Class<Pojo2> encodedClass() {
return Pojo2.class;
}

@Override
public int skip(PositionedByteRange src) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.apache.hadoop.hbase.types;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.testclassification.MiscTests;
Expand All @@ -30,9 +29,8 @@
import org.junit.Test;
import org.junit.experimental.categories.Category;

@Category({MiscTests.class, SmallTests.class})
@Category({ MiscTests.class, SmallTests.class })
public class TestUnion2 {

@ClassRule
public static final HBaseClassTestRule CLASS_RULE =
HBaseClassTestRule.forClass(TestUnion2.class);
Expand All @@ -41,7 +39,6 @@ public class TestUnion2 {
* An example <code>Union</code>
*/
private static class SampleUnion1 extends Union2<Integer, String> {

private static final byte IS_INTEGER = 0x00;
private static final byte IS_STRING = 0x01;

Expand Down Expand Up @@ -79,13 +76,19 @@ public int encodedLength(Object val) {
String s = null;
try {
i = (Integer) val;
} catch (ClassCastException e) {}
} catch (ClassCastException ignored) {}
try {
s = (String) val;
} catch (ClassCastException e) {}
} catch (ClassCastException ignored) {}

if (null != i) {
return 1 + typeA.encodedLength(i);
}

if (null != s) {
return 1 + typeB.encodedLength(s);
}

if (null != i) return 1 + typeA.encodedLength(i);
if (null != s) return 1 + typeB.encodedLength(s);
throw new IllegalArgumentException("val is not a valid member of this union.");
}

Expand All @@ -95,42 +98,42 @@ public int encode(PositionedByteRange dst, Object val) {
String s = null;
try {
i = (Integer) val;
} catch (ClassCastException e) {}
} catch (ClassCastException ignored) {}
try {
s = (String) val;
} catch (ClassCastException e) {}
} catch (ClassCastException ignored) {}

if (null != i) {
dst.put(IS_INTEGER);
return 1 + typeA.encode(dst, i);
} else if (null != s) {
dst.put(IS_STRING);
return 1 + typeB.encode(dst, s);
}
else
} else {
throw new IllegalArgumentException("val is not of a supported type.");
}
}
}

@Test
public void testEncodeDecode() {
Integer intVal = Integer.valueOf(10);
Integer intVal = 10;
String strVal = "hello";
PositionedByteRange buff = new SimplePositionedMutableByteRange(10);
SampleUnion1 type = new SampleUnion1();

type.encode(buff, intVal);
buff.setPosition(0);
assertTrue(0 == intVal.compareTo(type.decodeA(buff)));
assertEquals(0, intVal.compareTo(type.decodeA(buff)));
buff.setPosition(0);
type.encode(buff, strVal);
buff.setPosition(0);
assertTrue(0 == strVal.compareTo(type.decodeB(buff)));
assertEquals(0, strVal.compareTo(type.decodeB(buff)));
}

@Test
public void testSkip() {
Integer intVal = Integer.valueOf(10);
Integer intVal = 10;
String strVal = "hello";
PositionedByteRange buff = new SimplePositionedMutableByteRange(10);
SampleUnion1 type = new SampleUnion1();
Expand Down
Loading

0 comments on commit a1d5e81

Please sign in to comment.