diff --git a/core/src/test/java/org/apache/accumulo/core/conf/AccumuloConfigurationTest.java b/core/src/test/java/org/apache/accumulo/core/conf/AccumuloConfigurationTest.java index 7a0f3498903..df78ade64ac 100644 --- a/core/src/test/java/org/apache/accumulo/core/conf/AccumuloConfigurationTest.java +++ b/core/src/test/java/org/apache/accumulo/core/conf/AccumuloConfigurationTest.java @@ -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")); @@ -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 diff --git a/core/src/test/java/org/apache/accumulo/core/data/KeyExtentTest.java b/core/src/test/java/org/apache/accumulo/core/data/KeyExtentTest.java index e7abea9ad6d..71371ff4ad3 100644 --- a/core/src/test/java/org/apache/accumulo/core/data/KeyExtentTest.java +++ b/core/src/test/java/org/apache/accumulo/core/data/KeyExtentTest.java @@ -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; @@ -323,24 +319,26 @@ public void testKeyExtentsForRange() { Collection 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 diff --git a/core/src/test/java/org/apache/accumulo/core/security/crypto/CryptoTest.java b/core/src/test/java/org/apache/accumulo/core/security/crypto/CryptoTest.java index 1705fcc0450..bae418f802c 100644 --- a/core/src/test/java/org/apache/accumulo/core/security/crypto/CryptoTest.java +++ b/core/src/test/java/org/apache/accumulo/core/security/crypto/CryptoTest.java @@ -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; @@ -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; @@ -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); @@ -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 @@ -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 { @@ -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 diff --git a/core/src/test/java/org/apache/accumulo/core/util/PreAllocatedArrayTest.java b/core/src/test/java/org/apache/accumulo/core/util/PreAllocatedArrayTest.java index 14d28a1fca3..2c096e4c1c4 100644 --- a/core/src/test/java/org/apache/accumulo/core/util/PreAllocatedArrayTest.java +++ b/core/src/test/java/org/apache/accumulo/core/util/PreAllocatedArrayTest.java @@ -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)}. */ @@ -48,8 +44,7 @@ public void testPreAllocatedArray() { @Test public void testPreAllocatedArray_Fail() { - exception.expect(IllegalArgumentException.class); - new PreAllocatedArray(-5); + assertThrows(IllegalArgumentException.class, () -> new PreAllocatedArray(-5)); } /** @@ -87,31 +82,27 @@ public void testSet() { public void testSetIndexHigh() { PreAllocatedArray 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 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 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 strings = new PreAllocatedArray<>(3); strings.get(0); - exception.expect(IndexOutOfBoundsException.class); - strings.get(-3); + assertThrows(IndexOutOfBoundsException.class, () -> strings.get(-3)); } } diff --git a/core/src/test/java/org/apache/accumulo/core/util/UnsynchronizedBufferTest.java b/core/src/test/java/org/apache/accumulo/core/util/UnsynchronizedBufferTest.java index bc68ba28547..b842f3caeca 100644 --- a/core/src/test/java/org/apache/accumulo/core/util/UnsynchronizedBufferTest.java +++ b/core/src/test/java/org/apache/accumulo/core/util/UnsynchronizedBufferTest.java @@ -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; @@ -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); @@ -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 diff --git a/fate/src/test/java/org/apache/accumulo/fate/util/RetryTest.java b/fate/src/test/java/org/apache/accumulo/fate/util/RetryTest.java index 17518fd5383..c03da4ffb52 100644 --- a/fate/src/test/java/org/apache/accumulo/fate/util/RetryTest.java +++ b/fate/src/test/java/org/apache/accumulo/fate/util/RetryTest.java @@ -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; @@ -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 { @@ -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) @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/pom.xml b/pom.xml index 6606c0f1d64..55beb664b43 100644 --- a/pom.xml +++ b/pom.xml @@ -238,7 +238,7 @@ junit junit - 4.12 + 4.13.1 log4j diff --git a/server/base/src/test/java/org/apache/accumulo/server/util/TabletIteratorTest.java b/server/base/src/test/java/org/apache/accumulo/server/util/TabletIteratorTest.java index 4171b3c49d4..43888afd7a9 100644 --- a/server/base/src/test/java/org/apache/accumulo/server/util/TabletIteratorTest.java +++ b/server/base/src/test/java/org/apache/accumulo/server/util/TabletIteratorTest.java @@ -24,6 +24,7 @@ import static org.easymock.EasyMock.replay; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; import java.util.Arrays; import java.util.Iterator; @@ -45,26 +46,21 @@ import org.apache.hadoop.io.Text; import org.easymock.Capture; import org.easymock.IAnswer; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import com.google.common.collect.Iterators; public class TabletIteratorTest { - @Rule - public ExpectedException thrown = ExpectedException.none(); - @Test public void testSplits() { - TreeMap data1 = new TreeMap(); + TreeMap data1 = new TreeMap<>(); createTabletData(data1, "4", "j", null); createTabletData(data1, "4", "m", "j"); createTabletData(data1, "4", null, "x"); - TreeMap data2 = new TreeMap(data1); + TreeMap data2 = new TreeMap<>(data1); createTabletData(data2, "4", "s", "m"); createTabletData(data2, "4", "x", "s"); @@ -74,14 +70,14 @@ public void testSplits() { @Test public void testTableTransition1() { - TreeMap data1 = new TreeMap(); + TreeMap data1 = new TreeMap<>(); createTabletData(data1, "3", "c", null); createTabletData(data1, "3", "n", "c"); createTabletData(data1, "4", "f", null); createTabletData(data1, "4", null, "f"); - TreeMap data2 = new TreeMap(data1); + TreeMap data2 = new TreeMap<>(data1); createTabletData(data2, "3", null, "n"); @@ -90,14 +86,14 @@ public void testTableTransition1() { @Test public void testTableTransition2() { - TreeMap data1 = new TreeMap(); + TreeMap data1 = new TreeMap<>(); createTabletData(data1, "3", "c", null); createTabletData(data1, "3", "n", "c"); createTabletData(data1, "3", null, "n"); createTabletData(data1, "4", null, "f"); - TreeMap data2 = new TreeMap(data1); + TreeMap data2 = new TreeMap<>(data1); createTabletData(data2, "4", "f", null); @@ -106,12 +102,12 @@ public void testTableTransition2() { @Test public void testMissingFirstTablet() { - TreeMap data1 = new TreeMap(); + TreeMap data1 = new TreeMap<>(); createTabletData(data1, "3", "n", "c"); createTabletData(data1, "3", null, "n"); - TreeMap data2 = new TreeMap(data1); + TreeMap data2 = new TreeMap<>(data1); createTabletData(data2, "3", "c", null); @@ -120,34 +116,34 @@ public void testMissingFirstTablet() { @Test public void testMissingLastTablet() { - TreeMap data1 = new TreeMap(); + TreeMap data1 = new TreeMap<>(); createTabletData(data1, "3", "c", null); createTabletData(data1, "3", "n", "c"); - TreeMap data2 = new TreeMap(data1); + TreeMap data2 = new TreeMap<>(data1); createTabletData(data2, "3", null, "n"); - thrown.expect(IllegalStateException.class); - runTest(Arrays.asList(data1, data2), Arrays.asList("3;c", "3;n", "3<")); + assertThrows(IllegalStateException.class, + () -> runTest(Arrays.asList(data1, data2), Arrays.asList("3;c", "3;n", "3<"))); } @Test public void testDeletedTable() { - TreeMap data1 = new TreeMap(); + TreeMap data1 = new TreeMap<>(); createTabletData(data1, "3", "c", null); createTabletData(data1, "3", "n", "c"); createTabletData(data1, "4", null, "f"); createTabletData(data1, "4", "f", null); - TreeMap data2 = new TreeMap(); + TreeMap data2 = new TreeMap<>(); createTabletData(data1, "4", null, "f"); createTabletData(data1, "4", "f", null); - thrown.expect(TabletDeletedException.class); - runTest(Arrays.asList(data1, data2), Arrays.asList("3;c", "3;n")); + assertThrows(TabletDeletedException.class, + () -> runTest(Arrays.asList(data1, data2), Arrays.asList("3;c", "3;n"))); } private static class ScannerState { @@ -219,11 +215,12 @@ private void createTabletData(TreeMap data, String tableId, String en public void runTest(List> dataSets, List expectedEndRows) { final Iterator> dataSetIter = dataSets.iterator(); - final TreeMap data = new TreeMap(dataSetIter.next()); + final TreeMap data = new TreeMap<>(dataSetIter.next()); Scanner scanner = createMockScanner(data); TabletIterator tabIter = new TabletIterator(scanner, TabletsSection.getRange(), true, false) { + @Override protected void resetScanner() { data.clear(); data.putAll(dataSetIter.next()); diff --git a/server/tserver/src/test/java/org/apache/accumulo/tserver/InMemoryMapTest.java b/server/tserver/src/test/java/org/apache/accumulo/tserver/InMemoryMapTest.java index 16e68b10496..212e5166202 100644 --- a/server/tserver/src/test/java/org/apache/accumulo/tserver/InMemoryMapTest.java +++ b/server/tserver/src/test/java/org/apache/accumulo/tserver/InMemoryMapTest.java @@ -18,6 +18,7 @@ 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; @@ -67,7 +68,6 @@ import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import com.google.common.collect.ImmutableMap; @@ -97,9 +97,6 @@ public SamplerConfiguration getSamplerConfiguration() { } } - @Rule - public ExpectedException thrown = ExpectedException.none(); - @BeforeClass public static void setUp() throws Exception { // suppress log messages having to do with not having an instance @@ -494,8 +491,8 @@ public void testDuplicateKey() throws Exception { static long sum(long[] counts) { long result = 0; - for (int i = 0; i < counts.length; i++) - result += counts[i]; + for (long count : counts) + result += count; return result; } @@ -535,8 +532,8 @@ public void run() { String.format("%.1f mutations per second with %d threads", mutationsPerSecond, threads)); } // verify that more threads doesn't go a lot faster, or a lot slower than one thread - for (int i = 0; i < timings.size(); i++) { - double ratioFirst = timings.get(0) / timings.get(i); + for (Double timing : timings) { + double ratioFirst = timings.get(0) / timing; assertTrue(ratioFirst < 3); assertTrue(ratioFirst > 0.3); } @@ -835,8 +832,9 @@ public void testDeferredSamplerCreation() throws Exception { assertEquals(expectedAll, readAll(iter)); iter = imm.skvIterator(sampleConfig1); - thrown.expect(SampleNotPresentException.class); - iter.seek(new Range(), LocalityGroupUtil.EMPTY_CF_SET, false); + final MemoryIterator finalIter = iter; + assertThrows(SampleNotPresentException.class, + () -> finalIter.seek(new Range(), LocalityGroupUtil.EMPTY_CF_SET, false)); } private TreeMap readAll(SortedKeyValueIterator iter) throws IOException { diff --git a/server/tserver/src/test/java/org/apache/accumulo/tserver/compaction/CompactionPlanTest.java b/server/tserver/src/test/java/org/apache/accumulo/tserver/compaction/CompactionPlanTest.java index 1b7d00d726f..d2c96b72575 100644 --- a/server/tserver/src/test/java/org/apache/accumulo/tserver/compaction/CompactionPlanTest.java +++ b/server/tserver/src/test/java/org/apache/accumulo/tserver/compaction/CompactionPlanTest.java @@ -17,20 +17,17 @@ package org.apache.accumulo.tserver.compaction; +import static org.junit.Assert.assertThrows; + import java.util.Set; import org.apache.accumulo.server.fs.FileRef; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import com.google.common.collect.ImmutableSet; public class CompactionPlanTest { - @Rule - public ExpectedException exception = ExpectedException.none(); - @Test public void testOverlappingInputAndDelete() { CompactionPlan cp1 = new CompactionPlan(); @@ -45,8 +42,7 @@ public void testOverlappingInputAndDelete() { Set allFiles = ImmutableSet.of(fr1, fr2); - exception.expect(IllegalStateException.class); - cp1.validate(allFiles); + assertThrows(IllegalStateException.class, () -> cp1.validate(allFiles)); } @Test @@ -63,8 +59,7 @@ public void testInputNotInAllFiles() { Set allFiles = ImmutableSet.of(fr1, fr2); - exception.expect(IllegalStateException.class); - cp1.validate(allFiles); + assertThrows(IllegalStateException.class, () -> cp1.validate(allFiles)); } @Test @@ -81,8 +76,7 @@ public void testDeleteNotInAllFiles() { Set allFiles = ImmutableSet.of(fr1, fr2); - exception.expect(IllegalStateException.class); - cp1.validate(allFiles); + assertThrows(IllegalStateException.class, () -> cp1.validate(allFiles)); } } diff --git a/server/tserver/src/test/java/org/apache/accumulo/tserver/log/SortedLogRecoveryTest.java b/server/tserver/src/test/java/org/apache/accumulo/tserver/log/SortedLogRecoveryTest.java index a247c992b58..5bea3a8296a 100644 --- a/server/tserver/src/test/java/org/apache/accumulo/tserver/log/SortedLogRecoveryTest.java +++ b/server/tserver/src/test/java/org/apache/accumulo/tserver/log/SortedLogRecoveryTest.java @@ -22,6 +22,7 @@ import static org.apache.accumulo.tserver.logger.LogEvents.MUTATION; import static org.apache.accumulo.tserver.logger.LogEvents.OPEN; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -53,9 +54,7 @@ import org.apache.hadoop.io.MapFile; import org.apache.hadoop.io.MapFile.Writer; import org.apache.hadoop.io.Text; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; public class SortedLogRecoveryTest { @@ -65,9 +64,6 @@ public class SortedLogRecoveryTest { static final Text cq = new Text("cq"); static final Value value = new Value("value".getBytes()); - @Rule - public ExpectedException thrown = ExpectedException.none(); - static class KeyValue implements Comparable { public final LogFileKey key; public final LogFileValue value; @@ -864,9 +860,9 @@ public void testConsecutiveCompactionFinishEvents() throws IOException { Map logs = new TreeMap<>(); logs.put("entries1", entries1); - thrown.expect(IllegalStateException.class); - thrown.expectMessage("consecutive " + LogEvents.COMPACTION_FINISH.name()); - recover(logs, extent); + IllegalStateException e = + assertThrows(IllegalStateException.class, () -> recover(logs, extent)); + assertTrue(e.getMessage().contains("consecutive " + LogEvents.COMPACTION_FINISH.name())); } @Test @@ -986,8 +982,8 @@ public void testFileWithoutOpen() throws IOException { Map logs = new TreeMap<>(); logs.put("entries1", entries1); - thrown.expect(IllegalStateException.class); - thrown.expectMessage("not " + LogEvents.OPEN); - recover(logs, extent); + IllegalStateException e = + assertThrows(IllegalStateException.class, () -> recover(logs, extent)); + assertTrue(e.getMessage().contains("not " + LogEvents.OPEN)); } } diff --git a/test/src/main/java/org/apache/accumulo/test/functional/ClassLoaderIT.java b/test/src/main/java/org/apache/accumulo/test/functional/ClassLoaderIT.java index 390065cfdd0..ddeab28003f 100644 --- a/test/src/main/java/org/apache/accumulo/test/functional/ClassLoaderIT.java +++ b/test/src/main/java/org/apache/accumulo/test/functional/ClassLoaderIT.java @@ -46,7 +46,6 @@ import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; -import org.hamcrest.CoreMatchers; import org.junit.Assume; import org.junit.Before; import org.junit.Test; @@ -66,7 +65,7 @@ protected int defaultTimeoutSeconds() { @Before public void checkCluster() { - Assume.assumeThat(getClusterType(), CoreMatchers.is(ClusterType.MINI)); + Assume.assumeTrue(getClusterType() == ClusterType.MINI); MiniAccumuloClusterImpl mac = (MiniAccumuloClusterImpl) getCluster(); rootPath = mac.getConfig().getDir().getAbsolutePath(); } diff --git a/test/src/main/java/org/apache/accumulo/test/functional/KerberosProxyIT.java b/test/src/main/java/org/apache/accumulo/test/functional/KerberosProxyIT.java index fdc6866c07b..4ea76d1eb77 100644 --- a/test/src/main/java/org/apache/accumulo/test/functional/KerberosProxyIT.java +++ b/test/src/main/java/org/apache/accumulo/test/functional/KerberosProxyIT.java @@ -19,6 +19,7 @@ import static java.nio.charset.StandardCharsets.UTF_8; 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; @@ -72,16 +73,12 @@ import org.apache.thrift.transport.TSaslClientTransport; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransportException; -import org.hamcrest.Description; -import org.hamcrest.TypeSafeMatcher; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; -import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; -import org.junit.rules.ExpectedException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -99,9 +96,6 @@ public class KerberosProxyIT extends AccumuloITBase { private static final String PROXIED_USER1 = "proxied_user1", PROXIED_USER2 = "proxied_user2", PROXIED_USER3 = "proxied_user3"; - @Rule - public ExpectedException thrown = ExpectedException.none(); - private static TestingKdc kdc; private static String krbEnabledForITs = null; private static File proxyKeytab; @@ -414,19 +408,6 @@ public void testDisallowedClientForImpersonation() throws Exception { log.info("Logged in as " + ugi); - // Expect an AccumuloSecurityException - thrown.expect(AccumuloSecurityException.class); - // Error msg would look like: - // - // org.apache.accumulo.core.client.AccumuloSecurityException: Error BAD_CREDENTIALS for user - // Principal in credentials object should match kerberos - // principal. - // Expected 'proxy/hw10447.local@EXAMPLE.COM' but was - // 'testDisallowedClientForImpersonation@EXAMPLE.COM' - Username or Password is Invalid) - thrown.expect(new ThriftExceptionMatchesPattern(".*Error BAD_CREDENTIALS.*")); - thrown.expect(new ThriftExceptionMatchesPattern( - ".*Expected '" + proxyPrincipal + "' but was '" + kdc.qualifyUser(user) + "'.*")); - TSocket socket = new TSocket(hostname, proxyPort); log.info( "Connecting to proxy with server primary '" + proxyPrimary + "' running on " + hostname); @@ -446,7 +427,18 @@ public void testDisallowedClientForImpersonation() throws Exception { // Will fail because the proxy can't impersonate this user (per the site configuration) try { - client.login(kdc.qualifyUser(user), Collections.emptyMap()); + // Error msg would look like: + // + // org.apache.accumulo.core.client.AccumuloSecurityException: Error BAD_CREDENTIALS for user + // Principal in credentials object should match kerberos + // principal. + // Expected 'proxy/hw10447.local@EXAMPLE.COM' but was + // 'testDisallowedClientForImpersonation@EXAMPLE.COM' - Username or Password is Invalid) + AccumuloSecurityException e = assertThrows(AccumuloSecurityException.class, + () -> client.login(kdc.qualifyUser(user), Collections.emptyMap())); + assertTrue(thriftExceptionMatchesPattern(e, ".*Error BAD_CREDENTIALS.*")); + assertTrue(thriftExceptionMatchesPattern(e, + ".*Expected '" + proxyPrincipal + "' but was '" + kdc.qualifyUser(user) + "'.*")); } finally { if (null != ugiTransport) { ugiTransport.close(); @@ -457,10 +449,6 @@ public void testDisallowedClientForImpersonation() throws Exception { @Test public void testMismatchPrincipals() throws Exception { ClusterUser rootUser = kdc.getRootUser(); - // Should get an AccumuloSecurityException and the given message - thrown.expect(AccumuloSecurityException.class); - thrown - .expect(new ThriftExceptionMatchesPattern(ProxyServer.RPC_ACCUMULO_PRINCIPAL_MISMATCH_MSG)); // Make a new user String user = testName.getMethodName(); @@ -495,7 +483,9 @@ public void testMismatchPrincipals() throws Exception { // Accumulo should let this through -- we need to rely on the proxy to dump me before talking to // accumulo try { - client.login(rootUser.getPrincipal(), Collections.emptyMap()); + AccumuloSecurityException e = assertThrows(AccumuloSecurityException.class, + () -> client.login(rootUser.getPrincipal(), Collections.emptyMap())); + assertTrue(thriftExceptionMatchesPattern(e, ProxyServer.RPC_ACCUMULO_PRINCIPAL_MISMATCH_MSG)); } finally { if (null != ugiTransport) { ugiTransport.close(); @@ -607,28 +597,7 @@ public Void run() throws Exception { }); } - private static class ThriftExceptionMatchesPattern - extends TypeSafeMatcher { - private String pattern; - - public ThriftExceptionMatchesPattern(String pattern) { - this.pattern = pattern; - } - - @Override - protected boolean matchesSafely(AccumuloSecurityException item) { - return item.isSetMsg() && item.msg.matches(pattern); - } - - @Override - public void describeTo(Description description) { - description.appendText("matches pattern ").appendValue(pattern); - } - - @Override - protected void describeMismatchSafely(AccumuloSecurityException item, - Description mismatchDescription) { - mismatchDescription.appendText("does not match"); - } + private boolean thriftExceptionMatchesPattern(AccumuloSecurityException e, String pattern) { + return e.isSetMsg() && e.msg.matches(pattern); } } diff --git a/test/src/main/java/org/apache/accumulo/test/functional/MergeIT.java b/test/src/main/java/org/apache/accumulo/test/functional/MergeIT.java index f0d827008eb..abc862995f0 100644 --- a/test/src/main/java/org/apache/accumulo/test/functional/MergeIT.java +++ b/test/src/main/java/org/apache/accumulo/test/functional/MergeIT.java @@ -18,6 +18,7 @@ import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; import java.util.Arrays; import java.util.Collection; @@ -45,9 +46,7 @@ import org.apache.accumulo.server.util.TabletIterator; import org.apache.accumulo.server.util.TabletIterator.TabletDeletedException; import org.apache.hadoop.io.Text; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; public class MergeIT extends AccumuloClusterHarness { @@ -217,9 +216,6 @@ private void runMergeTest(Connector conn, String table, String[] splits, String[ } - @Rule - public ExpectedException exception = ExpectedException.none(); - private static class TestTabletIterator extends TabletIterator { private final Connector conn; @@ -282,9 +278,10 @@ public void testMerge() throws Exception { TestTabletIterator tabIter = new TestTabletIterator(getConnector(), metadataTableName); - exception.expect(TabletDeletedException.class); - while (tabIter.hasNext()) { - tabIter.next(); - } + assertThrows(TabletDeletedException.class, () -> { + while (tabIter.hasNext()) { + tabIter.next(); + } + }); } } diff --git a/test/src/main/java/org/apache/accumulo/test/functional/ScannerContextIT.java b/test/src/main/java/org/apache/accumulo/test/functional/ScannerContextIT.java index 12eadc8538b..b691d44c0fa 100644 --- a/test/src/main/java/org/apache/accumulo/test/functional/ScannerContextIT.java +++ b/test/src/main/java/org/apache/accumulo/test/functional/ScannerContextIT.java @@ -44,7 +44,6 @@ import org.apache.accumulo.minicluster.impl.MiniAccumuloClusterImpl; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; -import org.hamcrest.CoreMatchers; import org.junit.Assume; import org.junit.Before; import org.junit.Test; @@ -67,7 +66,7 @@ protected int defaultTimeoutSeconds() { @Before public void checkCluster() throws Exception { - Assume.assumeThat(getClusterType(), CoreMatchers.is(ClusterType.MINI)); + Assume.assumeTrue(getClusterType() == ClusterType.MINI); MiniAccumuloClusterImpl.class.cast(getCluster()); fs = FileSystem.get(CachedConfiguration.getInstance()); } diff --git a/test/src/main/java/org/apache/accumulo/test/functional/TableIT.java b/test/src/main/java/org/apache/accumulo/test/functional/TableIT.java index db49c8a905f..d1900ad70d9 100644 --- a/test/src/main/java/org/apache/accumulo/test/functional/TableIT.java +++ b/test/src/main/java/org/apache/accumulo/test/functional/TableIT.java @@ -40,7 +40,6 @@ import org.apache.accumulo.test.categories.MiniClusterOnlyTests; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; -import org.hamcrest.CoreMatchers; import org.junit.Assume; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -57,7 +56,7 @@ protected int defaultTimeoutSeconds() { @Test public void test() throws Exception { - Assume.assumeThat(getClusterType(), CoreMatchers.is(ClusterType.MINI)); + Assume.assumeTrue(getClusterType() == ClusterType.MINI); AccumuloCluster cluster = getCluster(); MiniAccumuloClusterImpl mac = (MiniAccumuloClusterImpl) cluster;