-
Notifications
You must be signed in to change notification settings - Fork 9.2k
HADOOP-19079. Check class is an exception class before constructing an instance #6557
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
HADOOP-19079. Check class is an exception class before constructing an instance #6557
Conversation
|
💔 -1 overall
This message was automatically generated. |
steveloughran
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, I can see the need for this
| Mockito.when(conn.getResponseCode()).thenReturn( | ||
| HttpURLConnection.HTTP_BAD_REQUEST); | ||
| try { | ||
| HttpExceptionUtils.validateResponse(conn, HttpURLConnection.HTTP_CREATED); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use LambdaTestUtils.intercept(), which is based off ScalaTest's version, includes test for error message text.
intercept(IOException.class,
"HTTP status [400], exception [java.lang.String], message [EX], URL [null]",
() -> HttpExceptionUtils.validateResponse(conn, HttpURLConnection.HTTP_CREATED);There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can change this but I just want to highlight that this is a modified copy of existing tests in this test class and this is the code style in the existing tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
old code, going near the test is the time to clean it up.
|
💔 -1 overall
This message was automatically generated. |
|
💔 -1 overall
This message was automatically generated. |
|
💔 -1 overall
This message was automatically generated. |
|
💔 -1 overall
This message was automatically generated. |
|
💔 -1 overall
This message was automatically generated. |
|
|
@steveloughran @slfan1989 is this something that could be considered for Hadoop 3.4.0 or 3.4.1? |
c44f222 to
b20a5d8
Compare
|
💔 -1 overall
This message was automatically generated. |
|
@steveloughran is this something that could be merged to trunk with a view to also merging into 3.4.1? |
steveloughran
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
quick review
| Constructor constr = klass.getConstructor(String.class); | ||
| toThrow = (Exception) constr.newInstance(exMsg); | ||
| } catch (Exception ex) { | ||
| if (!Exception.class.isAssignableFrom(klass)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use Preconditions.checkState() with error text including classname. if it ever does happen, we will want to debug
| Mockito.when(conn.getResponseCode()).thenReturn( | ||
| HttpURLConnection.HTTP_BAD_REQUEST); | ||
| try { | ||
| HttpExceptionUtils.validateResponse(conn, HttpURLConnection.HTTP_CREATED); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
old code, going near the test is the time to clean it up.
b20a5d8 to
65efd0d
Compare
|
🎊 +1 overall
This message was automatically generated. |
|
@steveloughran I made some changes based on your review. |
steveloughran
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
like the changes, now just wondering how much of a check should be done in intercept() versus having some uses of assertExceptionContains() afterwards
ioe e = intercept(IOE,"400", () -> {...})
assertExceptionContains(e, "EX")
...ommon-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestHttpExceptionUtils.java
Outdated
Show resolved
Hide resolved
65efd0d to
fa79af3
Compare
|
💔 -1 overall
This message was automatically generated. |
|
🎊 +1 overall
This message was automatically generated. |
steveloughran
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good, I am worried about overloaded methods though -we've had so many problems there in the past. the new method is good (one extra resilience change proposed) -but should we still use intercept() or give it a slightly different name.
we lifted the name from ScalaTest, if you've never played with that yet.
| throws Exception { | ||
| return intercept(clazz, | ||
| null, | ||
| (String) null, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ooh, I worry if there are going to be link problems by anything not in our codebase using the method (gcs etc). Hope not.
| Assert.assertTrue(ex.getMessage().contains("" + | ||
| HttpURLConnection.HTTP_BAD_REQUEST)); | ||
| } | ||
| Collection<String> expectedValues = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Arrays.asList() is easier
| ex = (E) e; | ||
| } | ||
| } | ||
| for (String contained : contains) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
skip if contains==null
I am very familiar with Scala test. I code mainly in Scala. Junit has assertThrows though. Would that be a bit more Java friendly? |
|
🎊 +1 overall
This message was automatically generated. |
|
🎊 +1 overall
This message was automatically generated. |
|
💔 -1 overall
This message was automatically generated. |
| * can be determined. | ||
| * @see GenericTestUtils#assertExceptionContains(String, Throwable) | ||
| */ | ||
| public static <T, E extends Throwable> E intercept( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do think this is a good feature, it's just we need to give it a name which doesn't run any risk of causing compilation problems.
alternatively, in GenericTestUtils add a new method assertExceptionContainsAllStrings() taking a varargs list of strings.
|
🎊 +1 overall
This message was automatically generated. |
|
🎊 +1 overall
This message was automatically generated. |
steveloughran
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code looking good now, one change left which is to use the java8 string concat stuff. commons-collections can also do this, but the JDK stuff works perfectly well
hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/LambdaTestUtils.java
Show resolved
Hide resolved
|
🎊 +1 overall
This message was automatically generated. |
steveloughran
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
+1
|
merged to trunk; cherrypicking to branch-3.4. branch-3.3 as well, perhaps |
…y an exception before instantiation (#6557) Security hardening + Adds new interceptAndValidateMessageContains() method in LambdaTestUtils to verify a list of strings can all be found in the toString() value of a raised exception Contributed by PJ Fanning
…y an exception before instantiation (#6557) Security hardening + Adds new interceptAndValidateMessageContains() method in LambdaTestUtils to verify a list of strings can all be found in the toString() value of a raised exception Contributed by PJ Fanning
one thing intercept does, which I haven't seen the others to, is include the toString() value of anything returned by the callable in the assertion. which lets you add tests that explicitly print their state on failures, rather than just "l-exp invoked didn't fail". |
Description of PR
HADOOP-19079
How was this patch tested?
New test added
For code changes:
LICENSE,LICENSE-binary,NOTICE-binaryfiles?