001package org.andromda.maven.plugin.translationlibrary; 002 003import java.io.File; 004import java.net.URL; 005import java.util.List; 006import junit.framework.TestResult; 007import org.andromda.core.common.AndroMDALogger; 008import org.andromda.core.common.ExceptionUtils; 009import org.andromda.core.common.ResourceUtils; 010import org.andromda.maven.plugin.configuration.AbstractConfigurationMojo; 011import org.andromda.translation.ocl.testsuite.TranslationTestProcessor; 012import org.apache.maven.artifact.factory.ArtifactFactory; 013import org.apache.maven.artifact.repository.ArtifactRepository; 014import org.apache.maven.model.Plugin; 015import org.apache.maven.plugin.MojoExecutionException; 016import org.apache.maven.plugin.MojoFailureException; 017import org.apache.maven.project.MavenProject; 018import org.apache.maven.settings.Settings; 019 020/** 021 * Provides the ability to compare cartridge output with existing output. 022 * 023 * @phase test 024 * @goal test 025 * @requiresDependencyResolution test 026 * @description runs AndroMDA Translation-Library tests 027 * @author Chad Brandon 028 */ 029public class TranslationLibraryTestMojo 030 extends AbstractConfigurationMojo 031{ 032 /** 033 * Base directory to which the cartridge test report is written 034 * 035 * @parameter expression="${project.build.directory}/translation-library-test/reports" 036 */ 037 private String reportDirectory; 038 039 /** 040 * Whether or not the expression shall be 'traced' (i.e. the TraceTranslator will run instead of the specified translator). 041 * This is helpful, in allowing us to see which expressions are being parsed in what order, etc. 042 * 043 * @parameter expression="${trace.expression}" 044 */ 045 protected boolean traceExpression; 046 047 /** 048 * When specified, only this translation will be tested (If more than one TestTranslation-* file is found). 049 * 050 * @parameter expression="${translation.name}" 051 */ 052 protected String translationName; 053 054 /** 055 * The directory containing the test source. 056 * 057 * @parameter expression="${basedir}" 058 * @required 059 * @readonly 060 */ 061 protected String testSourceDirectory; 062 063 /** 064 * This is the URI to the AndroMDA configuration file. 065 * 066 * @parameter expression="file:${basedir}/conf/test/andromda.xml" 067 * @required 068 */ 069 protected String configurationUri; 070 071 /** 072 * @parameter expression="${project}" 073 * @required 074 * @readonly 075 */ 076 private MavenProject project; 077 078 /** 079 * @parameter expression="${project.build.filters}" 080 */ 081 private List<String> propertyFiles; 082 083 /** 084 * The current user system settings for use in Maven. (allows us to pass the user 085 * settings to the AndroMDA configuration). 086 * 087 * @parameter expression="${settings}" 088 * @required 089 * @readonly 090 */ 091 private Settings settings; 092 093 /** 094 * @component role="org.apache.maven.artifact.factory.ArtifactFactory" 095 * @required 096 * @readonly 097 */ 098 private ArtifactFactory factory; 099 100 /** 101 * The registered plugin implementations. 102 * 103 * @parameter expression="${project.build.plugins}" 104 * @required 105 * @readonly 106 */ 107 protected List<Plugin> plugins; 108 109 /** 110 * @parameter expression="${localRepository}" 111 * @required 112 * @readonly 113 */ 114 protected ArtifactRepository localRepository; 115 116 /** 117 * Set this to 'true' to bypass cartridge tests entirely. Its use is NOT RECOMMENDED, but quite convenient on occasion. 118 * 119 * @parameter expression="${andromda.translation.test.skip}" default-value="false" 120 */ 121 protected boolean skipTranslation; 122 123 /** 124 * @see org.apache.maven.plugin.Mojo#execute() 125 */ 126 public void execute() 127 throws MojoExecutionException, MojoFailureException 128 { 129 if (!this.skip && !this.skipTests && !this.skipTranslation) 130 { 131 try 132 { 133 // - initialize the AndroMDA logger instance 134 AndroMDALogger.initialize(); 135 136 this.getLog().info("--------------------------------------------------------------------------------"); 137 this.getLog().info(" A n d r o M D A T r a n s l a t i o n - L i b r a r y T e s t S u i t e "); 138 this.getLog().info("--------------------------------------------------------------------------------"); 139 140 this.initializeClasspathFromClassPathElements(this.project.getTestClasspathElements()); 141 142 final TranslationTestProcessor processor = TranslationTestProcessor.instance(); 143 processor.setTranslationName(this.translationName); 144 processor.setUseTraceTranslator(this.traceExpression); 145 processor.setTestSourceDirectory(this.testSourceDirectory); 146 final URL configurationUri = ResourceUtils.toURL(this.configurationUri); 147 if (configurationUri == null) 148 { 149 throw new MojoExecutionException("No configuration could be loaded from --> '" + 150 this.configurationUri + '\''); 151 } 152 processor.setConfiguration(this.getConfiguration(configurationUri)); 153 154 final TranslationLibraryTestFormatter formatter = new TranslationLibraryTestFormatter(); 155 156 // - set the report location 157 final File report = new File(this.reportDirectory, this.getProject().getArtifactId() + ".txt"); 158 formatter.setReportFile(report); 159 final TestResult result = new TestResult(); 160 formatter.startTestSuite(this.getProject().getName()); 161 result.addListener(formatter); 162 processor.setResult(result); 163 processor.runSuite(); 164 this.getLog().info(""); 165 this.getLog().info("Results:"); 166 this.getLog().info(formatter.endTestSuite()); 167 if (result.failureCount() > 0 || result.errorCount() > 0) 168 { 169 throw new MojoExecutionException("There are some test failures"); 170 } 171 processor.shutdown(); 172 } 173 catch (final Throwable throwable) 174 { 175 if (throwable instanceof MojoExecutionException && !this.testFailureIgnore) 176 { 177 throw (MojoExecutionException)throwable; 178 } 179 else if (this.testFailureIgnore) 180 { 181 this.getLog().error("An error occurred while testing translation-library '" + 182 this.translationName + '\'', 183 ExceptionUtils.getRootCause(throwable)); 184 } 185 else 186 { 187 throw new MojoExecutionException("An error occurred while testing translation-library '" + 188 this.translationName + '\'', 189 ExceptionUtils.getRootCause(throwable)); 190 } 191 } 192 } 193 else 194 { 195 this.getLog().info("Skipping translation-library tests"); 196 } 197 } 198 199 /** 200 * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getProject() 201 */ 202 protected MavenProject getProject() 203 { 204 return this.project; 205 } 206 207 /** 208 * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getPropertyFiles() 209 */ 210 protected List<String> getPropertyFiles() 211 { 212 return this.propertyFiles; 213 } 214 215 /** 216 * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getSettings() 217 */ 218 protected Settings getSettings() 219 { 220 return this.settings; 221 } 222 223 /** 224 * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getFactory() 225 */ 226 protected ArtifactFactory getFactory() 227 { 228 return this.factory; 229 } 230 231 /** 232 * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getPlugins() 233 */ 234 protected List<Plugin> getPlugins() 235 { 236 return this.plugins; 237 } 238 239 /** 240 * @see org.andromda.maven.plugin.configuration.AbstractConfigurationMojo#getLocalRepository() 241 */ 242 protected ArtifactRepository getLocalRepository() 243 { 244 return this.localRepository; 245 } 246}