21
21
import java .nio .file .Files ;
22
22
import java .nio .file .Path ;
23
23
import java .util .ArrayList ;
24
+ import java .util .HashMap ;
24
25
import java .util .List ;
26
+ import java .util .Map ;
27
+ import java .util .Map .Entry ;
28
+ import java .util .regex .Matcher ;
29
+ import java .util .regex .Pattern ;
25
30
import java .util .stream .Collectors ;
26
31
27
32
import org .gradle .api .DefaultTask ;
@@ -52,6 +57,11 @@ public class ApplicationRunner extends DefaultTask {
52
57
53
58
private final Property <String > expectedLogging = getProject ().getObjects ().property (String .class );
54
59
60
+ private final Property <String > applicationJar = getProject ().getObjects ().property (String .class )
61
+ .convention ("/opt/apps/myapp.jar" );
62
+
63
+ private final Map <String , String > normalizations = new HashMap <>();
64
+
55
65
private FileCollection classpath ;
56
66
57
67
@ OutputFile
@@ -83,6 +93,25 @@ public Property<String> getExpectedLogging() {
83
93
return this .expectedLogging ;
84
94
}
85
95
96
+ @ Input
97
+ Map <String , String > getNormalizations () {
98
+ return this .normalizations ;
99
+ }
100
+
101
+ @ Input
102
+ public Property <String > getApplicationJar () {
103
+ return this .applicationJar ;
104
+ }
105
+
106
+ public void normalizeTomcatPort () {
107
+ this .normalizations .put ("(Tomcat started on port\\ (s\\ ): )[\\ d]+( \\ (http\\ ))" , "$18080$2" );
108
+ this .normalizations .put ("(Tomcat initialized with port\\ (s\\ ): )[\\ d]+( \\ (http\\ ))" , "$18080$2" );
109
+ }
110
+
111
+ public void normalizeLiveReloadPort () {
112
+ this .normalizations .put ("(LiveReload server is running on port )[\\ d]+" , "$135729" );
113
+ }
114
+
86
115
@ TaskAction
87
116
void runApplication () throws IOException {
88
117
List <String > command = new ArrayList <>();
@@ -98,6 +127,7 @@ void runApplication() throws IOException {
98
127
.start ();
99
128
awaitLogging (process );
100
129
process .destroy ();
130
+ normalizeLogging ();
101
131
}
102
132
103
133
private void awaitLogging (Process process ) {
@@ -126,4 +156,57 @@ private List<String> outputLines() {
126
156
}
127
157
}
128
158
159
+ private void normalizeLogging () {
160
+ List <String > outputLines = outputLines ();
161
+ List <String > normalizedLines = normalize (outputLines );
162
+ Path outputPath = this .output .get ().getAsFile ().toPath ();
163
+ try {
164
+ Files .write (outputPath , normalizedLines );
165
+ }
166
+ catch (IOException ex ) {
167
+ throw new RuntimeException ("Failed to write normalized lines of output to '" + outputPath + "'" , ex );
168
+ }
169
+ }
170
+
171
+ private List <String > normalize (List <String > lines ) {
172
+ List <String > normalizedLines = lines ;
173
+ Map <String , String > normalizations = new HashMap <>(this .normalizations );
174
+ normalizations .put ("(Starting .* using Java .* on ).*( with PID [\\ d]+ \\ ().*( started by ).*( in ).*(\\ ))" ,
175
+ "$1myhost$2" + this .applicationJar .get () + "$3myuser$4/opt/apps/$5" );
176
+ for (Entry <String , String > normalization : normalizations .entrySet ()) {
177
+ Pattern pattern = Pattern .compile (normalization .getKey ());
178
+ normalizedLines = normalize (normalizedLines , pattern , normalization .getValue ());
179
+ }
180
+ return normalizedLines ;
181
+ }
182
+
183
+ private List <String > normalize (List <String > lines , Pattern pattern , String replacement ) {
184
+ boolean matched = false ;
185
+ List <String > normalizedLines = new ArrayList <>();
186
+ for (String line : lines ) {
187
+ Matcher matcher = pattern .matcher (line );
188
+ StringBuffer transformed = new StringBuffer ();
189
+ while (matcher .find ()) {
190
+ matched = true ;
191
+ matcher .appendReplacement (transformed , replacement );
192
+ }
193
+ matcher .appendTail (transformed );
194
+ normalizedLines .add (transformed .toString ());
195
+ }
196
+ if (!matched ) {
197
+ reportUnmatchedNormalization (lines , pattern );
198
+ }
199
+ return normalizedLines ;
200
+ }
201
+
202
+ private void reportUnmatchedNormalization (List <String > lines , Pattern pattern ) {
203
+ StringBuilder message = new StringBuilder (
204
+ "'" + pattern + "' did not match any of the following lines of output:" );
205
+ message .append (String .format ("%n" ));
206
+ for (String line : lines ) {
207
+ message .append (String .format ("%s%n" , line ));
208
+ }
209
+ throw new IllegalStateException (message .toString ());
210
+ }
211
+
129
212
}
0 commit comments