HBASE-23626 Reduced number of Checkstyle violations in tests in hbase-common

Signed-off-by: Viraj Jasani <vjasani@apache.org>
This commit is contained in:
Jan Hentschel 2019-12-29 22:39:09 +01:00 committed by GitHub
parent c6a9a4d410
commit 923ba7763e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 355 additions and 335 deletions

View File

@ -17,6 +17,8 @@
*/ */
package org.apache.hadoop.hbase.net; package org.apache.hadoop.hbase.net;
import static org.junit.Assert.assertEquals;
import org.apache.hadoop.hbase.HBaseClassTestRule; import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.testclassification.MiscTests; import org.apache.hadoop.hbase.testclassification.MiscTests;
import org.apache.hadoop.hbase.testclassification.SmallTests; import org.apache.hadoop.hbase.testclassification.SmallTests;
@ -24,8 +26,6 @@ import org.junit.ClassRule;
import org.junit.Test; import org.junit.Test;
import org.junit.experimental.categories.Category; import org.junit.experimental.categories.Category;
import static org.junit.Assert.assertEquals;
@Category({ MiscTests.class, SmallTests.class }) @Category({ MiscTests.class, SmallTests.class })
public class TestAddress { public class TestAddress {
@ClassRule @ClassRule

View File

@ -47,12 +47,11 @@ import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class) @RunWith(Parameterized.class)
@Category({MiscTests.class, SmallTests.class}) @Category({MiscTests.class, SmallTests.class})
public class TestStruct { public class TestStruct {
@ClassRule @ClassRule
public static final HBaseClassTestRule CLASS_RULE = public static final HBaseClassTestRule CLASS_RULE =
HBaseClassTestRule.forClass(TestStruct.class); HBaseClassTestRule.forClass(TestStruct.class);
@Parameterized.Parameter(value = 0) @Parameterized.Parameter()
public Struct generic; public Struct generic;
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
@ -88,16 +87,21 @@ public class TestStruct {
return Arrays.asList(params); return Arrays.asList(params);
} }
static final Comparator<byte[]> NULL_SAFE_BYTES_COMPARATOR = static final Comparator<byte[]> NULL_SAFE_BYTES_COMPARATOR = (o1, o2) -> {
new Comparator<byte[]>() { if (o1 == o2) {
@Override return 0;
public int compare(byte[] o1, byte[] o2) { }
if (o1 == o2) return 0;
if (null == o1) return -1; if (null == o1) {
if (null == o2) return 1; return -1;
return Bytes.compareTo(o1, o2); }
}
}; if (null == o2) {
return 1;
}
return Bytes.compareTo(o1, o2);
};
/** /**
* A simple object to serialize. * A simple object to serialize.
@ -134,7 +138,7 @@ public class TestStruct {
if (cmp != 0) { if (cmp != 0) {
return cmp; return cmp;
} }
cmp = Integer.valueOf(intFieldAsc).compareTo(Integer.valueOf(o.intFieldAsc)); cmp = Integer.compare(intFieldAsc, o.intFieldAsc);
if (cmp != 0) { if (cmp != 0) {
return cmp; return cmp;
} }
@ -173,13 +177,10 @@ public class TestStruct {
return false; return false;
} }
if (stringFieldAsc == null) { if (stringFieldAsc == null) {
if (other.stringFieldAsc != null) { return other.stringFieldAsc == null;
return false; } else {
} return stringFieldAsc.equals(other.stringFieldAsc);
} else if (!stringFieldAsc.equals(other.stringFieldAsc)) {
return false;
} }
return true;
} }
} }
@ -225,16 +226,17 @@ public class TestStruct {
if (cmp != 0) { if (cmp != 0) {
return cmp; return cmp;
} }
if (null == stringFieldDsc) { if (null == stringFieldDsc) {
cmp = 1; cmp = 1;
} } else if (null == o.stringFieldDsc) {
else if (null == o.stringFieldDsc) {
cmp = -1; cmp = -1;
} } else if (stringFieldDsc.equals(o.stringFieldDsc)) {
else if (stringFieldDsc.equals(o.stringFieldDsc)) {
cmp = 0; cmp = 0;
} else {
cmp = -stringFieldDsc.compareTo(o.stringFieldDsc);
} }
else cmp = -stringFieldDsc.compareTo(o.stringFieldDsc);
if (cmp != 0) { if (cmp != 0) {
return cmp; return cmp;
} }
@ -274,13 +276,10 @@ public class TestStruct {
return false; return false;
} }
if (stringFieldDsc == null) { if (stringFieldDsc == null) {
if (other.stringFieldDsc != null) { return other.stringFieldDsc == null;
return false; } else {
} return stringFieldDsc.equals(other.stringFieldDsc);
} else if (!stringFieldDsc.equals(other.stringFieldDsc)) {
return false;
} }
return true;
} }
} }
@ -288,7 +287,6 @@ public class TestStruct {
* A custom data type implementation specialized for {@link Pojo1}. * A custom data type implementation specialized for {@link Pojo1}.
*/ */
private static class SpecializedPojo1Type1 implements DataType<Pojo1> { private static class SpecializedPojo1Type1 implements DataType<Pojo1> {
private static final RawStringTerminated stringField = new RawStringTerminated("/"); private static final RawStringTerminated stringField = new RawStringTerminated("/");
private static final RawInteger intField = new RawInteger(); private static final RawInteger intField = new RawInteger();
private static final RawDouble doubleField = new RawDouble(); private static final RawDouble doubleField = new RawDouble();
@ -296,34 +294,40 @@ public class TestStruct {
/** /**
* The {@link Struct} equivalent of this type. * The {@link Struct} equivalent of this type.
*/ */
public static Struct GENERIC = public static Struct GENERIC = new StructBuilder().add(stringField).add(intField)
new StructBuilder().add(stringField) .add(doubleField).toStruct();
.add(intField)
.add(doubleField)
.toStruct();
@Override @Override
public boolean isOrderPreserving() { return true; } public boolean isOrderPreserving() {
return true;
}
@Override @Override
public Order getOrder() { return null; } public Order getOrder() {
return null;
}
@Override @Override
public boolean isNullable() { return false; } public boolean isNullable() {
return false;
}
@Override @Override
public boolean isSkippable() { return true; } public boolean isSkippable() {
return true;
}
@Override @Override
public int encodedLength(Pojo1 val) { public int encodedLength(Pojo1 val) {
return return stringField.encodedLength(val.stringFieldAsc) +
stringField.encodedLength(val.stringFieldAsc) +
intField.encodedLength(val.intFieldAsc) + intField.encodedLength(val.intFieldAsc) +
doubleField.encodedLength(val.doubleFieldAsc); doubleField.encodedLength(val.doubleFieldAsc);
} }
@Override @Override
public Class<Pojo1> encodedClass() { return Pojo1.class; } public Class<Pojo1> encodedClass() {
return Pojo1.class;
}
@Override @Override
public int skip(PositionedByteRange src) { public int skip(PositionedByteRange src) {
@ -355,7 +359,6 @@ public class TestStruct {
* A custom data type implementation specialized for {@link Pojo2}. * A custom data type implementation specialized for {@link Pojo2}.
*/ */
private static class SpecializedPojo2Type1 implements DataType<Pojo2> { private static class SpecializedPojo2Type1 implements DataType<Pojo2> {
private static RawBytesTerminated byteField1 = new RawBytesTerminated("/"); private static RawBytesTerminated byteField1 = new RawBytesTerminated("/");
private static RawBytesTerminated byteField2 = private static RawBytesTerminated byteField2 =
new RawBytesTerminated(Order.DESCENDING, "/"); new RawBytesTerminated(Order.DESCENDING, "/");
@ -366,36 +369,41 @@ public class TestStruct {
/** /**
* The {@link Struct} equivalent of this type. * The {@link Struct} equivalent of this type.
*/ */
public static Struct GENERIC = public static Struct GENERIC = new StructBuilder().add(byteField1).add(byteField2)
new StructBuilder().add(byteField1) .add(stringField).add(byteField3).toStruct();
.add(byteField2)
.add(stringField)
.add(byteField3)
.toStruct();
@Override @Override
public boolean isOrderPreserving() { return true; } public boolean isOrderPreserving() {
return true;
}
@Override @Override
public Order getOrder() { return null; } public Order getOrder() {
return null;
}
@Override @Override
public boolean isNullable() { return false; } public boolean isNullable() {
return false;
}
@Override @Override
public boolean isSkippable() { return true; } public boolean isSkippable() {
return true;
}
@Override @Override
public int encodedLength(Pojo2 val) { public int encodedLength(Pojo2 val) {
return return byteField1.encodedLength(val.byteField1Asc) +
byteField1.encodedLength(val.byteField1Asc) +
byteField2.encodedLength(val.byteField2Dsc) + byteField2.encodedLength(val.byteField2Dsc) +
stringField.encodedLength(val.stringFieldDsc) + stringField.encodedLength(val.stringFieldDsc) +
byteField3.encodedLength(val.byteField3Dsc); byteField3.encodedLength(val.byteField3Dsc);
} }
@Override @Override
public Class<Pojo2> encodedClass() { return Pojo2.class; } public Class<Pojo2> encodedClass() {
return Pojo2.class;
}
@Override @Override
public int skip(PositionedByteRange src) { public int skip(PositionedByteRange src) {

View File

@ -18,7 +18,6 @@
package org.apache.hadoop.hbase.types; package org.apache.hadoop.hbase.types;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.apache.hadoop.hbase.HBaseClassTestRule; import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.testclassification.MiscTests; import org.apache.hadoop.hbase.testclassification.MiscTests;
@ -30,9 +29,8 @@ import org.junit.ClassRule;
import org.junit.Test; import org.junit.Test;
import org.junit.experimental.categories.Category; import org.junit.experimental.categories.Category;
@Category({MiscTests.class, SmallTests.class}) @Category({ MiscTests.class, SmallTests.class })
public class TestUnion2 { public class TestUnion2 {
@ClassRule @ClassRule
public static final HBaseClassTestRule CLASS_RULE = public static final HBaseClassTestRule CLASS_RULE =
HBaseClassTestRule.forClass(TestUnion2.class); HBaseClassTestRule.forClass(TestUnion2.class);
@ -41,7 +39,6 @@ public class TestUnion2 {
* An example <code>Union</code> * An example <code>Union</code>
*/ */
private static class SampleUnion1 extends Union2<Integer, String> { private static class SampleUnion1 extends Union2<Integer, String> {
private static final byte IS_INTEGER = 0x00; private static final byte IS_INTEGER = 0x00;
private static final byte IS_STRING = 0x01; private static final byte IS_STRING = 0x01;
@ -79,13 +76,19 @@ public class TestUnion2 {
String s = null; String s = null;
try { try {
i = (Integer) val; i = (Integer) val;
} catch (ClassCastException e) {} } catch (ClassCastException ignored) {}
try { try {
s = (String) val; 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."); throw new IllegalArgumentException("val is not a valid member of this union.");
} }
@ -95,10 +98,10 @@ public class TestUnion2 {
String s = null; String s = null;
try { try {
i = (Integer) val; i = (Integer) val;
} catch (ClassCastException e) {} } catch (ClassCastException ignored) {}
try { try {
s = (String) val; s = (String) val;
} catch (ClassCastException e) {} } catch (ClassCastException ignored) {}
if (null != i) { if (null != i) {
dst.put(IS_INTEGER); dst.put(IS_INTEGER);
@ -106,31 +109,31 @@ public class TestUnion2 {
} else if (null != s) { } else if (null != s) {
dst.put(IS_STRING); dst.put(IS_STRING);
return 1 + typeB.encode(dst, s); return 1 + typeB.encode(dst, s);
} } else {
else
throw new IllegalArgumentException("val is not of a supported type."); throw new IllegalArgumentException("val is not of a supported type.");
}
} }
} }
@Test @Test
public void testEncodeDecode() { public void testEncodeDecode() {
Integer intVal = Integer.valueOf(10); Integer intVal = 10;
String strVal = "hello"; String strVal = "hello";
PositionedByteRange buff = new SimplePositionedMutableByteRange(10); PositionedByteRange buff = new SimplePositionedMutableByteRange(10);
SampleUnion1 type = new SampleUnion1(); SampleUnion1 type = new SampleUnion1();
type.encode(buff, intVal); type.encode(buff, intVal);
buff.setPosition(0); buff.setPosition(0);
assertTrue(0 == intVal.compareTo(type.decodeA(buff))); assertEquals(0, intVal.compareTo(type.decodeA(buff)));
buff.setPosition(0); buff.setPosition(0);
type.encode(buff, strVal); type.encode(buff, strVal);
buff.setPosition(0); buff.setPosition(0);
assertTrue(0 == strVal.compareTo(type.decodeB(buff))); assertEquals(0, strVal.compareTo(type.decodeB(buff)));
} }
@Test @Test
public void testSkip() { public void testSkip() {
Integer intVal = Integer.valueOf(10); Integer intVal = 10;
String strVal = "hello"; String strVal = "hello";
PositionedByteRange buff = new SimplePositionedMutableByteRange(10); PositionedByteRange buff = new SimplePositionedMutableByteRange(10);
SampleUnion1 type = new SampleUnion1(); SampleUnion1 type = new SampleUnion1();

View File

@ -18,6 +18,7 @@
package org.apache.hadoop.hbase.util; package org.apache.hadoop.hbase.util;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
@ -43,11 +44,14 @@ import org.slf4j.LoggerFactory;
/** /**
* Some utilities to help class loader testing * Some utilities to help class loader testing
*/ */
public class ClassLoaderTestHelper { public final class ClassLoaderTestHelper {
private static final Logger LOG = LoggerFactory.getLogger(ClassLoaderTestHelper.class); private static final Logger LOG = LoggerFactory.getLogger(ClassLoaderTestHelper.class);
private static final int BUFFER_SIZE = 4096; private static final int BUFFER_SIZE = 4096;
private ClassLoaderTestHelper() {
}
/** /**
* Jar a list of files into a jar archive. * Jar a list of files into a jar archive.
* *
@ -57,28 +61,29 @@ public class ClassLoaderTestHelper {
*/ */
private static boolean createJarArchive(File archiveFile, File[] tobeJared) { private static boolean createJarArchive(File archiveFile, File[] tobeJared) {
try { try {
byte buffer[] = new byte[BUFFER_SIZE]; byte[] buffer = new byte[BUFFER_SIZE];
// Open archive file // Open archive file
FileOutputStream stream = new FileOutputStream(archiveFile); FileOutputStream stream = new FileOutputStream(archiveFile);
JarOutputStream out = new JarOutputStream(stream, new Manifest()); JarOutputStream out = new JarOutputStream(stream, new Manifest());
for (int i = 0; i < tobeJared.length; i++) { for (File file : tobeJared) {
if (tobeJared[i] == null || !tobeJared[i].exists() if (file == null || !file.exists() || file.isDirectory()) {
|| tobeJared[i].isDirectory()) {
continue; continue;
} }
// Add archive entry // Add archive entry
JarEntry jarAdd = new JarEntry(tobeJared[i].getName()); JarEntry jarAdd = new JarEntry(file.getName());
jarAdd.setTime(tobeJared[i].lastModified()); jarAdd.setTime(file.lastModified());
out.putNextEntry(jarAdd); out.putNextEntry(jarAdd);
// Write file to archive // Write file to archive
FileInputStream in = new FileInputStream(tobeJared[i]); FileInputStream in = new FileInputStream(file);
while (true) { while (true) {
int nRead = in.read(buffer, 0, buffer.length); int nRead = in.read(buffer, 0, buffer.length);
if (nRead <= 0) if (nRead <= 0) {
break; break;
}
out.write(buffer, 0, nRead); out.write(buffer, 0, nRead);
} }
in.close(); in.close();
@ -102,7 +107,7 @@ public class ClassLoaderTestHelper {
* @param testDir the folder under which to store the test class and jar * @param testDir the folder under which to store the test class and jar
* @param className the test class name * @param className the test class name
* @param code the optional test class code, which can be null. * @param code the optional test class code, which can be null.
* If null, a bare empty class will be used * If null, a bare empty class will be used
* @return the test jar file generated * @return the test jar file generated
*/ */
public static File buildJar(String testDir, public static File buildJar(String testDir,
@ -117,7 +122,7 @@ public class ClassLoaderTestHelper {
* @param testDir the folder under which to store the test class * @param testDir the folder under which to store the test class
* @param className the test class name * @param className the test class name
* @param code the optional test class code, which can be null. * @param code the optional test class code, which can be null.
* If null, an empty class will be used * If null, an empty class will be used
* @param folder the folder under which to store the generated jar * @param folder the folder under which to store the generated jar
* @return the test jar file generated * @return the test jar file generated
*/ */
@ -163,7 +168,7 @@ public class ClassLoaderTestHelper {
jarFile.getParentFile().mkdirs(); jarFile.getParentFile().mkdirs();
if (!createJarArchive(jarFile, if (!createJarArchive(jarFile,
new File[]{new File(srcDir.toString(), className + ".class")})){ new File[]{new File(srcDir.toString(), className + ".class")})){
assertTrue("Build jar file failed.", false); fail("Build jar file failed.");
} }
return jarFile; return jarFile;
} }
@ -183,7 +188,7 @@ public class ClassLoaderTestHelper {
String libPrefix, File... srcJars) throws Exception { String libPrefix, File... srcJars) throws Exception {
FileOutputStream stream = new FileOutputStream(targetJar); FileOutputStream stream = new FileOutputStream(targetJar);
JarOutputStream out = new JarOutputStream(stream, new Manifest()); JarOutputStream out = new JarOutputStream(stream, new Manifest());
byte buffer[] = new byte[BUFFER_SIZE]; byte[] buffer = new byte[BUFFER_SIZE];
for (File jarFile: srcJars) { for (File jarFile: srcJars) {
// Add archive entry // Add archive entry
@ -195,8 +200,10 @@ public class ClassLoaderTestHelper {
FileInputStream in = new FileInputStream(jarFile); FileInputStream in = new FileInputStream(jarFile);
while (true) { while (true) {
int nRead = in.read(buffer, 0, buffer.length); int nRead = in.read(buffer, 0, buffer.length);
if (nRead <= 0) if (nRead <= 0) {
break; break;
}
out.write(buffer, 0, nRead); out.write(buffer, 0, nRead);
} }
in.close(); in.close();
@ -210,5 +217,4 @@ public class ClassLoaderTestHelper {
return conf.get(ClassLoaderBase.LOCAL_DIR_KEY) return conf.get(ClassLoaderBase.LOCAL_DIR_KEY)
+ File.separator + "jars" + File.separator; + File.separator + "jars" + File.separator;
} }
} }

View File

@ -23,7 +23,9 @@ package org.apache.hadoop.hbase.util;
* the use of the injectEdge method giving it default permissions, but in * the use of the injectEdge method giving it default permissions, but in
* testing we may need to use this functionality elsewhere. * testing we may need to use this functionality elsewhere.
*/ */
public class EnvironmentEdgeManagerTestHelper { public final class EnvironmentEdgeManagerTestHelper {
private EnvironmentEdgeManagerTestHelper() {
}
public static void reset() { public static void reset() {
EnvironmentEdgeManager.reset(); EnvironmentEdgeManager.reset();

View File

@ -17,6 +17,7 @@
*/ */
package org.apache.hadoop.hbase.util; package org.apache.hadoop.hbase.util;
import static org.junit.Assert.assertArrayEquals;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.DataInputStream; import java.io.DataInputStream;
@ -41,7 +42,6 @@ import org.junit.experimental.categories.Category;
@Category({MiscTests.class, SmallTests.class}) @Category({MiscTests.class, SmallTests.class})
public class TestBytes extends TestCase { public class TestBytes extends TestCase {
@ClassRule @ClassRule
public static final HBaseClassTestRule CLASS_RULE = public static final HBaseClassTestRule CLASS_RULE =
HBaseClassTestRule.forClass(TestBytes.class); HBaseClassTestRule.forClass(TestBytes.class);
@ -99,141 +99,141 @@ public class TestBytes extends TestCase {
assertNotNull(ee); assertNotNull(ee);
} }
public void testAdd () throws Exception { public void testAdd() {
byte[] a = {0,0,0,0,0,0,0,0,0,0}; byte[] a = {0,0,0,0,0,0,0,0,0,0};
byte[] b = {1,1,1,1,1,1,1,1,1,1,1}; byte[] b = {1,1,1,1,1,1,1,1,1,1,1};
byte[] c = {2,2,2,2,2,2,2,2,2,2,2,2}; byte[] c = {2,2,2,2,2,2,2,2,2,2,2,2};
byte[] d = {3,3,3,3,3,3,3,3,3,3,3,3,3}; byte[] d = {3,3,3,3,3,3,3,3,3,3,3,3,3};
byte[] result1 = Bytes.add (a, b, c); byte[] result1 = Bytes.add(a, b, c);
byte[] result2 = Bytes.add (new byte[][] {a, b, c}); byte[] result2 = Bytes.add(new byte[][] {a, b, c});
assertEquals(0, Bytes.compareTo(result1, result2)); assertEquals(0, Bytes.compareTo(result1, result2));
byte[] result4 = Bytes.add (result1, d); byte[] result4 = Bytes.add(result1, d);
byte[] result5 = Bytes.add (new byte[][] {result1, d}); byte[] result5 = Bytes.add(new byte[][] {result1, d});
assertEquals(0, Bytes.compareTo(result1, result2)); assertEquals(0, Bytes.compareTo(result1, result2));
} }
public void testSplit() throws Exception { public void testSplit() {
byte [] lowest = Bytes.toBytes("AAA"); byte[] lowest = Bytes.toBytes("AAA");
byte [] middle = Bytes.toBytes("CCC"); byte[] middle = Bytes.toBytes("CCC");
byte [] highest = Bytes.toBytes("EEE"); byte[] highest = Bytes.toBytes("EEE");
byte [][] parts = Bytes.split(lowest, highest, 1); byte[][] parts = Bytes.split(lowest, highest, 1);
for (int i = 0; i < parts.length; i++) { for (byte[] bytes : parts) {
System.out.println(Bytes.toString(parts[i])); System.out.println(Bytes.toString(bytes));
} }
assertEquals(3, parts.length); assertEquals(3, parts.length);
assertTrue(Bytes.equals(parts[1], middle)); assertTrue(Bytes.equals(parts[1], middle));
// Now divide into three parts. Change highest so split is even. // Now divide into three parts. Change highest so split is even.
highest = Bytes.toBytes("DDD"); highest = Bytes.toBytes("DDD");
parts = Bytes.split(lowest, highest, 2); parts = Bytes.split(lowest, highest, 2);
for (int i = 0; i < parts.length; i++) { for (byte[] part : parts) {
System.out.println(Bytes.toString(parts[i])); System.out.println(Bytes.toString(part));
} }
assertEquals(4, parts.length); assertEquals(4, parts.length);
// Assert that 3rd part is 'CCC'. // Assert that 3rd part is 'CCC'.
assertTrue(Bytes.equals(parts[2], middle)); assertTrue(Bytes.equals(parts[2], middle));
} }
public void testSplit2() throws Exception { public void testSplit2() {
// More split tests. // More split tests.
byte [] lowest = Bytes.toBytes("http://A"); byte [] lowest = Bytes.toBytes("http://A");
byte [] highest = Bytes.toBytes("http://z"); byte [] highest = Bytes.toBytes("http://z");
byte [] middle = Bytes.toBytes("http://]"); byte [] middle = Bytes.toBytes("http://]");
byte [][] parts = Bytes.split(lowest, highest, 1); byte [][] parts = Bytes.split(lowest, highest, 1);
for (int i = 0; i < parts.length; i++) { for (byte[] part : parts) {
System.out.println(Bytes.toString(parts[i])); System.out.println(Bytes.toString(part));
} }
assertEquals(3, parts.length); assertEquals(3, parts.length);
assertTrue(Bytes.equals(parts[1], middle)); assertTrue(Bytes.equals(parts[1], middle));
} }
public void testSplit3() throws Exception { public void testSplit3() {
// Test invalid split cases // Test invalid split cases
byte [] low = { 1, 1, 1 }; byte[] low = { 1, 1, 1 };
byte [] high = { 1, 1, 3 }; byte[] high = { 1, 1, 3 };
// If swapped, should throw IAE // If swapped, should throw IAE
try { try {
Bytes.split(high, low, 1); Bytes.split(high, low, 1);
assertTrue("Should not be able to split if low > high", false); fail("Should not be able to split if low > high");
} catch(IllegalArgumentException iae) { } catch(IllegalArgumentException iae) {
// Correct // Correct
} }
// Single split should work // Single split should work
byte [][] parts = Bytes.split(low, high, 1); byte[][] parts = Bytes.split(low, high, 1);
for (int i = 0; i < parts.length; i++) { for (int i = 0; i < parts.length; i++) {
System.out.println("" + i + " -> " + Bytes.toStringBinary(parts[i])); System.out.println("" + i + " -> " + Bytes.toStringBinary(parts[i]));
} }
assertTrue("Returned split should have 3 parts but has " + parts.length, parts.length == 3); assertEquals("Returned split should have 3 parts but has " + parts.length, 3, parts.length);
// If split more than once, use additional byte to split // If split more than once, use additional byte to split
parts = Bytes.split(low, high, 2); parts = Bytes.split(low, high, 2);
assertTrue("Split with an additional byte", parts != null); assertNotNull("Split with an additional byte", parts);
assertEquals(parts.length, low.length + 1); assertEquals(parts.length, low.length + 1);
// Split 0 times should throw IAE // Split 0 times should throw IAE
try { try {
parts = Bytes.split(low, high, 0); Bytes.split(low, high, 0);
assertTrue("Should not be able to split 0 times", false); fail("Should not be able to split 0 times");
} catch(IllegalArgumentException iae) { } catch(IllegalArgumentException iae) {
// Correct // Correct
} }
} }
public void testToInt() throws Exception { public void testToInt() {
int [] ints = {-1, 123, Integer.MIN_VALUE, Integer.MAX_VALUE}; int[] ints = { -1, 123, Integer.MIN_VALUE, Integer.MAX_VALUE };
for (int i = 0; i < ints.length; i++) { for (int anInt : ints) {
byte [] b = Bytes.toBytes(ints[i]); byte[] b = Bytes.toBytes(anInt);
assertEquals(ints[i], Bytes.toInt(b)); assertEquals(anInt, Bytes.toInt(b));
byte [] b2 = bytesWithOffset(b); byte[] b2 = bytesWithOffset(b);
assertEquals(ints[i], Bytes.toInt(b2, 1)); assertEquals(anInt, Bytes.toInt(b2, 1));
assertEquals(ints[i], Bytes.toInt(b2, 1, Bytes.SIZEOF_INT)); assertEquals(anInt, Bytes.toInt(b2, 1, Bytes.SIZEOF_INT));
} }
} }
public void testToLong() throws Exception { public void testToLong() {
long [] longs = {-1l, 123l, Long.MIN_VALUE, Long.MAX_VALUE}; long[] longs = { -1L, 123L, Long.MIN_VALUE, Long.MAX_VALUE };
for (int i = 0; i < longs.length; i++) { for (long aLong : longs) {
byte [] b = Bytes.toBytes(longs[i]); byte[] b = Bytes.toBytes(aLong);
assertEquals(longs[i], Bytes.toLong(b)); assertEquals(aLong, Bytes.toLong(b));
byte [] b2 = bytesWithOffset(b); byte[] b2 = bytesWithOffset(b);
assertEquals(longs[i], Bytes.toLong(b2, 1)); assertEquals(aLong, Bytes.toLong(b2, 1));
assertEquals(longs[i], Bytes.toLong(b2, 1, Bytes.SIZEOF_LONG)); assertEquals(aLong, Bytes.toLong(b2, 1, Bytes.SIZEOF_LONG));
} }
} }
public void testToFloat() throws Exception { public void testToFloat() {
float [] floats = {-1f, 123.123f, Float.MAX_VALUE}; float[] floats = { -1f, 123.123f, Float.MAX_VALUE };
for (int i = 0; i < floats.length; i++) { for (float aFloat : floats) {
byte [] b = Bytes.toBytes(floats[i]); byte[] b = Bytes.toBytes(aFloat);
assertEquals(floats[i], Bytes.toFloat(b), 0.0f); assertEquals(aFloat, Bytes.toFloat(b), 0.0f);
byte [] b2 = bytesWithOffset(b); byte[] b2 = bytesWithOffset(b);
assertEquals(floats[i], Bytes.toFloat(b2, 1), 0.0f); assertEquals(aFloat, Bytes.toFloat(b2, 1), 0.0f);
} }
} }
public void testToDouble() throws Exception { public void testToDouble() {
double [] doubles = {Double.MIN_VALUE, Double.MAX_VALUE}; double [] doubles = {Double.MIN_VALUE, Double.MAX_VALUE};
for (int i = 0; i < doubles.length; i++) { for (double aDouble : doubles) {
byte [] b = Bytes.toBytes(doubles[i]); byte[] b = Bytes.toBytes(aDouble);
assertEquals(doubles[i], Bytes.toDouble(b), 0.0); assertEquals(aDouble, Bytes.toDouble(b), 0.0);
byte [] b2 = bytesWithOffset(b); byte[] b2 = bytesWithOffset(b);
assertEquals(doubles[i], Bytes.toDouble(b2, 1), 0.0); assertEquals(aDouble, Bytes.toDouble(b2, 1), 0.0);
} }
} }
public void testToBigDecimal() throws Exception { public void testToBigDecimal() {
BigDecimal [] decimals = {new BigDecimal("-1"), new BigDecimal("123.123"), BigDecimal[] decimals = { new BigDecimal("-1"), new BigDecimal("123.123"),
new BigDecimal("123123123123")}; new BigDecimal("123123123123") };
for (int i = 0; i < decimals.length; i++) { for (BigDecimal decimal : decimals) {
byte [] b = Bytes.toBytes(decimals[i]); byte[] b = Bytes.toBytes(decimal);
assertEquals(decimals[i], Bytes.toBigDecimal(b)); assertEquals(decimal, Bytes.toBigDecimal(b));
byte [] b2 = bytesWithOffset(b); byte[] b2 = bytesWithOffset(b);
assertEquals(decimals[i], Bytes.toBigDecimal(b2, 1, b.length)); assertEquals(decimal, Bytes.toBigDecimal(b2, 1, b.length));
} }
} }
private byte [] bytesWithOffset(byte [] src) { private byte[] bytesWithOffset(byte[] src) {
// add one byte in front to test offset // add one byte in front to test offset
byte [] result = new byte[src.length + 1]; byte [] result = new byte[src.length + 1];
result[0] = (byte) 0xAA; result[0] = (byte) 0xAA;
@ -249,7 +249,7 @@ public class TestBytes extends TestCase {
byte[] actual = Bytes.toBytes(target); byte[] actual = Bytes.toBytes(target);
byte[] expected = { 0, 1, 2, 3, 4, 5, 6 }; byte[] expected = { 0, 1, 2, 3, 4, 5, 6 };
assertTrue(Arrays.equals(expected, actual)); assertArrayEquals(expected, actual);
assertEquals(2, target.position()); assertEquals(2, target.position());
assertEquals(7, target.limit()); assertEquals(7, target.limit());
@ -259,7 +259,7 @@ public class TestBytes extends TestCase {
byte[] actual2 = Bytes.toBytes(target2); byte[] actual2 = Bytes.toBytes(target2);
byte[] expected2 = { 2, 3, 4, 5, 6 }; byte[] expected2 = { 2, 3, 4, 5, 6 };
assertTrue(Arrays.equals(expected2, actual2)); assertArrayEquals(expected2, actual2);
assertEquals(0, target2.position()); assertEquals(0, target2.position());
assertEquals(5, target2.limit()); assertEquals(5, target2.limit());
} }
@ -272,21 +272,21 @@ public class TestBytes extends TestCase {
byte[] actual = Bytes.getBytes(target); byte[] actual = Bytes.getBytes(target);
byte[] expected = { 2, 3, 4, 5, 6 }; byte[] expected = { 2, 3, 4, 5, 6 };
assertTrue(Arrays.equals(expected, actual)); assertArrayEquals(expected, actual);
assertEquals(2, target.position()); assertEquals(2, target.position());
assertEquals(7, target.limit()); assertEquals(7, target.limit());
} }
public void testReadAsVLong() throws Exception { public void testReadAsVLong() throws Exception {
long [] longs = {-1l, 123l, Long.MIN_VALUE, Long.MAX_VALUE}; long[] longs = { -1L, 123L, Long.MIN_VALUE, Long.MAX_VALUE };
for (int i = 0; i < longs.length; i++) { for (long aLong : longs) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(baos); DataOutputStream output = new DataOutputStream(baos);
WritableUtils.writeVLong(output, longs[i]); WritableUtils.writeVLong(output, aLong);
byte[] long_bytes_no_offset = baos.toByteArray(); byte[] long_bytes_no_offset = baos.toByteArray();
assertEquals(longs[i], Bytes.readAsVLong(long_bytes_no_offset, 0)); assertEquals(aLong, Bytes.readAsVLong(long_bytes_no_offset, 0));
byte[] long_bytes_with_offset = bytesWithOffset(long_bytes_no_offset); byte[] long_bytes_with_offset = bytesWithOffset(long_bytes_no_offset);
assertEquals(longs[i], Bytes.readAsVLong(long_bytes_with_offset, 1)); assertEquals(aLong, Bytes.readAsVLong(long_bytes_with_offset, 1));
} }
} }
@ -317,23 +317,23 @@ public class TestBytes extends TestCase {
assertEquals(expected, actual); assertEquals(expected, actual);
} }
public void testBinarySearch() throws Exception { public void testBinarySearch() {
byte [][] arr = { byte[][] arr = {
{1}, { 1 },
{3}, { 3 },
{5}, { 5 },
{7}, { 7 },
{9}, { 9 },
{11}, { 11 },
{13}, { 13 },
{15}, { 15 },
}; };
byte [] key1 = {3,1}; byte[] key1 = { 3, 1 };
byte [] key2 = {4,9}; byte[] key2 = { 4, 9 };
byte [] key2_2 = {4}; byte[] key2_2 = { 4 };
byte [] key3 = {5,11}; byte[] key3 = { 5, 11 };
byte [] key4 = {0}; byte[] key4 = { 0 };
byte [] key5 = {2}; byte[] key5 = { 2 };
assertEquals(1, Bytes.binarySearch(arr, key1, 0, 1)); assertEquals(1, Bytes.binarySearch(arr, key1, 0, 1));
assertEquals(0, Bytes.binarySearch(arr, key1, 1, 1)); assertEquals(0, Bytes.binarySearch(arr, key1, 1, 1));
@ -389,8 +389,7 @@ public class TestBytes extends TestCase {
assertFalse(Bytes.startsWith(Bytes.toBytes(""), Bytes.toBytes("hello"))); assertFalse(Bytes.startsWith(Bytes.toBytes(""), Bytes.toBytes("hello")));
} }
public void testIncrementBytes() throws IOException { public void testIncrementBytes() {
assertTrue(checkTestIncrementBytes(10, 1)); assertTrue(checkTestIncrementBytes(10, 1));
assertTrue(checkTestIncrementBytes(12, 123435445)); assertTrue(checkTestIncrementBytes(12, 123435445));
assertTrue(checkTestIncrementBytes(124634654, 1)); assertTrue(checkTestIncrementBytes(124634654, 1));
@ -410,10 +409,9 @@ public class TestBytes extends TestCase {
assertTrue(checkTestIncrementBytes(-1546543452, -34565445)); assertTrue(checkTestIncrementBytes(-1546543452, -34565445));
} }
private static boolean checkTestIncrementBytes(long val, long amount) private static boolean checkTestIncrementBytes(long val, long amount) {
throws IOException {
byte[] value = Bytes.toBytes(val); byte[] value = Bytes.toBytes(val);
byte [] testValue = {-1, -1, -1, -1, -1, -1, -1, -1}; byte[] testValue = { -1, -1, -1, -1, -1, -1, -1, -1 };
if (value[0] > 0) { if (value[0] > 0) {
testValue = new byte[Bytes.SIZEOF_LONG]; testValue = new byte[Bytes.SIZEOF_LONG];
} }
@ -450,14 +448,14 @@ public class TestBytes extends TestCase {
assertEquals("", Bytes.readStringFixedSize(dis, 9)); assertEquals("", Bytes.readStringFixedSize(dis, 9));
} }
public void testCopy() throws Exception { public void testCopy() {
byte [] bytes = Bytes.toBytes("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); byte[] bytes = Bytes.toBytes("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
byte [] copy = Bytes.copy(bytes); byte[] copy = Bytes.copy(bytes);
assertFalse(bytes == copy); assertNotSame(bytes, copy);
assertTrue(Bytes.equals(bytes, copy)); assertTrue(Bytes.equals(bytes, copy));
} }
public void testToBytesBinaryTrailingBackslashes() throws Exception { public void testToBytesBinaryTrailingBackslashes() {
try { try {
Bytes.toBytesBinary("abc\\x00\\x01\\"); Bytes.toBytesBinary("abc\\x00\\x01\\");
} catch (StringIndexOutOfBoundsException ex) { } catch (StringIndexOutOfBoundsException ex) {
@ -465,13 +463,13 @@ public class TestBytes extends TestCase {
} }
} }
public void testToStringBinary_toBytesBinary_Reversable() throws Exception { public void testToStringBinary_toBytesBinary_Reversable() {
String bytes = Bytes.toStringBinary(Bytes.toBytes(2.17)); String bytes = Bytes.toStringBinary(Bytes.toBytes(2.17));
assertEquals(2.17, Bytes.toDouble(Bytes.toBytesBinary(bytes)), 0); assertEquals(2.17, Bytes.toDouble(Bytes.toBytesBinary(bytes)), 0);
} }
public void testUnsignedBinarySearch(){ public void testUnsignedBinarySearch(){
byte[] bytes = new byte[]{0,5,123,127,-128,-100,-1}; byte[] bytes = new byte[] { 0,5,123,127,-128,-100,-1 };
Assert.assertEquals(1, Bytes.unsignedBinarySearch(bytes, 0, bytes.length, (byte)5)); Assert.assertEquals(1, Bytes.unsignedBinarySearch(bytes, 0, bytes.length, (byte)5));
Assert.assertEquals(3, Bytes.unsignedBinarySearch(bytes, 0, bytes.length, (byte)127)); Assert.assertEquals(3, Bytes.unsignedBinarySearch(bytes, 0, bytes.length, (byte)127));
Assert.assertEquals(4, Bytes.unsignedBinarySearch(bytes, 0, bytes.length, (byte)-128)); Assert.assertEquals(4, Bytes.unsignedBinarySearch(bytes, 0, bytes.length, (byte)-128));
@ -484,17 +482,17 @@ public class TestBytes extends TestCase {
public void testUnsignedIncrement(){ public void testUnsignedIncrement(){
byte[] a = Bytes.toBytes(0); byte[] a = Bytes.toBytes(0);
int a2 = Bytes.toInt(Bytes.unsignedCopyAndIncrement(a), 0); int a2 = Bytes.toInt(Bytes.unsignedCopyAndIncrement(a), 0);
Assert.assertTrue(a2==1); Assert.assertEquals(1, a2);
byte[] b = Bytes.toBytes(-1); byte[] b = Bytes.toBytes(-1);
byte[] actual = Bytes.unsignedCopyAndIncrement(b); byte[] actual = Bytes.unsignedCopyAndIncrement(b);
Assert.assertNotSame(b, actual); Assert.assertNotSame(b, actual);
byte[] expected = new byte[]{1,0,0,0,0}; byte[] expected = new byte[]{1,0,0,0,0};
Assert.assertArrayEquals(expected, actual); assertArrayEquals(expected, actual);
byte[] c = Bytes.toBytes(255);//should wrap to the next significant byte byte[] c = Bytes.toBytes(255);//should wrap to the next significant byte
int c2 = Bytes.toInt(Bytes.unsignedCopyAndIncrement(c), 0); int c2 = Bytes.toInt(Bytes.unsignedCopyAndIncrement(c), 0);
Assert.assertTrue(c2==256); Assert.assertEquals(256, c2);
} }
public void testIndexOf() { public void testIndexOf() {
@ -511,7 +509,7 @@ public class TestBytes extends TestCase {
byte[] array = Bytes.toBytes("hello world"); byte[] array = Bytes.toBytes("hello world");
assertTrue(Bytes.contains(array, (byte) 'e')); assertTrue(Bytes.contains(array, (byte) 'e'));
assertTrue(Bytes.contains(array, (byte) 'd')); assertTrue(Bytes.contains(array, (byte) 'd'));
assertFalse( Bytes.contains(array, (byte) 'a')); assertFalse(Bytes.contains(array, (byte) 'a'));
assertTrue(Bytes.contains(array, Bytes.toBytes("world"))); assertTrue(Bytes.contains(array, Bytes.toBytes("world")));
assertTrue(Bytes.contains(array, Bytes.toBytes("ello"))); assertTrue(Bytes.contains(array, Bytes.toBytes("ello")));
assertFalse(Bytes.contains(array, Bytes.toBytes("owo"))); assertFalse(Bytes.contains(array, Bytes.toBytes("owo")));
@ -520,8 +518,8 @@ public class TestBytes extends TestCase {
public void testZero() { public void testZero() {
byte[] array = Bytes.toBytes("hello"); byte[] array = Bytes.toBytes("hello");
Bytes.zero(array); Bytes.zero(array);
for (int i = 0; i < array.length; i++) { for (byte b : array) {
assertEquals(0, array[i]); assertEquals(0, b);
} }
array = Bytes.toBytes("hello world"); array = Bytes.toBytes("hello world");
Bytes.zero(array, 2, 7); Bytes.zero(array, 2, 7);
@ -538,7 +536,7 @@ public class TestBytes extends TestCase {
public void testPutBuffer() { public void testPutBuffer() {
byte[] b = new byte[100]; byte[] b = new byte[100];
for (byte i = 0; i < 100; i++) { for (byte i = 0; i < 100; i++) {
Bytes.putByteBuffer(b, i, ByteBuffer.wrap(new byte[]{i})); Bytes.putByteBuffer(b, i, ByteBuffer.wrap(new byte[] { i }));
} }
for (byte i = 0; i < 100; i++) { for (byte i = 0; i < 100; i++) {
Assert.assertEquals(i, b[i]); Assert.assertEquals(i, b[i]);
@ -547,18 +545,9 @@ public class TestBytes extends TestCase {
public void testToFromHex() { public void testToFromHex() {
List<String> testStrings = new ArrayList<>(8); List<String> testStrings = new ArrayList<>(8);
testStrings.addAll(Arrays.asList(new String[] { testStrings.addAll(Arrays.asList("", "00", "A0", "ff", "FFffFFFFFFFFFF", "12",
"", "0123456789abcdef", "283462839463924623984692834692346ABCDFEDDCA0"));
"00", for (String testString : testStrings) {
"A0",
"ff",
"FFffFFFFFFFFFF",
"12",
"0123456789abcdef",
"283462839463924623984692834692346ABCDFEDDCA0",
}));
for (String testString : testStrings)
{
byte[] byteData = Bytes.fromHex(testString); byte[] byteData = Bytes.fromHex(testString);
Assert.assertEquals(testString.length() / 2, byteData.length); Assert.assertEquals(testString.length() / 2, byteData.length);
String result = Bytes.toHex(byteData); String result = Bytes.toHex(byteData);
@ -566,27 +555,20 @@ public class TestBytes extends TestCase {
} }
List<byte[]> testByteData = new ArrayList<>(5); List<byte[]> testByteData = new ArrayList<>(5);
testByteData.addAll(Arrays.asList(new byte[][] { testByteData.addAll(Arrays.asList(new byte[0], new byte[1], new byte[10],
new byte[0], new byte[] { 1, 2, 3, 4, 5 }, new byte[] { (byte) 0xFF }));
new byte[1],
new byte[10],
new byte[] {1, 2, 3, 4, 5},
new byte[] {(byte) 0xFF},
}));
Random r = new Random(); Random r = new Random();
for (int i = 0; i < 20; i++) for (int i = 0; i < 20; i++) {
{
byte[] bytes = new byte[r.nextInt(100)]; byte[] bytes = new byte[r.nextInt(100)];
r.nextBytes(bytes); r.nextBytes(bytes);
testByteData.add(bytes); testByteData.add(bytes);
} }
for (byte[] testData : testByteData) for (byte[] testData : testByteData) {
{
String hexString = Bytes.toHex(testData); String hexString = Bytes.toHex(testData);
Assert.assertEquals(testData.length * 2, hexString.length()); Assert.assertEquals(testData.length * 2, hexString.length());
byte[] result = Bytes.fromHex(hexString); byte[] result = Bytes.fromHex(hexString);
Assert.assertArrayEquals(testData, result); assertArrayEquals(testData, result);
} }
} }
} }

View File

@ -42,7 +42,6 @@ import org.junit.experimental.categories.Category;
*/ */
@Category({MiscTests.class, SmallTests.class}) @Category({MiscTests.class, SmallTests.class})
public class TestCoprocessorClassLoader { public class TestCoprocessorClassLoader {
@ClassRule @ClassRule
public static final HBaseClassTestRule CLASS_RULE = public static final HBaseClassTestRule CLASS_RULE =
HBaseClassTestRule.forClass(TestCoprocessorClassLoader.class); HBaseClassTestRule.forClass(TestCoprocessorClassLoader.class);
@ -60,7 +59,11 @@ public class TestCoprocessorClassLoader {
File jarFile = ClassLoaderTestHelper.buildJar( File jarFile = ClassLoaderTestHelper.buildJar(
folder, className, null, ClassLoaderTestHelper.localDirPath(conf)); folder, className, null, ClassLoaderTestHelper.localDirPath(conf));
File tmpJarFile = new File(jarFile.getParent(), "/tmp/" + className + ".test.jar"); File tmpJarFile = new File(jarFile.getParent(), "/tmp/" + className + ".test.jar");
if (tmpJarFile.exists()) tmpJarFile.delete();
if (tmpJarFile.exists()) {
tmpJarFile.delete();
}
assertFalse("tmp jar file should not exist", tmpJarFile.exists()); assertFalse("tmp jar file should not exist", tmpJarFile.exists());
ClassLoader parent = TestCoprocessorClassLoader.class.getClassLoader(); ClassLoader parent = TestCoprocessorClassLoader.class.getClassLoader();
CoprocessorClassLoader.getClassLoader(new Path(jarFile.getParent()), parent, "112", conf); CoprocessorClassLoader.getClassLoader(new Path(jarFile.getParent()), parent, "112", conf);
@ -133,12 +136,10 @@ public class TestCoprocessorClassLoader {
ClassLoader parent = TestCoprocessorClassLoader.class.getClassLoader(); ClassLoader parent = TestCoprocessorClassLoader.class.getClassLoader();
CoprocessorClassLoader.parentDirLockSet.clear(); // So that clean up can be triggered CoprocessorClassLoader.parentDirLockSet.clear(); // So that clean up can be triggered
CoprocessorClassLoader coprocessorClassLoader = null;
Path testPath = null;
// Directory // Directory
testPath = new Path(localDirContainingJar); Path testPath = new Path(localDirContainingJar);
coprocessorClassLoader = CoprocessorClassLoader.getClassLoader(testPath, parent, "113_1", conf); CoprocessorClassLoader coprocessorClassLoader = CoprocessorClassLoader.getClassLoader(testPath,
parent, "113_1", conf);
verifyCoprocessorClassLoader(coprocessorClassLoader, testClassName); verifyCoprocessorClassLoader(coprocessorClassLoader, testClassName);
// Wildcard - *.jar // Wildcard - *.jar
@ -156,10 +157,11 @@ public class TestCoprocessorClassLoader {
* Verify the coprocessorClassLoader is not null and the expected class can be loaded successfully * Verify the coprocessorClassLoader is not null and the expected class can be loaded successfully
* @param coprocessorClassLoader the CoprocessorClassLoader to verify * @param coprocessorClassLoader the CoprocessorClassLoader to verify
* @param className the expected class to be loaded by the coprocessorClassLoader * @param className the expected class to be loaded by the coprocessorClassLoader
* @throws ClassNotFoundException * @throws ClassNotFoundException if the class, which should be loaded via the
* coprocessorClassLoader, does not exist
*/ */
private void verifyCoprocessorClassLoader(CoprocessorClassLoader coprocessorClassLoader, String className) private void verifyCoprocessorClassLoader(CoprocessorClassLoader coprocessorClassLoader,
throws ClassNotFoundException{ String className) throws ClassNotFoundException {
assertNotNull("Classloader should be created and not null", coprocessorClassLoader); assertNotNull("Classloader should be created and not null", coprocessorClassLoader);
assertEquals(className, coprocessorClassLoader.loadClass(className).getName()); assertEquals(className, coprocessorClassLoader.loadClass(className).getName());
} }

View File

@ -116,12 +116,13 @@ public class TestOrderedBytes {
*/ */
@Test @Test
public void testVaruint64Boundaries() { public void testVaruint64Boundaries() {
long vals[] = long[] vals = {
{ 239L, 240L, 2286L, 2287L, 67822L, 67823L, 16777214L, 16777215L, 4294967294L, 4294967295L, 239L, 240L, 2286L, 2287L, 67822L, 67823L, 16777214L, 16777215L, 4294967294L, 4294967295L,
1099511627774L, 1099511627775L, 281474976710654L, 281474976710655L, 72057594037927934L, 1099511627774L, 1099511627775L, 281474976710654L, 281474976710655L, 72057594037927934L,
72057594037927935L, Long.MAX_VALUE - 1, Long.MAX_VALUE, Long.MIN_VALUE + 1, 72057594037927935L, Long.MAX_VALUE - 1, Long.MAX_VALUE, Long.MIN_VALUE + 1,
Long.MIN_VALUE, -2L, -1L }; Long.MIN_VALUE, -2L, -1L
int lens[] = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 9, 9, 9, 9 }; };
int[] lens = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 9, 9, 9, 9 };
assertEquals("Broken test!", vals.length, lens.length); assertEquals("Broken test!", vals.length, lens.length);
/* /*
@ -208,8 +209,12 @@ public class TestOrderedBytes {
Arrays.sort(encoded, Bytes.BYTES_COMPARATOR); Arrays.sort(encoded, Bytes.BYTES_COMPARATOR);
Long[] sortedVals = Arrays.copyOf(I_VALS, I_VALS.length); Long[] sortedVals = Arrays.copyOf(I_VALS, I_VALS.length);
if (ord == Order.ASCENDING) Arrays.sort(sortedVals);
else Arrays.sort(sortedVals, Collections.reverseOrder()); if (ord == Order.ASCENDING) {
Arrays.sort(sortedVals);
} else {
Arrays.sort(sortedVals, Collections.reverseOrder());
}
for (int i = 0; i < sortedVals.length; i++) { for (int i = 0; i < sortedVals.length; i++) {
pbr.set(encoded[i]); pbr.set(encoded[i]);
@ -256,8 +261,8 @@ public class TestOrderedBytes {
// verify decode // verify decode
buf1.setPosition(1); buf1.setPosition(1);
assertEquals("Deserialization failed.", assertEquals("Deserialization failed.", D_VALS[i],
D_VALS[i].doubleValue(), OrderedBytes.decodeNumericAsDouble(buf1), MIN_EPSILON); OrderedBytes.decodeNumericAsDouble(buf1), MIN_EPSILON);
assertEquals("Did not consume enough bytes.", D_LENGTHS[i], buf1.getPosition() - 1); assertEquals("Did not consume enough bytes.", D_LENGTHS[i], buf1.getPosition() - 1);
} }
} }
@ -275,8 +280,12 @@ public class TestOrderedBytes {
Arrays.sort(encoded, Bytes.BYTES_COMPARATOR); Arrays.sort(encoded, Bytes.BYTES_COMPARATOR);
Double[] sortedVals = Arrays.copyOf(D_VALS, D_VALS.length); Double[] sortedVals = Arrays.copyOf(D_VALS, D_VALS.length);
if (ord == Order.ASCENDING) Arrays.sort(sortedVals);
else Arrays.sort(sortedVals, Collections.reverseOrder()); if (ord == Order.ASCENDING) {
Arrays.sort(sortedVals);
} else {
Arrays.sort(sortedVals, Collections.reverseOrder());
}
for (int i = 0; i < sortedVals.length; i++) { for (int i = 0; i < sortedVals.length; i++) {
pbr.set(encoded[i]); pbr.set(encoded[i]);
@ -284,8 +293,7 @@ public class TestOrderedBytes {
assertEquals( assertEquals(
String.format( String.format(
"Encoded representations do not preserve natural order: <%s>, <%s>, %s", "Encoded representations do not preserve natural order: <%s>, <%s>, %s",
sortedVals[i], decoded, ord), sortedVals[i], decoded, ord), sortedVals[i], decoded, MIN_EPSILON);
sortedVals[i].doubleValue(), decoded, MIN_EPSILON);
} }
} }
} }
@ -378,17 +386,16 @@ public class TestOrderedBytes {
* starting at an offset to detect over/underflow conditions. * starting at an offset to detect over/underflow conditions.
*/ */
for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
for (int i = 0; i < vals.length; i++) { for (Byte val : vals) {
// allocate a buffer 3-bytes larger than necessary to detect over/underflow // allocate a buffer 3-bytes larger than necessary to detect over/underflow
byte[] a = new byte[2 + 3]; byte[] a = new byte[2 + 3];
PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, 2 + 1); PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, 2 + 1);
buf1.setPosition(1); buf1.setPosition(1);
// verify encode // verify encode
assertEquals("Surprising return value.", assertEquals("Surprising return value.", 2, OrderedBytes.encodeInt8(buf1, val, ord));
2, OrderedBytes.encodeInt8(buf1, vals[i], ord)); assertEquals("Broken test: serialization did not consume entire buffer.", buf1.getLength(),
assertEquals("Broken test: serialization did not consume entire buffer.", buf1.getPosition());
buf1.getLength(), buf1.getPosition());
assertEquals("Surprising serialized length.", 2, buf1.getPosition() - 1); assertEquals("Surprising serialized length.", 2, buf1.getPosition() - 1);
assertEquals("Buffer underflow.", 0, a[0]); assertEquals("Buffer underflow.", 0, a[0]);
assertEquals("Buffer underflow.", 0, a[1]); assertEquals("Buffer underflow.", 0, a[1]);
@ -401,8 +408,7 @@ public class TestOrderedBytes {
// verify decode // verify decode
buf1.setPosition(1); buf1.setPosition(1);
assertEquals("Deserialization failed.", assertEquals("Deserialization failed.", val.byteValue(), OrderedBytes.decodeInt8(buf1));
vals[i].byteValue(), OrderedBytes.decodeInt8(buf1));
assertEquals("Did not consume enough bytes.", 2, buf1.getPosition() - 1); assertEquals("Did not consume enough bytes.", 2, buf1.getPosition() - 1);
} }
} }
@ -419,8 +425,12 @@ public class TestOrderedBytes {
Arrays.sort(encoded, Bytes.BYTES_COMPARATOR); Arrays.sort(encoded, Bytes.BYTES_COMPARATOR);
Byte[] sortedVals = Arrays.copyOf(vals, vals.length); Byte[] sortedVals = Arrays.copyOf(vals, vals.length);
if (ord == Order.ASCENDING) Arrays.sort(sortedVals);
else Arrays.sort(sortedVals, Collections.reverseOrder()); if (ord == Order.ASCENDING) {
Arrays.sort(sortedVals);
} else {
Arrays.sort(sortedVals, Collections.reverseOrder());
}
for (int i = 0; i < sortedVals.length; i++) { for (int i = 0; i < sortedVals.length; i++) {
int decoded = OrderedBytes.decodeInt8(pbr.set(encoded[i])); int decoded = OrderedBytes.decodeInt8(pbr.set(encoded[i]));
@ -446,17 +456,16 @@ public class TestOrderedBytes {
* starting at an offset to detect over/underflow conditions. * starting at an offset to detect over/underflow conditions.
*/ */
for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
for (int i = 0; i < vals.length; i++) { for (Short val : vals) {
// allocate a buffer 3-bytes larger than necessary to detect over/underflow // allocate a buffer 3-bytes larger than necessary to detect over/underflow
byte[] a = new byte[3 + 3]; byte[] a = new byte[3 + 3];
PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, 3 + 1); PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, 3 + 1);
buf1.setPosition(1); buf1.setPosition(1);
// verify encode // verify encode
assertEquals("Surprising return value.", assertEquals("Surprising return value.", 3, OrderedBytes.encodeInt16(buf1, val, ord));
3, OrderedBytes.encodeInt16(buf1, vals[i], ord)); assertEquals("Broken test: serialization did not consume entire buffer.", buf1.getLength(),
assertEquals("Broken test: serialization did not consume entire buffer.", buf1.getPosition());
buf1.getLength(), buf1.getPosition());
assertEquals("Surprising serialized length.", 3, buf1.getPosition() - 1); assertEquals("Surprising serialized length.", 3, buf1.getPosition() - 1);
assertEquals("Buffer underflow.", 0, a[0]); assertEquals("Buffer underflow.", 0, a[0]);
assertEquals("Buffer underflow.", 0, a[1]); assertEquals("Buffer underflow.", 0, a[1]);
@ -469,8 +478,7 @@ public class TestOrderedBytes {
// verify decode // verify decode
buf1.setPosition(1); buf1.setPosition(1);
assertEquals("Deserialization failed.", assertEquals("Deserialization failed.", val.shortValue(), OrderedBytes.decodeInt16(buf1));
vals[i].shortValue(), OrderedBytes.decodeInt16(buf1));
assertEquals("Did not consume enough bytes.", 3, buf1.getPosition() - 1); assertEquals("Did not consume enough bytes.", 3, buf1.getPosition() - 1);
} }
} }
@ -487,8 +495,12 @@ public class TestOrderedBytes {
Arrays.sort(encoded, Bytes.BYTES_COMPARATOR); Arrays.sort(encoded, Bytes.BYTES_COMPARATOR);
Short[] sortedVals = Arrays.copyOf(vals, vals.length); Short[] sortedVals = Arrays.copyOf(vals, vals.length);
if (ord == Order.ASCENDING) Arrays.sort(sortedVals);
else Arrays.sort(sortedVals, Collections.reverseOrder()); if (ord == Order.ASCENDING) {
Arrays.sort(sortedVals);
} else {
Arrays.sort(sortedVals, Collections.reverseOrder());
}
for (int i = 0; i < sortedVals.length; i++) { for (int i = 0; i < sortedVals.length; i++) {
int decoded = OrderedBytes.decodeInt16(pbr.set(encoded[i])); int decoded = OrderedBytes.decodeInt16(pbr.set(encoded[i]));
@ -514,17 +526,16 @@ public class TestOrderedBytes {
* starting at an offset to detect over/underflow conditions. * starting at an offset to detect over/underflow conditions.
*/ */
for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
for (int i = 0; i < vals.length; i++) { for (Integer val : vals) {
// allocate a buffer 3-bytes larger than necessary to detect over/underflow // allocate a buffer 3-bytes larger than necessary to detect over/underflow
byte[] a = new byte[5 + 3]; byte[] a = new byte[5 + 3];
PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, 5 + 1); PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, 5 + 1);
buf1.setPosition(1); buf1.setPosition(1);
// verify encode // verify encode
assertEquals("Surprising return value.", assertEquals("Surprising return value.", 5, OrderedBytes.encodeInt32(buf1, val, ord));
5, OrderedBytes.encodeInt32(buf1, vals[i], ord)); assertEquals("Broken test: serialization did not consume entire buffer.", buf1.getLength(),
assertEquals("Broken test: serialization did not consume entire buffer.", buf1.getPosition());
buf1.getLength(), buf1.getPosition());
assertEquals("Surprising serialized length.", 5, buf1.getPosition() - 1); assertEquals("Surprising serialized length.", 5, buf1.getPosition() - 1);
assertEquals("Buffer underflow.", 0, a[0]); assertEquals("Buffer underflow.", 0, a[0]);
assertEquals("Buffer underflow.", 0, a[1]); assertEquals("Buffer underflow.", 0, a[1]);
@ -537,8 +548,7 @@ public class TestOrderedBytes {
// verify decode // verify decode
buf1.setPosition(1); buf1.setPosition(1);
assertEquals("Deserialization failed.", assertEquals("Deserialization failed.", val.intValue(), OrderedBytes.decodeInt32(buf1));
vals[i].intValue(), OrderedBytes.decodeInt32(buf1));
assertEquals("Did not consume enough bytes.", 5, buf1.getPosition() - 1); assertEquals("Did not consume enough bytes.", 5, buf1.getPosition() - 1);
} }
} }
@ -555,8 +565,12 @@ public class TestOrderedBytes {
Arrays.sort(encoded, Bytes.BYTES_COMPARATOR); Arrays.sort(encoded, Bytes.BYTES_COMPARATOR);
Integer[] sortedVals = Arrays.copyOf(vals, vals.length); Integer[] sortedVals = Arrays.copyOf(vals, vals.length);
if (ord == Order.ASCENDING) Arrays.sort(sortedVals);
else Arrays.sort(sortedVals, Collections.reverseOrder()); if (ord == Order.ASCENDING) {
Arrays.sort(sortedVals);
} else {
Arrays.sort(sortedVals, Collections.reverseOrder());
}
for (int i = 0; i < sortedVals.length; i++) { for (int i = 0; i < sortedVals.length; i++) {
int decoded = OrderedBytes.decodeInt32(pbr.set(encoded[i])); int decoded = OrderedBytes.decodeInt32(pbr.set(encoded[i]));
@ -581,17 +595,16 @@ public class TestOrderedBytes {
* starting at an offset to detect over/underflow conditions. * starting at an offset to detect over/underflow conditions.
*/ */
for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
for (int i = 0; i < vals.length; i++) { for (Long val : vals) {
// allocate a buffer 3-bytes larger than necessary to detect over/underflow // allocate a buffer 3-bytes larger than necessary to detect over/underflow
byte[] a = new byte[9 + 3]; byte[] a = new byte[9 + 3];
PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, 9 + 1); PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, 9 + 1);
buf1.setPosition(1); buf1.setPosition(1);
// verify encode // verify encode
assertEquals("Surprising return value.", assertEquals("Surprising return value.", 9, OrderedBytes.encodeInt64(buf1, val, ord));
9, OrderedBytes.encodeInt64(buf1, vals[i], ord)); assertEquals("Broken test: serialization did not consume entire buffer.", buf1.getLength(),
assertEquals("Broken test: serialization did not consume entire buffer.", buf1.getPosition());
buf1.getLength(), buf1.getPosition());
assertEquals("Surprising serialized length.", 9, buf1.getPosition() - 1); assertEquals("Surprising serialized length.", 9, buf1.getPosition() - 1);
assertEquals("Buffer underflow.", 0, a[0]); assertEquals("Buffer underflow.", 0, a[0]);
assertEquals("Buffer underflow.", 0, a[1]); assertEquals("Buffer underflow.", 0, a[1]);
@ -604,8 +617,7 @@ public class TestOrderedBytes {
// verify decode // verify decode
buf1.setPosition(1); buf1.setPosition(1);
assertEquals("Deserialization failed.", assertEquals("Deserialization failed.", val.longValue(), OrderedBytes.decodeInt64(buf1));
vals[i].longValue(), OrderedBytes.decodeInt64(buf1));
assertEquals("Did not consume enough bytes.", 9, buf1.getPosition() - 1); assertEquals("Did not consume enough bytes.", 9, buf1.getPosition() - 1);
} }
} }
@ -622,8 +634,12 @@ public class TestOrderedBytes {
Arrays.sort(encoded, Bytes.BYTES_COMPARATOR); Arrays.sort(encoded, Bytes.BYTES_COMPARATOR);
Long[] sortedVals = Arrays.copyOf(vals, vals.length); Long[] sortedVals = Arrays.copyOf(vals, vals.length);
if (ord == Order.ASCENDING) Arrays.sort(sortedVals);
else Arrays.sort(sortedVals, Collections.reverseOrder()); if (ord == Order.ASCENDING) {
Arrays.sort(sortedVals);
} else {
Arrays.sort(sortedVals, Collections.reverseOrder());
}
for (int i = 0; i < sortedVals.length; i++) { for (int i = 0; i < sortedVals.length; i++) {
long decoded = OrderedBytes.decodeInt64(pbr.set(encoded[i])); long decoded = OrderedBytes.decodeInt64(pbr.set(encoded[i]));
@ -649,17 +665,16 @@ public class TestOrderedBytes {
* starting at an offset to detect over/underflow conditions. * starting at an offset to detect over/underflow conditions.
*/ */
for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
for (int i = 0; i < vals.length; i++) { for (Float val : vals) {
// allocate a buffer 3-bytes larger than necessary to detect over/underflow // allocate a buffer 3-bytes larger than necessary to detect over/underflow
byte[] a = new byte[5 + 3]; byte[] a = new byte[5 + 3];
PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, 5 + 1); PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, 5 + 1);
buf1.setPosition(1); buf1.setPosition(1);
// verify encode // verify encode
assertEquals("Surprising return value.", assertEquals("Surprising return value.", 5, OrderedBytes.encodeFloat32(buf1, val, ord));
5, OrderedBytes.encodeFloat32(buf1, vals[i], ord)); assertEquals("Broken test: serialization did not consume entire buffer.", buf1.getLength(),
assertEquals("Broken test: serialization did not consume entire buffer.", buf1.getPosition());
buf1.getLength(), buf1.getPosition());
assertEquals("Surprising serialized length.", 5, buf1.getPosition() - 1); assertEquals("Surprising serialized length.", 5, buf1.getPosition() - 1);
assertEquals("Buffer underflow.", 0, a[0]); assertEquals("Buffer underflow.", 0, a[0]);
assertEquals("Buffer underflow.", 0, a[1]); assertEquals("Buffer underflow.", 0, a[1]);
@ -672,8 +687,7 @@ public class TestOrderedBytes {
// verify decode // verify decode
buf1.setPosition(1); buf1.setPosition(1);
assertEquals("Deserialization failed.", assertEquals("Deserialization failed.", Float.floatToIntBits(val),
Float.floatToIntBits(vals[i].floatValue()),
Float.floatToIntBits(OrderedBytes.decodeFloat32(buf1))); Float.floatToIntBits(OrderedBytes.decodeFloat32(buf1)));
assertEquals("Did not consume enough bytes.", 5, buf1.getPosition() - 1); assertEquals("Did not consume enough bytes.", 5, buf1.getPosition() - 1);
} }
@ -691,8 +705,12 @@ public class TestOrderedBytes {
Arrays.sort(encoded, Bytes.BYTES_COMPARATOR); Arrays.sort(encoded, Bytes.BYTES_COMPARATOR);
Float[] sortedVals = Arrays.copyOf(vals, vals.length); Float[] sortedVals = Arrays.copyOf(vals, vals.length);
if (ord == Order.ASCENDING) Arrays.sort(sortedVals);
else Arrays.sort(sortedVals, Collections.reverseOrder()); if (ord == Order.ASCENDING) {
Arrays.sort(sortedVals);
} else {
Arrays.sort(sortedVals, Collections.reverseOrder());
}
for (int i = 0; i < sortedVals.length; i++) { for (int i = 0; i < sortedVals.length; i++) {
float decoded = OrderedBytes.decodeFloat32(pbr.set(encoded[i])); float decoded = OrderedBytes.decodeFloat32(pbr.set(encoded[i]));
@ -700,7 +718,7 @@ public class TestOrderedBytes {
String.format( String.format(
"Encoded representations do not preserve natural order: <%s>, <%s>, %s", "Encoded representations do not preserve natural order: <%s>, <%s>, %s",
sortedVals[i], decoded, ord), sortedVals[i], decoded, ord),
Float.floatToIntBits(sortedVals[i].floatValue()), Float.floatToIntBits(sortedVals[i]),
Float.floatToIntBits(decoded)); Float.floatToIntBits(decoded));
} }
} }
@ -719,17 +737,16 @@ public class TestOrderedBytes {
* starting at an offset to detect over/underflow conditions. * starting at an offset to detect over/underflow conditions.
*/ */
for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
for (int i = 0; i < vals.length; i++) { for (Double val : vals) {
// allocate a buffer 3-bytes larger than necessary to detect over/underflow // allocate a buffer 3-bytes larger than necessary to detect over/underflow
byte[] a = new byte[9 + 3]; byte[] a = new byte[9 + 3];
PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, 9 + 1); PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, 9 + 1);
buf1.setPosition(1); buf1.setPosition(1);
// verify encode // verify encode
assertEquals("Surprising return value.", assertEquals("Surprising return value.", 9, OrderedBytes.encodeFloat64(buf1, val, ord));
9, OrderedBytes.encodeFloat64(buf1, vals[i], ord)); assertEquals("Broken test: serialization did not consume entire buffer.", buf1.getLength(),
assertEquals("Broken test: serialization did not consume entire buffer.", buf1.getPosition());
buf1.getLength(), buf1.getPosition());
assertEquals("Surprising serialized length.", 9, buf1.getPosition() - 1); assertEquals("Surprising serialized length.", 9, buf1.getPosition() - 1);
assertEquals("Buffer underflow.", 0, a[0]); assertEquals("Buffer underflow.", 0, a[0]);
assertEquals("Buffer underflow.", 0, a[1]); assertEquals("Buffer underflow.", 0, a[1]);
@ -742,8 +759,7 @@ public class TestOrderedBytes {
// verify decode // verify decode
buf1.setPosition(1); buf1.setPosition(1);
assertEquals("Deserialization failed.", assertEquals("Deserialization failed.", Double.doubleToLongBits(val),
Double.doubleToLongBits(vals[i].doubleValue()),
Double.doubleToLongBits(OrderedBytes.decodeFloat64(buf1))); Double.doubleToLongBits(OrderedBytes.decodeFloat64(buf1)));
assertEquals("Did not consume enough bytes.", 9, buf1.getPosition() - 1); assertEquals("Did not consume enough bytes.", 9, buf1.getPosition() - 1);
} }
@ -761,8 +777,12 @@ public class TestOrderedBytes {
Arrays.sort(encoded, Bytes.BYTES_COMPARATOR); Arrays.sort(encoded, Bytes.BYTES_COMPARATOR);
Double[] sortedVals = Arrays.copyOf(vals, vals.length); Double[] sortedVals = Arrays.copyOf(vals, vals.length);
if (ord == Order.ASCENDING) Arrays.sort(sortedVals);
else Arrays.sort(sortedVals, Collections.reverseOrder()); if (ord == Order.ASCENDING) {
Arrays.sort(sortedVals);
} else {
Arrays.sort(sortedVals, Collections.reverseOrder());
}
for (int i = 0; i < sortedVals.length; i++) { for (int i = 0; i < sortedVals.length; i++) {
double decoded = OrderedBytes.decodeFloat64(pbr.set(encoded[i])); double decoded = OrderedBytes.decodeFloat64(pbr.set(encoded[i]));
@ -770,7 +790,7 @@ public class TestOrderedBytes {
String.format( String.format(
"Encoded representations do not preserve natural order: <%s>, <%s>, %s", "Encoded representations do not preserve natural order: <%s>, <%s>, %s",
sortedVals[i], decoded, ord), sortedVals[i], decoded, ord),
Double.doubleToLongBits(sortedVals[i].doubleValue()), Double.doubleToLongBits(sortedVals[i]),
Double.doubleToLongBits(decoded)); Double.doubleToLongBits(decoded));
} }
} }
@ -782,7 +802,7 @@ public class TestOrderedBytes {
@Test @Test
public void testString() { public void testString() {
String[] vals = { "foo", "baaaar", "bazz" }; String[] vals = { "foo", "baaaar", "bazz" };
int expectedLengths[] = { 5, 8, 6 }; int[] expectedLengths = { 5, 8, 6 };
/* /*
* assert encoded values match decoded values. encode into target buffer * assert encoded values match decoded values. encode into target buffer
@ -831,8 +851,12 @@ public class TestOrderedBytes {
Arrays.sort(encoded, Bytes.BYTES_COMPARATOR); Arrays.sort(encoded, Bytes.BYTES_COMPARATOR);
String[] sortedVals = Arrays.copyOf(vals, vals.length); String[] sortedVals = Arrays.copyOf(vals, vals.length);
if (ord == Order.ASCENDING) Arrays.sort(sortedVals);
else Arrays.sort(sortedVals, Collections.reverseOrder()); if (ord == Order.ASCENDING) {
Arrays.sort(sortedVals);
} else {
Arrays.sort(sortedVals, Collections.reverseOrder());
}
for (int i = 0; i < sortedVals.length; i++) { for (int i = 0; i < sortedVals.length; i++) {
pbr.set(encoded[i]); pbr.set(encoded[i]);
@ -952,8 +976,12 @@ public class TestOrderedBytes {
Arrays.sort(encoded, Bytes.BYTES_COMPARATOR); Arrays.sort(encoded, Bytes.BYTES_COMPARATOR);
byte[][] sortedVals = Arrays.copyOf(vals, vals.length); byte[][] sortedVals = Arrays.copyOf(vals, vals.length);
if (ord == Order.ASCENDING) Arrays.sort(sortedVals, Bytes.BYTES_COMPARATOR);
else Arrays.sort(sortedVals, Collections.reverseOrder(Bytes.BYTES_COMPARATOR)); if (ord == Order.ASCENDING) {
Arrays.sort(sortedVals, Bytes.BYTES_COMPARATOR);
} else {
Arrays.sort(sortedVals, Collections.reverseOrder(Bytes.BYTES_COMPARATOR));
}
for (int i = 0; i < sortedVals.length; i++) { for (int i = 0; i < sortedVals.length; i++) {
pbr.set(encoded[i]); pbr.set(encoded[i]);
@ -1029,8 +1057,12 @@ public class TestOrderedBytes {
Arrays.sort(encoded, Bytes.BYTES_COMPARATOR); Arrays.sort(encoded, Bytes.BYTES_COMPARATOR);
byte[][] sortedVals = Arrays.copyOf(vals, vals.length); byte[][] sortedVals = Arrays.copyOf(vals, vals.length);
if (ord == Order.ASCENDING) Arrays.sort(sortedVals, Bytes.BYTES_COMPARATOR);
else Arrays.sort(sortedVals, Collections.reverseOrder(Bytes.BYTES_COMPARATOR)); if (ord == Order.ASCENDING) {
Arrays.sort(sortedVals, Bytes.BYTES_COMPARATOR);
} else {
Arrays.sort(sortedVals, Collections.reverseOrder(Bytes.BYTES_COMPARATOR));
}
for (int i = 0; i < sortedVals.length; i++) { for (int i = 0; i < sortedVals.length; i++) {
pbr.set(encoded[i]); pbr.set(encoded[i]);
@ -1082,7 +1114,7 @@ public class TestOrderedBytes {
BigDecimal negLarge = longMax.multiply(longMax).negate(); BigDecimal negLarge = longMax.multiply(longMax).negate();
BigDecimal negMed = new BigDecimal("-10.0"); BigDecimal negMed = new BigDecimal("-10.0");
BigDecimal negSmall = new BigDecimal("-0.0010"); BigDecimal negSmall = new BigDecimal("-0.0010");
long zero = 0l; long zero = 0L;
BigDecimal posSmall = negSmall.negate(); BigDecimal posSmall = negSmall.negate();
BigDecimal posMed = negMed.negate(); BigDecimal posMed = negMed.negate();
BigDecimal posLarge = negLarge.negate(); BigDecimal posLarge = negLarge.negate();
@ -1091,7 +1123,7 @@ public class TestOrderedBytes {
byte int8 = 100; byte int8 = 100;
short int16 = 100; short int16 = 100;
int int32 = 100; int int32 = 100;
long int64 = 100l; long int64 = 100L;
float float32 = 100.0f; float float32 = 100.0f;
double float64 = 100.0d; double float64 = 100.0d;
String text = "hello world."; String text = "hello world.";
@ -1213,7 +1245,7 @@ public class TestOrderedBytes {
BigDecimal negLarge = longMax.multiply(longMax).negate(); BigDecimal negLarge = longMax.multiply(longMax).negate();
BigDecimal negMed = new BigDecimal("-10.0"); BigDecimal negMed = new BigDecimal("-10.0");
BigDecimal negSmall = new BigDecimal("-0.0010"); BigDecimal negSmall = new BigDecimal("-0.0010");
long zero = 0l; long zero = 0L;
BigDecimal posSmall = negSmall.negate(); BigDecimal posSmall = negSmall.negate();
BigDecimal posMed = negMed.negate(); BigDecimal posMed = negMed.negate();
BigDecimal posLarge = negLarge.negate(); BigDecimal posLarge = negLarge.negate();
@ -1222,7 +1254,7 @@ public class TestOrderedBytes {
byte int8 = 100; byte int8 = 100;
short int16 = 100; short int16 = 100;
int int32 = 100; int int32 = 100;
long int64 = 100l; long int64 = 100L;
float float32 = 100.0f; float float32 = 100.0f;
double float64 = 100.0d; double float64 = 100.0d;
String text = "hello world."; String text = "hello world.";

View File

@ -38,9 +38,7 @@ public class TestSimplePositionedMutableByteRange {
PositionedByteRange r = new SimplePositionedMutableByteRange(new byte[5], 1, 3); PositionedByteRange r = new SimplePositionedMutableByteRange(new byte[5], 1, 3);
// exercise single-byte put // exercise single-byte put
r.put(Bytes.toBytes("f")[0]) r.put(Bytes.toBytes("f")[0]).put(Bytes.toBytes("o")[0]).put(Bytes.toBytes("o")[0]);
.put(Bytes.toBytes("o")[0])
.put(Bytes.toBytes("o")[0]);
Assert.assertEquals(3, r.getPosition()); Assert.assertEquals(3, r.getPosition());
Assert.assertArrayEquals( Assert.assertArrayEquals(
new byte[] { 0, Bytes.toBytes("f")[0], Bytes.toBytes("o")[0], Bytes.toBytes("o")[0], 0 }, new byte[] { 0, Bytes.toBytes("f")[0], Bytes.toBytes("o")[0], Bytes.toBytes("o")[0], 0 },
@ -48,9 +46,7 @@ public class TestSimplePositionedMutableByteRange {
// exercise multi-byte put // exercise multi-byte put
r.setPosition(0); r.setPosition(0);
r.put(Bytes.toBytes("f")) r.put(Bytes.toBytes("f")).put(Bytes.toBytes("o")).put(Bytes.toBytes("o"));
.put(Bytes.toBytes("o"))
.put(Bytes.toBytes("o"));
Assert.assertEquals(3, r.getPosition()); Assert.assertEquals(3, r.getPosition());
Assert.assertArrayEquals( Assert.assertArrayEquals(
new byte[] { 0, Bytes.toBytes("f")[0], Bytes.toBytes("o")[0], Bytes.toBytes("o")[0], 0 }, new byte[] { 0, Bytes.toBytes("f")[0], Bytes.toBytes("o")[0], Bytes.toBytes("o")[0], 0 },
@ -76,7 +72,7 @@ public class TestSimplePositionedMutableByteRange {
} }
@Test @Test
public void testPutAndGetPrimitiveTypes() throws Exception { public void testPutAndGetPrimitiveTypes() {
PositionedByteRange pbr = new SimplePositionedMutableByteRange(100); PositionedByteRange pbr = new SimplePositionedMutableByteRange(100);
int i1 = 18, i2 = 2; int i1 = 18, i2 = 2;
short s1 = 0; short s1 = 0;
@ -102,7 +98,7 @@ public class TestSimplePositionedMutableByteRange {
} }
@Test @Test
public void testPutGetAPIsCompareWithBBAPIs() throws Exception { public void testPutGetAPIsCompareWithBBAPIs() {
// confirm that the long/int/short writing is same as BBs // confirm that the long/int/short writing is same as BBs
PositionedByteRange pbr = new SimplePositionedMutableByteRange(100); PositionedByteRange pbr = new SimplePositionedMutableByteRange(100);
int i1 = -234, i2 = 2; int i1 = -234, i2 = 2;

View File

@ -16,7 +16,6 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.apache.hadoop.hbase.util; package org.apache.hadoop.hbase.util;
import org.apache.yetus.audience.InterfaceAudience; import org.apache.yetus.audience.InterfaceAudience;
@ -25,19 +24,10 @@ import org.apache.yetus.audience.InterfaceAudience;
public class TimeOffsetEnvironmentEdge implements EnvironmentEdge { public class TimeOffsetEnvironmentEdge implements EnvironmentEdge {
private long offset; private long offset;
public TimeOffsetEnvironmentEdge(){} public TimeOffsetEnvironmentEdge() {
public void setTimeOffset(long off){
this.offset = off;
} }
public long getTimeOffset() public void increment(long incr) {
{
return offset;
}
public void increment(long incr)
{
this.offset += incr; this.offset += incr;
} }
@ -45,5 +35,4 @@ public class TimeOffsetEnvironmentEdge implements EnvironmentEdge {
public long currentTime() { public long currentTime() {
return System.currentTimeMillis() + offset; return System.currentTimeMillis() + offset;
} }
} }