From 4d754c494b999e805239987c942d4c931748fd05 Mon Sep 17 00:00:00 2001 From: "chris.arnott@softwire.com" Date: Wed, 2 Dec 2015 20:37:09 +0000 Subject: [PATCH] JENKINS-29059: Update to use FormValidation.aggregate() --- .../java/hudson/triggers/TimerTrigger.java | 30 ++- .../main/java/hudson/util/ResponseObject.java | 67 ------- .../java/hudson/util/ResponseObjectTest.java | 182 ------------------ 3 files changed, 12 insertions(+), 267 deletions(-) delete mode 100644 core/src/main/java/hudson/util/ResponseObject.java delete mode 100644 test/src/test/java/hudson/util/ResponseObjectTest.java diff --git a/core/src/main/java/hudson/triggers/TimerTrigger.java b/core/src/main/java/hudson/triggers/TimerTrigger.java index 41b85e2400..c2a3ee4574 100644 --- a/core/src/main/java/hudson/triggers/TimerTrigger.java +++ b/core/src/main/java/hudson/triggers/TimerTrigger.java @@ -34,8 +34,10 @@ import hudson.scheduler.CronTabList; import hudson.scheduler.Hash; import hudson.util.FormValidation; import java.text.DateFormat; +import java.util.ArrayList; import java.util.Calendar; -import hudson.util.ResponseObject; +import java.util.Collection; + import org.kohsuke.stapler.AncestorInPath; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.QueryParameter; @@ -84,14 +86,10 @@ public class TimerTrigger extends Trigger { public FormValidation doCheckSpec(@QueryParameter String value, @AncestorInPath Item item) { try { CronTabList ctl = CronTabList.create(fixNull(value), item != null ? Hash.from(item.getFullName()) : null); - ResponseObject response = new ResponseObject(); - response = getResponseForSanity(response, ctl); - response = getResponseForNextRun(response, ctl); - if (response.hasWarning()) { - return FormValidation.warning(response.getWarningAndMessge()); - } else { - return FormValidation.ok(response.getMessage()); - } + Collection validations = new ArrayList<>(); + updateValidationsForSanity(validations, ctl); + updateValidationsForNextRun(validations, ctl); + return FormValidation.aggregate(validations); } catch (ANTLRException e) { if (value.trim().indexOf('\n')==-1 && value.contains("**")) return FormValidation.error(Messages.TimerTrigger_MissingWhitespace()); @@ -99,23 +97,19 @@ public class TimerTrigger extends Trigger { } } - private ResponseObject getResponseForSanity(ResponseObject response, CronTabList ctl) { + private void updateValidationsForSanity(Collection validations, CronTabList ctl) { String msg = ctl.checkSanity(); - if(msg!=null) { - return response.withExtraWarning(msg); - } else { - return response; - } + if(msg!=null) validations.add(FormValidation.warning(msg)); } - private ResponseObject getResponseForNextRun(ResponseObject response, CronTabList ctl) { + private void updateValidationsForNextRun(Collection validations, CronTabList ctl) { Calendar prev = ctl.previous(); Calendar next = ctl.next(); if (prev != null && next != null) { DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL); - return response.withExtraMessage(Messages.TimerTrigger_would_last_have_run_at_would_next_run_at(fmt.format(prev.getTime()), fmt.format(next.getTime()))); + validations.add(FormValidation.ok(Messages.TimerTrigger_would_last_have_run_at_would_next_run_at(fmt.format(prev.getTime()), fmt.format(next.getTime())))); } else { - return response.withExtraWarning(Messages.TimerTrigger_no_schedules_so_will_never_run()); + validations.add(FormValidation.warning(Messages.TimerTrigger_no_schedules_so_will_never_run())); } } } diff --git a/core/src/main/java/hudson/util/ResponseObject.java b/core/src/main/java/hudson/util/ResponseObject.java deleted file mode 100644 index d9dd327283..0000000000 --- a/core/src/main/java/hudson/util/ResponseObject.java +++ /dev/null @@ -1,67 +0,0 @@ -package hudson.util; - -/** - * A class used to contain types of string response. Extra messages and warnings - * can be appended to the class until a response is required. - * The purpose of this class is to allow methods to update the message or - * warning response without needing to return the entire response, allowing more - * complex responses to be constructed. - */ -public class ResponseObject { - private final String message; - private final String warning; - - public ResponseObject() { - message = ""; - warning = ""; - } - - public ResponseObject(String message, String warning) { - this.message = message; - this.warning = warning; - } - - public boolean hasMessage() { - return !message.isEmpty(); - } - - public boolean hasWarning() { - return !warning.isEmpty(); - } - - public String getMessage() { - return message; - } - - public String getWarning() { - return warning; - } - - public String getWarningAndMessge() { - if (hasWarning() && hasMessage()) { - return warning + "; " + message; - } else { - return warning + message; - } - } - - public ResponseObject withExtraMessage(String message) { - String updatedMessage; - if (this.hasMessage()) { - updatedMessage = this.message + "; " + message; - } else { - updatedMessage = message; - } - return new ResponseObject(updatedMessage, warning); - } - - public ResponseObject withExtraWarning(String warning) { - String updatedWarning; - if (this.hasWarning()) { - updatedWarning = this.warning + "; " + warning; - } else { - updatedWarning = warning; - } - return new ResponseObject(message, updatedWarning); - } -} diff --git a/test/src/test/java/hudson/util/ResponseObjectTest.java b/test/src/test/java/hudson/util/ResponseObjectTest.java deleted file mode 100644 index 4b2b2c9e73..0000000000 --- a/test/src/test/java/hudson/util/ResponseObjectTest.java +++ /dev/null @@ -1,182 +0,0 @@ -package hudson.util; - -import org.junit.Test; -import static org.junit.Assert.*; - -public class ResponseObjectTest { - - @Test - public void emptyMessageReportsAsEmpty() throws Exception { - // given - ResponseObject response = new ResponseObject(); - - // when - boolean hasMessage = response.hasMessage(); - - // then - assertFalse(hasMessage); - } - - @Test - public void emptyWarningReportsAsEmpty() throws Exception { - // given - ResponseObject response = new ResponseObject(); - - // when - boolean hasWarning = response.hasWarning(); - - // then - assertFalse(hasWarning); - } - - @Test - public void nonEmptyMessageReportsAsNonEmpty() throws Exception { - // given - ResponseObject response = new ResponseObject("test", ""); - - // when - boolean hasMessage = response.hasMessage(); - - // then - assertTrue(hasMessage); - } - - @Test - public void nonEmptWarningReportsAsNonEmpty() throws Exception { - // given - ResponseObject response = new ResponseObject("", "test"); - - // when - boolean hasWarning = response.hasWarning(); - - // then - assertTrue(hasWarning); - } - - @Test - public void messageIsReturned() throws Exception { - // given - String expected = "test123"; - ResponseObject response = new ResponseObject(expected, ""); - - // when - String actual = response.getMessage(); - - // then - assertEquals(expected, actual); - } - - @Test - public void warningIsReturned() throws Exception { - // given - String expected = "test123"; - ResponseObject response = new ResponseObject("", expected); - - // when - String actual = response.getWarning(); - - // then - assertEquals(expected, actual); - } - - @Test - public void emptyMessageIsUpdatedCorrectly() throws Exception { - // given - String expected = "test123"; - ResponseObject response = new ResponseObject(); - - // when - ResponseObject newResponse = response.withExtraMessage(expected); - String actual = newResponse.getMessage(); - - // then - assertEquals(expected, actual); - } - - @Test - public void emptyWarningIsUpdatedCorrectly() throws Exception { - // given - String expected = "test123"; - ResponseObject response = new ResponseObject(); - - // when - ResponseObject newResponse = response.withExtraWarning(expected); - String actual = newResponse.getWarning(); - - // then - assertEquals(expected, actual); - } - - @Test - public void nonEmptyMessageIsUpdatedCorrectly() throws Exception { - // given - String extra = "123"; - String initial = "test"; - ResponseObject response = new ResponseObject(initial, ""); - - // when - ResponseObject newResponse = response.withExtraMessage(extra); - String actual = newResponse.getMessage(); - - // then - String expected = initial + "; " + extra; - assertEquals(expected, actual); - } - - @Test - public void nonEmptyWarningIsUpdatedCorrectly() throws Exception { - // given - String extra = "123"; - String initial = "test"; - ResponseObject response = new ResponseObject("", initial); - - // when - ResponseObject newResponse = response.withExtraWarning(extra); - String actual = newResponse.getWarning(); - - // then - String expected = initial + "; " + extra; - assertEquals(expected, actual); - } - - @Test - public void warningAndMessageAreSeparated() throws Exception { - // given - String warning = "warning"; - String message = "message"; - ResponseObject response = new ResponseObject(message, warning); - - // when - String actual = response.getWarningAndMessge(); - - // then - String expected = warning + "; " + message; - assertEquals(expected, actual); - } - - @Test - public void onlyWarningNotSeparated() throws Exception { - // given - String warning = "warning"; - ResponseObject response = new ResponseObject("", warning); - - // when - String actual = response.getWarningAndMessge(); - - // then - assertEquals(warning, actual); - } - - @Test - public void onlyMessageNotSeparated() throws Exception { - // given - String message = "message"; - ResponseObject response = new ResponseObject(message, ""); - - // when - String actual = response.getWarningAndMessge(); - - // then - assertEquals(message, actual); - } -} -- GitLab