Skip to content

Commit

Permalink
HBASE-26709 Ban the usage of junit 3 TestCase (#4065)
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Dimiduk <ndimiduk@apache.org>
  • Loading branch information
Apache9 committed Feb 9, 2022
1 parent 12002e6 commit b64a507
Show file tree
Hide file tree
Showing 15 changed files with 216 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
package org.apache.hadoop.hbase.util;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
Expand All @@ -31,17 +38,17 @@
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import junit.framework.TestCase;
import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.testclassification.MediumTests;
import org.apache.hadoop.hbase.testclassification.MiscTests;
import org.apache.hadoop.io.WritableUtils;
import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;

@Category({MiscTests.class, MediumTests.class})
public class TestBytes extends TestCase {
public class TestBytes {
@ClassRule
public static final HBaseClassTestRule CLASS_RULE =
HBaseClassTestRule.forClass(TestBytes.class);
Expand All @@ -61,10 +68,12 @@ private static void setUnsafe(boolean value) throws Exception {
assertEquals(Bytes.UNSAFE_UNALIGNED, value);
}

@Test
public void testShort() throws Exception {
testShort(false);
}

@Test
public void testShortUnsafe() throws Exception {
testShort(true);
}
Expand All @@ -88,6 +97,7 @@ private static void testShort(boolean unsafe) throws Exception {
}
}

@Test
public void testNullHashCode() {
byte [] b = null;
Exception ee = null;
Expand All @@ -99,6 +109,7 @@ public void testNullHashCode() {
assertNotNull(ee);
}

@Test
public void testAdd() {
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};
Expand All @@ -112,6 +123,7 @@ public void testAdd() {
assertEquals(0, Bytes.compareTo(result1, result2));
}

@Test
public void testSplit() {
byte[] lowest = Bytes.toBytes("AAA");
byte[] middle = Bytes.toBytes("CCC");
Expand All @@ -133,6 +145,7 @@ public void testSplit() {
assertTrue(Bytes.equals(parts[2], middle));
}

@Test
public void testSplit2() {
// More split tests.
byte [] lowest = Bytes.toBytes("http://A");
Expand All @@ -146,6 +159,7 @@ public void testSplit2() {
assertTrue(Bytes.equals(parts[1], middle));
}

@Test
public void testSplit3() {
// Test invalid split cases
byte[] low = { 1, 1, 1 };
Expand Down Expand Up @@ -180,6 +194,7 @@ public void testSplit3() {
}
}

@Test
public void testToInt() {
int[] ints = { -1, 123, Integer.MIN_VALUE, Integer.MAX_VALUE };
for (int anInt : ints) {
Expand All @@ -191,6 +206,7 @@ public void testToInt() {
}
}

@Test
public void testToLong() {
long[] longs = { -1L, 123L, Long.MIN_VALUE, Long.MAX_VALUE };
for (long aLong : longs) {
Expand All @@ -202,6 +218,7 @@ public void testToLong() {
}
}

@Test
public void testToFloat() {
float[] floats = { -1f, 123.123f, Float.MAX_VALUE };
for (float aFloat : floats) {
Expand All @@ -212,6 +229,7 @@ public void testToFloat() {
}
}

@Test
public void testToDouble() {
double [] doubles = {Double.MIN_VALUE, Double.MAX_VALUE};
for (double aDouble : doubles) {
Expand All @@ -222,6 +240,7 @@ public void testToDouble() {
}
}

@Test
public void testToBigDecimal() {
BigDecimal[] decimals = { new BigDecimal("-1"), new BigDecimal("123.123"),
new BigDecimal("123123123123") };
Expand All @@ -241,6 +260,7 @@ private byte[] bytesWithOffset(byte[] src) {
return result;
}

@Test
public void testToBytesForByteBuffer() {
byte[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
ByteBuffer target = ByteBuffer.wrap(array);
Expand All @@ -264,6 +284,7 @@ public void testToBytesForByteBuffer() {
assertEquals(5, target2.limit());
}

@Test
public void testGetBytesForByteBuffer() {
byte[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
ByteBuffer target = ByteBuffer.wrap(array);
Expand All @@ -277,6 +298,7 @@ public void testGetBytesForByteBuffer() {
assertEquals(7, target.limit());
}

@Test
public void testReadAsVLong() throws Exception {
long[] longs = { -1L, 123L, Long.MIN_VALUE, Long.MAX_VALUE };
for (long aLong : longs) {
Expand All @@ -290,6 +312,7 @@ public void testReadAsVLong() throws Exception {
}
}

@Test
public void testToStringBinaryForBytes() {
byte[] array = { '0', '9', 'a', 'z', 'A', 'Z', '@', 1 };
String actual = Bytes.toStringBinary(array);
Expand All @@ -301,6 +324,7 @@ public void testToStringBinaryForBytes() {
assertEquals(expected2, actual2);
}

@Test
public void testToStringBinaryForArrayBasedByteBuffer() {
byte[] array = { '0', '9', 'a', 'z', 'A', 'Z', '@', 1 };
ByteBuffer target = ByteBuffer.wrap(array);
Expand All @@ -309,6 +333,7 @@ public void testToStringBinaryForArrayBasedByteBuffer() {
assertEquals(expected, actual);
}

@Test
public void testToStringBinaryForReadOnlyByteBuffer() {
byte[] array = { '0', '9', 'a', 'z', 'A', 'Z', '@', 1 };
ByteBuffer target = ByteBuffer.wrap(array).asReadOnlyBuffer();
Expand All @@ -317,6 +342,7 @@ public void testToStringBinaryForReadOnlyByteBuffer() {
assertEquals(expected, actual);
}

@Test
public void testBinarySearch() {
byte[][] arr = {
{ 1 },
Expand Down Expand Up @@ -357,6 +383,7 @@ public void testBinarySearch() {
}
}

@Test
public void testToStringBytesBinaryReversible() {
// let's run test with 1000 randomly generated byte arrays
Random rand = new Random(EnvironmentEdgeManager.currentTime());
Expand All @@ -381,6 +408,7 @@ private void verifyReversibleForBytes(byte[] originalBytes) {
}
}

@Test
public void testStartsWith() {
assertTrue(Bytes.startsWith(Bytes.toBytes("hello"), Bytes.toBytes("h")));
assertTrue(Bytes.startsWith(Bytes.toBytes("hello"), Bytes.toBytes("")));
Expand All @@ -389,6 +417,7 @@ public void testStartsWith() {
assertFalse(Bytes.startsWith(Bytes.toBytes(""), Bytes.toBytes("hello")));
}

@Test
public void testIncrementBytes() {
assertTrue(checkTestIncrementBytes(10, 1));
assertTrue(checkTestIncrementBytes(12, 123435445));
Expand Down Expand Up @@ -423,6 +452,7 @@ private static boolean checkTestIncrementBytes(long val, long amount) {
return (Bytes.toLong(testValue) + amount) == incrementResult;
}

@Test
public void testFixedSizeString() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
Expand All @@ -448,13 +478,15 @@ public void testFixedSizeString() throws IOException {
assertEquals("", Bytes.readStringFixedSize(dis, 9));
}

@Test
public void testCopy() {
byte[] bytes = Bytes.toBytes("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
byte[] copy = Bytes.copy(bytes);
assertNotSame(bytes, copy);
assertTrue(Bytes.equals(bytes, copy));
}

@Test
public void testToBytesBinaryTrailingBackslashes() {
try {
Bytes.toBytesBinary("abc\\x00\\x01\\");
Expand All @@ -463,11 +495,13 @@ public void testToBytesBinaryTrailingBackslashes() {
}
}

@Test
public void testToStringBinary_toBytesBinary_Reversable() {
String bytes = Bytes.toStringBinary(Bytes.toBytes(2.17));
assertEquals(2.17, Bytes.toDouble(Bytes.toBytesBinary(bytes)), 0);
}

@Test
public void testUnsignedBinarySearch(){
byte[] bytes = new byte[] { 0,5,123,127,-128,-100,-1 };
Assert.assertEquals(1, Bytes.unsignedBinarySearch(bytes, 0, bytes.length, (byte)5));
Expand All @@ -479,6 +513,7 @@ public void testUnsignedBinarySearch(){
Assert.assertEquals(-6-1, Bytes.unsignedBinarySearch(bytes, 0, bytes.length, (byte)-5));
}

@Test
public void testUnsignedIncrement(){
byte[] a = Bytes.toBytes(0);
int a2 = Bytes.toInt(Bytes.unsignedCopyAndIncrement(a), 0);
Expand All @@ -495,6 +530,7 @@ public void testUnsignedIncrement(){
Assert.assertEquals(256, c2);
}

@Test
public void testIndexOf() {
byte[] array = Bytes.toBytes("hello");
assertEquals(1, Bytes.indexOf(array, (byte) 'e'));
Expand All @@ -505,6 +541,7 @@ public void testIndexOf() {
assertEquals(-1, Bytes.indexOf(array, Bytes.toBytes("hll")));
}

@Test
public void testContains() {
byte[] array = Bytes.toBytes("hello world");
assertTrue(Bytes.contains(array, (byte) 'e'));
Expand All @@ -515,6 +552,7 @@ public void testContains() {
assertFalse(Bytes.contains(array, Bytes.toBytes("owo")));
}

@Test
public void testZero() {
byte[] array = Bytes.toBytes("hello");
Bytes.zero(array);
Expand All @@ -533,6 +571,7 @@ public void testZero() {
}
}

@Test
public void testPutBuffer() {
byte[] b = new byte[100];
for (byte i = 0; i < 100; i++) {
Expand All @@ -543,6 +582,7 @@ public void testPutBuffer() {
}
}

@Test
public void testToFromHex() {
List<String> testStrings = new ArrayList<>(8);
testStrings.addAll(Arrays.asList("", "00", "A0", "ff", "FFffFFFFFFFFFF", "12",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
*/
package org.apache.hadoop.hbase.http.conf;

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

import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import junit.framework.TestCase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.testclassification.MiscTests;
Expand All @@ -44,8 +47,8 @@
* Basic test case that the ConfServlet can write configuration
* to its output in XML and JSON format.
*/
@Category({MiscTests.class, SmallTests.class})
public class TestConfServlet extends TestCase {
@Category({ MiscTests.class, SmallTests.class })
public class TestConfServlet {

@ClassRule
public static final HBaseClassTestRule CLASS_RULE =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.NavigableMap;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
Expand Down Expand Up @@ -51,7 +49,7 @@
* @see <a href="https://issues.apache.org/jira/browse/HBASE-11912">HBASE-11912</a>
*/
@Deprecated
public abstract class HBaseTestCase extends TestCase {
public abstract class HBaseTestCase extends junit.framework.TestCase {
private static final Logger LOG = LoggerFactory.getLogger(HBaseTestCase.class);

protected final static byte [] fam1 = Bytes.toBytes("colfamily11");
Expand Down Expand Up @@ -432,21 +430,17 @@ protected void closeRootAndMeta() throws IOException {
HBaseTestingUtility.closeRegionAndWAL(meta);
}

public static void assertByteEquals(byte[] expected,
byte[] actual) {
public static void assertByteEquals(byte[] expected, byte[] actual) {
if (Bytes.compareTo(expected, actual) != 0) {
throw new AssertionFailedError("expected:<" +
Bytes.toString(expected) + "> but was:<" +
Bytes.toString(actual) + ">");
throw new junit.framework.AssertionFailedError(
"expected:<" + Bytes.toString(expected) + "> but was:<" + Bytes.toString(actual) + ">");
}
}

public static void assertEquals(byte[] expected,
byte[] actual) {
public static void assertEquals(byte[] expected, byte[] actual) {
if (Bytes.compareTo(expected, actual) != 0) {
throw new AssertionFailedError("expected:<" +
Bytes.toStringBinary(expected) + "> but was:<" +
Bytes.toStringBinary(actual) + ">");
throw new junit.framework.AssertionFailedError("expected:<" + Bytes.toStringBinary(expected) +
"> but was:<" + Bytes.toStringBinary(actual) + ">");
}
}
}
Loading

0 comments on commit b64a507

Please sign in to comment.