Skip to content

Commit

Permalink
Merge pull request #14 from tomaszbech/master
Browse files Browse the repository at this point in the history
    JENKINS-28064   'Replace nr. of failed test cases' option doesn't work
  • Loading branch information
tomaszbech committed Jul 9, 2015
2 parents acaec33 + 230e24d commit ed463a7
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 33 deletions.
21 changes: 20 additions & 1 deletion pom.xml
Expand Up @@ -12,7 +12,7 @@
<artifactId>xfpanel</artifactId>
<packaging>hpi</packaging>
<name>eXtreme Feedback Panel</name>
<version>1.2.4-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
<description>Provides an eXtreme Feedback Panel for Hudson. Thanks to Mark Howard and his work on the Radiator View
Plugin from which this was based.
</description>
Expand Down Expand Up @@ -65,6 +65,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<powermock.version>1.6.2</powermock.version>
</properties>

<pluginRepositories>
Expand Down Expand Up @@ -122,5 +123,23 @@
<artifactId>jna</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>3.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-easymock</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
74 changes: 42 additions & 32 deletions src/main/java/maps/hudson/plugin/xfpanel/XFPanelEntry.java
@@ -1,18 +1,18 @@
package maps.hudson.plugin.xfpanel;

import hudson.Functions;
import hudson.Plugin;
import hudson.model.Result;
import hudson.model.AbstractBuild;
import hudson.model.Action;
import hudson.model.Hudson;
import hudson.model.Job;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.User;
import hudson.plugins.claim.ClaimBuildAction;
import hudson.plugins.claim.ClaimTestAction;
import hudson.scm.ChangeLogSet.Entry;
import hudson.tasks.junit.CaseResult;
import hudson.tasks.junit.TestResultAction;
import hudson.tasks.junit.CaseResult;
import hudson.tasks.test.AbstractTestResultAction;

import java.text.NumberFormat;
Expand All @@ -23,7 +23,8 @@
import java.util.List;
import java.util.Set;

import maps.hudson.plugin.xfpanel.XFPanelView;
import jenkins.model.Jenkins;
import maps.hudson.plugin.xfpanel.XFPanelView.Blame;

/**
* Represents a job to be shown on the panel
Expand All @@ -32,7 +33,7 @@
*
* @author jrenaut
*/
public final class XFPanelEntry extends XFPanelView {
public final class XFPanelEntry {

private Job<?, ?> job;
private String backgroundColor;
Expand All @@ -42,14 +43,15 @@ public final class XFPanelEntry extends XFPanelView {
private Boolean building = false;
private String completionTimestampString = "";
private Calendar completionTimestamp;
private XFPanelView view;

/**
/**
* C'tor
* @param job the job to be represented
*/
public XFPanelEntry(XFPanelView view, Job<?, ?> job) {
super(view.getDisplayName(), view.getNumColumns());
this.job = job;
this.view = view;
this.job = job;
this.findStatus();
this.setTimes();
}
Expand All @@ -66,7 +68,7 @@ public XFPanelEntry(XFPanelView view, Job<?, ?> job) {
*/
public String getName() {
String label = job.getDisplayName().toUpperCase();
if (getShowDescription() == true && !job.getDescription().isEmpty()) {
if (getView().getShowDescription() == true && !job.getDescription().isEmpty()) {
label += ": " + job.getDescription();
}
return label;
Expand All @@ -84,7 +86,7 @@ public Boolean getQueued() {
*/
public Integer getQueueNumber() {
// placeInQueue==null right after deserialization because it's transient
return placeInQueue==null ? null : placeInQueue.get(this.job.getQueueItem());
return getView().placeInQueue==null ? null : getView().placeInQueue.get(this.job.getQueueItem());
}

public AbstractBuild<?, ?> getLastBuild() {
Expand Down Expand Up @@ -155,7 +157,7 @@ public Boolean getBroken() {
* @return 1 on success
*/
public Boolean getShowResponsibles() {
if (BlameState == Blame.NOTATALL)
if (getView().BlameState == XFPanelView.Blame.NOTATALL)
return false;
return true;
}
Expand Down Expand Up @@ -296,22 +298,22 @@ public String convertCulpritsToString(HashSet<User> input) {

int i=0;
for (; it.hasNext(); i++) {
if (i < getMaxAmmountOfResponsibles()) {
if (i < getView().getMaxAmmountOfResponsibles()) {
output += it.next().getFullName() + ((it.hasNext()) ? ", " : "");
} else {
it.next();
}
}
if (i > getMaxAmmountOfResponsibles()){
output += "... <"+ (i-getMaxAmmountOfResponsibles()) + " more>";
if (i > getView().getMaxAmmountOfResponsibles()){
output += "... <"+ (i-getView().getMaxAmmountOfResponsibles()) + " more>";
}
if (!output.isEmpty())
return output;
return " - ";
}

public String getCulprits() {
if (BlameState == Blame.ONLYFIRSTFAILEDBUILD) {
if (getView().BlameState == Blame.ONLYFIRSTFAILEDBUILD) {
Run<?, ?> run = this.job.getLastStableBuild(); //getLastSuccessfulBuild();
if ( run == null ){ // if there aren't any successful builds
run = this.job.getFirstBuild();
Expand All @@ -322,13 +324,13 @@ public String getCulprits() {
AbstractBuild<?, ?> firstFailedBuild = (AbstractBuild<?, ?>) run;
return convertCulpritsToString( getCulpritFromBuild( firstFailedBuild ) );
}
} else if (BlameState == Blame.ONLYLASTFAILEDBUILD) {
} else if (getView().BlameState == Blame.ONLYLASTFAILEDBUILD) {
Run<?, ?> run = this.job.getLastFailedBuild();
if (run instanceof AbstractBuild<?, ?>) {
AbstractBuild<?, ?> lastFailedBuild = (AbstractBuild<?, ?>) run;
return convertCulpritsToString( getCulpritFromBuild( lastFailedBuild ) );
}
} else if (BlameState == Blame.EVERYINVOLVED) {
} else if (getView().BlameState == Blame.EVERYINVOLVED) {
AbstractBuild<?, ?> build = this.getLastBuild();
HashSet<User> BlameList = new HashSet<User>( build.getCulprits() );
return convertCulpritsToString( BlameList );
Expand Down Expand Up @@ -367,7 +369,7 @@ private ClaimBuildAction getClaimAction() {
*/
public boolean isClaimed() {
boolean result = false;
if (getIsClaimPluginInstalled()) {
if (getView().getIsClaimPluginInstalled()) {
ClaimBuildAction cba = getClaimAction();
if (cba != null) {
result = cba.isClaimed();
Expand Down Expand Up @@ -406,7 +408,8 @@ public String getClaimInfo() {
}

public hudson.tasks.junit.TestResult getClaimedTestCases(){
if (Hudson.getInstance().getPlugin("claim") != null) {
Plugin plugin = Jenkins.getInstance().getPlugin("claim");
if (plugin != null) {
Run<?, ?> lastBuild = job.getLastBuild();
if (lastBuild == null) {
return null;
Expand All @@ -418,9 +421,8 @@ public hudson.tasks.junit.TestResult getClaimedTestCases(){
return null;
}
}
List<Action> claimTestActionList = lastBuild.getActions();
List<TestResultAction> results = lastBuild.getActions(TestResultAction.class);
if ( results == null || claimTestActionList == null || results.size() == 0) {
if ( results == null || results.size() == 0) {
return null;
}
return results.get(0).getResult();
Expand Down Expand Up @@ -486,10 +488,10 @@ public int getNumClaimedTests() {
*/
public String getNumberOfTests(){
final int failedTests = getFailCount();
if (failedTests == 0 && getShowZeroTestCounts() == false) {
if (failedTests == 0 && getView().getShowZeroTestCounts() == false) {
return "";
}
if (getReplaceNumberOfTestCases()) {
if (getView().getReplaceNumberOfTestCases()) {
final int claimedTests = getNumClaimedTests();
if (claimedTests >= 0) {
return Integer.toString(failedTests - claimedTests);
Expand All @@ -510,7 +512,7 @@ public String getDiffColor() {
return "#00FF00";
}
}
return "#" + getBuildFontColor();
return "#" + getView().getBuildFontColor();
}

/**
Expand Down Expand Up @@ -565,24 +567,24 @@ private void findStatus() {
case BLUE_ANIME:
this.building = true;
case BLUE:
this.backgroundColor = getColors().getOkBG();
this.color = colors.getOkFG();
this.backgroundColor = getView().getColors().getOkBG();
this.color = getView().getColors().getOkFG();
this.colorFade = "build-fade-ok.png";
this.broken = false;
break;
case YELLOW_ANIME:
this.building = true;
case YELLOW:
this.backgroundColor = getColors().getFailedBG();
this.color = colors.getFailedFG();
this.backgroundColor = getView().getColors().getFailedBG();
this.color = getView().getColors().getFailedFG();
this.colorFade = "build-fade-fail.png";
this.broken = false;
break;
case RED_ANIME:
this.building = true;
case RED:
this.backgroundColor = getColors().getBrokenBG();
this.color = colors.getBrokenFG();
this.backgroundColor = getView().getColors().getBrokenBG();
this.color = getView().getColors().getBrokenFG();
this.colorFade = "build-fade-broken.png";
this.broken = true;
break;
Expand All @@ -591,10 +593,18 @@ private void findStatus() {
case ABORTED_ANIME:
this.building = true;
default:
this.backgroundColor = getColors().getOtherBG();
this.color = colors.getOtherFG();
this.backgroundColor = getView().getColors().getOtherBG();
this.color = getView().getColors().getOtherFG();
this.colorFade = "build-fade-other.png";
this.broken = true;
}
}

/**
* @return view of this entry
*/
public XFPanelView getView() {
return view;
}

}
122 changes: 122 additions & 0 deletions src/test/java/maps/hudson/plugin/xfpanel/XFPanelEntryTest.java
@@ -0,0 +1,122 @@
package maps.hudson.plugin.xfpanel;

import static org.junit.Assert.*;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import jenkins.model.Jenkins;
import hudson.DescriptorExtensionList;
import hudson.Plugin;
import hudson.model.BallColor;
import hudson.model.Describable;
import hudson.model.Descriptor;
import hudson.model.FreeStyleBuild;
import hudson.model.AbstractBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Job;
import hudson.model.Run;
import hudson.plugins.claim.ClaimTestAction;
import hudson.tasks.junit.TestResult;
import hudson.tasks.junit.TestResultAction;
import hudson.tasks.junit.CaseResult;
import hudson.tasks.test.AbstractTestResultAction;
import hudson.views.ListViewColumn;

import maps.hudson.plugin.xfpanel.XFPanelView.XFPanelColors;

import org.easymock.ConstructorArgs;
import org.easymock.EasyMock;
import org.easymock.EasyMockSupport;
import org.easymock.IMockBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest( {TestResult.class, XFPanelEntry.class, XFPanelColors.class, CaseResult.class })
public class XFPanelEntryTest {

@Test
public void testOneClaimed() throws Exception {
XFPanelEntry xfPanelEntry = prepareData(3, true, true);
assertEquals("2", xfPanelEntry.getNumberOfTests());
}
@Test
public void testNoneClaimed() throws Exception {
XFPanelEntry xfPanelEntry = prepareData(4, false, true);
assertEquals("4", xfPanelEntry.getNumberOfTests());
//verifyAll();
}
@Test
public void testAllFailed() throws Exception {
XFPanelEntry xfPanelEntry = prepareData(4, true, false);
assertEquals("4", xfPanelEntry.getNumberOfTests());
//verifyAll();
}

private XFPanelEntry prepareData(int allFailed, boolean oneClaimed, boolean showClaimed) throws NoSuchFieldException,
IllegalAccessException {
Field field = Jenkins.class.getDeclaredField("theInstance");
field.setAccessible(true);
Jenkins jenkins = PowerMock.createNiceMock(Jenkins.class);
field.set(null, jenkins);
//EasyMock.expect(jenkins.getInstance()).andReturn(jenkins);
//<T extends Describable<T>, D extends Descriptor<T>>
DescriptorExtensionList del = PowerMock.createNiceMock(DescriptorExtensionList.class);
List<String> al = new ArrayList<String>();
EasyMock.expect(del.iterator()).andReturn(al.iterator());
EasyMock.expect(jenkins.getDescriptorList(ListViewColumn.class)).andReturn(del);
FreeStyleProject project = PowerMock.createNiceMock(FreeStyleProject.class);
Job job = project;
XFPanelView view = PowerMock.createNiceMock(XFPanelView.class);
EasyMock.expect(view.getDisplayName()).andReturn("testview");
EasyMock.expect(job.getIconColor()).andReturn(BallColor.BLUE);
XFPanelColors xfPanelColors = PowerMock.createNiceMock(XFPanelColors.class);
EasyMock.expect(view.getColors()).andReturn(xfPanelColors).anyTimes();
PowerMock.replayAll();

// XFPanelEntry xfPanelEntry = PowerMock.createMock(XFPanelEntry.class,
// view, job);
XFPanelEntry xfPanelEntry = new XFPanelEntry(view, job);
PowerMock.resetAll();

EasyMock.expect(view.getIsClaimPluginInstalled()).andReturn(true).anyTimes();
EasyMock.expect(view.getReplaceNumberOfTestCases()).andReturn(showClaimed).anyTimes();

AbstractBuild lastBuild = PowerMock.createNiceMock(AbstractBuild.class);
AbstractTestResultAction atra = PowerMock.createNiceMock(AbstractTestResultAction.class);
EasyMock.expect(atra.getFailCount()).andReturn(allFailed);
EasyMock.expect(lastBuild.getAction(AbstractTestResultAction.class)).andReturn(atra);

TestResult testResult = PowerMock.createNiceMock(TestResult.class);

List<CaseResult> crl = new ArrayList<CaseResult>();
CaseResult cr = PowerMock.createNiceMock(CaseResult.class);
ClaimTestAction cta = PowerMock.createNiceMock(ClaimTestAction.class);
EasyMock.expect(cta.isClaimed()).andReturn(oneClaimed);
EasyMock.expect(cr.getTestAction(ClaimTestAction.class)).andReturn(cta);
crl.add(cr);

EasyMock.expect(testResult.getTotalCount()).andReturn(-1); //whatever
EasyMock.expect(testResult.getFailedTests()).andReturn(crl);

TestResultAction tra = PowerMock.createNiceMock(TestResultAction.class);
EasyMock.expect(tra.getResult()).andReturn(testResult);
List<TestResultAction> tral = new ArrayList<TestResultAction>();
tral.add(tra);

EasyMock.expect(lastBuild.getActions(TestResultAction.class)).andReturn(tral);
EasyMock.expect(job.getLastSuccessfulBuild()).andReturn(lastBuild).anyTimes();
EasyMock.expect(job.getLastBuild()).andReturn(lastBuild).anyTimes();

EasyMock.expect(jenkins.getPlugin("claim")).andReturn(new Plugin.DummyImpl()).anyTimes();

PowerMock.replayAll();
assertNotNull(Jenkins.getInstance().getPlugin("claim"));
return xfPanelEntry;
}
}

0 comments on commit ed463a7

Please sign in to comment.