aboutsummaryrefslogtreecommitdiff
path: root/gunit-maven-plugin/src/main/java/org/antlr/mojo/antlr3/GUnitExecuteMojo.java
blob: a7e2317692cea1ebe7915355cd719cc0892172bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package org.antlr.mojo.antlr3;

import java.util.List;
import java.util.Set;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Collections;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.net.URL;
import java.net.MalformedURLException;
import java.net.URLClassLoader;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.compiler.util.scan.mapping.SourceMapping;
import org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping;
import org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner;
import org.codehaus.plexus.compiler.util.scan.SimpleSourceInclusionScanner;
import org.codehaus.plexus.compiler.util.scan.InclusionScanException;
import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.gunit.GrammarInfo;
import org.antlr.gunit.gUnitExecutor;
import org.antlr.gunit.AbstractTest;
import org.antlr.gunit.Interp;

/**
 * Takes gUnit scripts and directly performs testing.
 *
 * @goal gunit
 *
 * @phase test
 * @requiresDependencyResolution test
 * @requiresProject true
 *
 * @author Steve Ebersole
 */
public class GUnitExecuteMojo extends AbstractMojo {
	public static final String ANTLR_GROUP_ID = "org.antlr";
	public static final String ANTLR_ARTIFACT_NAME = "antlr";
	public static final String ANTLR_RUNTIME_ARTIFACT_NAME = "antlr-runtime";

	/**
     * INTERNAL : The Maven Project to which we are attached
     *
     * @parameter expression="${project}"
     * @required
     */
    private MavenProject project;

	/**
	 * INTERNAL : The artifacts associated to the dependencies defined as part
	 * of our configuration within the project to which we are being attached.
	 *
	 * @parameter expression="${plugin.artifacts}"
     * @required
     * @readonly
	 */
	private List<Artifact> pluginArtifacts;

	/**
     * Specifies the directory containing the gUnit testing files.
     *
     * @parameter expression="${basedir}/src/test/gunit"
     * @required
     */
    private File sourceDirectory;

    /**
     * A set of patterns for matching files from the sourceDirectory that
     * should be included as gUnit source files.
     *
     * @parameter
     */
    private Set<String> includes;

    /**
     * A set of exclude patterns.
     *
     * @parameter
     */
    private Set<String> excludes;

	/**
     * Specifies directory to which gUnit reports should get written.
     *
     * @parameter expression="${basedir}/target/gunit-report"
     * @required
     */
    private File reportDirectory;

	/**
	 * Should gUnit functionality be completely by-passed?
	 * <p>
	 * By default we skip gUnit tests if the user requested that all testing be skipped using 'maven.test.skip'</p>
	 *
	 * @parameter expression="${maven.test.skip}"
	 */
	private boolean skip;

	public Set<String> getIncludePatterns() {
		return includes == null || includes.isEmpty()
				? Collections.singleton( "**/*.testsuite" )
				: includes;
	}

	public Set<String> getExcludePatterns() {
		return excludes == null
				? Collections.<String>emptySet()
				: excludes;
	}


	public final void execute() throws MojoExecutionException, MojoFailureException {
		if ( skip ) {
			getLog().info( "Skipping gUnit processing" );
			return;
		}
		Artifact pluginAntlrArtifact = determinePluginAntlrArtifact();

		validateProjectsAntlrVersion( determineArtifactVersion( pluginAntlrArtifact ) );

		performExecution( determineProjectCompileScopeClassLoader( pluginAntlrArtifact ) );
	}

	private Artifact determinePluginAntlrArtifact() throws MojoExecutionException {
		for ( Artifact artifact : pluginArtifacts ) {
			boolean match = ANTLR_GROUP_ID.equals( artifact.getGroupId() )
					&& ANTLR_ARTIFACT_NAME.equals( artifact.getArtifactId() );
			if ( match ) {
				return artifact;
			}
		}
		throw new MojoExecutionException(
				"Unexpected state : could not locate " + ANTLR_GROUP_ID + ':' + ANTLR_ARTIFACT_NAME +
						" in plugin dependencies"
		);
	}

	private ArtifactVersion determineArtifactVersion(Artifact artifact) throws MojoExecutionException {
		try {
			return artifact.getVersion() != null
					? new DefaultArtifactVersion( artifact.getVersion() )
					: artifact.getSelectedVersion();
		}
		catch ( OverConstrainedVersionException e ) {
			throw new MojoExecutionException( "artifact [" + artifact.getId() + "] defined an overly constrained version range" );
		}
	}

	private void validateProjectsAntlrVersion(ArtifactVersion pluginAntlrVersion) throws MojoExecutionException {
		Artifact antlrArtifact = null;
		Artifact antlrRuntimeArtifact = null;

		if ( project.getCompileArtifacts() != null ) {
			for ( Object o : project.getCompileArtifacts() ) {
				final Artifact artifact = ( Artifact ) o;
				if ( ANTLR_GROUP_ID.equals( artifact.getGroupId() ) ) {
					if ( ANTLR_ARTIFACT_NAME.equals( artifact.getArtifactId() ) ) {
						antlrArtifact = artifact;
						break;
					}
					if ( ANTLR_RUNTIME_ARTIFACT_NAME.equals( artifact.getArtifactId() ) ) {
						antlrRuntimeArtifact = artifact;
					}
				}
			}
		}

		validateBuildTimeArtifact( antlrArtifact, pluginAntlrVersion );
		validateRunTimeArtifact( antlrRuntimeArtifact, pluginAntlrVersion );
	}

	@SuppressWarnings(value = "unchecked")
	protected void validateBuildTimeArtifact(Artifact antlrArtifact, ArtifactVersion pluginAntlrVersion)
			throws MojoExecutionException {
		if ( antlrArtifact == null ) {
			validateMissingBuildtimeArtifact();
			return;
		}

		// otherwise, lets make sure they match...
		ArtifactVersion projectAntlrVersion = determineArtifactVersion( antlrArtifact );
		if ( pluginAntlrVersion.compareTo( projectAntlrVersion ) != 0 ) {
			getLog().warn(
					"Encountered " + ANTLR_GROUP_ID + ':' + ANTLR_ARTIFACT_NAME + ':' + projectAntlrVersion.toString() +
							" which did not match Antlr version used by plugin [" + pluginAntlrVersion.toString() + "]"
			);
		}
	}

	protected void validateMissingBuildtimeArtifact() {
		// generally speaking, its ok for the project to not define a dep on the build-time artifact...
	}

	@SuppressWarnings(value = "unchecked")
	protected void validateRunTimeArtifact(Artifact antlrRuntimeArtifact, ArtifactVersion pluginAntlrVersion)
			throws MojoExecutionException {
		if ( antlrRuntimeArtifact == null ) {
			// its possible, if the project instead depends on the build-time (or full) artifact.
			return;
		}

		ArtifactVersion projectAntlrVersion = determineArtifactVersion( antlrRuntimeArtifact );
		if ( pluginAntlrVersion.compareTo( projectAntlrVersion ) != 0 ) {
			getLog().warn(
					"Encountered " + ANTLR_GROUP_ID + ':' + ANTLR_RUNTIME_ARTIFACT_NAME + ':' + projectAntlrVersion.toString() +
							" which did not match Antlr version used by plugin [" + pluginAntlrVersion.toString() + "]"
			);
		}
	}

	/**
	 * Builds the classloader to pass to gUnit.
	 *
	 * @param antlrArtifact The plugin's (our) Antlr dependency artifact.
	 *
	 * @return The classloader for gUnit to use
	 *
	 * @throws MojoExecutionException Problem resolving artifacts to {@link java.net.URL urls}.
	 */
	private ClassLoader determineProjectCompileScopeClassLoader(Artifact antlrArtifact)
			throws MojoExecutionException {
		ArrayList<URL> classPathUrls = new ArrayList<URL>();
		getLog().info( "Adding Antlr artifact : " + antlrArtifact.getId() );
		classPathUrls.add( resolveLocalURL( antlrArtifact ) );

		for ( String path : classpathElements() ) {
			try {
				getLog().info( "Adding project compile classpath element : " + path );
				classPathUrls.add( new File( path ).toURI().toURL() );
			}
			catch ( MalformedURLException e ) {
				throw new MojoExecutionException( "Unable to build path URL [" + path + "]" );
			}
		}

		return new URLClassLoader( classPathUrls.toArray( new URL[classPathUrls.size()] ), getClass().getClassLoader() );
	}

	protected static URL resolveLocalURL(Artifact artifact) throws MojoExecutionException {
		try {
			return artifact.getFile().toURI().toURL();
		}
		catch ( MalformedURLException e ) {
			throw new MojoExecutionException( "Unable to resolve artifact url : " + artifact.getId(), e );
		}
	}

	@SuppressWarnings( "unchecked" )
	private List<String> classpathElements() throws MojoExecutionException {
		try {
			// todo : should we combine both compile and test scoped elements?
			return ( List<String> ) project.getTestClasspathElements();
		}
		catch ( DependencyResolutionRequiredException e ) {
			throw new MojoExecutionException( "Call to Project#getCompileClasspathElements required dependency resolution" );
		}
	}

	private void performExecution(ClassLoader projectCompileScopeClassLoader) throws MojoExecutionException {
		getLog().info( "gUnit report directory : " + reportDirectory.getAbsolutePath() );
		if ( !reportDirectory.exists() ) {
			boolean directoryCreated = reportDirectory.mkdirs();
			if ( !directoryCreated ) {
				getLog().warn( "mkdirs() reported problem creating report directory" );
			}
		}

		Result runningResults = new Result();
		ArrayList<String> failureNames = new ArrayList<String>();

		System.out.println();
		System.out.println( "-----------------------------------------------------------" );
		System.out.println( " G U N I T   R E S U L T S" );
		System.out.println( "-----------------------------------------------------------" );

		for ( File script : collectIncludedSourceGrammars() ) {
			final String scriptPath = script.getAbsolutePath();
			System.out.println( "Executing script " + scriptPath );
			try {
				String scriptBaseName = StringUtils.chompLast( FileUtils.basename( script.getName() ), "." );

				ANTLRFileStream antlrStream = new ANTLRFileStream( scriptPath );
				GrammarInfo grammarInfo = Interp.parse( antlrStream );
				gUnitExecutor executor = new gUnitExecutor(
						grammarInfo,
						projectCompileScopeClassLoader,
						script.getParentFile().getAbsolutePath()
				);

				String report = executor.execTest();
				writeReportFile( new File( reportDirectory, scriptBaseName + ".txt" ), report );

				Result testResult = new Result();
				testResult.tests = executor.numOfTest;
				testResult.failures = executor.numOfFailure;
				testResult.invalids = executor.numOfInvalidInput;

				System.out.println( testResult.render() );

				runningResults.add( testResult );
				for ( AbstractTest test : executor.failures ) {
					failureNames.add( scriptBaseName + "#" + test.getHeader() );
				}
			}
			catch ( IOException e ) {
				throw new MojoExecutionException( "Could not open specified script file", e );
			}
			catch ( RecognitionException e ) {
				throw new MojoExecutionException( "Could not parse gUnit script", e );
			}
		}

		System.out.println();
		System.out.println( "Summary :" );
		if ( ! failureNames.isEmpty() ) {
			System.out.println( "  Found " + failureNames.size() + " failures" );
			for ( String name : failureNames ) {
				System.out.println( "    - " + name );
			}
		}
		System.out.println( runningResults.render() );
		System.out.println();

		if ( runningResults.failures > 0 ) {
			throw new MojoExecutionException( "Found gUnit test failures" );
		}

		if ( runningResults.invalids > 0 ) {
			throw new MojoExecutionException( "Found invalid gUnit tests" );
		}
	}

	private Set<File> collectIncludedSourceGrammars() throws MojoExecutionException {
		SourceMapping mapping = new SuffixMapping( "g", Collections.<String>emptySet() );
        SourceInclusionScanner scan = new SimpleSourceInclusionScanner( getIncludePatterns(), getExcludePatterns() );
        scan.addSourceMapping( mapping );
		try {
			return scan.getIncludedSources( sourceDirectory, null );
		}
		catch ( InclusionScanException e ) {
			throw new MojoExecutionException( "Error determining gUnit sources", e );
		}
	}

	private void writeReportFile(File reportFile, String results) {
		try {
			Writer writer = new FileWriter( reportFile );
			writer = new BufferedWriter( writer );
			try {
				writer.write( results );
				writer.flush();
			}
			finally {
				try {
					writer.close();
				}
				catch ( IOException ignore ) {
				}
			}
		}
		catch ( IOException e ) {
			getLog().warn(  "Error writing gUnit report file", e );
		}
	}

	private static class Result {
		private int tests = 0;
		private int failures = 0;
		private int invalids = 0;

		public String render() {
			return String.format( "Tests run: %d,  Failures: %d,  Invalid: %d", tests, failures, invalids );
		}

		public void add(Result result) {
			this.tests += result.tests;
			this.failures += result.failures;
			this.invalids += result.invalids;
		}
	}

}