1
1
package org .utplsql .cli ;
2
2
3
+ import org .junit .jupiter .api .AfterEach ;
4
+ import org .junit .jupiter .api .BeforeEach ;
3
5
import org .junit .jupiter .api .Test ;
4
6
5
7
import java .io .File ;
6
8
import java .nio .file .Files ;
7
9
import java .nio .file .Path ;
8
10
import java .nio .file .Paths ;
9
- import java .util .List ;
11
+ import java .util .HashSet ;
10
12
import java .util .Scanner ;
13
+ import java .util .Set ;
11
14
import java .util .regex .Matcher ;
12
15
import java .util .regex .Pattern ;
13
16
@@ -23,6 +26,12 @@ public class RunCommandCoverageReporterIT {
23
26
24
27
private static final Pattern REGEX_COVERAGE_TITLE = Pattern .compile ("<a href=\" [a-zA-Z0-9#]+\" class=\" src_link\" title=\" [a-zA-Z\\ ._]+\" >([a-zA-Z0-9\\ ._]+)<\\ /a>" );
25
28
29
+ private Set <Path > tempPaths ;
30
+
31
+ private void addTempPath (Path path ) {
32
+ tempPaths .add (path );
33
+ }
34
+
26
35
private String getTempCoverageFileName (int counter ) {
27
36
28
37
return "tmpCoverage_" + String .valueOf (System .currentTimeMillis ()) + "_" + String .valueOf (counter ) + ".html" ;
@@ -34,15 +43,19 @@ private String getTempCoverageFileName(int counter) {
34
43
* @return
35
44
*/
36
45
private Path getTempCoverageFilePath () {
46
+
37
47
int i = 1 ;
38
48
Path p = Paths .get (getTempCoverageFileName (i ));
39
49
40
- while (Files .exists (p ) && i < 100 )
50
+ while (( Files .exists (p ) || tempPaths . contains ( p ) ) && i < 100 )
41
51
p = Paths .get (getTempCoverageFileName (i ++));
42
52
43
53
if (i >= 100 )
44
54
throw new IllegalStateException ("Could not get temporary file for coverage output" );
45
55
56
+ addTempPath (p );
57
+ addTempPath (Paths .get (p .toString ()+"_assets" ));
58
+
46
59
return p ;
47
60
}
48
61
@@ -64,6 +77,11 @@ private boolean hasCoverageListed(String content, String packageName) {
64
77
return false ;
65
78
}
66
79
80
+ @ BeforeEach
81
+ public void setupTest () {
82
+ tempPaths = new HashSet <>();
83
+ }
84
+
67
85
@ Test
68
86
public void run_CodeCoverageWithIncludeAndExclude () throws Exception {
69
87
@@ -72,39 +90,48 @@ public void run_CodeCoverageWithIncludeAndExclude() throws Exception {
72
90
RunCommand runCmd = RunCommandTestHelper .createRunCommand (RunCommandTestHelper .getConnectionString (),
73
91
"-f=ut_coverage_html_reporter" , "-o=" + coveragePath , "-s" , "-exclude=app.award_bonus,app.betwnstr" );
74
92
75
- try {
76
- int result = runCmd .run ();
77
93
78
- String content = new Scanner ( coveragePath ). useDelimiter ( " \\ Z" ). next ();
94
+ int result = runCmd . run ();
79
95
80
- assertEquals (true , hasCoverageListed (content , "app.remove_rooms_by_name" ));
81
- assertEquals (false , hasCoverageListed (content , "app.award_bonus" ));
82
- assertEquals (false , hasCoverageListed (content , "app.betwnstr" ));
96
+ String content = new Scanner (coveragePath ).useDelimiter ("\\ Z" ).next ();
83
97
84
- } finally {
85
- Files . delete ( coveragePath );
86
- }
98
+ assertEquals ( true , hasCoverageListed ( content , "app.remove_rooms_by_name" ));
99
+ assertEquals ( false , hasCoverageListed ( content , "app.award_bonus" ) );
100
+ assertEquals ( false , hasCoverageListed ( content , "app.betwnstr" ));
87
101
88
102
}
89
103
90
104
@ Test
91
105
public void coverageReporterWriteAssetsToOutput () throws Exception {
92
106
Path coveragePath = getTempCoverageFilePath ();
107
+ Path coverageAssetsPath = Paths .get (coveragePath .toString () + "_assets" );
93
108
94
109
RunCommand runCmd = RunCommandTestHelper .createRunCommand (RunCommandTestHelper .getConnectionString (),
95
110
"-f=ut_coverage_html_reporter" , "-o=" + coveragePath , "-s" );
96
- try {
97
- int result = runCmd .run ();
98
111
99
- List <ReporterOptions > reporterOptions = runCmd .getReporterOptionsList ();
100
- File applicationJs = coveragePath .resolve (Paths .get ("assets" , "application.js" )).toFile ();
112
+ runCmd .run ();
101
113
102
- assertTrue ( applicationJs . exists () );
114
+ File applicationJs = coverageAssetsPath . resolve ( Paths . get ( "application.js" )). toFile ( );
103
115
104
- } finally {
105
- if ( Files . exists ( coveragePath ))
106
- Files . delete ( coveragePath );
107
- }
116
+ assertTrue ( applicationJs . exists ());
117
+
118
+
119
+ }
108
120
121
+ @ AfterEach
122
+ public void deleteTempFiles () {
123
+ tempPaths .forEach (p -> deleteDir (p .toFile ()));
124
+ }
125
+
126
+ void deleteDir (File file ) {
127
+ if (file .exists ()) {
128
+ File [] contents = file .listFiles ();
129
+ if (contents != null ) {
130
+ for (File f : contents ) {
131
+ deleteDir (f );
132
+ }
133
+ }
134
+ file .delete ();
135
+ }
109
136
}
110
137
}
0 commit comments