From c98d2031fa5b43925b962812df74960e6d534da6 Mon Sep 17 00:00:00 2001 From: Nicola Di Falco Date: Mon, 7 Aug 2023 13:39:30 +0200 Subject: [PATCH] feat: better handling of input validation for number --- .../shell/component/NumberInput.java | 18 +++++++++++++----- .../shell/component/NumberInputTests.java | 11 +++++++++-- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/spring-shell-core/src/main/java/org/springframework/shell/component/NumberInput.java b/spring-shell-core/src/main/java/org/springframework/shell/component/NumberInput.java index c15f560ad..1bed0d7c1 100644 --- a/spring-shell-core/src/main/java/org/springframework/shell/component/NumberInput.java +++ b/spring-shell-core/src/main/java/org/springframework/shell/component/NumberInput.java @@ -126,10 +126,14 @@ protected boolean read(BindingReader bindingReader, KeyMap keyMap, Numbe checkInput(input, context); break; case OPERATION_EXIT: - if (StringUtils.hasText(context.getInput())) { + var num = parseNumber(context.getInput()); + + if (num != null) { context.setResultValue(parseNumber(context.getInput())); - } - else if (context.getDefaultValue() != null) { + } else if (StringUtils.hasText(context.getInput())) { + printInvalidInput(context.getInput(), context); + break; + } else if (context.getDefaultValue() != null) { context.setResultValue(context.getDefaultValue()); } else if (required) { context.setMessage("This field is mandatory", TextComponentContext.MessageLevel.ERROR); @@ -161,14 +165,18 @@ private void checkInput(String input, NumberInputContext context) { } Number num = parseNumber(input); if (num == null) { - String msg = String.format("Sorry, your input is invalid: '%s', try again", input); - context.setMessage(msg, MessageLevel.ERROR); + printInvalidInput(input, context); } else { context.setMessage(null); } } + private void printInvalidInput(String input, NumberInputContext context) { + String msg = String.format("Sorry, your input is invalid: '%s', try again", input); + context.setMessage(msg, MessageLevel.ERROR); + } + public interface NumberInputContext extends TextComponentContext { /** diff --git a/spring-shell-core/src/test/java/org/springframework/shell/component/NumberInputTests.java b/spring-shell-core/src/test/java/org/springframework/shell/component/NumberInputTests.java index d27153baf..3e3502e12 100644 --- a/spring-shell-core/src/test/java/org/springframework/shell/component/NumberInputTests.java +++ b/spring-shell-core/src/test/java/org/springframework/shell/component/NumberInputTests.java @@ -238,12 +238,19 @@ public void testResultUserInputInvalidInput() throws InterruptedException, IOExc latch1.await(2, TimeUnit.SECONDS); + NumberInputContext run1Context = result1.get(); assertThat(consoleOut()).contains("input is invalid"); + assertThat(run1Context).isNull(); - NumberInputContext run1Context = result1.get(); + // backspace 2 : cr + input + testBuffer.backspace(2).append("2").cr(); + write(testBuffer.getBytes()); + latch1.await(2, TimeUnit.SECONDS); + + run1Context = result1.get(); assertThat(run1Context).isNotNull(); - assertThat(run1Context.getResultValue()).isNull(); + assertThat(run1Context.getResultValue()).isEqualTo(2); } @Test