|
6 | 6 | import static org.junit.jupiter.api.Assertions.assertThrows; |
7 | 7 | import static org.junit.jupiter.api.Assertions.assertTrue; |
8 | 8 | import static org.mockito.Mockito.mock; |
| 9 | +import static org.mockito.Mockito.when; |
9 | 10 |
|
10 | 11 | import com.scalar.db.dataloader.core.DataLoaderError; |
11 | 12 | import java.util.Map; |
@@ -185,4 +186,128 @@ public void validatePositiveValue_differentErrorTypes_shouldUseCorrectErrorMessa |
185 | 186 | .getMessage() |
186 | 187 | .contains(DataLoaderError.INVALID_DATA_CHUNK_QUEUE_SIZE.buildMessage())); |
187 | 188 | } |
| 189 | + |
| 190 | + @Test |
| 191 | + public void validateDeprecatedOptionPair_onlyDeprecatedSpecified_shouldNotThrowException() { |
| 192 | + // Arrange |
| 193 | + CommandLine commandLine = mock(CommandLine.class); |
| 194 | + CommandLine.ParseResult parseResult = mock(CommandLine.ParseResult.class); |
| 195 | + when(commandLine.getParseResult()).thenReturn(parseResult); |
| 196 | + when(parseResult.hasMatchedOption("--old-option")).thenReturn(true); |
| 197 | + when(parseResult.hasMatchedOption("--new-option")).thenReturn(false); |
| 198 | + when(parseResult.hasMatchedOption("-n")).thenReturn(false); |
| 199 | + |
| 200 | + // Act & Assert - No exception should be thrown |
| 201 | + assertDoesNotThrow( |
| 202 | + () -> |
| 203 | + CommandLineInputUtils.validateDeprecatedOptionPair( |
| 204 | + commandLine, "--old-option", "--new-option", "-n")); |
| 205 | + } |
| 206 | + |
| 207 | + @Test |
| 208 | + public void validateDeprecatedOptionPair_onlyNewOptionSpecified_shouldNotThrowException() { |
| 209 | + // Arrange |
| 210 | + CommandLine commandLine = mock(CommandLine.class); |
| 211 | + CommandLine.ParseResult parseResult = mock(CommandLine.ParseResult.class); |
| 212 | + when(commandLine.getParseResult()).thenReturn(parseResult); |
| 213 | + when(parseResult.hasMatchedOption("--old-option")).thenReturn(false); |
| 214 | + when(parseResult.hasMatchedOption("--new-option")).thenReturn(true); |
| 215 | + when(parseResult.hasMatchedOption("-n")).thenReturn(false); |
| 216 | + |
| 217 | + // Act & Assert - No exception should be thrown |
| 218 | + assertDoesNotThrow( |
| 219 | + () -> |
| 220 | + CommandLineInputUtils.validateDeprecatedOptionPair( |
| 221 | + commandLine, "--old-option", "--new-option", "-n")); |
| 222 | + } |
| 223 | + |
| 224 | + @Test |
| 225 | + public void |
| 226 | + validateDeprecatedOptionPair_onlyNewOptionShortFormSpecified_shouldNotThrowException() { |
| 227 | + // Arrange |
| 228 | + CommandLine commandLine = mock(CommandLine.class); |
| 229 | + CommandLine.ParseResult parseResult = mock(CommandLine.ParseResult.class); |
| 230 | + when(commandLine.getParseResult()).thenReturn(parseResult); |
| 231 | + when(parseResult.hasMatchedOption("--old-option")).thenReturn(false); |
| 232 | + when(parseResult.hasMatchedOption("--new-option")).thenReturn(false); |
| 233 | + when(parseResult.hasMatchedOption("-n")).thenReturn(true); |
| 234 | + |
| 235 | + // Act & Assert - No exception should be thrown |
| 236 | + assertDoesNotThrow( |
| 237 | + () -> |
| 238 | + CommandLineInputUtils.validateDeprecatedOptionPair( |
| 239 | + commandLine, "--old-option", "--new-option", "-n")); |
| 240 | + } |
| 241 | + |
| 242 | + @Test |
| 243 | + public void validateDeprecatedOptionPair_neitherSpecified_shouldNotThrowException() { |
| 244 | + // Arrange |
| 245 | + CommandLine commandLine = mock(CommandLine.class); |
| 246 | + CommandLine.ParseResult parseResult = mock(CommandLine.ParseResult.class); |
| 247 | + when(commandLine.getParseResult()).thenReturn(parseResult); |
| 248 | + when(parseResult.hasMatchedOption("--old-option")).thenReturn(false); |
| 249 | + when(parseResult.hasMatchedOption("--new-option")).thenReturn(false); |
| 250 | + when(parseResult.hasMatchedOption("-n")).thenReturn(false); |
| 251 | + |
| 252 | + // Act & Assert - No exception should be thrown |
| 253 | + assertDoesNotThrow( |
| 254 | + () -> |
| 255 | + CommandLineInputUtils.validateDeprecatedOptionPair( |
| 256 | + commandLine, "--old-option", "--new-option", "-n")); |
| 257 | + } |
| 258 | + |
| 259 | + @Test |
| 260 | + public void validateDeprecatedOptionPair_bothSpecified_shouldThrowException() { |
| 261 | + // Arrange |
| 262 | + CommandLine commandLine = mock(CommandLine.class); |
| 263 | + CommandLine.ParseResult parseResult = mock(CommandLine.ParseResult.class); |
| 264 | + when(commandLine.getParseResult()).thenReturn(parseResult); |
| 265 | + when(parseResult.hasMatchedOption("--old-option")).thenReturn(true); |
| 266 | + when(parseResult.hasMatchedOption("--new-option")).thenReturn(true); |
| 267 | + when(parseResult.hasMatchedOption("-n")).thenReturn(false); |
| 268 | + |
| 269 | + // Act & Assert |
| 270 | + CommandLine.ParameterException exception = |
| 271 | + assertThrows( |
| 272 | + CommandLine.ParameterException.class, |
| 273 | + () -> |
| 274 | + CommandLineInputUtils.validateDeprecatedOptionPair( |
| 275 | + commandLine, "--old-option", "--new-option", "-n")); |
| 276 | + |
| 277 | + // Verify the exception message contains the error message |
| 278 | + assertTrue( |
| 279 | + exception |
| 280 | + .getMessage() |
| 281 | + .contains( |
| 282 | + DataLoaderError.DEPRECATED_AND_NEW_OPTION_BOTH_SPECIFIED.buildMessage( |
| 283 | + "--old-option", "--new-option", "--new-option"))); |
| 284 | + } |
| 285 | + |
| 286 | + @Test |
| 287 | + public void |
| 288 | + validateDeprecatedOptionPair_deprecatedAndNewShortFormSpecified_shouldThrowException() { |
| 289 | + // Arrange |
| 290 | + CommandLine commandLine = mock(CommandLine.class); |
| 291 | + CommandLine.ParseResult parseResult = mock(CommandLine.ParseResult.class); |
| 292 | + when(commandLine.getParseResult()).thenReturn(parseResult); |
| 293 | + when(parseResult.hasMatchedOption("--old-option")).thenReturn(true); |
| 294 | + when(parseResult.hasMatchedOption("--new-option")).thenReturn(false); |
| 295 | + when(parseResult.hasMatchedOption("-n")).thenReturn(true); |
| 296 | + |
| 297 | + // Act & Assert |
| 298 | + CommandLine.ParameterException exception = |
| 299 | + assertThrows( |
| 300 | + CommandLine.ParameterException.class, |
| 301 | + () -> |
| 302 | + CommandLineInputUtils.validateDeprecatedOptionPair( |
| 303 | + commandLine, "--old-option", "--new-option", "-n")); |
| 304 | + |
| 305 | + // Verify the exception message contains the error message |
| 306 | + assertTrue( |
| 307 | + exception |
| 308 | + .getMessage() |
| 309 | + .contains( |
| 310 | + DataLoaderError.DEPRECATED_AND_NEW_OPTION_BOTH_SPECIFIED.buildMessage( |
| 311 | + "--old-option", "--new-option", "--new-option"))); |
| 312 | + } |
188 | 313 | } |
0 commit comments