Package org.apache.maven.continuum.buildcontroller

Source Code of org.apache.maven.continuum.buildcontroller.DefaultBuildControllerTest

package org.apache.maven.continuum.buildcontroller;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import org.apache.continuum.dao.BuildDefinitionDao;
import org.apache.continuum.dao.BuildResultDao;
import org.apache.maven.continuum.AbstractContinuumTest;
import org.apache.maven.continuum.execution.ContinuumBuildExecutorConstants;
import org.apache.maven.continuum.model.project.BuildDefinition;
import org.apache.maven.continuum.model.project.BuildResult;
import org.apache.maven.continuum.model.project.Project;
import org.apache.maven.continuum.model.project.ProjectDependency;
import org.apache.maven.continuum.model.scm.ScmResult;
import org.apache.maven.continuum.project.ContinuumProjectState;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;

public class DefaultBuildControllerTest
    extends AbstractContinuumTest
{
    private DefaultBuildController controller;

    int projectId1;

    int projectId2;

    int buildDefinitionId1;

    int buildDefinitionId2;

    public void setUp()
        throws Exception
    {
        super.setUp();

        BuildDefinitionDao buildDefinitionDao = (BuildDefinitionDao) lookup( BuildDefinitionDao.class.getName() );

        BuildResultDao buildResultDao = (BuildResultDao) lookup( BuildResultDao.class.getName() );

        Project project1 = createProject( "project1" );
        BuildDefinition bd1 = createBuildDefinition();
        project1.addBuildDefinition( bd1 );
        project1.setState( ContinuumProjectState.OK );
        projectId1 = addProject( project1 ).getId();
        buildDefinitionId1 = buildDefinitionDao.getDefaultBuildDefinition( projectId1 ).getId();
        project1 = getProjectDao().getProject( projectId1 );
        BuildResult buildResult1 = new BuildResult();
        buildResult1.setStartTime( Calendar.getInstance().getTimeInMillis() );
        buildResult1.setEndTime( Calendar.getInstance().getTimeInMillis() );
        buildResult1.setState( ContinuumProjectState.OK );
        buildResult1.setSuccess( true );
        buildResult1.setBuildDefinition( bd1 );
        buildResultDao.addBuildResult( project1, buildResult1 );
        BuildResult buildResult2 = new BuildResult();
        buildResult2.setStartTime( Calendar.getInstance().getTimeInMillis() - 7200000 );
        buildResult2.setEndTime( Calendar.getInstance().getTimeInMillis() - 7200000 );
        buildResult2.setSuccess( true );
        buildResult2.setState( ContinuumProjectState.OK );
        buildResult2.setBuildDefinition( bd1 );
        buildResultDao.addBuildResult( project1, buildResult2 );
        createPomFile( getProjectDao().getProjectWithAllDetails( projectId1 ) );

        Project project2 = createProject( "project2" );
        ProjectDependency dep1 = new ProjectDependency();
        dep1.setGroupId( "org.apache.maven.testproject" );
        dep1.setArtifactId( "project1" );
        dep1.setVersion( "1.0-SNAPSHOT" );
        project2.addDependency( dep1 );
        ProjectDependency dep2 = new ProjectDependency();
        dep2.setGroupId( "junit" );
        dep2.setArtifactId( "junit" );
        dep2.setVersion( "3.8.1" );
        project2.addDependency( dep2 );
        BuildDefinition bd2 = createBuildDefinition();
        project2.addBuildDefinition( bd2 );
        project2.setState( ContinuumProjectState.OK );
        projectId2 = addProject( project2 ).getId();
        buildDefinitionId2 = buildDefinitionDao.getDefaultBuildDefinition( projectId2 ).getId();
        createPomFile( getProjectDao().getProjectWithAllDetails( projectId2 ) );
       
        controller = (DefaultBuildController) lookup( BuildController.ROLE );
    }

    private Project createProject( String artifactId )
    {
        Project project = new Project();
        project.setExecutorId( ContinuumBuildExecutorConstants.MAVEN_TWO_BUILD_EXECUTOR );
        project.setName( artifactId );
        project.setGroupId( "org.apache.maven.testproject" );
        project.setArtifactId( artifactId );
        project.setVersion( "1.0-SNAPSHOT" );
        return project;
    }

    private BuildDefinition createBuildDefinition()
    {
        BuildDefinition builddef = new BuildDefinition();
        builddef.setBuildFile( "pom.xml" );
        builddef.setGoals( "clean" );
        builddef.setDefaultForProject( true );
        return builddef;
    }

    private BuildContext getContext()
        throws Exception
    {
        return controller.initializeBuildContext( projectId2, buildDefinitionId2,
                                                  ContinuumProjectState.TRIGGER_SCHEDULED, new ScmResult() );
    }

    private BuildContext getContext( int hourOfLastExecution )
        throws Exception
    {
        BuildContext context = getContext();
        BuildResult oldBuildResult = new BuildResult();
        oldBuildResult.setEndTime( Calendar.getInstance().getTimeInMillis() + ( hourOfLastExecution * 3600000 ) );
        context.setOldBuildResult( oldBuildResult );
        context.setScmResult( new ScmResult() );
        return context;
    }

    public void testWithoutDependencyChanges()
        throws Exception
    {
        BuildContext context = getContext( +1 );
        controller.checkProjectDependencies( context );
        assertEquals( 0, context.getModifiedDependencies().size() );
        assertFalse( controller.shouldBuild( context ) );
    }

    public void testWithNewProjects()
        throws Exception
    {
        Project p1 = getProjectDao().getProject( projectId1 );
        p1.setState( ContinuumProjectState.NEW );
        getProjectDao().updateProject( p1 );

        Project p2 = getProjectDao().getProject( projectId2 );
        p2.setState( ContinuumProjectState.NEW );
        getProjectDao().updateProject( p2 );

        BuildContext context = getContext();
        controller.checkProjectDependencies( context );
        assertEquals( 0, context.getModifiedDependencies().size() );
        assertTrue( controller.shouldBuild( context ) );
    }

    public void testWithNewBuildDefinition()
        throws Exception
    {
        BuildContext context = getContext();
        assertNull( context.getOldBuildResult() );
        assertTrue( controller.shouldBuild( context ) );
    }

    public void testWithDependencyChanges()
        throws Exception
    {
        BuildContext context = getContext( -1 );
        controller.checkProjectDependencies( context );
        assertEquals( 1, context.getModifiedDependencies().size() );
        assertTrue( controller.shouldBuild( context ) );
    }

    private File getWorkingDirectory()
        throws Exception
    {
        File workingDirectory = getTestFile( "target/working-directory" );
       
        if ( !workingDirectory.exists() )
        {
            workingDirectory.mkdir();
        }
       
        return workingDirectory;
    }
   
    private File getWorkingDirectory( Project project )
        throws Exception
    {
        File projectDir = new File( getWorkingDirectory(), Integer.toString( project.getId() ) );

        if ( !projectDir.exists() )
        {
            projectDir.mkdirs();
            System.out.println( "projectdirectory created" + projectDir.getAbsolutePath() );
        }

        return projectDir;
    }
   
    private void createPomFile( Project project )
        throws Exception
    {
        File pomFile = new File( getWorkingDirectory( project ), "pom.xml" );
       
        BufferedWriter out = new BufferedWriter( new FileWriter( pomFile ) );
        out.write( "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" " +
               "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
               "xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">\n" );
        out.write( "<modelVersion>4.0.0</modelVersion>\n" );
        out.write( "<groupId>" + project.getGroupId() + "</groupId>\n" );
        out.write( "<artifactId>" + project.getArtifactId() + "</artifactId>\n" );
        out.write( "<version>" + project.getVersion() + "</version>\n" );
        out.write( "<scm>\n" );
        out.write( "<connection>" + "scm:local|" + getWorkingDirectory().getAbsolutePath() +
                   "|" + project.getId() + "</connection>\n" );
        out.write( "</scm>" );

        if ( project.getDependencies().size() > 0 )
        {
            out.write( "<dependencies>\n" );

            List<ProjectDependency> dependencies = project.getDependencies();

            for ( ProjectDependency dependency : dependencies )
            {
                out.write( "<dependency>\n" );
                out.write( "<groupId>" + dependency.getGroupId() + "</groupId>\n" );
                out.write( "<artifactId>" + dependency.getArtifactId() + "</artifactId>\n" );
                out.write( "<version>" + dependency.getVersion() + "</version>\n" );
                out.write( "</dependency>\n" );
            }
            out.write( "</dependencies>\n" );
        }

        out.write( "</project>" );
        out.close();
       
        System.out.println( "pom file created" );
    }
}
TOP

Related Classes of org.apache.maven.continuum.buildcontroller.DefaultBuildControllerTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.