View Javadoc

1   /*
2    *   Copyright (c) 2009 The JOMC Project
3    *   Copyright (c) 2005 Christian Schulte <cs@jomc.org>
4    *   All rights reserved.
5    *
6    *   Redistribution and use in source and binary forms, with or without
7    *   modification, are permitted provided that the following conditions
8    *   are met:
9    *
10   *     o Redistributions of source code must retain the above copyright
11   *       notice, this list of conditions and the following disclaimer.
12   *
13   *     o Redistributions in binary form must reproduce the above copyright
14   *       notice, this list of conditions and the following disclaimer in
15   *       the documentation and/or other materials provided with the
16   *       distribution.
17   *
18   *   THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS"
19   *   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20   *   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21   *   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR
22   *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23   *   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24   *   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25   *   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26   *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27   *   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28   *   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   *
30   *   $Id: TestJavaSourcesMojo.java 1397 2010-01-27 20:59:48Z schulte2005 $
31   *
32   */
33  package org.jomc.mojo;
34  
35  import java.io.File;
36  import java.util.ResourceBundle;
37  import java.util.logging.Level;
38  import javax.xml.bind.JAXBContext;
39  import javax.xml.bind.util.JAXBSource;
40  import org.apache.maven.plugin.MojoExecutionException;
41  import org.jomc.model.ModelContext;
42  import org.jomc.model.ModelValidationReport;
43  import org.jomc.model.Module;
44  import org.jomc.model.ObjectFactory;
45  import org.jomc.tools.JavaSources;
46  
47  /**
48   * Manages a projects' test java sources.
49   *
50   * @author <a href="mailto:cs@jomc.org">Christian Schulte</a>
51   * @version $Id: TestJavaSourcesMojo.java 1397 2010-01-27 20:59:48Z schulte2005 $
52   *
53   * @phase process-test-resources
54   * @goal test-java-sources
55   * @requiresDependencyResolution test
56   */
57  public final class TestJavaSourcesMojo extends AbstractJomcMojo
58  {
59  
60      /** Constant for the name of the tool backing the mojo. */
61      private static final String TOOLNAME = "JavaSources";
62  
63      /** Creates a new {@code TestJavaSourcesMojo} instance. */
64      public TestJavaSourcesMojo()
65      {
66          super();
67      }
68  
69      @Override
70      protected void executeTool() throws Exception
71      {
72          if ( this.isSourceProcessingEnabled() )
73          {
74              File testSourceDirectory = new File( this.getMavenProject().getBuild().getTestSourceDirectory() );
75  
76              if ( !testSourceDirectory.isAbsolute() )
77              {
78                  testSourceDirectory = new File( this.getMavenProject().getBasedir(),
79                                                  this.getMavenProject().getBuild().getTestSourceDirectory() );
80  
81              }
82  
83              final ModelContext context = this.getModelContext( this.getTestClassLoader() );
84              final JavaSources tool = this.getJavaSourcesTool( context );
85              final JAXBContext jaxbContext = context.createContext();
86  
87              final ModelValidationReport validationReport = context.validateModel( new JAXBSource(
88                  jaxbContext, new ObjectFactory().createModules( tool.getModules() ) ) );
89  
90              this.log( context, validationReport.isModelValid() ? Level.INFO : Level.SEVERE, validationReport );
91  
92              if ( validationReport.isModelValid() )
93              {
94                  this.logSeparator( Level.INFO );
95                  final Module module = tool.getModules().getModule( this.getJomcTestModuleName() );
96  
97                  if ( module != null )
98                  {
99                      this.logProcessingModule( TOOLNAME, module.getName() );
100                     tool.manageSources( module, testSourceDirectory );
101                     this.logToolSuccess( TOOLNAME );
102                 }
103                 else
104                 {
105                     this.logMissingModule( this.getJomcTestModuleName() );
106                 }
107 
108                 this.logSeparator( Level.INFO );
109             }
110             else
111             {
112                 throw new MojoExecutionException( getMessage( "failed" ) );
113             }
114         }
115         else
116         {
117             this.logSeparator( Level.INFO );
118             this.log( Level.INFO, getMessage( "disabled" ), null );
119             this.logSeparator( Level.INFO );
120         }
121     }
122 
123     private static String getMessage( final String key )
124     {
125         return ResourceBundle.getBundle( TestJavaSourcesMojo.class.getName().replace( '.', '/' ) ).getString( key );
126     }
127 
128 }