This is an automated email from the ASF dual-hosted git repository.
granthenke pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kudu.git
The following commit(s) were added to refs/heads/master by this push:
new 964b333 [java] Use Assert.assertThrows
964b333 is described below
commit 964b333d6a0c8441ee5577812e6aee9d6388f055
Author: Grant Henke <[email protected]>
AuthorDate: Mon Feb 10 11:37:44 2020 -0600
[java] Use Assert.assertThrows
This patch converts all usage of JUnit expected exception rules
to `Assert.assertThrows`. This is the new way to test expected
exceptions in JUnit 4.13+.
Change-Id: I1959d6a33bfd634f01332e3c3ef4d7af77b60fb0
Reviewed-on: http://gerrit.cloudera.org:8080/15196
Tested-by: Kudu Jenkins
Reviewed-by: Grant Henke <[email protected]>
---
.../mapreduce/tools/ITImportParquetPreCheck.java | 34 ++++++++---------
.../java/org/apache/kudu/TestColumnSchema.java | 30 +++++++++------
.../kudu/subprocess/echo/TestEchoSubprocess.java | 43 +++++++++++----------
.../org/apache/kudu/subprocess/TestMessageIO.java | 44 ++++++++++++----------
4 files changed, 82 insertions(+), 69 deletions(-)
diff --git
a/java/kudu-client-tools/src/test/java/org/apache/kudu/mapreduce/tools/ITImportParquetPreCheck.java
b/java/kudu-client-tools/src/test/java/org/apache/kudu/mapreduce/tools/ITImportParquetPreCheck.java
index e617f8c..1e13093 100644
---
a/java/kudu-client-tools/src/test/java/org/apache/kudu/mapreduce/tools/ITImportParquetPreCheck.java
+++
b/java/kudu-client-tools/src/test/java/org/apache/kudu/mapreduce/tools/ITImportParquetPreCheck.java
@@ -37,11 +37,11 @@ import org.apache.parquet.hadoop.ParquetWriter;
import org.apache.parquet.hadoop.example.GroupWriteSupport;
import org.apache.parquet.schema.MessageType;
import org.junit.After;
+import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.junit.rules.RuleChain;
+import org.junit.function.ThrowingRunnable;
import org.apache.kudu.ColumnSchema;
import org.apache.kudu.Schema;
@@ -78,15 +78,8 @@ public class ITImportParquetPreCheck {
schema = new Schema(columns);
}
- public KuduTestHarness harness = new KuduTestHarness();
- public ExpectedException thrown = ExpectedException.none();
-
- // ExpectedException misbehaves when combined with other rules; we use a
- // RuleChain to beat it into submission.
- //
- // See https://stackoverflow.com/q/28846088 for more information.
@Rule
- public RuleChain chain = RuleChain.outerRule(harness).around(thrown);
+ public KuduTestHarness harness = new KuduTestHarness();
@Before
public void setUp() throws Exception {
@@ -112,16 +105,21 @@ public class ITImportParquetPreCheck {
String[] args = new String[] { "-D" +
CommandLineParser.MASTER_ADDRESSES_KEY + "=" +
harness.getMasterAddressesAsString(), TABLE_NAME, data.toString()};
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("The column column1_i does not exist in Parquet
schema");
-
- GenericOptionsParser parser = new GenericOptionsParser(conf, args);
- Job job =
- ImportParquet.createSubmittableJob(parser.getConfiguration(),
parser.getRemainingArgs());
- job.waitForCompletion(true);
+ Throwable thrown = Assert.assertThrows(IllegalArgumentException.class, new
ThrowingRunnable() {
+ @Override
+ public void run() throws Exception {
+ GenericOptionsParser parser = new GenericOptionsParser(conf, args);
+ Job job = ImportParquet.createSubmittableJob(parser.getConfiguration(),
+ parser.getRemainingArgs());
+ job.waitForCompletion(true);
+ }
+ });
+ Assert.assertTrue(thrown.getMessage()
+ .contains("The column column1_i does not exist in Parquet
schema"));
KuduTable openTable = harness.getClient().openTable(TABLE_NAME);
- assertEquals(0,
countRowsInScan(harness.getAsyncClient().newScannerBuilder(openTable).build()));
+ assertEquals(0,
+
countRowsInScan(harness.getAsyncClient().newScannerBuilder(openTable).build()));
}
@SuppressWarnings("deprecation")
diff --git
a/java/kudu-client/src/test/java/org/apache/kudu/TestColumnSchema.java
b/java/kudu-client/src/test/java/org/apache/kudu/TestColumnSchema.java
index 84678cc..a3385a0 100644
--- a/java/kudu-client/src/test/java/org/apache/kudu/TestColumnSchema.java
+++ b/java/kudu-client/src/test/java/org/apache/kudu/TestColumnSchema.java
@@ -20,9 +20,10 @@ package org.apache.kudu;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
+import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
+import org.junit.function.ThrowingRunnable;
import org.apache.kudu.ColumnSchema.ColumnSchemaBuilder;
import org.apache.kudu.test.junit.RetryRule;
@@ -34,9 +35,6 @@ public class TestColumnSchema {
@Rule
public RetryRule retryRule = new RetryRule();
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
@Test
public void testToString() {
final ColumnSchema col1 = new ColumnSchemaBuilder("col1",
Type.STRING).build();
@@ -105,17 +103,27 @@ public class TestColumnSchema {
@Test
public void testOutOfRangeVarchar() throws Exception {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("VARCHAR's length must be set and between
1 and 65535");
- new ColumnSchemaBuilder("col1", Type.VARCHAR)
- .typeAttributes(CharUtil.typeAttributes(70000)).build();
+ Throwable thrown = Assert.assertThrows(IllegalArgumentException.class, new
ThrowingRunnable() {
+ @Override
+ public void run() throws Exception {
+ new ColumnSchemaBuilder("col1", Type.VARCHAR)
+ .typeAttributes(CharUtil.typeAttributes(70000)).build();
+ }
+ });
+ Assert.assertTrue(thrown.getMessage()
+ .contains("VARCHAR's length must be set and between 1 and 65535"));
}
@Test
public void testVarcharWithoutLength() throws Exception {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("VARCHAR's length must be set and between
1 and 65535");
- new ColumnSchemaBuilder("col1", Type.VARCHAR).build();
+ Throwable thrown = Assert.assertThrows(IllegalArgumentException.class, new
ThrowingRunnable() {
+ @Override
+ public void run() throws Exception {
+ new ColumnSchemaBuilder("col1", Type.VARCHAR).build();
+ }
+ });
+ Assert.assertTrue(thrown.getMessage()
+ .contains("VARCHAR's length must be set and between 1 and 65535"));
}
}
diff --git
a/java/kudu-subprocess-echo/src/test/java/org/apache/kudu/subprocess/echo/TestEchoSubprocess.java
b/java/kudu-subprocess-echo/src/test/java/org/apache/kudu/subprocess/echo/TestEchoSubprocess.java
index 338e55b..7151261 100644
---
a/java/kudu-subprocess-echo/src/test/java/org/apache/kudu/subprocess/echo/TestEchoSubprocess.java
+++
b/java/kudu-subprocess-echo/src/test/java/org/apache/kudu/subprocess/echo/TestEchoSubprocess.java
@@ -31,8 +31,7 @@ import java.util.function.Function;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.junit.rules.RuleChain;
+import org.junit.function.ThrowingRunnable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -57,16 +56,8 @@ public class TestEchoSubprocess {
return null;
};
- public ExpectedException thrown = ExpectedException.none();
- public RetryRule retryRule = new RetryRule();
-
- // ExpectedException misbehaves when combined with other rules; we use a
- // RuleChain to beat it into submission.
- //
- // See https://stackoverflow.com/q/28846088 for more information.
@Rule
- public RuleChain chain = RuleChain.outerRule(retryRule).around(thrown);
-
+ public RetryRule retryRule = new RetryRule();
public static class PrintStreamWithIOException extends PrintStream {
public PrintStreamWithIOException(OutputStream out, boolean autoFlush,
String encoding)
@@ -134,9 +125,13 @@ public class TestEchoSubprocess {
final PrintStream out =
new PrintStream(new ByteArrayOutputStream(), false, "UTF-8");
final String[] args = {""};
- thrown.expect(ExecutionException.class);
- thrown.expectMessage("Unable to read the protobuf message");
- runEchoSubprocess(in, out, args, HAS_ERR, /* injectInterrupt= */false);
+ Throwable thrown = Assert.assertThrows(ExecutionException.class, new
ThrowingRunnable() {
+ @Override
+ public void run() throws Exception {
+ runEchoSubprocess(in, out, args, HAS_ERR, /* injectInterrupt= */false);
+ }
+ });
+ Assert.assertTrue(thrown.getMessage().contains("Unable to read the
protobuf message"));
}
/**
@@ -154,9 +149,13 @@ public class TestEchoSubprocess {
// Only use one writer task to avoid get TimeoutException instead for
// writer tasks that haven't encountered any exceptions.
final String[] args = {"-w", "1"};
- thrown.expect(ExecutionException.class);
- thrown.expectMessage("Unable to write the protobuf message");
- runEchoSubprocess(in, out, args, HAS_ERR, /* injectInterrupt= */false);
+ Throwable thrown = Assert.assertThrows(ExecutionException.class, new
ThrowingRunnable() {
+ @Override
+ public void run() throws Exception {
+ runEchoSubprocess(in, out, args, HAS_ERR, /* injectInterrupt= */false);
+ }
+ });
+ Assert.assertTrue(thrown.getMessage().contains("Unable to write the
protobuf messag"));
}
/**
@@ -172,8 +171,12 @@ public class TestEchoSubprocess {
final PrintStream out =
new PrintStream(new ByteArrayOutputStream(), false, "UTF-8");
final String[] args = {""};
- thrown.expect(ExecutionException.class);
- thrown.expectMessage("Unable to put the message to the queue");
- runEchoSubprocess(in, out, args, HAS_ERR, /* injectInterrupt= */true);
+ Throwable thrown = Assert.assertThrows(ExecutionException.class, new
ThrowingRunnable() {
+ @Override
+ public void run() throws Exception {
+ runEchoSubprocess(in, out, args, HAS_ERR, /* injectInterrupt= */true);
+ }
+ });
+ Assert.assertTrue(thrown.getMessage().contains("Unable to put the message
to the queue"));
}
}
diff --git
a/java/kudu-subprocess/src/test/java/org/apache/kudu/subprocess/TestMessageIO.java
b/java/kudu-subprocess/src/test/java/org/apache/kudu/subprocess/TestMessageIO.java
index 0f99afa..fba9d89 100644
---
a/java/kudu-subprocess/src/test/java/org/apache/kudu/subprocess/TestMessageIO.java
+++
b/java/kudu-subprocess/src/test/java/org/apache/kudu/subprocess/TestMessageIO.java
@@ -30,8 +30,7 @@ import com.google.common.primitives.Bytes;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.junit.rules.RuleChain;
+import org.junit.function.ThrowingRunnable;
import org.apache.kudu.subprocess.Subprocess.SubprocessRequestPB;
import org.apache.kudu.test.junit.RetryRule;
@@ -41,15 +40,8 @@ import org.apache.kudu.test.junit.RetryRule;
*/
public class TestMessageIO {
- public ExpectedException thrown = ExpectedException.none();
- public RetryRule retryRule = new RetryRule();
-
- // ExpectedException misbehaves when combined with other rules; we use a
- // RuleChain to beat it into submission.
- //
- // See https://stackoverflow.com/q/28846088 for more information.
@Rule
- public RuleChain chainRule = RuleChain.outerRule(retryRule).around(thrown);
+ public RetryRule retryRule = new RetryRule();
public static class PrintStreamOverload extends PrintStream {
public PrintStreamOverload(OutputStream out) {
@@ -94,10 +86,14 @@ public class TestMessageIO {
new SubprocessOutputStream(printStreamOverload));
final MessageIO messageIO = new MessageIO(
SubprocessConfiguration.MAX_MESSAGE_BYTES_DEFAULT, /* in= */null, out);
- thrown.expect(IOException.class);
- thrown.expectMessage(SubprocessOutputStream.WRITE_ERR);
- printStreamOverload.setError();
- messageIO.writeMessage(request);
+ Throwable thrown = Assert.assertThrows(IOException.class, new
ThrowingRunnable() {
+ @Override
+ public void run() throws Exception {
+ printStreamOverload.setError();
+ messageIO.writeMessage(request);
+ }
+ });
+
Assert.assertTrue(thrown.getMessage().contains(SubprocessOutputStream.WRITE_ERR));
}
/**
@@ -113,9 +109,13 @@ public class TestMessageIO {
BufferedInputStream in = new BufferedInputStream(byteInputStream);
MessageIO messageIO = new
MessageIO(SubprocessConfiguration.MAX_MESSAGE_BYTES_DEFAULT,
in, /* out= */null);
- thrown.expect(IOException.class);
- thrown.expectMessage("exceeds maximum message size");
- messageIO.readBytes();
+ Throwable thrown = Assert.assertThrows(IOException.class, new
ThrowingRunnable() {
+ @Override
+ public void run() throws Exception {
+ messageIO.readBytes();
+ }
+ });
+ Assert.assertTrue(thrown.getMessage().contains("exceeds maximum message
size"));
}
/**
@@ -131,8 +131,12 @@ public class TestMessageIO {
BufferedInputStream in = new BufferedInputStream(new
ByteArrayInputStream(malformedMessage));
MessageIO messageIO = new
MessageIO(SubprocessConfiguration.MAX_MESSAGE_BYTES_DEFAULT,
in, /* out= */null);
- thrown.expect(IOException.class);
- thrown.expectMessage("unable to receive message");
- messageIO.readBytes();
+ Throwable thrown = Assert.assertThrows(IOException.class, new
ThrowingRunnable() {
+ @Override
+ public void run() throws Exception {
+ messageIO.readBytes();
+ }
+ });
+ Assert.assertTrue(thrown.getMessage().contains("unable to receive
message"));
}
}