Skip to content

Commit

Permalink
JENKINS-63900 - Add possibility to use build display name as graph la…
Browse files Browse the repository at this point in the history
…bel (#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
  • Loading branch information
asimell committed Feb 22, 2024
1 parent 3f0d2cd commit 8c5df6a
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 113 deletions.
4 changes: 2 additions & 2 deletions src/main/java/hudson/plugins/robot/RobotParser.java
Expand Up @@ -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";
Expand Down
Expand Up @@ -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);
}

Expand Down
Expand Up @@ -24,7 +24,7 @@ limitations under the License.
<f:number name="buildsToShowInResultsColumn"/>Amount of runs to show in trend preview of "Robot Results" column
</f:entry>
<f:entry field="xAxisLabelFormat">
<f:textbox name="xAxisLabelFormat"/>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.
<f:textbox name="xAxisLabelFormat"/>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.
</f:entry>
</f:section>
</j:jelly>
Expand Up @@ -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
Expand Down
Expand Up @@ -14,5 +14,5 @@
limitations under the License.
-->
<div>
<p>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</p>
<p>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.</p>
</div>
Expand Up @@ -14,5 +14,5 @@
limitations under the License.
-->
<div>
<p>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</p>
<p>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.</p>
</div>
223 changes: 116 additions & 107 deletions 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());
}

}

0 comments on commit 8c5df6a

Please sign in to comment.