001    /*
002     * Copyright 2005 Stephen J. McConnell
003     *
004     * Licensed  under the  Apache License,  Version 2.0  (the "License");
005     * you may not use  this file  except in  compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed  under the  License is distributed on an "AS IS" BASIS,
012     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
013     * implied.
014     *
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.tools.tasks;
020    
021    import java.io.File;
022    
023    import net.dpml.library.info.Scope;
024    
025    import net.dpml.tools.Context;
026    
027    import org.apache.tools.ant.Project;
028    import org.apache.tools.ant.types.Path;
029    
030    /**
031     * Cleanup of generated target directory.
032     *
033     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
034     * @version 1.1.3
035     */
036    public class TestTask extends GenericTask
037    {
038       /**
039        * Compiles main and test classes resulting in the creation of the 
040        * target/classes/main and target/classes/test directories.
041        */
042        public void execute()
043        {
044            Context context = getContext();
045            Project project = context.getProject();
046            File src = context.getTargetBuildTestDirectory();
047            if( src.exists() )
048            {
049                Path path = context.getPath( Scope.TEST  );
050                final File jar = getJarFile( context );
051                if( jar.exists() )
052                {
053                    path.createPathElement().setLocation( jar );
054                }
055                else if( context.getTargetClassesMainDirectory().exists() )
056                {
057                    File main = context.getTargetClassesMainDirectory();
058                    path.createPathElement().setLocation( main );
059                }
060                final File dest = context.getTargetClassesTestDirectory();
061                path.createPathElement().setLocation( dest );
062                final JUnitTestTask task = new JUnitTestTask();
063                task.setProject( project );
064                task.setTaskName( "junit" );
065                task.setSrc( src );
066                task.setClasspath( path );
067                task.init();
068                task.execute();
069            }
070        }
071        
072        private File getJarFile( Context context )
073        {
074            Project project = context.getProject();
075            File deliverables = context.getTargetDeliverablesDirectory();
076            File jars = new File( deliverables, "jars" );
077            String filename = context.getLayoutFilename( "jar" );
078            return new File( jars, filename );
079        }
080    }