/** * 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.squalecommon.daolayer.result; import java.util.Collection; import java.util.List; import org.squale.jraf.commons.exception.JrafDaoException; import org.squale.jraf.spi.persistence.ISession; import org.squale.squalecommon.SqualeTestCase; import org.squale.squalecommon.daolayer.rule.QualityRuleDAOImpl; import org.squale.squalecommon.enterpriselayer.businessobject.component.AbstractComponentBO; import org.squale.squalecommon.enterpriselayer.businessobject.component.ApplicationBO; import org.squale.squalecommon.enterpriselayer.businessobject.component.AuditBO; import org.squale.squalecommon.enterpriselayer.businessobject.component.ComponentType; import org.squale.squalecommon.enterpriselayer.businessobject.component.PackageBO; import org.squale.squalecommon.enterpriselayer.businessobject.component.ProjectBO; import org.squale.squalecommon.enterpriselayer.businessobject.result.MarkBO; import org.squale.squalecommon.enterpriselayer.businessobject.result.PracticeResultBO; import org.squale.squalecommon.enterpriselayer.businessobject.rule.PracticeRuleBO; import org.squale.squalecommon.util.mapping.Mapping; /** * @author M400843 */ public class MarkDAOImplTest extends SqualeTestCase { /** Nombre de filtres possibles */ private static final int NB_FILTER = 3; /** l'application */ private ApplicationBO mAppli; /** le projet */ private ProjectBO mProject; /** le package qui sert d'exemple de component */ private PackageBO mPkg; /** l'audit */ private AuditBO mAudit; /** les regles */ private PracticeRuleBO mRule; /** la note */ private MarkBO mMark; /** le r�sultat de la pratique */ private PracticeResultBO mPracticeResult; /** le DAOImpl pour manipuler des notes */ private MarkDAOImpl mMarkDAO; /** * (non-Javadoc) * * @see junit.framework.TestCase#setUp() */ public void setUp() throws Exception { super.setUp(); ISession session = getSession(); session.beginTransaction(); mMarkDAO = MarkDAOImpl.getInstance(); mAppli = getComponentFactory().createApplication( session ); mProject = getComponentFactory().createProject( session, mAppli, null ); mPkg = getComponentFactory().createPackage( session, mProject ); mAudit = getComponentFactory().createAudit( session, mProject ); mRule = new PracticeRuleBO(); QualityRuleDAOImpl.getInstance().create( session, mRule ); mPracticeResult = new PracticeResultBO(); mPracticeResult.setRule( mRule ); mPracticeResult.setProject( mProject ); mPracticeResult.setAudit( mAudit ); QualityResultDAOImpl.getInstance().create( session, mPracticeResult ); mMark = new MarkBO(); mMark.setComponent( mPkg ); mMark.setPractice( mPracticeResult ); mMark.setValue( 2 ); session.commitTransactionWithoutClose(); } /** * @see MarkDAOImpl#removeWhereComponent(ISession, AbstractComponentBO) */ public void testRemoveWhereComponent() throws JrafDaoException { ISession session = getSession(); session.beginTransaction(); mMarkDAO.create( session, mMark ); List col = mMarkDAO.findWhere( session, "where markbo.component.id = " + mPkg.getId() ); assertEquals( 1, col.size() ); mMarkDAO.removeWhereComponent( session, mPkg ); col = mMarkDAO.findWhere( session, "where markbo.component.id = " + mPkg.getId() ); session.commitTransactionWithoutClose(); assertTrue( col.isEmpty() ); } /** * @see MarkDAOImpl#removeWhereProject(ISession pSession, ProjectBO pProject) */ public void testRemoveWhereProject() throws JrafDaoException { ISession session = getSession(); session.beginTransaction(); mMark.getPractice().setProject( mProject ); mMark.getPractice().setAudit( mAudit ); mMark.getPractice().setRule( mRule ); mMarkDAO.create( session, mMark ); List col = mMarkDAO.findWhere( session, "where markbo.practice.project.id = " + mProject.getId() ); assertEquals( 1, col.size() ); mMarkDAO.removeWhereProject( session, mProject ); col = mMarkDAO.findWhere( session, "where markbo.practice.project.id = " + mProject.getId() ); session.commitTransactionWithoutClose(); assertTrue( col.isEmpty() ); } /** * Teste la remont�e du nombre de corrections pour le calcul du ROI lorsque correction=composant qui passe de 0 � > * 0. * * @throws JrafDaoException si erreur */ public void testFindProgressions() throws JrafDaoException { getSession().beginTransaction(); // On cr�e un nouvel audit AuditBO audit = getComponentFactory().createAudit( getSession(), mProject ); // une nouvelle pratique PracticeResultBO practice = new PracticeResultBO(); practice.setRule( mRule ); practice.setProject( mProject ); practice.setAudit( audit ); QualityResultDAOImpl.getInstance().create( getSession(), practice ); MarkBO mark = new MarkBO(); mark.setComponent( mPkg ); mark.setPractice( practice ); // On met la note � z�ro pour qu'il y ait une progression entre les audits mark.setValue( 0 ); mMarkDAO.create( getSession(), mark ); mMarkDAO.create( getSession(), mMark ); // On va r�cup�rer la progression int nbProgressions = mMarkDAO.findCorrectionsWithProgessions( getSession(), new Long( mAudit.getId() ), new Long( audit.getId() ) ); getSession().commitTransactionWithoutClose(); assertEquals( 1, nbProgressions ); } /** * Teste la remont�e du nombre de corrections pour le calcul du ROI lorsque correction=composant qui �tait � 0 et * qui n'existe plus. * * @throws JrafDaoException si erreur */ public void testFindSuppressions() throws JrafDaoException { ISession session = getSession(); session.beginTransaction(); // La note �tait � z�ro pour le package mPackage mMark.setValue( 0 ); mMark.getPractice().setRule( mRule ); mMarkDAO.create( session, mMark ); // On cr�e un nouvel audit sans composant AuditBO audit = getComponentFactory().createAudit( getSession(), mProject ); // On va r�cup�rer la suppression int nbProgressions = mMarkDAO.findCorrectionsWithSuppressions( getSession(), new Long( audit.getId() ), new Long( mAudit.getId() ) ); getSession().commitTransactionWithoutClose(); assertEquals( 1, nbProgressions ); } /** * Teste la m�thode findWhere * * @throws JrafDaoException si erreur */ public void testFindWhere() throws JrafDaoException { getSession().beginTransaction(); // On cr�e une nouvelle note MarkBO mark = new MarkBO(); // La note est sur un nouveau package PackageBO newPkg = getComponentFactory().createPackage( getSession(), mProject ); mark.setComponent( newPkg ); mark.setPractice( mPracticeResult ); // On met une note qui doit �tre accept� avec r�serve final float between = 1.25698f; mark.setValue( between ); mMarkDAO.create( getSession(), mark ); mMarkDAO.create( getSession(), mMark ); Long auditId = new Long( mAudit.getId() ); Long projectId = new Long( mProject.getId() ); Long treId = new Long( mPracticeResult.getRule().getId() ); Integer pIndex = new Integer( PracticeResultBO.NEARLY_ACCEPTED_MIN ); Integer max = new Integer( 2 ); /* On r�cup�re les composants accept�s avec r�serve */ Collection components = mMarkDAO.findWhere( getSession(), auditId, projectId, treId, pIndex, max ); // Il doit y en avoir un (newPkg) assertEquals( 1, components.size() ); MarkBO nearlyAccepted = (MarkBO) components.iterator().next(); assertEquals( newPkg.getId(), nearlyAccepted.getComponent().getId() ); /* On r�cup�re les composants accept�s */ pIndex = new Integer( PracticeResultBO.ACCEPTED_MIN ); Collection accepetedComponents = mMarkDAO.findWhere( getSession(), auditId, projectId, treId, pIndex, max ); // Il doit y en avoir un (mPkg) assertEquals( 1, accepetedComponents.size() ); MarkBO accepted = (MarkBO) accepetedComponents.iterator().next(); assertEquals( mPkg.getId(), accepted.getComponent().getId() ); } /** * Teste la r�cup�ration des nouveaux composants entre deux audits * * @throws JrafDaoException si erreur */ public void testFindNewComponents() throws JrafDaoException { getSession().beginTransaction(); // On cr�e un nouvel audit AuditBO audit = getComponentFactory().createAudit( getSession(), mProject ); // une nouvelle pratique PracticeResultBO practice = new PracticeResultBO(); practice.setRule( mRule ); practice.setProject( mProject ); practice.setAudit( audit ); QualityResultDAOImpl.getInstance().create( getSession(), practice ); MarkBO mark = new MarkBO(); // La note est sur un nouveau package PackageBO newPkg = getComponentFactory().createPackage( getSession(), mProject ); mark.setComponent( newPkg ); mark.setPractice( practice ); // On cr�e les notes mMarkDAO.create( getSession(), mark ); mMarkDAO.create( getSession(), mMark ); getSession().commitTransactionWithoutClose(); // On va r�cup�rer les nouveaux packages entre mAudit et audit Collection t1 = mMarkDAO.findWhere( getSession(), new Long( mPkg.getId() ), new Long( mAudit.getId() ) ); Collection t2 = mMarkDAO.findWhere( getSession(), new Long( newPkg.getId() ), new Long( audit.getId() ) ); String type = Mapping.getComponentClass( ComponentType.PACKAGE ).getName(); Collection pkgs = mMarkDAO.findDeletedComponents( getSession(), new Long( audit.getId() ), new Long( mAudit.getId() ), new Long( mProject.getId() ), new Object[NB_FILTER], 1 ); // Il doit y avoir un seul nouveau package (newPkg) assertEquals( 1, pkgs.size() ); } /** * Teste la r�cup�ration des notes diff�rentes entre deux audits pour un m�me composant et une m�me pratique * * @throws JrafDaoException si erreur */ public void testFindChangedComponent() throws JrafDaoException { getSession().beginTransaction(); // On cr�e un nouvel audit AuditBO audit = getComponentFactory().createAudit( getSession(), mProject ); // une nouvelle pratique PracticeResultBO practice = new PracticeResultBO(); practice.setRule( mRule ); practice.setProject( mProject ); practice.setAudit( audit ); QualityResultDAOImpl.getInstance().create( getSession(), practice ); MarkBO mark = new MarkBO(); mark.setComponent( mPkg ); mark.setPractice( practice ); // On met la note � z�ro pour qu'il y ait une modification entre les audits mark.setValue( 0 ); mMarkDAO.create( getSession(), mark ); mMarkDAO.create( getSession(), mMark ); // On va r�cup�rer la modification String type = Mapping.getComponentClass( ComponentType.PACKAGE ).getName(); Collection pkgs = mMarkDAO.findChangedComponentWhere( getSession(), new Long( mAudit.getId() ), new Long( audit.getId() ), new Long( mProject.getId() ), new Object[NB_FILTER], 1 ); getSession().commitTransactionWithoutClose(); assertEquals( 1, pkgs.size() ); } }