From f69b2de3a5dafa15319d1d86e6657353f909dd4d Mon Sep 17 00:00:00 2001 From: Aleksandar Kostadinov Date: Wed, 13 Dec 2017 15:21:56 +0200 Subject: [PATCH] fix dep and FindBug issues --- pom.xml | 2 +- .../logstash/LogstashBuildWrapper.java | 1 + .../AbstractLogstashIndexerDao.java | 7 ++--- .../logstash/persistence/BuildData.java | 28 +++++++++---------- .../plugins/logstash/LogstashWriterTest.java | 10 +++---- .../logstash/persistence/BuildDataTest.java | 8 ++---- 6 files changed, 24 insertions(+), 32 deletions(-) diff --git a/pom.xml b/pom.xml index 73216d60..2eb144a7 100644 --- a/pom.xml +++ b/pom.xml @@ -90,7 +90,7 @@ org.mockito mockito-core - 2.1.0 + 2.13.0 jar test diff --git a/src/main/java/jenkins/plugins/logstash/LogstashBuildWrapper.java b/src/main/java/jenkins/plugins/logstash/LogstashBuildWrapper.java index 34f5dfee..f55772b1 100644 --- a/src/main/java/jenkins/plugins/logstash/LogstashBuildWrapper.java +++ b/src/main/java/jenkins/plugins/logstash/LogstashBuildWrapper.java @@ -120,6 +120,7 @@ public DescriptorImpl getDescriptor() { return (DescriptorImpl) super.getDescriptor(); } + @CheckForNull public SecureGroovyScript getSecureGroovyScript() { return secureGroovyScript; } diff --git a/src/main/java/jenkins/plugins/logstash/persistence/AbstractLogstashIndexerDao.java b/src/main/java/jenkins/plugins/logstash/persistence/AbstractLogstashIndexerDao.java index ebbbb2ad..1c1ddaf8 100644 --- a/src/main/java/jenkins/plugins/logstash/persistence/AbstractLogstashIndexerDao.java +++ b/src/main/java/jenkins/plugins/logstash/persistence/AbstractLogstashIndexerDao.java @@ -24,7 +24,7 @@ package jenkins.plugins.logstash.persistence; -import java.util.Calendar; +import java.util.Date; import java.util.List; import org.apache.commons.lang.StringUtils; @@ -59,9 +59,6 @@ abstract class AbstractLogstashIndexerDao implements LogstashIndexerDao { } @Override - @SuppressFBWarnings( - value="STCAL_INVOKE_ON_STATIC_DATE_FORMAT_INSTANCE", - justification="TODO: not sure how to fix this") public JSONObject buildPayload(BuildData buildData, String jenkinsUrl, List logLines) { JSONObject payload = new JSONObject(); payload.put("data", buildData.toJson()); @@ -69,7 +66,7 @@ public JSONObject buildPayload(BuildData buildData, String jenkinsUrl, List build, Date currentTime, TaskListener listener) { } } - @SuppressFBWarnings( - value={"NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE","STCAL_INVOKE_ON_STATIC_DATE_FORMAT_INSTANCE"}, - justification="TODO: not sure how to fix this") + // ISO 8601 date format + public static String formatDateIso(Date date) { + return DATE_FORMATTER.format(date); + } + private void initData(Run build, Date currentTime) { - result = build.getResult() == null ? null : build.getResult().toString(); + Result buildResult = build.getResult(); + result = buildResult == null ? null : buildResult.toString(); id = build.getId(); projectName = build.getParent().getName(); fullProjectName = build.getParent().getFullName(); @@ -235,7 +236,7 @@ private void initData(Run build, Date currentTime) { } buildDuration = currentTime.getTime() - build.getStartTimeInMillis(); - timestamp = DATE_FORMATTER.format(build.getTimestamp().getTime()); + timestamp = formatDateIso(build.getTime()); } @Override @@ -349,11 +350,8 @@ public String getTimestamp() { return timestamp; } - @SuppressFBWarnings( - value="STCAL_INVOKE_ON_STATIC_DATE_FORMAT_INSTANCE", - justification="TODO: not sure how to fix this") public void setTimestamp(Calendar timestamp) { - this.timestamp = DATE_FORMATTER.format(timestamp.getTime()); + this.timestamp = formatDateIso(timestamp.getTime()); } public String getRootProjectName() { diff --git a/src/test/java/jenkins/plugins/logstash/LogstashWriterTest.java b/src/test/java/jenkins/plugins/logstash/LogstashWriterTest.java index eaa03f45..071633cb 100644 --- a/src/test/java/jenkins/plugins/logstash/LogstashWriterTest.java +++ b/src/test/java/jenkins/plugins/logstash/LogstashWriterTest.java @@ -28,7 +28,7 @@ import java.io.OutputStream; import java.util.Arrays; import java.util.Collections; -import java.util.GregorianCalendar; +import java.util.Date; import java.util.List; import static org.hamcrest.core.StringContains.containsString; @@ -104,15 +104,13 @@ public void before() throws Exception { when(mockBuild.getParent()).thenReturn(mockProject); when(mockBuild.getBuiltOn()).thenReturn(null); when(mockBuild.getNumber()).thenReturn(123456); - when(mockBuild.getTimestamp()).thenReturn(new GregorianCalendar()); + when(mockBuild.getTime()).thenReturn(new Date()); when(mockBuild.getRootBuild()).thenReturn(mockBuild); when(mockBuild.getBuildVariables()).thenReturn(Collections.emptyMap()); when(mockBuild.getSensitiveBuildVariables()).thenReturn(Collections.emptySet()); when(mockBuild.getEnvironments()).thenReturn(null); when(mockBuild.getAction(AbstractTestResultAction.class)).thenReturn(mockTestResultAction); - when(mockBuild.getLog(0)).thenReturn(Arrays.asList()); when(mockBuild.getLog(3)).thenReturn(Arrays.asList("line 1", "line 2", "line 3", "Log truncated...")); - when(mockBuild.getLog(Integer.MAX_VALUE)).thenReturn(Arrays.asList("line 1", "line 2", "line 3", "line 4")); when(mockBuild.getEnvironment(null)).thenReturn(new EnvVars()); when(mockTestResultAction.getTotalCount()).thenReturn(0); @@ -158,7 +156,7 @@ public void constructorSuccess() throws Exception { // Verify that the BuildData constructor is what is being called here. // This also lets us verify that in the instantiation failure cases we do not construct BuildData. verify(mockBuild).getId(); - verify(mockBuild, times(2)).getResult(); + verify(mockBuild, times(1)).getResult(); verify(mockBuild, times(2)).getParent(); verify(mockBuild, times(2)).getProject(); verify(mockBuild, times(1)).getStartTimeInMillis(); @@ -169,7 +167,7 @@ public void constructorSuccess() throws Exception { verify(mockBuild).getAction(AbstractTestResultAction.class); verify(mockBuild).getBuiltOn(); verify(mockBuild, times(2)).getNumber(); - verify(mockBuild).getTimestamp(); + verify(mockBuild).getTime(); verify(mockBuild, times(4)).getRootBuild(); verify(mockBuild).getBuildVariables(); verify(mockBuild).getSensitiveBuildVariables(); diff --git a/src/test/java/jenkins/plugins/logstash/persistence/BuildDataTest.java b/src/test/java/jenkins/plugins/logstash/persistence/BuildDataTest.java index 51dfb09b..857029f5 100644 --- a/src/test/java/jenkins/plugins/logstash/persistence/BuildDataTest.java +++ b/src/test/java/jenkins/plugins/logstash/persistence/BuildDataTest.java @@ -9,7 +9,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.Date; -import java.util.GregorianCalendar; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -69,10 +68,9 @@ public void before() throws Exception { when(mockBuild.getDisplayName()).thenReturn("BuildData Test"); when(mockBuild.getFullDisplayName()).thenReturn("BuildData Test #123456"); when(mockBuild.getDescription()).thenReturn("Mock project for testing BuildData"); - when(mockBuild.getProject()).thenReturn(mockProject); when(mockBuild.getParent()).thenReturn(mockProject); when(mockBuild.getNumber()).thenReturn(123456); - when(mockBuild.getTimestamp()).thenReturn(new GregorianCalendar()); + when(mockBuild.getTime()).thenReturn(new Date()); when(mockBuild.getRootBuild()).thenReturn(mockBuild); when(mockBuild.getBuildVariables()).thenReturn(Collections.emptyMap()); when(mockBuild.getSensitiveBuildVariables()).thenReturn(Collections.emptySet()); @@ -116,7 +114,7 @@ private void verifyMocks() throws Exception verify(mockProject).getFullName(); verify(mockBuild).getId(); - verify(mockBuild, times(2)).getResult(); + verify(mockBuild, times(1)).getResult(); verify(mockBuild, times(2)).getParent(); verify(mockBuild).getDisplayName(); verify(mockBuild).getFullDisplayName(); @@ -126,7 +124,7 @@ private void verifyMocks() throws Exception verify(mockBuild).getAction(AbstractTestResultAction.class); verify(mockBuild).getBuiltOn(); verify(mockBuild).getNumber(); - verify(mockBuild).getTimestamp(); + verify(mockBuild).getTime(); verify(mockBuild, times(4)).getRootBuild(); verify(mockBuild).getBuildVariables(); verify(mockBuild).getSensitiveBuildVariables();