View Javadoc

1   /*
2    *  jDTAUS Core Container Mojo
3    *  Copyright (C) 2005 Christian Schulte
4    *  <cs@schulte.it>
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19   *
20   */
21  package org.jdtaus.core.container.mojo;
22  
23  import java.util.Locale;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.MojoFailureException;
26  import org.jdtaus.core.container.ContainerError;
27  import org.jdtaus.core.container.ContextError;
28  import org.jdtaus.core.container.Implementations;
29  import org.jdtaus.core.container.ModelError;
30  import org.jdtaus.core.container.Module;
31  import org.jdtaus.core.container.Specifications;
32  
33  /**
34   * Mojo to generate test java code.
35   *
36   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
37   * @version $JDTAUS: JavaContainerTestsMojo.java 8743 2012-10-07 03:06:20Z schulte $
38   * @goal java-container-tests
39   * @phase process-test-resources
40   * @requiresDependencyResolution test
41   */
42  public class JavaContainerTestsMojo extends JavaContainerMojo
43  {
44  
45      /** Creates a new {@code JavaContainerTestsMojo} instance. */
46      public JavaContainerTestsMojo()
47      {
48          super();
49      }
50  
51      /** {@inheritDoc} */
52      public void execute() throws MojoExecutionException, MojoFailureException
53      {
54          final ClassLoader mavenLoader = Thread.currentThread().
55              getContextClassLoader();
56  
57          try
58          {
59              this.model = null;
60  
61              Thread.currentThread().setContextClassLoader(
62                  this.getTestClassLoader( mavenLoader ) );
63  
64              enableThreadContextClassLoader();
65  
66              final Module testMod = this.getTestModule();
67  
68              if ( testMod != null )
69              {
70                  final Specifications specs = testMod.getSpecifications();
71                  final Implementations impls = testMod.getImplementations();
72  
73                  for ( int i = specs.size() - 1; i >= 0; i-- )
74                  {
75                      this.generateSpecification(
76                          this.getMavenProject().getTestCompileSourceRoots(),
77                          specs.getSpecification( i ) );
78  
79                  }
80  
81                  for ( int i = impls.size() - 1; i >= 0; i-- )
82                  {
83                      this.generateImplementation(
84                          this.getMavenProject().getTestCompileSourceRoots(),
85                          impls.getImplementation( i ) );
86  
87                  }
88  
89                  this.writeContainerReport( this.getModel(),
90                                             "container-tests-report.xml" );
91  
92                  this.getLog().info( JavaContainerMojoBundle.getInstance().
93                      getProcessingModuleMessage( Locale.getDefault(),
94                                                  testMod.getName() ) );
95  
96              }
97          }
98          catch ( final ContextError e )
99          {
100             throw new MojoExecutionException( e.getMessage(), e );
101         }
102         catch ( final ContainerError e )
103         {
104             throw new MojoExecutionException( e.getMessage(), e );
105         }
106         catch ( final ModelError e )
107         {
108             throw new MojoExecutionException( e.getMessage(), e );
109         }
110         catch ( final Exception e )
111         {
112             throw new MojoExecutionException( e.getMessage(), e );
113         }
114         finally
115         {
116             disableThreadContextClassLoader();
117             Thread.currentThread().setContextClassLoader( mavenLoader );
118         }
119     }
120 
121 }