/**
* Copyright (C) 2008-2010, Squale Project - http://www.squale.org
*
* This file is part of Squale.
*
* Squale is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or any later version.
*
* Squale is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Squale. If not, see <http://www.gnu.org/licenses/>.
*/
package org.squale.squalix.tools.checkstyle;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import junit.framework.TestCase;
import org.squale.jraf.commons.exception.JrafDaoException;
import org.squale.squalecommon.SqualeTestCase;
import org.squale.squalecommon.daolayer.component.ProjectDAOImpl;
import org.squale.squalecommon.daolayer.component.ProjectParameterDAOImpl;
import org.squale.squalecommon.daolayer.result.rulechecking.RuleCheckingTransgressionDAOImpl;
import org.squale.squalecommon.datatransfertobject.rulechecking.CheckstyleDTO;
import org.squale.squalecommon.enterpriselayer.businessobject.component.ApplicationBO;
import org.squale.squalecommon.enterpriselayer.businessobject.component.AuditBO;
import org.squale.squalecommon.enterpriselayer.businessobject.component.ProjectBO;
import org.squale.squalecommon.enterpriselayer.businessobject.component.parameters.ListParameterBO;
import org.squale.squalecommon.enterpriselayer.businessobject.component.parameters.MapParameterBO;
import org.squale.squalecommon.enterpriselayer.businessobject.component.parameters.ParametersConstants;
import org.squale.squalecommon.enterpriselayer.businessobject.component.parameters.StringParameterBO;
import org.squale.squalecommon.enterpriselayer.businessobject.result.IntegerMetricBO;
import org.squale.squalecommon.enterpriselayer.businessobject.result.rulechecking.RuleCheckingTransgressionBO;
import org.squale.squalecommon.enterpriselayer.businessobject.rule.QualityGridBO;
import org.squale.squalecommon.enterpriselayer.facade.checkstyle.CheckstyleFacade;
import org.squale.squalix.core.AbstractTask;
import org.squale.squalix.core.TaskData;
import org.squale.squalix.tools.ruleschecking.RulesCheckingTask;
/**
* Test pour la t�che checkstyle.
*/
public class RulesCheckingTaskTest
extends SqualeTestCase
{
/** Projet */
private ProjectBO sp = null;
/** Audit */
private AuditBO audit = null;
/** Application */
private ApplicationBO appl = null;
/** Param�tres temporaires */
private TaskData mDatas = new TaskData();
/**
* Chemin absolu vers la vue.
*/
private static final String VIEW_PATH = ".";
/**
* Chemin relatif vers les sources depuis la racine de la vue.
*/
private static final String SRC_DIR = "data/samples/testBatch/testbatch/";
/**
* @see TestCase#setUp()
*/
protected void setUp()
throws Exception
{
super.setUp();
getSession().beginTransaction();
InputStream stream = new FileInputStream( new File( "data/checkstyle/checkstyle_parsing.xml" ) );
StringBuffer errors = new StringBuffer();
// parsing du contenu du fichier
CheckstyleDTO versionRes = CheckstyleFacade.importCheckstyleConfFile( stream, errors );
appl = getComponentFactory().createApplication( getSession() );
QualityGridBO grid = getComponentFactory().createGrid( getSession() );
sp = getComponentFactory().createProject( getSession(), appl, grid );
mDatas.putData( TaskData.VIEW_PATH, VIEW_PATH );
MapParameterBO projectMap = new MapParameterBO();
StringParameterBO ck = new StringParameterBO();
ck.setValue( versionRes.getName() );
projectMap.getParameters().put( ParametersConstants.CHECKSTYLE_RULESET_NAME, ck );
StringParameterBO dialect = new StringParameterBO();
dialect.setValue( ParametersConstants.JAVA1_4 );
projectMap.getParameters().put( ParametersConstants.DIALECT, dialect );
List paths = new ArrayList( 0 );
StringParameterBO src = new StringParameterBO();
src.setValue( SRC_DIR );
paths.add( src );
ListParameterBO srcs = new ListParameterBO();
srcs.setParameters( paths );
projectMap.getParameters().put( ParametersConstants.SOURCES, srcs );
ProjectParameterDAOImpl.getInstance().create( getSession(), projectMap );
sp.setParameters( projectMap );
ProjectDAOImpl.getInstance().save( getSession(), sp );
audit = getComponentFactory().createAudit( getSession(), sp );
// On fait le commit pour permettre l'acc�s aux donn�es dans une autre session
getSession().commitTransactionWithoutClose();
}
/**
* V�rifie la correcte ex�cution de la t�che Checkstyle.
*/
public void testRun()
{
RulesCheckingTask task = new RulesCheckingTask();
task.setAuditId( new Long( audit.getId() ) );
task.setProjectId( new Long( sp.getId() ) );
task.setApplicationId( new Long( appl.getId() ) );
task.setStatus( AbstractTask.NOT_ATTEMPTED );
task.setData( mDatas );
task.run();
assertEquals( "t�che termin�e correctement", AbstractTask.TERMINATED, task.getStatus() );
try
{
Collection coll =
RuleCheckingTransgressionDAOImpl.getInstance().load( getSession(), new Long( sp.getId() ),
new Long( audit.getId() ) );
assertEquals( 1, coll.size() );
Iterator it = coll.iterator();
Map metrics = ( (RuleCheckingTransgressionBO) it.next() ).getMetrics();
final int errorsNb = 21;
assertEquals( "il y a 21 transgressions", errorsNb, metrics.size() );
IntegerMetricBO metric = (IntegerMetricBO) metrics.get( "COM01" );
assertNotNull( "on r�cup�re la r�gle de code COM01", metric );
final int errorsCOM01 = 11;
assertEquals( "il y a 11 transgressions de la r�gle COM01", errorsCOM01,
( (Integer) metric.getValue() ).intValue() );
}
catch ( JrafDaoException e )
{
e.printStackTrace();
fail( "unexpected exception" );
}
}
}