amahussein commented on a change in pull request #2143: URL: https://github.com/apache/hadoop/pull/2143#discussion_r456672451
########## File path: hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/unguava/TestValidate.java ########## @@ -0,0 +1,154 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.util.unguava; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class TestValidate { + public static final Logger LOG = + LoggerFactory.getLogger(TestValidate.class); + + private static final String NON_NULL_STRING = "NON_NULL_OBJECT"; + private static final String NON_INT_STRING = "NOT_INT"; + private static final String EXPECTED_ERROR_MSG = "Expected-Error-MSG"; + private static final String EXPECTED_ERROR_MSG_ARGS = + EXPECTED_ERROR_MSG + "%s number %d"; + private static final String NULL_FORMATTER = null; + @Rule + public ExpectedException exceptionRule = ExpectedException.none(); + + private String errorMessage; + + @Test + public void testCheckNotNullSuccess() { + Validate.checkNotNull(NON_NULL_STRING); + // null supplier + Validate.checkNotNull(NON_NULL_STRING, null); + // exception in string supplier + Validate.checkNotNull(NON_NULL_STRING, ()-> String.format("%d", + NON_INT_STRING)); + // null pattern to string formatter + Validate.checkNotNull(NON_NULL_STRING, NULL_FORMATTER, null, 1); + // null arguments to string formatter + Validate.checkNotNull(NON_NULL_STRING, EXPECTED_ERROR_MSG_ARGS, + null, null); + // illegal format exception + Validate.checkNotNull(NON_NULL_STRING, "message %d %d", + NON_INT_STRING, 1); + // insufficient arguments + Validate.checkNotNull(NON_NULL_STRING, EXPECTED_ERROR_MSG_ARGS, + NON_INT_STRING); + // null format in string supplier + Validate.checkNotNull(NON_NULL_STRING, + () -> String.format(NULL_FORMATTER, NON_INT_STRING)); + } + + @Test + public void testCheckNotNullFailure() { + exceptionRule.expect(NullPointerException.class); + exceptionRule.expectMessage(Validate.VALIDATE_IS_NOT_NULL_EX_MESSAGE); + Validate.checkNotNull(null); + } + + @Test + public void testCheckNotNullFailureWithMSG() { + exceptionRule.expect(NullPointerException.class); Review comment: Thanks @steveloughran for your feedback. `LambdaTestUtils.intercept` is a very good tip. I did not know about it before. The unit test looks better now. I made another push addressing your comments and added some few comments. If you are okay with it, then we can go ahead and merge that into trunk, branch-3.3, branch-3.2, branch-3.1 ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
