Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HBASE-26709 Ban the usage of junit 3 TestCase #4065

Merged
merged 2 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,37 @@
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;
import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.util.ArrayList;
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 @@ -64,10 +69,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 @@ -91,6 +98,7 @@ private static void testShort(boolean unsafe) throws Exception {
}
}

@Test
public void testNullHashCode() {
byte [] b = null;
Exception ee = null;
Expand All @@ -102,6 +110,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 @@ -115,6 +124,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 @@ -136,6 +146,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 @@ -149,6 +160,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 @@ -183,6 +195,7 @@ public void testSplit3() {
}
}

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

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

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

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

@Test
public void testToBigDecimal() {
BigDecimal[] decimals = { new BigDecimal("-1"), new BigDecimal("123.123"),
new BigDecimal("123123123123") };
Expand All @@ -244,6 +261,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 @@ -267,6 +285,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 @@ -280,6 +299,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 @@ -293,6 +313,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 @@ -304,6 +325,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 @@ -312,6 +334,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 @@ -320,6 +343,7 @@ public void testToStringBinaryForReadOnlyByteBuffer() {
assertEquals(expected, actual);
}

@Test
public void testBinarySearch() {
byte[][] arr = {
{ 1 },
Expand Down Expand Up @@ -360,6 +384,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 @@ -384,6 +409,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 @@ -392,6 +418,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 @@ -426,6 +453,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 @@ -451,13 +479,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 @@ -466,11 +496,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 @@ -482,6 +514,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 @@ -498,6 +531,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 @@ -508,6 +542,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 @@ -518,6 +553,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 @@ -536,6 +572,7 @@ public void testZero() {
}
}

@Test
public void testPutBuffer() {
byte[] b = new byte[100];
for (byte i = 0; i < 100; i++) {
Expand All @@ -546,6 +583,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
Loading