Skip to content

Commit

Permalink
Backport JUnit upgrade from #1562 to 1.10.1
Browse files Browse the repository at this point in the history
Update JUnit dependency, and use assertThrows instead of
ExpectedException Rule and hamcrest library, to avoid deprecation
warnings and an unnecessary test dependency.
  • Loading branch information
ctubbsii committed Dec 15, 2020
1 parent 56142a8 commit d4fd27f
Show file tree
Hide file tree
Showing 16 changed files with 110 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,20 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableMap;

public class AccumuloConfigurationTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testGetMemoryInBytes() throws Exception {
assertEquals(42l, AccumuloConfiguration.getMemoryInBytes("42"));
Expand Down Expand Up @@ -205,8 +201,7 @@ public void testMutatePrefixMap() {
expected1.put(Property.TABLE_ARBITRARY_PROP_PREFIX.getKey() + "a2", "asg34");
assertEquals(expected1, pm1);

thrown.expect(UnsupportedOperationException.class);
pm1.put("k9", "v3");
assertThrows(UnsupportedOperationException.class, () -> pm1.put("k9", "v3"));
}

@Test
Expand Down
26 changes: 12 additions & 14 deletions core/src/test/java/org/apache/accumulo/core/data/KeyExtentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@
*/
package org.apache.accumulo.core.data;

import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -323,24 +319,26 @@ public void testKeyExtentsForRange() {
Collection<KeyExtent> results;

results = KeyExtent.getKeyExtentsForRange(null, null, set0);
assertThat("infinite range should return full set", results.size(), is(5));
assertThat("infinite range should return full set", results, hasItems(b, e, h, m, z));
assertEquals("infinite range should return full set", 5, results.size());
assertTrue("infinite range should return full set",
results.containsAll(Arrays.asList(b, e, h, m, z)));

results = KeyExtent.getKeyExtentsForRange(new Text("a"), new Text("z"), set0);
assertThat("full overlap should return full set", results.size(), is(5));
assertThat("full overlap should return full set", results, hasItems(b, e, h, m, z));
assertEquals("full overlap should return full set", 5, results.size(), 5);
assertTrue("full overlap should return full set",
results.containsAll(Arrays.asList(b, e, h, m, z)));

results = KeyExtent.getKeyExtentsForRange(null, new Text("f"), set0);
assertThat("end row should return head set", results.size(), is(3));
assertThat("end row should return head set", results, hasItems(b, e, h));
assertEquals("end row should return head set", 3, results.size());
assertTrue("end row should return head set", results.containsAll(Arrays.asList(b, e, h)));

results = KeyExtent.getKeyExtentsForRange(new Text("f"), null, set0);
assertThat("start row should return tail set", results.size(), is(3));
assertThat("start row should return tail set", results, hasItems(h, m, z));
assertEquals("start row should return tail set", 3, results.size());
assertTrue("start row should return tail set", results.containsAll(Arrays.asList(h, m, z)));

results = KeyExtent.getKeyExtentsForRange(new Text("f"), new Text("g"), set0);
assertThat("slice should return correct subset", results.size(), is(1));
assertThat("slice should return correct subset", results, hasItem(h));
assertEquals("slice should return correct subset", 1, results.size());
assertTrue("slice should return correct subset", results.contains(h));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
Expand All @@ -46,9 +47,7 @@
import org.apache.accumulo.core.conf.ConfigurationCopy;
import org.apache.accumulo.core.conf.Property;
import org.apache.hadoop.conf.Configuration;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import com.google.common.primitives.Bytes;

Expand All @@ -62,9 +61,6 @@ public class CryptoTest {
public static final String CRYPTO_ON_KEK_OFF_CONF =
"crypto-on-no-key-encryption-accumulo-site.xml";

@Rule
public ExpectedException exception = ExpectedException.none();

@Test
public void testNoCryptoStream() throws IOException {
AccumuloConfiguration conf = setAndGetAccumuloConfig(CRYPTO_OFF_CONF);
Expand Down Expand Up @@ -143,8 +139,7 @@ public void testCryptoModuleParamsValidation1() throws IOException {

assertTrue(cryptoModule instanceof DefaultCryptoModule);

exception.expect(RuntimeException.class);
cryptoModule.getEncryptingOutputStream(params);
assertThrows(RuntimeException.class, () -> cryptoModule.getEncryptingOutputStream(params));
}

@Test
Expand All @@ -157,8 +152,7 @@ public void testCryptoModuleParamsValidation2() throws IOException {

assertTrue(cryptoModule instanceof DefaultCryptoModule);

exception.expect(RuntimeException.class);
cryptoModule.getDecryptingInputStream(params);
assertThrows(RuntimeException.class, () -> cryptoModule.getDecryptingInputStream(params));
}

private String getStringifiedBytes(String s) throws IOException {
Expand Down Expand Up @@ -273,10 +267,7 @@ public void testKeyEncryptionAndCheckThatFileCannotBeReadWithoutKEK() throws IOE

assertNotNull(params.getPlaintextInputStream());
DataInputStream dataIn = new DataInputStream(params.getPlaintextInputStream());
// We expect the following operation to fail and throw an exception
exception.expect(IOException.class);
@SuppressWarnings("unused")
String markerString = dataIn.readUTF();
assertThrows(IOException.class, () -> dataIn.readUTF());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,14 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;

import java.util.Iterator;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class PreAllocatedArrayTest {

@Rule
public ExpectedException exception = ExpectedException.none();

/**
* Test method for {@link org.apache.accumulo.core.util.PreAllocatedArray#PreAllocatedArray(int)}.
*/
Expand All @@ -48,8 +44,7 @@ public void testPreAllocatedArray() {

@Test
public void testPreAllocatedArray_Fail() {
exception.expect(IllegalArgumentException.class);
new PreAllocatedArray<String>(-5);
assertThrows(IllegalArgumentException.class, () -> new PreAllocatedArray<String>(-5));
}

/**
Expand Down Expand Up @@ -87,31 +82,27 @@ public void testSet() {
public void testSetIndexHigh() {
PreAllocatedArray<String> strings = new PreAllocatedArray<>(3);
strings.set(2, "in bounds");
exception.expect(IndexOutOfBoundsException.class);
strings.set(3, "out of bounds");
assertThrows(IndexOutOfBoundsException.class, () -> strings.set(3, "out of bounds"));
}

@Test
public void testSetIndexNegative() {
PreAllocatedArray<String> strings = new PreAllocatedArray<>(3);
strings.set(0, "in bounds");
exception.expect(IndexOutOfBoundsException.class);
strings.set(-3, "out of bounds");
assertThrows(IndexOutOfBoundsException.class, () -> strings.set(-3, "out of bounds"));
}

@Test
public void testGetIndexHigh() {
PreAllocatedArray<String> strings = new PreAllocatedArray<>(3);
strings.get(2);
exception.expect(IndexOutOfBoundsException.class);
strings.get(3);
assertThrows(IndexOutOfBoundsException.class, () -> strings.get(3));
}

@Test
public void testGetIndexNegative() {
PreAllocatedArray<String> strings = new PreAllocatedArray<>(3);
strings.get(0);
exception.expect(IndexOutOfBoundsException.class);
strings.get(-3);
assertThrows(IndexOutOfBoundsException.class, () -> strings.get(-3));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayOutputStream;
Expand All @@ -26,15 +27,10 @@
import java.util.Arrays;

import org.apache.hadoop.io.WritableUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class UnsynchronizedBufferTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testByteBufferConstructor() {
byte[] test = "0123456789".getBytes(UTF_8);
Expand All @@ -54,9 +50,11 @@ public void testByteBufferConstructor() {
assertEquals("34567", new String(buf, UTF_8));

buf = new byte[6];
// the byte buffer has the extra byte, but should not be able to read it...
thrown.expect(ArrayIndexOutOfBoundsException.class);
ub.readBytes(buf);

final UnsynchronizedBuffer.Reader finalUb = ub;
final byte[] finalBuf = buf;
assertThrows("the byte buffer has the extra byte, but should not be able to read it",
ArrayIndexOutOfBoundsException.class, () -> finalUb.readBytes(finalBuf));
}

@Test
Expand Down
39 changes: 13 additions & 26 deletions fate/src/test/java/org/apache/accumulo/fate/util/RetryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;

Expand All @@ -36,9 +36,7 @@
import org.apache.accumulo.fate.util.Retry.RetryFactory;
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.slf4j.Logger;

public class RetryTest {
Expand All @@ -51,9 +49,6 @@ public class RetryTest {
private Retry unlimitedRetry;
private static final TimeUnit MS = MILLISECONDS;

@Rule
public ExpectedException exception = ExpectedException.none();

@Before
public void setup() {
retry = Retry.builder().maxRetries(MAX_RETRIES).retryAfter(INITIAL_WAIT, MS)
Expand Down Expand Up @@ -98,11 +93,8 @@ public void usingNonExistentRetryFails() {
retry.useRetry();
}
assertFalse(retry.canRetry());

// Calling useRetry when canRetry returns false throws an exception
exception.expect(IllegalStateException.class);
retry.useRetry();
fail("previous command should have thrown IllegalStateException");
assertThrows("Calling useRetry when canRetry returns false throws an exception",
IllegalStateException.class, () -> retry.useRetry());
}

@Test
Expand Down Expand Up @@ -207,9 +199,8 @@ public void testMaxRetries() {
NeedsRetries builder = Retry.builder();
builder.maxRetries(10);
builder.maxRetries(0);
exception.expect(IllegalArgumentException.class);
builder.maxRetries(-1);
fail("Should not allow negative retries");
assertThrows("Should not allow negative retries", IllegalArgumentException.class,
() -> builder.maxRetries(-1));
}

@Test
Expand All @@ -222,9 +213,8 @@ public void testInitialWait() {
builder.retryAfter(0, MILLISECONDS);
builder.retryAfter(0, DAYS);

exception.expect(IllegalArgumentException.class);
builder.retryAfter(-1, NANOSECONDS);
fail("Should not allow negative wait times");
assertThrows("Should not allow negative wait times", IllegalArgumentException.class,
() -> builder.retryAfter(-1, NANOSECONDS));
}

@Test
Expand All @@ -237,9 +227,8 @@ public void testIncrementBy() {
builder.incrementBy(0, HOURS);
builder.incrementBy(0, NANOSECONDS);

exception.expect(IllegalArgumentException.class);
builder.incrementBy(-1, NANOSECONDS);
fail("Should not allow negative increments");
assertThrows("Should not allow negative increments", IllegalArgumentException.class,
() -> builder.incrementBy(-1, NANOSECONDS));
}

@Test
Expand All @@ -249,9 +238,8 @@ public void testMaxWait() {
builder.maxWait(15, MILLISECONDS);
builder.maxWait(16, MILLISECONDS);

exception.expect(IllegalArgumentException.class);
builder.maxWait(14, MILLISECONDS);
fail("Max wait time should be greater than or equal to initial wait time");
assertThrows("Max wait time should be greater than or equal to initial wait time",
IllegalArgumentException.class, () -> builder.maxWait(14, MILLISECONDS));
}

@Test
Expand All @@ -265,9 +253,8 @@ public void testLogInterval() {
builder.logInterval(0, HOURS);
builder.logInterval(0, NANOSECONDS);

exception.expect(IllegalArgumentException.class);
builder.logInterval(-1, NANOSECONDS);
fail("Log interval must not be negative");
assertThrows("Log interval must not be negative", IllegalArgumentException.class,
() -> builder.logInterval(-1, NANOSECONDS));
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>4.13.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
Expand Down
Loading

0 comments on commit d4fd27f

Please sign in to comment.