EMMA Coverage Report (generated Thu Mar 15 07:05:13 CET 2012)
[all classes][org.jomc.ant]

COVERAGE SUMMARY FOR SOURCE FILE [ValidateClassesTask.java]

nameclass, %method, %block, %line, %
ValidateClassesTask.java100% (1/1)100% (6/6)80%  (258/324)86%  (56.6/66)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ValidateClassesTask100% (1/1)100% (6/6)80%  (258/324)86%  (56.6/66)
clone (): ValidateClassesTask 100% (1/1)63%  (12/19)88%  (2.6/3)
processClassFiles (): void 100% (1/1)79%  (228/287)84%  (46/55)
ValidateClassesTask (): void 100% (1/1)100% (3/3)100% (2/2)
getClassesDirectory (): File 100% (1/1)100% (3/3)100% (1/1)
preExecuteTask (): void 100% (1/1)100% (8/8)100% (3/3)
setClassesDirectory (File): void 100% (1/1)100% (4/4)100% (2/2)

1/*
2 *   Copyright (C) Christian Schulte, 2005-206
3 *   All rights reserved.
4 *
5 *   Redistribution and use in source and binary forms, with or without
6 *   modification, are permitted provided that the following conditions
7 *   are met:
8 *
9 *     o Redistributions of source code must retain the above copyright
10 *       notice, this list of conditions and the following disclaimer.
11 *
12 *     o Redistributions in binary form must reproduce the above copyright
13 *       notice, this list of conditions and the following disclaimer in
14 *       the documentation and/or other materials provided with the
15 *       distribution.
16 *
17 *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19 *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20 *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 *   $JOMC: ValidateClassesTask.java 4174 2012-01-15 09:30:01Z schulte2005 $
29 *
30 */
31package org.jomc.ant;
32 
33import java.io.File;
34import java.io.IOException;
35import java.util.logging.Level;
36import javax.xml.bind.JAXBContext;
37import javax.xml.bind.JAXBException;
38import javax.xml.bind.util.JAXBSource;
39import javax.xml.transform.Source;
40import org.apache.tools.ant.BuildException;
41import org.jomc.model.Implementation;
42import org.jomc.model.Module;
43import org.jomc.model.Specification;
44import org.jomc.modlet.Model;
45import org.jomc.modlet.ModelContext;
46import org.jomc.modlet.ModelException;
47import org.jomc.modlet.ModelValidationReport;
48import org.jomc.modlet.ObjectFactory;
49import org.jomc.tools.ClassFileProcessor;
50 
51/**
52 * Task for validating class file model objects.
53 *
54 * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a>
55 * @version $JOMC: ValidateClassesTask.java 4174 2012-01-15 09:30:01Z schulte2005 $
56 */
57public final class ValidateClassesTask extends ClassFileProcessorTask
58{
59 
60    /** The directory holding the class files to validate model objects of. */
61    private File classesDirectory;
62 
63    /** Creates a new {@code ValidateClassesTask} instance. */
64    public ValidateClassesTask()
65    {
66        super();
67    }
68 
69    /**
70     * Gets the directory holding the class files to validate model objects of.
71     *
72     * @return The directory holding the class files to validate model objects of or {@code null}.
73     *
74     * @see #setClassesDirectory(java.io.File)
75     */
76    public File getClassesDirectory()
77    {
78        return this.classesDirectory;
79    }
80 
81    /**
82     * Sets the directory holding the class files to validate model objects of.
83     *
84     * @param value The new directory holding the class files to validate model objects of or {@code null}.
85     *
86     * @see #getClassesDirectory()
87     */
88    public void setClassesDirectory( final File value )
89    {
90        this.classesDirectory = value;
91    }
92 
93    /** {@inheritDoc} */
94    @Override
95    public void preExecuteTask() throws BuildException
96    {
97        super.preExecuteTask();
98 
99        this.assertNotNull( "classesDirectory", this.getClassesDirectory() );
100    }
101 
102    /**
103     * Validates class file model objects.
104     *
105     * @throws BuildException if validating class file model objects fails.
106     */
107    @Override
108    public void processClassFiles() throws BuildException
109    {
110        ProjectClassLoader classLoader = null;
111        boolean suppressExceptionOnClose = true;
112 
113        try
114        {
115            this.log( Messages.getMessage( "validatingModelObjects", this.getModel() ) );
116 
117            classLoader = this.newProjectClassLoader();
118            final ModelContext context = this.newModelContext( classLoader );
119            final ClassFileProcessor tool = this.newClassFileProcessor();
120            final JAXBContext jaxbContext = context.createContext( this.getModel() );
121            final Model model = this.getModel( context );
122            final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( model ) );
123            ModelValidationReport validationReport = context.validateModel( this.getModel(), source );
124 
125            this.logValidationReport( context, validationReport );
126            tool.setModel( model );
127 
128            if ( validationReport.isModelValid() )
129            {
130                final Specification s = this.getSpecification( model );
131                final Implementation i = this.getImplementation( model );
132                final Module m = this.getModule( model );
133 
134                if ( s != null )
135                {
136                    validationReport = tool.validateModelObjects( s, context, this.getClassesDirectory() );
137                    this.logValidationReport( context, validationReport );
138 
139                    if ( !validationReport.isModelValid() )
140                    {
141                        throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
142                    }
143                }
144 
145                if ( i != null )
146                {
147                    validationReport = tool.validateModelObjects( i, context, this.getClassesDirectory() );
148                    this.logValidationReport( context, validationReport );
149 
150                    if ( !validationReport.isModelValid() )
151                    {
152                        throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
153                    }
154                }
155 
156 
157                if ( m != null )
158                {
159                    validationReport = tool.validateModelObjects( m, context, this.getClassesDirectory() );
160                    this.logValidationReport( context, validationReport );
161 
162                    if ( !validationReport.isModelValid() )
163                    {
164                        throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
165                    }
166                }
167 
168                if ( this.isModulesProcessingRequested() )
169                {
170                    validationReport = tool.validateModelObjects( context, this.getClassesDirectory() );
171                    this.logValidationReport( context, validationReport );
172 
173                    if ( !validationReport.isModelValid() )
174                    {
175                        throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
176                    }
177                }
178 
179                suppressExceptionOnClose = false;
180            }
181            else
182            {
183                throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
184            }
185        }
186        catch ( final IOException e )
187        {
188            throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() );
189        }
190        catch ( final JAXBException e )
191        {
192            throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() );
193        }
194        catch ( final ModelException e )
195        {
196            throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() );
197        }
198        finally
199        {
200            try
201            {
202                if ( classLoader != null )
203                {
204                    classLoader.close();
205                }
206            }
207            catch ( final IOException e )
208            {
209                if ( suppressExceptionOnClose )
210                {
211                    this.logMessage( Level.SEVERE, Messages.getMessage( e ), e );
212                }
213                else
214                {
215                    throw new ClassProcessingException( Messages.getMessage( e ), e, this.getLocation() );
216                }
217            }
218        }
219    }
220 
221    /** {@inheritDoc} */
222    @Override
223    public ValidateClassesTask clone()
224    {
225        final ValidateClassesTask clone = (ValidateClassesTask) super.clone();
226        clone.classesDirectory =
227            this.classesDirectory != null ? new File( this.classesDirectory.getAbsolutePath() ) : null;
228 
229        return clone;
230    }
231 
232}

[all classes][org.jomc.ant]
EMMA 2.1.5320 (stable) (C) Vladimir Roubtsov