This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JENKINS-16580: extract graph creation, add test helpers and create in…
…itial test
- Loading branch information
1 parent
29bcd67
commit 79110dc
Showing
5 changed files
with
204 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,5 +13,8 @@ work | |
.project | ||
build | ||
|
||
# Generated images from CoverageObjectGraphTest | ||
/*.png | ||
|
||
# other | ||
*~ |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/test/java/hudson/plugins/jacoco/model/CoverageObjectGraphTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
101
src/test/java/hudson/plugins/jacoco/model/TestCoverageObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} |