Skip to content

Commit 7232279

Browse files
tcyrusbmcutler
authored andcommitted
Add JaCoCo to Java Coverage Example (#14)
* Testing new Instrumentation * Trying to get Coverage Reports working * Trying a new approach * Trying Again * Re-adding Emma for Comparison * Trying Again... * Try Try Try Again * Finally, It (Kinda) Works Instead of doing Instrumentation the way Emma does, I am using on-demand instrumentation. It's better documented and (kinda) works. The corresponding changes involving libraries and filenames have not been added to Submitty yet, but I have tested this on my computer and it seems to work. I have not been able to test TestRunner specifically, but I did test running student tests through `org.junit.runner.JUnitCore` and it seemed to work fine. * Other Fixes This is so the new descriptions are accurate * Trying to View Results * modified the java coverage test to use show use of jacoco and emma * replace tabs with spaces
1 parent 8307040 commit 7232279

File tree

2 files changed

+120
-60
lines changed

2 files changed

+120
-60
lines changed

examples/10_java_coverage/config/config.json

Lines changed: 71 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"autograding" : {
3+
"work_to_details": [ "*.csv" ]
4+
},
25
"resource_limits" : {
36
"RLIMIT_CPU" : 60,
47
"RLIMIT_NPROC" : 100,
@@ -12,21 +15,80 @@
1215
// instructor-provided JUnit test-based grading.
1316
{
1417
"type" : "Compilation",
15-
"title" : "Java - Compilation",
18+
"title" : "Class Compilation",
1619
"command" : "javac -cp submitty_junit.jar hw0/Factorial.java",
1720
"executable_name" : "hw0/Factorial.class",
1821
"points" : 2
1922
},
2023
{
2124
"type" : "Compilation",
22-
"title" : "Java - Student and Instructor JUnit Test Compilation",
25+
"title" : "Student and Instructor JUnit Test Compilation",
2326
"command" : "javac -cp submitty_junit.jar:. hw0/test/*Test.java",
2427
"executable_name" : "hw0/test/FactorialTest.class",
2528
"points" : 2
2629
},
2730

31+
32+
// -----------------------------------------------------------------------
33+
// COVERAGE USING JACOCO
34+
// JaCoCo can do on-demand instrumentation
35+
{
36+
"title" : "JaCoCo - Running Student JUnit Tests in hw0/tests/",
37+
"command" : "java -noverify -javaagent:submitty_jacocoagent.jar=destfile=coverage.exec -cp submitty_junit.jar:submitty_hamcrest.jar:submitty_junit/:. TestRunner hw0",
38+
"points" : 4,
39+
"validation" : [
40+
{
41+
"method" : "MultipleJUnitTestGrader",
42+
"actual_file" : "STDOUT.txt"
43+
}
44+
]
45+
},
46+
{
47+
"title" : "JaCoCo - Generating Coverage Report for Student Tests",
48+
"command" : "java -jar submitty_jacococli.jar report coverage.exec --classfiles hw0 --csv jacoco_report.csv",
49+
"points" : 6,
50+
"validation" : [
51+
{
52+
"method" : "errorIfEmpty",
53+
"actual_file" : "STDOUT.txt",
54+
"description" : "JaCoCo report generation output",
55+
"deduction" : 0.0
56+
},
57+
{
58+
"method" : "JaCoCoCoverageReportGrader",
59+
"actual_file" : "jacoco_report.csv",
60+
"description" : "JaCoCo coverage report",
61+
// specify the package & class to check (can omit to check all)
62+
"package" : "hw0",
63+
//"class" : "Factorial",
64+
// which threshold(s) to apply
65+
"instruction_coverage_threshold" : 90,
66+
//"branch_coverage_threshold" : 90,
67+
//"line_coverage_threshold" : 70,
68+
//"complexity_coverage_threshold" : 80,
69+
//"method_coverage_threshold" : 80,
70+
"deduction" : 1.0
71+
}
72+
]
73+
},
74+
{
75+
"title" : "JaCoCo - Instructor JUnit Tests",
76+
"command" : "java -noverify -cp submitty_junit.jar:submitty_hamcrest.jar:. org.junit.runner.JUnitCore hw0.test.FactorialTest",
77+
"points" : 6,
78+
"validation" : [
79+
{
80+
"method" : "JUnitTestGrader",
81+
"actual_file" : "STDOUT.txt",
82+
"num_tests" : 4
83+
}
84+
]
85+
},
86+
87+
88+
// ------------------------------------------------------------------------------
89+
// COVERAGE USING EMMA
2890
{
29-
"title" : "Java - Instrumentation of Student Code",
91+
"title" : "EMMA - Instrumentation of Student Code",
3092
"command" : "java -cp submitty_emma.jar emma instr -m overwrite -ip hw0",
3193
"points" : 0,
3294
"validation" : [
@@ -36,9 +98,8 @@
3698
}
3799
]
38100
},
39-
40101
{
41-
"title" : "Java - Running Student JUnit Tests in hw0/tests/",
102+
"title" : "EMMA - Running Student JUnit Tests in hw0/tests/",
42103
"command" : "java -noverify -cp submitty_junit.jar:submitty_hamcrest.jar:submitty_emma.jar:submitty_junit/:. TestRunner hw0",
43104
"points" : 4,
44105
"validation" : [
@@ -48,9 +109,8 @@
48109
}
49110
]
50111
},
51-
52112
{
53-
"title" : "Java - Generating Coverage Report for Student Tests",
113+
"title" : "EMMA - Generating Coverage Report for Student Tests",
54114
"command" : "java -cp submitty_emma.jar emma report -r txt -in coverage.em,coverage.ec -Dreport.txt.out.file=emma_report.txt",
55115
"points" : 6,
56116
"validation" : [
@@ -63,15 +123,15 @@
63123
{
64124
"method" : "EmmaCoverageReportGrader",
65125
"actual_file" : "emma_report.txt",
126+
"description" : "EclEmma coverage report",
66127
"coverage_threshold" : 90,
67128
"deduction" : 1.0
68129
}
69130
]
70131
},
71-
72132
{
73-
"title" : "Java - Instructor JUnit Tests",
74-
"command" : "java -noverify -cp submitty_junit.jar:submitty_hamcrest.jar:submitty_emma.jar:. org.junit.runner.JUnitCore hw0.test.FactorialTest",
133+
"title" : "EMMA - Instructor JUnit Tests",
134+
"command" : "java -noverify -cp submitty_junit.jar:submitty_hamcrest.jar:submitty_emma.jar:. org.junit.runner.JUnitCore hw0.test.FactorialTest",
75135
"points" : 6,
76136
"validation" : [
77137
{
@@ -81,5 +141,6 @@
81141
}
82142
]
83143
}
144+
84145
]
85146
}

examples/10_java_coverage/config/provided_code/hw0/test/ALMHiddenFactorialTest.java

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -18,67 +18,66 @@
1818

1919
public class ALMHiddenFactorialTest {
2020

21-
private static Factorial fac = null;
21+
private static Factorial fac = null;
2222

23-
@BeforeClass
23+
@BeforeClass
2424
public static void setupBeforeTests() throws Exception {
25-
fac = new Factorial();
26-
}
25+
fac = new Factorial();
26+
}
2727

28-
/**
29-
* Tests that Factorial throws an IllegalArgumentException
30-
* for a negative number.
31-
*/
32-
@Test(expected=IllegalArgumentException.class)
28+
/**
29+
* Tests that Factorial throws an IllegalArgumentException
30+
* for a negative number.
31+
*/
32+
@Test(expected=IllegalArgumentException.class)
3333
public void expectedIllegalArgumentException() {
34-
fac.getFact(-1);
35-
}
34+
fac.getFact(-1);
35+
}
3636

37-
/**
38-
* Tests that Factorial throws no IllegalArgumentException
39-
* for zero or for a positive number.
40-
*/
41-
@Test
37+
/**
38+
* Tests that Factorial throws no IllegalArgumentException
39+
* for zero or for a positive number.
40+
*/
41+
@Test
4242
public void testThrowsIllegalArgumentException() {
43-
44-
// test 0
45-
try {
46-
fac.getFact(0);
47-
} catch (IllegalArgumentException ex) {
48-
fail("Threw IllegalArgumentException for 0 but 0 is nonnegative: "
43+
// test 0
44+
try {
45+
fac.getFact(0);
46+
} catch (IllegalArgumentException ex) {
47+
fail("Threw IllegalArgumentException for 0 but 0 is nonnegative: "
4948
+ ex);
50-
}
51-
// test 1
52-
try {
53-
fac.getFact(1);
54-
} catch (IllegalArgumentException ex) {
55-
fail("Threw IllegalArgumentException for 1 but 1 is nonnegative: "
49+
}
50+
// test 1
51+
try {
52+
fac.getFact(1);
53+
} catch (IllegalArgumentException ex) {
54+
fail("Threw IllegalArgumentException for 1 but 1 is nonnegative: "
5655
+ ex);
57-
}
58-
}
56+
}
57+
}
5958

60-
/** Tests to see that Fibonacci returns the correct value for the base cases, n=0 and n=1 */
61-
@Test
59+
/** Tests to see that Fibonacci returns the correct value for the base cases, n=0 and n=1 */
60+
@Test
6261
public void testBaseCase() {
63-
assertEquals("getFact(0)", 1, fac.getFact(0));
64-
assertEquals("getFact(1)", 1, fac.getFact(1));
65-
}
62+
assertEquals("getFact(0)", 1, fac.getFact(0));
63+
assertEquals("getFact(1)", 1, fac.getFact(1));
64+
}
6665

67-
/** Tests inductive cases of the Factorial sequence */
68-
@Test
69-
public void testInductiveCase() {
70-
int[][] cases = new int[][] {
71-
{ 2, 2 },
72-
{ 3, 6 },
73-
{ 4, 24 },
74-
{ 5, 120 },
75-
{ 6, 720 },
76-
{ 7, 5040 }
77-
};
78-
for (int i = 0; i < cases.length; i++) {
79-
assertEquals("getFact(" + cases[i][0] + ")",
80-
cases[i][1], fac.getFact(cases[i][0]));
66+
/** Tests inductive cases of the Factorial sequence */
67+
@Test
68+
public void testInductiveCase() {
69+
int[][] cases = new int[][] {
70+
{ 2, 2 },
71+
{ 3, 6 },
72+
{ 4, 24 },
73+
{ 5, 120 },
74+
{ 6, 720 },
75+
{ 7, 5040 }
76+
};
77+
for (int i = 0; i < cases.length; i++) {
78+
assertEquals("getFact(" + cases[i][0] + ")",
79+
cases[i][1], fac.getFact(cases[i][0]));
80+
}
8181
}
82-
}
8382

8483
}

0 commit comments

Comments
 (0)