View Javadoc
1   package org.andromda.maven.plugin.cartridge;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import junit.framework.Test;
6   import junit.framework.TestResult;
7   import org.andromda.core.common.ExceptionUtils;
8   import org.andromda.maven.plugin.AndroMDAMojo;
9   import org.apache.commons.io.FileUtils;
10  import org.apache.maven.plugin.MojoExecutionException;
11  import org.apache.maven.plugin.MojoFailureException;
12  
13  
14  /**
15   * Provides the ability to compare cartridge output with existing output.
16   *
17   * @phase generate-test-sources
18   * @goal test
19   * @requiresDependencyResolution test
20   * @description runs AndroMDA Cartridge tests
21   * @author Chad Brandon
22   * @author Bob Fields
23   */
24  public class CartridgeTestMojo
25      extends AbstractCartridgeTestMojo
26  {
27      /**
28       * @see org.apache.maven.plugin.Mojo#execute()
29       */
30      public void execute()
31          throws MojoExecutionException, MojoFailureException
32      {
33          if (!this.skip)
34          {
35              final File expectedOutputArchive = this.expectedOutputArchive;
36              if (!expectedOutputArchive.exists() || !expectedOutputArchive.isFile())
37              {
38                  if (this.testFailureIgnore)
39                  {
40                      this.getLog().error("The path specifying the expectedOutputArchive '" +
41                              this.expectedOutputArchive + "' must be a file");
42                  }
43                  else
44                  {
45                      throw new MojoExecutionException("The path specifying the expectedOutputArchive '" +
46                              this.expectedOutputArchive + "' must be a file");
47                  }
48              }
49              final File expectedOutputDir = this.expectedDirectory;
50              if (!expectedOutputDir.exists())
51              {
52                  expectedOutputDir.mkdirs();
53              }
54              else if (!expectedOutputDir.isDirectory())
55              {
56                  throw new MojoExecutionException("The path specifying the expectedDirectory '" +
57                      this.expectedDirectory + "' must be a directory");
58              }
59              else if (!this.lastModifiedCheck)
60              {
61                  try
62                  {
63                      FileUtils.cleanDirectory(expectedOutputDir);
64                  }
65                  catch (IOException ex)
66                  {
67                      throw new MojoExecutionException("Could not clean the expectedDirectory '" +
68                              this.expectedDirectory + "'");
69                  }
70              }
71  
72              final File actualOutputDir = this.actualDirectory;
73              if (!actualOutputDir.exists())
74              {
75                  actualOutputDir.mkdirs();
76              }
77              else if (!actualOutputDir.isDirectory())
78              {
79                  throw new MojoExecutionException("The path specifying the actualOutputDir '" +
80                      this.actualDirectory + "' must be a directory");
81              }
82              else if (!this.lastModifiedCheck)
83              {
84                  try
85                  {
86                      FileUtils.cleanDirectory(actualOutputDir);
87                  }
88                  catch (IOException ex)
89                  {
90                      throw new MojoExecutionException("Could not clean the actualOutputDir '" +
91                              this.actualDirectory + "'");
92                  }
93              }
94  
95              try
96              {
97                  this.getLog().info("-----------------------------------------------------------------------------");
98                  this.getLog().info("          A n d r o M D A   C a r t r i d g e   T e s t   S u i t e          ");
99                  this.getLog().info("-----------------------------------------------------------------------------");
100                 this.getLog().info("configurationUri=" + this.configurationUri);
101                 this.getLog().info("actualDirectory=" + this.actualDirectory);
102                 this.getLog().info("expectedDirectory=" + this.expectedDirectory);
103                 this.getLog().info("expectedOutputArchive=" + this.expectedOutputArchive);
104                 this.getLog().info("lastModifiedCheck=" + this.lastModifiedCheck);
105                 if (this.testFailureIgnore)
106                 {
107                     this.getLog().info("testFailureIgnore=" + this.testFailureIgnore);
108                 }
109 
110                 // - change scope of test dependencies to runtime
111                 // TODO Add test dependencies required to compile all of the generated source code, and run testCompile for all cartridges
112                 this.changeScopeForTestDependencies();
113                 // TODO Clear the error list, carried over from a previous cartridge run failure.
114 
115                 // - first run AndroMDA with the test configuration
116                 final AndroMDAMojo andromdaMojo = new AndroMDAMojo();
117                 andromdaMojo.setConfigurationUri(this.configurationUri);
118                 andromdaMojo.setProject(this.project);
119                 andromdaMojo.setSettings(this.settings);
120                 andromdaMojo.setPropertyFiles(this.propertyFiles);
121                 andromdaMojo.setLastModifiedCheck(this.lastModifiedCheck);
122                 andromdaMojo.setModelOutputHistory(new File(this.actualDirectory, ".."));
123                 // TODO This causes the build output expected directory to compile when running javadocs.
124                 //andromdaMojo.setBuildSourceDirectory(this.actualDirectory);
125                 andromdaMojo.execute();
126 
127                 // - unpack the expected output archive
128                 this.unpack(
129                     expectedOutputArchive,
130                     this.expectedDirectory);
131 
132                 final CartridgeTest cartridgeTest = CartridgeTest.instance();
133                 cartridgeTest.setActualOutputPath(this.actualDirectory.getAbsolutePath());
134                 cartridgeTest.setExpectedOutputPath(this.expectedDirectory.getAbsolutePath());
135                 cartridgeTest.setActualOutputDirectory(null);
136                 cartridgeTest.setExpectedOutputDirectory(null);
137                 cartridgeTest.setBinarySuffixes(this.binaryOutputSuffixes);
138 
139                 final CartridgeTestFormatter formatter = new CartridgeTestFormatter();
140 
141                 // - set the report location
142                 final File report = new File(this.reportDirectory, this.project.getArtifactId() + ".txt");
143                 formatter.setReportFile(report);
144                 formatter.setTestFailureIgnore(this.testFailureIgnore);
145                 final TestResult result = new TestResult();
146                 result.addListener(formatter);
147                 final Test suite = CartridgeTest.suite();
148                 formatter.startTestSuite(this.project.getName());
149                 suite.run(result);
150                 this.getLog().info("");
151                 this.getLog().info("Results:");
152                 this.getLog().info(formatter.endTestSuite(suite));
153                 cartridgeTest.shutdown();
154 
155                 if (result.failureCount() > 0 || result.errorCount() > 0)
156                 {
157                     if (this.testFailureIgnore)
158                     {
159                         this.getLog().error("There are test failures, failureCount=" + result.failureCount() + " errorCount=" + result.errorCount()
160                                 + ", Cartridge=" + this.project.getArtifactId());
161                     }
162                     else
163                     {
164                         throw new MojoExecutionException("There are test failures, failureCount=" + result.failureCount() + " errorCount=" + result.errorCount());
165                     }
166                 }
167             }
168             catch (final Throwable throwable)
169             {
170                 if (throwable instanceof MojoExecutionException && !this.testFailureIgnore)
171                 {
172                     throw (MojoExecutionException)throwable;
173                 }
174                 else if (this.testFailureIgnore)
175                 {
176                     this.getLog().error("An error occurred while testing cartridge '" +
177                         this.project.getArtifactId() + '\'',
178                         ExceptionUtils.getRootCause(throwable));
179                 }
180                 else
181                 {
182                     throw new MojoExecutionException("An error occurred while testing cartridge '" +
183                             this.project.getArtifactId() + '\'',
184                             ExceptionUtils.getRootCause(throwable));
185                 }
186             }
187         }
188         else
189         {
190             this.getLog().info("Skipping cartridge tests");
191         }
192     }
193 }