From 8c5df6a8a55da6f57f7f12fe849aba47a26655e5 Mon Sep 17 00:00:00 2001 From: Aleksi Simell Date: Thu, 22 Feb 2024 12:28:15 +0200 Subject: [PATCH] JENKINS-63900 - Add possibility to use build display name as graph label (#66) * Indent comments * Add possibility to use display_name as graph label * Add help texts * Add more help texts * Add unit test for custom labels --- .../hudson/plugins/robot/RobotParser.java | 4 +- .../plugins/robot/graph/RobotBuildLabel.java | 1 + .../plugins/robot/RobotConfig/config.jelly | 2 +- .../robot/RobotPublisher/config.properties | 2 +- .../help-overwriteXAxisLabel.html | 2 +- .../RobotStep/help-overwriteXAxisLabel.html | 2 +- .../robot/graph/RobotGraphHelperTest.java | 223 +++++++++--------- 7 files changed, 123 insertions(+), 113 deletions(-) diff --git a/src/main/java/hudson/plugins/robot/RobotParser.java b/src/main/java/hudson/plugins/robot/RobotParser.java index 3a3c8502..a969752d 100644 --- a/src/main/java/hudson/plugins/robot/RobotParser.java +++ b/src/main/java/hudson/plugins/robot/RobotParser.java @@ -124,8 +124,8 @@ else if("robot".equals(tagName)){ String value = reader.getAttributeValue(null, "schemaversion"); value = value == null ? "0" : value; schemaVersion = Integer.parseInt(value); -// RF schemaVersion does not follow major version number. -// schemaVersion 5 == RF7.0 + // RF schemaVersion does not follow major version number. + // schemaVersion 5 == RF7.0 if (schemaVersion >= 5) { startLocalName = "start"; elapsedLocalName = "elapsed"; diff --git a/src/main/java/hudson/plugins/robot/graph/RobotBuildLabel.java b/src/main/java/hudson/plugins/robot/graph/RobotBuildLabel.java index 6aef0ddf..9cdadb5a 100644 --- a/src/main/java/hudson/plugins/robot/graph/RobotBuildLabel.java +++ b/src/main/java/hudson/plugins/robot/graph/RobotBuildLabel.java @@ -19,6 +19,7 @@ public RobotBuildLabel(RobotTestObject obj, String format) { private String formatBuildLabel(String format, Date startTime) { String pattern = format.replace("$build",""+run.number); + pattern = pattern.replace("$display_name", run.getDisplayName()); return new SimpleDateFormat(pattern).format(startTime); } diff --git a/src/main/resources/hudson/plugins/robot/RobotConfig/config.jelly b/src/main/resources/hudson/plugins/robot/RobotConfig/config.jelly index cf8f1b8c..9ad96c2f 100644 --- a/src/main/resources/hudson/plugins/robot/RobotConfig/config.jelly +++ b/src/main/resources/hudson/plugins/robot/RobotConfig/config.jelly @@ -24,7 +24,7 @@ limitations under the License. Amount of runs to show in trend preview of "Robot Results" column - Pattern to format x axis label in trend graphs. You can use $build for build number beside all letters from java class DateTimeFormatter (e.g. MM-dd HH:mm). Already created and cached images are not affected. + Pattern to format x axis label in trend graphs. You can use $build for build number and $display_name for build display name beside all letters from java class DateTimeFormatter (e.g. MM-dd HH:mm). Already created and cached images are not affected. \ No newline at end of file diff --git a/src/main/resources/hudson/plugins/robot/RobotPublisher/config.properties b/src/main/resources/hudson/plugins/robot/RobotPublisher/config.properties index 099e0b41..cacfda48 100644 --- a/src/main/resources/hudson/plugins/robot/RobotPublisher/config.properties +++ b/src/main/resources/hudson/plugins/robot/RobotPublisher/config.properties @@ -17,7 +17,7 @@ advanced.otherfiles.description=Comma separated list of robot related artifacts advanced.enableCache=Enable cache advanced.enableCache.description=Enable cache for test results advanced.overwriteXAxisLabel=X-axis label -advanced.overwriteXAxisLabel.description=Overwrite default x-axis label for publish trend +advanced.overwriteXAxisLabel.description=Overwrite default x-axis label for publish trend. You can use $display_name to change the label for the build display name. thresholds.label=Thresholds for build result diff --git a/src/main/resources/hudson/plugins/robot/RobotPublisher/help-overwriteXAxisLabel.html b/src/main/resources/hudson/plugins/robot/RobotPublisher/help-overwriteXAxisLabel.html index 69a8b189..9b516a5a 100644 --- a/src/main/resources/hudson/plugins/robot/RobotPublisher/help-overwriteXAxisLabel.html +++ b/src/main/resources/hudson/plugins/robot/RobotPublisher/help-overwriteXAxisLabel.html @@ -14,5 +14,5 @@ limitations under the License. -->
-

Pattern to format x axis label in trend graphs. You can use $build for build number beside all letters from java class DateTimeFormatter (e.g. MM-dd HH:mm). Already created and cached images are not affected

+

Pattern to format x axis label in trend graphs. You can use $build for build number and $display_name for build display name beside all letters from java class DateTimeFormatter (e.g. MM-dd HH:mm). Already created and cached images are not affected.

diff --git a/src/main/resources/hudson/plugins/robot/RobotStep/help-overwriteXAxisLabel.html b/src/main/resources/hudson/plugins/robot/RobotStep/help-overwriteXAxisLabel.html index 69a8b189..9b516a5a 100644 --- a/src/main/resources/hudson/plugins/robot/RobotStep/help-overwriteXAxisLabel.html +++ b/src/main/resources/hudson/plugins/robot/RobotStep/help-overwriteXAxisLabel.html @@ -14,5 +14,5 @@ limitations under the License. -->
-

Pattern to format x axis label in trend graphs. You can use $build for build number beside all letters from java class DateTimeFormatter (e.g. MM-dd HH:mm). Already created and cached images are not affected

+

Pattern to format x axis label in trend graphs. You can use $build for build number and $display_name for build display name beside all letters from java class DateTimeFormatter (e.g. MM-dd HH:mm). Already created and cached images are not affected.

diff --git a/src/test/java/hudson/plugins/robot/graph/RobotGraphHelperTest.java b/src/test/java/hudson/plugins/robot/graph/RobotGraphHelperTest.java index 6096f52f..d02950e9 100644 --- a/src/test/java/hudson/plugins/robot/graph/RobotGraphHelperTest.java +++ b/src/test/java/hudson/plugins/robot/graph/RobotGraphHelperTest.java @@ -1,107 +1,116 @@ -/* -* Copyright 2008-2014 Nokia Solutions and Networks Oy -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ -package hudson.plugins.robot.graph; - -import hudson.model.FreeStyleBuild; -import hudson.plugins.robot.RobotParser; -import hudson.plugins.robot.model.RobotResult; -import junit.framework.TestCase; - -import java.io.File; -import java.util.Calendar; -import java.util.GregorianCalendar; - -import static org.mockito.Mockito.*; - -public class RobotGraphHelperTest extends TestCase { - - private static final String xLabelFormat = "#$build"; - - private RobotResult mockResult1; - private RobotResult mockResult2; - - protected void setUp() throws Exception { - super.setUp(); - - RobotParser.RobotParserCallable remoteOperation = new RobotParser.RobotParserCallable("output.xml", null, null); - RobotResult result = remoteOperation.invoke(new File(new RobotGraphHelperTest().getClass().getResource("output.xml").toURI()).getParentFile(), null); - result.tally(null); - - // Mocked builds to play as owners of test results - FreeStyleBuild mockBuild1 = mock(FreeStyleBuild.class); - FreeStyleBuild mockBuild2 = mock(FreeStyleBuild.class); - when(mockBuild2.compareTo(mockBuild1)).thenReturn(1); - when(mockBuild1.compareTo(mockBuild2)).thenReturn(-1); - - // This is to pass hudson.util.Graph constructor - GregorianCalendar c = new GregorianCalendar(); - c.setTimeInMillis(0L); - when(mockBuild1.getTimestamp()).thenReturn(c); - when(mockBuild2.getTimestamp()).thenReturn(c); - - // set up some results chains - mockResult1 = spy(result); - doReturn(null).when(mockResult1).getPreviousResult(); - doReturn(mockBuild1).when(mockResult1).getOwner(); - - mockResult2 = spy(result); - doReturn(mockResult1).when(mockResult2).getPreviousResult(); - doReturn(mockBuild2).when(mockResult2).getOwner(); - } - - public void testShouldLimitResultsGraphDataSet() throws Exception { - RobotGraph limitedResultsGraph = RobotGraphHelper.createTestResultsGraphForTestObject( - mockResult2, false, false, false, false, false, xLabelFormat,1); - - assertEquals(1, limitedResultsGraph.getDataset().getColumnCount()); - } - - public void testShouldReturnAllResultsGraphDataIfNotLimited() throws Exception { - RobotGraph notlimitedResultsGraph = RobotGraphHelper.createTestResultsGraphForTestObject( - mockResult2, false, false, false, false, false, xLabelFormat,0); - - assertEquals(2, notlimitedResultsGraph.getDataset().getColumnCount()); - } - - public void testShouldReturnAllResultsGraphDataIfLimitIsBiggerThanDataAmount() throws Exception { - RobotGraph notlimitedResultsGraph = RobotGraphHelper.createTestResultsGraphForTestObject( - mockResult2, false, false, false, false, false, xLabelFormat,10); - - assertEquals(2, notlimitedResultsGraph.getDataset().getColumnCount()); - } - - public void testShouldLimitDurationGraphDataSet() throws Exception { - RobotGraph limitedResultsGraph = RobotGraphHelper.createDurationGraphForTestObject( - mockResult2, false, 1,xLabelFormat,false); - - assertEquals(1, limitedResultsGraph.getDataset().getColumnCount()); - } - - public void testShouldReturnAllDurationGraphDataIfNotLimited() throws Exception { - RobotGraph notlimitedResultsGraph = RobotGraphHelper.createDurationGraphForTestObject( - mockResult2, false, 0, xLabelFormat,false); - - assertEquals(2, notlimitedResultsGraph.getDataset().getColumnCount()); - } - - public void testShouldReturnAllDurationDataIfLimitIsBiggerThanDataAmount() throws Exception { - RobotGraph notlimitedResultsGraph = RobotGraphHelper.createDurationGraphForTestObject( - mockResult2, false, 10, xLabelFormat,false); - - assertEquals(2, notlimitedResultsGraph.getDataset().getColumnCount()); - } - -} +/* +* Copyright 2008-2014 Nokia Solutions and Networks Oy +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package hudson.plugins.robot.graph; + +import hudson.model.FreeStyleBuild; +import hudson.plugins.robot.RobotParser; +import hudson.plugins.robot.model.RobotResult; +import junit.framework.TestCase; + +import java.io.File; +import java.util.GregorianCalendar; + +import static org.mockito.Mockito.*; + +public class RobotGraphHelperTest extends TestCase { + + private static final String xLabelFormat = "#$build"; + + private RobotResult mockResult1; + private RobotResult mockResult2; + + protected void setUp() throws Exception { + super.setUp(); + + RobotParser.RobotParserCallable remoteOperation = new RobotParser.RobotParserCallable("output.xml", null, null); + RobotResult result = remoteOperation.invoke(new File(new RobotGraphHelperTest().getClass().getResource("output.xml").toURI()).getParentFile(), null); + result.tally(null); + + // Mocked builds to play as owners of test results + FreeStyleBuild mockBuild1 = mock(FreeStyleBuild.class); + FreeStyleBuild mockBuild2 = mock(FreeStyleBuild.class); + when(mockBuild2.compareTo(mockBuild1)).thenReturn(1); + when(mockBuild1.compareTo(mockBuild2)).thenReturn(-1); + + // This is to pass hudson.util.Graph constructor + GregorianCalendar c = new GregorianCalendar(); + c.setTimeInMillis(0L); + when(mockBuild1.getTimestamp()).thenReturn(c); + when(mockBuild2.getTimestamp()).thenReturn(c); + when(mockBuild1.getDisplayName()).thenReturn("1.2.3"); + when(mockBuild2.getDisplayName()).thenReturn("3.2.1"); + + // set up some results chains + mockResult1 = spy(result); + doReturn(null).when(mockResult1).getPreviousResult(); + doReturn(mockBuild1).when(mockResult1).getOwner(); + + mockResult2 = spy(result); + doReturn(mockResult1).when(mockResult2).getPreviousResult(); + doReturn(mockBuild2).when(mockResult2).getOwner(); + } + + public void testShouldLimitResultsGraphDataSet() throws Exception { + RobotGraph limitedResultsGraph = RobotGraphHelper.createTestResultsGraphForTestObject( + mockResult2, false, false, false, false, false, xLabelFormat,1); + + assertEquals(1, limitedResultsGraph.getDataset().getColumnCount()); + } + + public void testShouldReturnAllResultsGraphDataIfNotLimited() throws Exception { + RobotGraph notlimitedResultsGraph = RobotGraphHelper.createTestResultsGraphForTestObject( + mockResult2, false, false, false, false, false, xLabelFormat,0); + + assertEquals(2, notlimitedResultsGraph.getDataset().getColumnCount()); + } + + public void testShouldReturnAllResultsGraphDataIfLimitIsBiggerThanDataAmount() throws Exception { + RobotGraph notlimitedResultsGraph = RobotGraphHelper.createTestResultsGraphForTestObject( + mockResult2, false, false, false, false, false, xLabelFormat,10); + + assertEquals(2, notlimitedResultsGraph.getDataset().getColumnCount()); + } + + public void testShouldShowCustomLabel() throws Exception { + RobotGraph customLabelGraph = RobotGraphHelper.createTestResultsGraphForTestObject( + mockResult2, false, false, false, false, false, "$display_name",0); + + assertEquals("3.2.1", customLabelGraph.getDataset().getColumnKey(0).toString()); + assertEquals("1.2.3", customLabelGraph.getDataset().getColumnKey(1).toString()); + } + + public void testShouldLimitDurationGraphDataSet() throws Exception { + RobotGraph limitedResultsGraph = RobotGraphHelper.createDurationGraphForTestObject( + mockResult2, false, 1,xLabelFormat,false); + + assertEquals(1, limitedResultsGraph.getDataset().getColumnCount()); + } + + public void testShouldReturnAllDurationGraphDataIfNotLimited() throws Exception { + RobotGraph notlimitedResultsGraph = RobotGraphHelper.createDurationGraphForTestObject( + mockResult2, false, 0, xLabelFormat,false); + + assertEquals(2, notlimitedResultsGraph.getDataset().getColumnCount()); + } + + public void testShouldReturnAllDurationDataIfLimitIsBiggerThanDataAmount() throws Exception { + RobotGraph notlimitedResultsGraph = RobotGraphHelper.createDurationGraphForTestObject( + mockResult2, false, 10, xLabelFormat,false); + + assertEquals(2, notlimitedResultsGraph.getDataset().getColumnCount()); + } + +}