|
18 | 18 |
|
19 | 19 | package org.apache.hadoop.util;
|
20 | 20 |
|
| 21 | +import java.util.function.Supplier; |
| 22 | + |
21 | 23 | import org.junit.Test;
|
22 | 24 |
|
23 | 25 | import org.apache.hadoop.test.LambdaTestUtils;
|
@@ -119,4 +121,104 @@ public void testCheckNotNullFailure() throws Exception {
|
119 | 121 | () -> Preconditions.checkNotNull(null,
|
120 | 122 | () -> String.format(NULL_FORMATTER, NON_NULL_STRING)));
|
121 | 123 | }
|
| 124 | + |
| 125 | + @Test |
| 126 | + public void testCheckArgumentWithSuccess() throws Exception { |
| 127 | + // success |
| 128 | + Preconditions.checkArgument(true); |
| 129 | + // null supplier |
| 130 | + Preconditions.checkArgument(true, null); |
| 131 | + // null message |
| 132 | + Preconditions.checkArgument(true, (String) null); |
| 133 | + Preconditions.checkArgument(true, NON_NULL_STRING); |
| 134 | + // null in string format |
| 135 | + Preconditions.checkArgument(true, EXPECTED_ERROR_MSG_ARGS, null, null); |
| 136 | + // illegalformat |
| 137 | + Preconditions.checkArgument(true, EXPECTED_ERROR_MSG_ARGS, 1, 2); |
| 138 | + // ill-formated string supplier |
| 139 | + Preconditions.checkArgument(true, ()-> String.format("%d", |
| 140 | + NON_INT_STRING)); |
| 141 | + // null pattern to string formatter |
| 142 | + Preconditions.checkArgument(true, NULL_FORMATTER, null, 1); |
| 143 | + // null arguments to string formatter |
| 144 | + Preconditions.checkArgument(true, EXPECTED_ERROR_MSG_ARGS, |
| 145 | + null, null); |
| 146 | + // illegal format exception |
| 147 | + Preconditions.checkArgument(true, "message %d %d", |
| 148 | + NON_INT_STRING, 1); |
| 149 | + // insufficient arguments |
| 150 | + Preconditions.checkArgument(true, EXPECTED_ERROR_MSG_ARGS, |
| 151 | + NON_INT_STRING); |
| 152 | + // null format in string supplier |
| 153 | + Preconditions.checkArgument(true, |
| 154 | + () -> String.format(NULL_FORMATTER, NON_INT_STRING)); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + public void testCheckArgumentWithFailure() throws Exception { |
| 159 | + // failure without message |
| 160 | + LambdaTestUtils.intercept(IllegalArgumentException.class, |
| 161 | + () -> Preconditions.checkArgument(false)); |
| 162 | + errorMessage = null; |
| 163 | + // failure with Null message |
| 164 | + LambdaTestUtils.intercept(IllegalArgumentException.class, |
| 165 | + null, |
| 166 | + () -> Preconditions.checkArgument(false, errorMessage)); |
| 167 | + // failure with message |
| 168 | + errorMessage = EXPECTED_ERROR_MSG; |
| 169 | + LambdaTestUtils.intercept(IllegalArgumentException.class, |
| 170 | + errorMessage, |
| 171 | + () -> Preconditions.checkArgument(false, errorMessage)); |
| 172 | + |
| 173 | + // failure with message format |
| 174 | + errorMessage = EXPECTED_ERROR_MSG + " %s"; |
| 175 | + String arg = "IllegalArgExcep"; |
| 176 | + String expectedMSG = String.format(errorMessage, arg); |
| 177 | + LambdaTestUtils.intercept(IllegalArgumentException.class, |
| 178 | + expectedMSG, |
| 179 | + () -> Preconditions.checkArgument(false, errorMessage, arg)); |
| 180 | + |
| 181 | + // failure with multiple arg message format |
| 182 | + errorMessage = EXPECTED_ERROR_MSG_ARGS; |
| 183 | + expectedMSG = |
| 184 | + String.format(errorMessage, arg, 1); |
| 185 | + LambdaTestUtils.intercept(IllegalArgumentException.class, |
| 186 | + expectedMSG, |
| 187 | + () -> Preconditions.checkArgument(false, errorMessage, arg, 1)); |
| 188 | + |
| 189 | + // ignore illegal format will be thrown if the case is not handled correctly |
| 190 | + LambdaTestUtils.intercept(IllegalArgumentException.class, |
| 191 | + Preconditions.getDefaultCheckArgumentMSG(), |
| 192 | + () -> Preconditions.checkArgument(false, |
| 193 | + errorMessage, 1, NON_INT_STRING)); |
| 194 | + |
| 195 | + // ignore illegal format will be thrown for insufficient Insufficient Args |
| 196 | + LambdaTestUtils.intercept(IllegalArgumentException.class, |
| 197 | + Preconditions.getDefaultCheckArgumentMSG(), |
| 198 | + () -> Preconditions.checkArgument(false, errorMessage, NON_NULL_STRING)); |
| 199 | + |
| 200 | + // failure with Null supplier |
| 201 | + final Supplier<String> nullSupplier = null; |
| 202 | + LambdaTestUtils.intercept(IllegalArgumentException.class, |
| 203 | + null, |
| 204 | + () -> Preconditions.checkArgument(false, nullSupplier)); |
| 205 | + |
| 206 | + // ignore illegal format in supplier |
| 207 | + LambdaTestUtils.intercept(IllegalArgumentException.class, |
| 208 | + Preconditions.getDefaultCheckArgumentMSG(), |
| 209 | + () -> Preconditions.checkArgument(false, |
| 210 | + () -> String.format(errorMessage, 1, NON_INT_STRING))); |
| 211 | + |
| 212 | + // ignore insufficient arguments in string Supplier |
| 213 | + LambdaTestUtils.intercept(IllegalArgumentException.class, |
| 214 | + Preconditions.getDefaultCheckArgumentMSG(), |
| 215 | + () -> Preconditions.checkArgument(false, |
| 216 | + () -> String.format(errorMessage, NON_NULL_STRING))); |
| 217 | + |
| 218 | + // ignore null formatter |
| 219 | + LambdaTestUtils.intercept(IllegalArgumentException.class, |
| 220 | + Preconditions.getDefaultCheckArgumentMSG(), |
| 221 | + () -> Preconditions.checkArgument(false, |
| 222 | + () -> String.format(NULL_FORMATTER, NON_NULL_STRING))); |
| 223 | + } |
122 | 224 | }
|
0 commit comments