Skip to content

Commit 679dbd9

Browse files
authored
feat: Support the code coverage of unit test (#24)
feat: Support the coverage of unit test 1. Add .codecov.yml to ignore unnecessary code 2. Upload the code coverage see issue: #23
1 parent 8f92b33 commit 679dbd9

File tree

26 files changed

+477
-210
lines changed

26 files changed

+477
-210
lines changed

.codecov.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
coverage:
2+
status:
3+
# pull-requests only
4+
patch:
5+
default:
6+
threshold: 0.1%
7+
ignore:
8+
- "example/.*"

.github/workflows/build_and_test.yml

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,38 @@ jobs:
6060
name: "checkstyle-file"
6161
path: ${{ github.workspace }}/checkstyle.zip
6262

63+
coverage:
64+
name: "Code Coverage"
65+
needs: [ build-source ]
66+
runs-on: ubuntu-latest
67+
steps:
68+
- name: "Checkout the source code"
69+
uses: actions/checkout@v2
70+
with:
71+
path: capa
72+
- name: "Set up OpenJDK 8"
73+
uses: actions/setup-java@v1
74+
with:
75+
distribution: 'adopt'
76+
java-version: 8
77+
- uses: actions/cache@v2
78+
name: "Cache local Maven repository"
79+
with:
80+
path: ~/.m2/repository
81+
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
82+
restore-keys: |
83+
${{ runner.os }}-maven-
84+
- name: "Calculate code coverage"
85+
run: |
86+
cd ${{ github.workspace }}/capa
87+
mvn --batch-mode --no-snapshot-updates -e --no-transfer-progress --fail-fast clean test verify -Pjacoco -Dmaven.wagon.httpconnectionManager.ttlSeconds=120 -Dmaven.wagon.http.retryHandler.count=5 -DskipTests=false -DskipIntegrationTests=false -Dcheckstyle.skip=true -Drat.skip=true -Dmaven.javadoc.skip=true -Dgpg.skip=true
88+
- name: "Upload to Codecov"
89+
uses: codecov/codecov-action@v1
90+
with:
91+
token: ${{ secrets.CODECOV_TOKEN }}
92+
file: ./**/target/site/jacoco/jacoco.xml
93+
name: codecov
94+
6395
unit-test:
6496
needs: [ build-source ]
6597
name: "Unit Test On ${{ matrix.os }} (OpenJDK: ${{ matrix.jdk }})"
@@ -91,6 +123,4 @@ jobs:
91123
timeout-minutes: 10
92124
run: |
93125
cd ${{ github.workspace }}/capa
94-
mvn --batch-mode --no-snapshot-updates -e --no-transfer-progress --fail-fast clean test verify -Pjacoco -Dmaven.wagon.httpconnectionManager.ttlSeconds=120 -Dmaven.wagon.http.retryHandler.count=5 -DskipTests=false -Dcheckstyle.skip=false -Drat.skip=false -Dmaven.javadoc.skip=true -Dgpg.skip=true
95-
- name: "Upload Unit Test Coverage"
96-
uses: codecov/codecov-action@v1
126+
mvn --batch-mode --no-snapshot-updates -e --no-transfer-progress --fail-fast clean test verify -Pjacoco,rat,checkstyle -Dmaven.wagon.httpconnectionManager.ttlSeconds=120 -Dmaven.wagon.http.retryHandler.count=5 -DskipTests=false -Dcheckstyle.skip=false -Drat.skip=false -Dmaven.javadoc.skip=true -Dgpg.skip=true

.travis.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: java
2+
3+
# Cobertura is not supported in JDK11 so you must downgrade the JDK that Travis uses if you want to use Cobertura with Travis.
4+
# See https://github.com/cobertura/cobertura/issues/381
5+
jdk:
6+
- openjdk8
7+
8+
script: "mvn cobertura:cobertura"
9+
10+
after_success:
11+
- bash <(curl -s https://codecov.io/bash)

examples/pom.xml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,41 @@
7676
</dependency>
7777
</dependencies>
7878

79+
<build>
80+
<plugins>
81+
<!-- With JUnit 5 explicitely needed to let our tests run with Maven & as there seems to be a transitively
82+
added older surefire-plugin, that doesnt support JUnit 5 as described in https://stackoverflow.com/a/49019437/4964553 -->
83+
<plugin>
84+
<groupId>org.apache.maven.plugins</groupId>
85+
<artifactId>maven-surefire-plugin</artifactId>
86+
<version>${maven.surefire.version}</version>
87+
</plugin>
88+
<plugin>
89+
<groupId>org.jacoco</groupId>
90+
<artifactId>jacoco-maven-plugin</artifactId>
91+
<version>${maven.jacoco.version}</version>
92+
<executions>
93+
<!-- Prepares the property pointing to the JaCoCo
94+
runtime agent which is passed as VM argument when Maven the Surefire plugin
95+
is executed. -->
96+
<execution>
97+
<id>pre-unit-test</id>
98+
<goals>
99+
<goal>prepare-agent</goal>
100+
</goals>
101+
</execution>
102+
<!-- Ensures that the code coverage report for
103+
unit tests is created after unit tests have been run. -->
104+
<execution>
105+
<id>post-unit-test</id>
106+
<phase>test</phase>
107+
<goals>
108+
<goal>report</goal>
109+
</goals>
110+
</execution>
111+
</executions>
112+
</plugin>
113+
</plugins>
114+
</build>
115+
79116
</project>

pom.xml

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,17 @@
7474
<maven.version>3.8.1</maven.version>
7575
<cloud-runtimes.version>1.0.1.RELEASE</cloud-runtimes.version>
7676
<reactor-core.version>3.3.11.RELEASE</reactor-core.version>
77-
<junit.version>4.13.1</junit.version>
77+
<junit.version>5.3.1</junit.version>
7878
<mockito-core.version>3.6.0</mockito-core.version>
7979
<checkstyle.version>3.1.2</checkstyle.version>
80-
<apache-rat-plugin.version>0.13</apache-rat-plugin.version>
80+
<apache.rat.version>0.13</apache.rat.version>
81+
<cobertura.maven.version>2.7</cobertura.maven.version>
82+
<maven.surefire.version>3.0.0-M1</maven.surefire.version>
8183
<maven.jacoco.version>0.8.6</maven.jacoco.version>
8284
<!-- Build args -->
83-
<argline>-server -Xms256m -Xmx512m -Dfile.encoding=UTF-8
85+
<arg.line>-server -Xms256m -Xmx512m -Dfile.encoding=UTF-8
8486
-Djava.net.preferIPv4Stack=true -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=128m
85-
</argline>
87+
</arg.line>
8688
<checkstyle.skip>true</checkstyle.skip>
8789
<rat.skip>true</rat.skip>
8890
<jacoco.skip>true</jacoco.skip>
@@ -134,8 +136,8 @@
134136

135137
<!-- unit test -->
136138
<dependency>
137-
<groupId>junit</groupId>
138-
<artifactId>junit</artifactId>
139+
<groupId>org.junit.jupiter</groupId>
140+
<artifactId>junit-jupiter-engine</artifactId>
139141
<version>${junit.version}</version>
140142
<scope>test</scope>
141143
</dependency>
@@ -149,7 +151,7 @@
149151
</dependencyManagement>
150152

151153
<profiles>
152-
<!-- jacoco: mvn validate -Pjacoco -->
154+
<!-- jacoco: mvn verify -Pjacoco -->
153155
<profile>
154156
<id>jacoco</id>
155157
<properties>
@@ -263,7 +265,7 @@
263265
<plugin>
264266
<groupId>org.apache.rat</groupId>
265267
<artifactId>apache-rat-plugin</artifactId>
266-
<version>${apache-rat-plugin.version}</version>
268+
<version>${apache.rat.version}</version>
267269
<executions>
268270
<execution>
269271
<id>rat-validate</id>
@@ -295,7 +297,6 @@
295297
<exclude>**/.project</exclude>
296298
<exclude>**/target/**</exclude>
297299
<exclude>**/*.log</exclude>
298-
<exclude>CODE_OF_CONDUCT.md</exclude>
299300
<exclude>.codecov.yml</exclude>
300301
<exclude>.travis.yml</exclude>
301302
<exclude>PULL_REQUEST_TEMPLATE.md</exclude>
@@ -312,41 +313,41 @@
312313
</excludes>
313314
</configuration>
314315
</plugin>
316+
<plugin>
317+
<groupId>org.codehaus.mojo</groupId>
318+
<artifactId>cobertura-maven-plugin</artifactId>
319+
<version>${cobertura.maven.version}</version>
320+
<configuration>
321+
<formats>
322+
<format>html</format>
323+
<format>xml</format>
324+
</formats>
325+
<check />
326+
</configuration>
327+
</plugin>
315328
<plugin>
316329
<groupId>org.jacoco</groupId>
317330
<artifactId>jacoco-maven-plugin</artifactId>
318331
<version>${maven.jacoco.version}</version>
319332
<executions>
333+
<!-- Prepares the property pointing to the JaCoCo runtime agent
334+
which is passed as VM argument when Maven the Surefire plugin is executed. -->
320335
<execution>
321-
<id>jacoco-initialize</id>
336+
<id>prepare-agent</id>
322337
<goals>
323338
<goal>prepare-agent</goal>
324339
</goals>
325-
<configuration>
326-
<propertyName>jacocoArgLine</propertyName>
327-
</configuration>
328340
</execution>
341+
<!-- Ensures that the code coverage report is created after
342+
all tests have been run. -->
329343
<execution>
330-
<id>report-aggregate</id>
331-
<phase>verify</phase>
344+
<id>generate-report</id>
332345
<goals>
333-
<goal>report-aggregate</goal>
346+
<goal>report</goal>
334347
</goals>
335348
</execution>
336349
</executions>
337350
</plugin>
338-
<plugin>
339-
<groupId>org.apache.maven.plugins</groupId>
340-
<artifactId>maven-surefire-plugin</artifactId>
341-
<configuration>
342-
<useSystemClassLoader>true</useSystemClassLoader>
343-
<forkMode>once</forkMode>
344-
<argLine>${argline} ${jacocoArgLine}</argLine>
345-
<systemProperties>
346-
<!-- common shared -->
347-
</systemProperties>
348-
</configuration>
349-
</plugin>
350351
<plugin>
351352
<groupId>org.apache.maven.plugins</groupId>
352353
<artifactId>maven-javadoc-plugin</artifactId>

sdk-component/pom.xml

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@
7171
<version>${kotlin-stdlib.version}</version>
7272
</dependency>
7373
<dependency>
74-
<groupId>junit</groupId>
75-
<artifactId>junit</artifactId>
74+
<groupId>org.junit.jupiter</groupId>
75+
<artifactId>junit-jupiter-engine</artifactId>
7676
<scope>test</scope>
7777
</dependency>
7878
<dependency>
@@ -98,6 +98,38 @@
9898
</execution>
9999
</executions>
100100
</plugin>
101+
<!-- With JUnit 5 explicitely needed to let our tests run with Maven & as there seems to be a transitively
102+
added older surefire-plugin, that doesnt support JUnit 5 as described in https://stackoverflow.com/a/49019437/4964553 -->
103+
<plugin>
104+
<groupId>org.apache.maven.plugins</groupId>
105+
<artifactId>maven-surefire-plugin</artifactId>
106+
<version>${maven.surefire.version}</version>
107+
</plugin>
108+
<plugin>
109+
<groupId>org.jacoco</groupId>
110+
<artifactId>jacoco-maven-plugin</artifactId>
111+
<version>${maven.jacoco.version}</version>
112+
<executions>
113+
<!-- Prepares the property pointing to the JaCoCo
114+
runtime agent which is passed as VM argument when Maven the Surefire plugin
115+
is executed. -->
116+
<execution>
117+
<id>pre-unit-test</id>
118+
<goals>
119+
<goal>prepare-agent</goal>
120+
</goals>
121+
</execution>
122+
<!-- Ensures that the code coverage report for
123+
unit tests is created after unit tests have been run. -->
124+
<execution>
125+
<id>post-unit-test</id>
126+
<phase>test</phase>
127+
<goals>
128+
<goal>report</goal>
129+
</goals>
130+
</execution>
131+
</executions>
132+
</plugin>
101133
</plugins>
102134
</build>
103135

sdk-component/src/test/java/group/rxcloud/capa/component/configstore/CapaConfigStoreBuilderTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
import com.google.common.collect.Lists;
2020
import group.rxcloud.capa.infrastructure.serializer.DefaultObjectSerializer;
21-
import org.junit.Assert;
22-
import org.junit.Test;
21+
import org.junit.jupiter.api.Assertions;
22+
import org.junit.jupiter.api.Test;
2323

2424
import java.util.Collections;
2525

@@ -31,29 +31,29 @@ public class CapaConfigStoreBuilderTest {
3131

3232
@Test
3333
public void testWithObjectSerializer_FailWhenCapaObjectSerializerIsNull() {
34-
Assert.assertThrows(IllegalArgumentException.class, () -> {
34+
Assertions.assertThrows(IllegalArgumentException.class, () -> {
3535
new CapaConfigStoreBuilder(constructStoreConfig()).withObjectSerializer(null);
3636
});
3737
}
3838

3939
@Test
4040
public void testWithObjectSerializer_FailWhenContentTypeIsNull() {
41-
IllegalArgumentException illegalArgumentException = Assert.assertThrows(IllegalArgumentException.class, () -> {
41+
IllegalArgumentException illegalArgumentException = Assertions.assertThrows(IllegalArgumentException.class, () -> {
4242
new CapaConfigStoreBuilder(constructStoreConfig()).withObjectSerializer(new TestObjectSerializer());
4343
});
4444
}
4545

4646
@Test
4747
public void testWithObjectSerializer_SuccessWhenDefaultObjectSerializerIsUsed() {
4848
CapaConfigStoreBuilder capaConfigStoreBuilder = new CapaConfigStoreBuilder(constructStoreConfig()).withObjectSerializer(new DefaultObjectSerializer());
49-
Assert.assertNotNull(capaConfigStoreBuilder);
49+
Assertions.assertNotNull(capaConfigStoreBuilder);
5050
}
5151

5252
@Test
5353
public void testBuild_Success() {
5454
CapaConfigStoreBuilder capaConfigStoreBuilder = new CapaConfigStoreBuilder(constructStoreConfig());
5555
CapaConfigStore build = capaConfigStoreBuilder.build();
56-
Assert.assertNotNull(build);
56+
Assertions.assertNotNull(build);
5757
}
5858

5959
private StoreConfig constructStoreConfig() {

sdk-component/src/test/java/group/rxcloud/capa/component/configstore/ConfigurationItemTest.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
package group.rxcloud.capa.component.configstore;
21
/*
32
* Licensed to the Apache Software Foundation (ASF) under one or more
43
* contributor license agreements. See the NOTICE file distributed with
@@ -15,8 +14,10 @@
1514
* See the License for the specific language governing permissions and
1615
* limitations under the License.
1716
*/
18-
import org.junit.Assert;
19-
import org.junit.Test;
17+
package group.rxcloud.capa.component.configstore;
18+
19+
import org.junit.jupiter.api.Assertions;
20+
import org.junit.jupiter.api.Test;
2021

2122
import java.util.HashMap;
2223
import java.util.Map;
@@ -41,16 +42,16 @@ public void testConfigurationItemSetterGetter_Success() {
4142
configurationItem.setMetadata(metaDataMap);
4243
configurationItem.setTags(metaDataMap);
4344

44-
Assert.assertNotNull(configurationItem);
45-
Assert.assertEquals("testKey", configurationItem.getKey());
46-
Assert.assertEquals("testContent", configurationItem.getContent());
47-
Assert.assertEquals("testGroup", configurationItem.getGroup());
48-
Assert.assertEquals("testLabel", configurationItem.getLabel());
45+
Assertions.assertNotNull(configurationItem);
46+
Assertions.assertEquals("testKey", configurationItem.getKey());
47+
Assertions.assertEquals("testContent", configurationItem.getContent());
48+
Assertions.assertEquals("testGroup", configurationItem.getGroup());
49+
Assertions.assertEquals("testLabel", configurationItem.getLabel());
4950

50-
Assert.assertEquals(2, configurationItem.getMetadata().size());
51-
Assert.assertEquals("default", configurationItem.getMetadata().get("cluster"));
51+
Assertions.assertEquals(2, configurationItem.getMetadata().size());
52+
Assertions.assertEquals("default", configurationItem.getMetadata().get("cluster"));
5253

53-
Assert.assertEquals(2, configurationItem.getTags().size());
54-
Assert.assertEquals("testNamespace", configurationItem.getTags().get("namespace"));
54+
Assertions.assertEquals(2, configurationItem.getTags().size());
55+
Assertions.assertEquals("testNamespace", configurationItem.getTags().get("namespace"));
5556
}
5657
}

0 commit comments

Comments
 (0)