Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
JENKINS-16580: extract graph creation, add test helpers and create in…
Browse files Browse the repository at this point in the history
…itial test
  • Loading branch information
mheinzerling committed May 30, 2016
1 parent 29bcd67 commit 79110dc
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ work
.project
build

# Generated images from CoverageObjectGraphTest
/*.png

# other
*~
Binary file added resources/test/simple.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 13 additions & 4 deletions src/main/java/hudson/plugins/jacoco/model/CoverageObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
* Base class of all coverage objects.
*
* @author Kohsuke Kawaguchi
* @author Martin Heinzerling
*/
@ExportedBean
public abstract class CoverageObject<SELF extends CoverageObject<SELF>> {
Expand Down Expand Up @@ -370,8 +371,12 @@ public void doGraph(StaplerRequest req, StaplerResponse rsp) throws IOException
int width = (w != null) ? Integer.valueOf(w) : 500;
int height = (h != null) ? Integer.valueOf(h) : 200;

new GraphImpl(this, t, width, height) {
createGraph(t, width, height).doPng(req, rsp);
}

GraphImpl createGraph(final Calendar t, final int width, final int height) throws IOException
{
return new GraphImpl(this, t, width, height) {
@Override
protected DataSetBuilder<String, NumberOnlyBuildLabel> createDataSet(CoverageObject<SELF> obj) {
DataSetBuilder<String, NumberOnlyBuildLabel> dsb = new DataSetBuilder<String, NumberOnlyBuildLabel>();
Expand All @@ -386,7 +391,6 @@ protected DataSetBuilder<String, NumberOnlyBuildLabel> createDataSet(CoverageObj
if (a.line != null) {
dsb.add(a.line.getCovered(), Messages.CoverageObject_Legend_LineCovered(), label);
dsb.add(a.line.getMissed(), Messages.CoverageObject_Legend_LineMissed(), label);

} else {
dsb.add(0, Messages.CoverageObject_Legend_LineCovered(), label);
dsb.add(0, Messages.CoverageObject_Legend_LineMissed(), label);
Expand All @@ -395,14 +399,14 @@ protected DataSetBuilder<String, NumberOnlyBuildLabel> createDataSet(CoverageObj

return dsb;
}
}.doPng(req, rsp);
};
}

public Api getApi() {
return new Api(this);
}

private abstract class GraphImpl extends Graph {
abstract class GraphImpl extends Graph {

private CoverageObject<SELF> obj;

Expand All @@ -413,6 +417,11 @@ public GraphImpl(CoverageObject<SELF> obj, Calendar timestamp, int defaultW, int

protected abstract DataSetBuilder<String, NumberOnlyBuildLabel> createDataSet(CoverageObject<SELF> obj);

public JFreeChart getGraph( )
{
return createGraph();
}

@Override
protected JFreeChart createGraph() {
final CategoryDataset dataset = createDataSet(obj).build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package hudson.plugins.jacoco.model;


import static org.junit.Assert.assertArrayEquals;

import hudson.plugins.jacoco.AbstractJacocoTestBase;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.GregorianCalendar;
import org.apache.commons.io.FileUtils;
import org.easymock.EasyMock;
import org.easymock.IMocksControl;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
* @author Martin Heinzerling
*/
public class CoverageObjectGraphTest extends AbstractJacocoTestBase
{
public static final int WIDTH = 500;
public static final int HEIGHT = 200;
private IMocksControl ctl;

@Before
public void prepareMock()
{
ctl = EasyMock.createControl();
TestCoverageObject.setEasyMock(ctl);
}

@After
public void verifyMock()
{
ctl.verify();
TestCoverageObject.setEasyMock(null);
}

@Test
public void simpleLineCoverage() throws IOException
{
TestCoverageObject t5 = new TestCoverageObject().line(5000, 19000);
TestCoverageObject t4 = new TestCoverageObject().line(5000, 19000).previous(t5);
TestCoverageObject t3 = new TestCoverageObject().line(5000, 19000).previous(t4);
TestCoverageObject t2 = new TestCoverageObject().line(10000, 15000).previous(t3);
TestCoverageObject t1 = new TestCoverageObject().line(12000, 18000).previous(t2);
TestCoverageObject t0 = new TestCoverageObject().previous(t1);
ctl.replay();

JFreeChart chart = t0.createGraph(new GregorianCalendar(), WIDTH, HEIGHT).getGraph();
assertGraph(chart, "simple.png");

}

private void assertGraph(JFreeChart chart, String file) throws IOException
{
byte[] expected = FileUtils.readFileToByteArray(new File("resources/test/" + file));
byte[] actual;

ByteArrayOutputStream out = new ByteArrayOutputStream();
try
{
ChartUtilities.writeChartAsPNG(out, chart, WIDTH, HEIGHT, null);
actual = out.toByteArray();
}
finally
{
out.close();
}
try
{
assertArrayEquals(expected, actual);
}
catch (AssertionError e)
{
File f = new File(file);
ChartUtilities.saveChartAsPNG(f, chart, WIDTH, HEIGHT);
System.err.println("Stored wrong graph file to " + f.getAbsolutePath());
throw e;
}

}
}
101 changes: 101 additions & 0 deletions src/test/java/hudson/plugins/jacoco/model/TestCoverageObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package hudson.plugins.jacoco.model;

import hudson.model.AbstractBuild;
import hudson.model.FreeStyleBuild;
import org.easymock.EasyMock;
import org.easymock.IAnswer;
import org.easymock.IMocksControl;

/**
* @author Martin Heinzerling
*/
public class TestCoverageObject extends CoverageObject<TestCoverageObject>
{
private TestCoverageObject previous = null;
private AbstractBuild<?, ?> build = null;
private int buildNumber = 1;
private static IMocksControl mocksControl;

public static void setEasyMock(IMocksControl mocksControl)
{
TestCoverageObject.mocksControl = mocksControl;
}

public TestCoverageObject()
{
super();
build = mocksControl.createMock("build", FreeStyleBuild.class);
EasyMock.expect(build.getDisplayName()).andAnswer(new IAnswer<String>()
{
public String answer() throws Throwable
{
return "#" + buildNumber;
}
}).anyTimes();
}

@Override
public AbstractBuild<?, ?> getBuild()
{
return build;
}

@Override
public TestCoverageObject getPreviousResult()
{
return previous;
}

public TestCoverageObject clazz(int missed, int covered)
{
clazz = new Coverage(missed, covered);
return this;
}

public TestCoverageObject method(int missed, int covered)
{
method = new Coverage(missed, covered);
return this;
}

public TestCoverageObject line(int missed, int covered)
{
line = new Coverage(missed, covered);
return this;
}

public TestCoverageObject complexity(int missed, int covered)
{
complexity = new Coverage(missed, covered);
return this;
}

public TestCoverageObject instruction(int missed, int covered)
{
instruction = new Coverage(missed, covered);
return this;
}


public TestCoverageObject branch(int missed, int covered)
{
branch = new Coverage(missed, covered);
return this;
}

public TestCoverageObject previous(TestCoverageObject previous)
{
this.previous = previous;
int i = 1;
TestCoverageObject t = this;
while (t != null)
{
t.buildNumber = i;
i++;
t = t.previous;
}
return this;
}


}

0 comments on commit 79110dc

Please sign in to comment.