|
1 | 1 | package org.scoverage |
2 | 2 |
|
| 3 | +import org.gradle.api.GradleException |
3 | 4 | import org.gradle.api.Project |
4 | | -import org.gradle.api.tasks.TaskExecutionException |
5 | 5 | import org.gradle.testfixtures.ProjectBuilder |
6 | 6 | import org.hamcrest.Description |
7 | 7 | import org.hamcrest.TypeSafeMatcher |
8 | 8 | import org.junit.Rule |
9 | 9 | import org.junit.Test |
10 | 10 | import org.junit.rules.ExpectedException |
11 | 11 |
|
| 12 | +/** |
| 13 | + * Copied from the Internet, just to check if we have correct exception thrown. |
| 14 | + */ |
| 15 | +class CauseMatcher extends TypeSafeMatcher<Throwable> { |
| 16 | + |
| 17 | + private final Class<? extends Throwable> type; |
| 18 | + private final String expectedMessage; |
| 19 | + |
| 20 | + public CauseMatcher(Class<? extends Throwable> type, String expectedMessage) { |
| 21 | + this.type = type; |
| 22 | + this.expectedMessage = expectedMessage; |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + protected boolean matchesSafely(Throwable item) { |
| 27 | + return item.getClass().isAssignableFrom(type) && item.getMessage().contains(expectedMessage); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public void describeTo(Description description) { |
| 32 | + description.appendText("expects type ") |
| 33 | + .appendValue(type) |
| 34 | + .appendText(" and a message ") |
| 35 | + .appendValue(expectedMessage); |
| 36 | + } |
| 37 | +} |
| 38 | + |
12 | 39 | class OverallCheckTaskTest { |
13 | 40 |
|
14 | 41 | @Rule |
15 | 42 | public ExpectedException expectedException = ExpectedException.none() |
16 | 43 |
|
17 | | - private Project projectForLineRate(Number lineRate) { |
| 44 | + private Project projectForRate(Number coverageRate, CoverageType type) { |
18 | 45 | Project project = ProjectBuilder.builder().build() |
19 | 46 | project.plugins.apply(ScoveragePlugin) |
20 | 47 | project.tasks.create('bob', OverallCheckTask) { |
21 | | - minimumLineRate = lineRate |
22 | | - cobertura = new File('src/test/resources/cobertura.xml') |
| 48 | + minimumRate = coverageRate |
| 49 | + reportDir = new File('src/test/resources') |
| 50 | + coverageType = type |
23 | 51 | } |
24 | 52 | project |
25 | 53 | } |
26 | 54 |
|
| 55 | + // error when report file is not there |
| 56 | + |
27 | 57 | @Test |
28 | | - void failsWhenLineRateIsBelowTarget(){ |
29 | | - Project project = projectForLineRate(1) |
30 | | - expectedException.expect(TaskExecutionException) |
| 58 | + void failsWhenReportFileIsNotFound() { |
| 59 | + Project project = ProjectBuilder.builder().build() |
| 60 | + project.plugins.apply(ScoveragePlugin) |
| 61 | + project.tasks.create('bob', OverallCheckTask) { |
| 62 | + minimumRate = 1.0 |
| 63 | + reportDir = new File('src/test/nothingthere') |
| 64 | + coverageType = CoverageType.Line |
| 65 | + } |
| 66 | + expectedException.expectCause(new CauseMatcher( |
| 67 | + GradleException.class, |
| 68 | + OverallCheckTask.fileNotFoundErrorMsg(CoverageType.Line) |
| 69 | + )) |
| 70 | + project.tasks.bob.execute() |
| 71 | + } |
| 72 | + |
| 73 | + // line coverage |
| 74 | + |
| 75 | + @Test |
| 76 | + void failsWhenLineRateIsBelowTarget() { |
| 77 | + Project project = projectForRate(1, CoverageType.Line) |
| 78 | + expectedException.expectCause(new CauseMatcher( |
| 79 | + GradleException.class, |
| 80 | + OverallCheckTask.errorMsg("66", "100", CoverageType.Line) |
| 81 | + )) |
31 | 82 | project.tasks.bob.execute() |
32 | 83 | } |
33 | 84 |
|
34 | 85 | @Test |
35 | 86 | void doesNotFailWhenLineRateIsAtTarget() throws Exception { |
36 | | - Project project = projectForLineRate(0.66) |
| 87 | + Project project = projectForRate(0.66, CoverageType.Line) |
37 | 88 | project.tasks.bob.execute() |
38 | 89 | } |
39 | 90 |
|
40 | 91 | @Test |
41 | 92 | void doesNotFailWhenLineRateIsAboveTarget() throws Exception { |
42 | | - Project project = projectForLineRate(0.6) |
| 93 | + Project project = projectForRate(0.6, CoverageType.Line) |
| 94 | + project.tasks.bob.execute() |
| 95 | + } |
| 96 | + |
| 97 | + // Statement coverage |
| 98 | + |
| 99 | + @Test |
| 100 | + void failsWhenStatementRateIsBelowTarget() { |
| 101 | + Project project = projectForRate(1, CoverageType.Statement) |
| 102 | + expectedException.expectCause(new CauseMatcher( |
| 103 | + GradleException.class, |
| 104 | + OverallCheckTask.errorMsg("33.33", "100", CoverageType.Statement) |
| 105 | + )) |
| 106 | + project.tasks.bob.execute() |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + void doesNotFailWhenStatementRateIsAtTarget() throws Exception { |
| 111 | + Project project = projectForRate(0.33, CoverageType.Statement) |
| 112 | + project.tasks.bob.execute() |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void doesNotFailWhenStatementRateIsAboveTarget() throws Exception { |
| 117 | + Project project = projectForRate(0.3, CoverageType.Statement) |
| 118 | + project.tasks.bob.execute() |
| 119 | + } |
| 120 | + |
| 121 | + // Branch coverage |
| 122 | + |
| 123 | + @Test |
| 124 | + void failsWhenBranchRateIsBelowTarget() { |
| 125 | + Project project = projectForRate(1, CoverageType.Branch) |
| 126 | + expectedException.expectCause(new CauseMatcher( |
| 127 | + GradleException.class, |
| 128 | + OverallCheckTask.errorMsg("50", "100", CoverageType.Branch) |
| 129 | + )) |
| 130 | + project.tasks.bob.execute() |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + void doesNotFailWhenBranchRateIsAtTarget() throws Exception { |
| 135 | + Project project = projectForRate(0.50, CoverageType.Branch) |
| 136 | + project.tasks.bob.execute() |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + void doesNotFailWhenBranchRateIsAboveTarget() throws Exception { |
| 141 | + Project project = projectForRate(0.45, CoverageType.Branch) |
43 | 142 | project.tasks.bob.execute() |
44 | 143 | } |
45 | 144 |
|
|
0 commit comments