r3020 - in trunk/pollen-business/src: main/java/org/chorem/pollen/service main/xmi test/java/org/chorem/pollen test/java/org/chorem/pollen/service test/java/org/chorem/pollen/test
Author: fdesbois Date: 2010-05-28 15:21:09 +0200 (Fri, 28 May 2010) New Revision: 3020 Url: http://chorem.org/repositories/revision/pollen/3020 Log: Evo #197 : Tests for saveVote Added: trunk/pollen-business/src/test/java/org/chorem/pollen/PollenUtilsTest.java Removed: trunk/pollen-business/src/test/java/org/chorem/pollen/TopiaQueryBuilderTest.java trunk/pollen-business/src/test/java/org/chorem/pollen/test/TestData.java Modified: trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceVoteImpl.java trunk/pollen-business/src/main/xmi/pollen.properties trunk/pollen-business/src/test/java/org/chorem/pollen/service/ServiceVoteImplTest.java trunk/pollen-business/src/test/java/org/chorem/pollen/test/AbstractServiceTest.java Modified: trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceVoteImpl.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceVoteImpl.java 2010-05-28 08:40:01 UTC (rev 3019) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceVoteImpl.java 2010-05-28 13:21:09 UTC (rev 3020) @@ -57,13 +57,28 @@ context.closeTransaction(transaction); } + /** + * Copy from account {@code source} to {@code destination}. The {@code + * destination} will be persisted. The anonymous case is manage, properties + * that identify the person are excluded from the copy in this case. + * + * @param source PollAccount to save + * @param destination PollAccount with up to date data to persist + * @see TopiaEntityBinder#copyExcluding(Object, Object, String...) + */ protected void copyPollAccount(PollAccount source, PollAccount destination) { + // Uid is the naturalId, don't copy it + String[] excludes = new String[] {PollAccount.UID}; - String[] excludes = null; - // Exclude properties for anonymous account if (source.isAnonymous()) { + if (log.isInfoEnabled()) { + log.info("Anonymous vote, doesn't copy participant's data : " + + "name, email, userAccount"); + } + excludes = new String[]{ + PollAccount.UID, PollAccount.NAME, PollAccount.EMAIL, PollAccount.USER_ACCOUNT}; @@ -117,25 +132,25 @@ if (poll.getPollType().isGroup()) { // Add the group into the query query.addFrom(PollAccount.class, groupProperty.name()). - // It's not really necessary, but better to be sure - addEquals(groupProperty.nameProperty(PollAccount.LIST), + // It's not really necessary, but better to be sure + addEquals(groupProperty.nameProperty(PollAccount.LIST), Boolean.TRUE). - // Add link between the group and the poll - addInElements(groupProperty.name(), parentCollectionProperty); + // Add link between the group and the poll + addInElements(groupProperty.name(), parentCollectionProperty); // In case of group, the parentCollection is the child // pollAccount from the group parentCollectionProperty = - groupProperty.nameProperty(PollAccount.CHILD); + groupProperty.nameProperty(PollAccount.CHILD); } // Add link between the participant to find and his parent query.addInElements(participantProperty.name(), parentCollectionProperty). - addEquals(participantProperty.name(), participant). - addWhere(pollProperty.nameProperty(Poll.CREATOR), - TopiaQuery.Op.NEQ, - participant - ); + addEquals(participantProperty.name(), participant). + addWhere(pollProperty.nameProperty(Poll.CREATOR), + TopiaQuery.Op.NEQ, + participant + ); if (log.isDebugEnabled()) { log.debug("Query : " + query); @@ -143,6 +158,8 @@ result = dao.existByQuery(query); } + + // Note for freePoll, maybe check doubloons on participant name/email return result; } @@ -193,6 +210,9 @@ @Override protected void executeDeleteVote(TopiaContext transaction, Poll poll, PollAccount participant) throws Exception { + + // For free poll, delete the participant + // For other types, delete all votes and set voteDate to null } @Override @@ -204,7 +224,7 @@ @Override protected String executeSaveVote(TopiaContext transaction, Poll poll, - PollAccount participant) + PollAccount participant) throws TopiaException { // 1- executeCanVote(transaction, poll, participant); @@ -231,6 +251,9 @@ // canVote method check the existence of participant for a // restricted poll, so the create case is only for FREE poll. if (participantToSave == null) { + if (log.isInfoEnabled()) { + log.info("Create new participant for FREE poll"); + } participantToSave = createNewParticipant(transaction, poll); } @@ -257,6 +280,16 @@ return result; } + /** + * Create a new participant for a running {@code poll}. The participant uid + * will be generated and the new instance of pollAccount will be added to + * the {@code poll}. + * + * @param transaction TopiaContext to retrieve daos + * @param poll Poll where the new participant will be added + * @return a new PollAccount added to the poll + * @throws TopiaException for Topia errors + */ protected PollAccount createNewParticipant(TopiaContext transaction, Poll poll) throws TopiaException { @@ -277,12 +310,22 @@ return newParticipant; } + /** + * Save all votes from a participant. Votes will be added/updated or deleted + * depends on voteValue and existence in database. + * + * @param transaction TopiaContext to retrieve daos + * @param source PollAccount source that contains vote to save + * @param destination PollAccount destination where votes will be added to + * be persisted + * @throws TopiaException for Topia errors + */ protected void saveParticipantVotes(TopiaContext transaction, PollAccount source, PollAccount destination) throws TopiaException { - VoteDAO voteDAO = PollenDAOHelper.getVoteDAO(transaction); + VoteDAO voteDAO = PollenDAOHelper.getVoteDAO(transaction); for (Vote vote : source.getChoiceVote()) { Vote voteToSave = voteDAO.findByTopiaId(vote.getTopiaId()); @@ -290,6 +333,9 @@ if (vote.getVoteValue() != 0) { // Create vote if (voteToSave == null) { + if (log.isDebugEnabled()) { + log.debug("Create vote for " + destination.getUid()); + } voteToSave = voteDAO.create(); voteToSave.setChoice(vote.getChoice()); // Add new vote to destination participant @@ -300,11 +346,13 @@ voteToSave.setVoteValue(vote.getVoteValue()); voteDAO.update(voteToSave); - // Delete vote if exist and new value is 0 + // Delete vote if exist and new value is 0 } else if (voteToSave != null) { - // Normally the vote will be removed from ChoiceVote list in - // pollAccount. - voteDAO.delete(voteToSave); + if (log.isDebugEnabled()) { + log.debug("Delete vote for " + destination.getUid()); + } + // Normally the vote will be deleted + destination.removeChoiceVote(voteToSave); } } } Modified: trunk/pollen-business/src/main/xmi/pollen.properties =================================================================== --- trunk/pollen-business/src/main/xmi/pollen.properties 2010-05-28 08:40:01 UTC (rev 3019) +++ trunk/pollen-business/src/main/xmi/pollen.properties 2010-05-28 13:21:09 UTC (rev 3020) @@ -11,6 +11,8 @@ org.chorem.pollen.entity.PollAccount.attribute.uid.tagvalue.naturalId=true +org.chorem.pollen.entity.Vote.attribute.choice.tagvalue.lazy=false + org.chorem.pollen.entity.FavoriteList.attribute.userAccount.tagvalue.naturalId=true org.chorem.pollen.entity.FavoriteList.attribute.name.tagvalue.naturalId=true Copied: trunk/pollen-business/src/test/java/org/chorem/pollen/PollenUtilsTest.java (from rev 3018, trunk/pollen-business/src/test/java/org/chorem/pollen/TopiaQueryBuilderTest.java) =================================================================== --- trunk/pollen-business/src/test/java/org/chorem/pollen/PollenUtilsTest.java (rev 0) +++ trunk/pollen-business/src/test/java/org/chorem/pollen/PollenUtilsTest.java 2010-05-28 13:21:09 UTC (rev 3020) @@ -0,0 +1,122 @@ + +package org.chorem.pollen; + +import java.io.IOException; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.chorem.pollen.bean.Filter; +import org.chorem.pollen.entity.FavoriteList; +import org.chorem.pollen.entity.FavoriteParticipant; +import org.chorem.pollen.entity.UserAccount; +import org.junit.Assert; +import org.junit.Test; +import org.nuiton.topia.framework.TopiaQuery; +import org.nuiton.topia.persistence.TopiaEntity; + +/** + * + * @author fdesbois + */ +public class PollenUtilsTest { + + private static final Log log = + LogFactory.getLog(PollenUtilsTest.class); + + /** + * Test of prepareQuery method, of class Filter. + */ + @Test + public void testPrepareQueryForFilter() { + log.info("testPrepareQueryForFilter"); + + Filter filter = new Filter(); + filter.setStartIndex(1); + filter.setEndIndex(40); + filter.setOrderBy(UserAccount.LOGIN); + +// TopiaQueryBuilder builder = new TopiaQueryBuilder(); +// builder.setQuery(new TopiaQuery(UserAccount.class)); + TopiaQuery query = new TopiaQuery(UserAccount.class); + PollenUtils.prepareQuery(query, filter); + + log.debug("Query : " + query); + + Assert.assertEquals(query.fullQuery(), + "FROM " + UserAccount.class.getName() + + " ORDER BY " + UserAccount.LOGIN); + + filter.setOrderBy(null); + + query = new TopiaQuery(UserAccount.class); + PollenUtils.prepareQuery(query, filter); + + log.debug("Query : " + query); + + Assert.assertEquals(query.fullQuery(), + "FROM " + UserAccount.class.getName() + + " ORDER BY " + TopiaEntity.TOPIA_CREATE_DATE + " DESC"); + + } + + @Test + public void testCreateQueryFindElementsByCollection() throws IOException { + + //TopiaQueryBuilder builder = new TopiaQueryBuilder(); + + Filter filter = new Filter(); + filter.setStartIndex(1); + filter.setEndIndex(40); + filter.setOrderBy(FavoriteParticipant.NAME); + + EntityQueryProperty participantPropertyProvider = + PollenUtils.getEntityQueryProperty(FavoriteParticipant.class, "P"); + + EntityQueryProperty listPropertyProvider = + PollenUtils.getEntityQueryProperty(FavoriteList.class, "L"); + listPropertyProvider.setPropertyJoin(FavoriteList.FAVORITE_PARTICIPANT); + + log.info("test1 : orderBy FavoriteParticipant name"); + TopiaQuery result = PollenUtils.createQueryFindElementsByCollection( + participantPropertyProvider, listPropertyProvider, filter); + + log.debug("Query : " + result); + + Assert.assertEquals(result.fullQuery(), + "SELECT P FROM " + FavoriteParticipant.class.getName() + " P, " + + FavoriteList.class.getName() + " L" + + " WHERE P IN elements(L." + FavoriteList.FAVORITE_PARTICIPANT + + ") ORDER BY P." + FavoriteParticipant.NAME); + + log.info("test2 : orderBy not set (default to topiaCreateDate desc)"); + filter.setOrderBy(null); + + result = PollenUtils.createQueryFindElementsByCollection( + participantPropertyProvider, listPropertyProvider, filter); + + log.debug("Query : " + result); + + Assert.assertEquals(result.fullQuery(), + "SELECT P FROM " + FavoriteParticipant.class.getName() + " P, " + + FavoriteList.class.getName() + " L" + + " WHERE P IN elements(L." + FavoriteList.FAVORITE_PARTICIPANT + + ") ORDER BY P." + TopiaEntity.TOPIA_CREATE_DATE + " DESC"); + + log.info("test3 : orderBy name and email desc"); + String orderBy = FavoriteParticipant.NAME + ", " + FavoriteParticipant.EMAIL + " desc"; + filter.setOrderBy(orderBy); + + result = PollenUtils.createQueryFindElementsByCollection( + participantPropertyProvider, listPropertyProvider, filter); + + log.debug("Query : " + result); + + Assert.assertEquals(result.fullQuery(), + "SELECT P FROM " + FavoriteParticipant.class.getName() + " P, " + + FavoriteList.class.getName() + " L" + + " WHERE P IN elements(L." + FavoriteList.FAVORITE_PARTICIPANT + + ") ORDER BY P." + FavoriteParticipant.NAME + ", P." + + FavoriteParticipant.EMAIL + " desc"); + + } + +} \ No newline at end of file Property changes on: trunk/pollen-business/src/test/java/org/chorem/pollen/PollenUtilsTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Deleted: trunk/pollen-business/src/test/java/org/chorem/pollen/TopiaQueryBuilderTest.java =================================================================== --- trunk/pollen-business/src/test/java/org/chorem/pollen/TopiaQueryBuilderTest.java 2010-05-28 08:40:01 UTC (rev 3019) +++ trunk/pollen-business/src/test/java/org/chorem/pollen/TopiaQueryBuilderTest.java 2010-05-28 13:21:09 UTC (rev 3020) @@ -1,122 +0,0 @@ - -package org.chorem.pollen; - -import java.io.IOException; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.chorem.pollen.bean.Filter; -import org.chorem.pollen.entity.FavoriteList; -import org.chorem.pollen.entity.FavoriteParticipant; -import org.chorem.pollen.entity.UserAccount; -import org.junit.Assert; -import org.junit.Test; -import org.nuiton.topia.framework.TopiaQuery; -import org.nuiton.topia.persistence.TopiaEntity; - -/** - * - * @author fdesbois - */ -public class TopiaQueryBuilderTest { - - private static final Log log = - LogFactory.getLog(TopiaQueryBuilderTest.class); - - /** - * Test of prepareQuery method, of class Filter. - */ - @Test - public void testPrepareQueryForFilter() { - log.info("testPrepareQueryForFilter"); - - Filter filter = new Filter(); - filter.setStartIndex(1); - filter.setEndIndex(40); - filter.setOrderBy(UserAccount.LOGIN); - -// TopiaQueryBuilder builder = new TopiaQueryBuilder(); -// builder.setQuery(new TopiaQuery(UserAccount.class)); - TopiaQuery query = new TopiaQuery(UserAccount.class); - PollenUtils.prepareQuery(query, filter); - - log.debug("Query : " + query); - - Assert.assertEquals(query.fullQuery(), - "FROM " + UserAccount.class.getName() + - " ORDER BY " + UserAccount.LOGIN); - - filter.setOrderBy(null); - - query = new TopiaQuery(UserAccount.class); - PollenUtils.prepareQuery(query, filter); - - log.debug("Query : " + query); - - Assert.assertEquals(query.fullQuery(), - "FROM " + UserAccount.class.getName() + - " ORDER BY " + TopiaEntity.TOPIA_CREATE_DATE + " DESC"); - - } - - @Test - public void testCreateQueryFindElementsByCollection() throws IOException { - - //TopiaQueryBuilder builder = new TopiaQueryBuilder(); - - Filter filter = new Filter(); - filter.setStartIndex(1); - filter.setEndIndex(40); - filter.setOrderBy(FavoriteParticipant.NAME); - - EntityQueryProperty participantPropertyProvider = - PollenUtils.getEntityQueryProperty(FavoriteParticipant.class, "P"); - - EntityQueryProperty listPropertyProvider = - PollenUtils.getEntityQueryProperty(FavoriteList.class, "L"); - listPropertyProvider.setPropertyJoin(FavoriteList.FAVORITE_PARTICIPANT); - - log.info("test1 : orderBy FavoriteParticipant name"); - TopiaQuery result = PollenUtils.createQueryFindElementsByCollection( - participantPropertyProvider, listPropertyProvider, filter); - - log.debug("Query : " + result); - - Assert.assertEquals(result.fullQuery(), - "SELECT P FROM " + FavoriteParticipant.class.getName() + " P, " - + FavoriteList.class.getName() + " L" + - " WHERE P IN elements(L." + FavoriteList.FAVORITE_PARTICIPANT + - ") ORDER BY P." + FavoriteParticipant.NAME); - - log.info("test2 : orderBy not set (default to topiaCreateDate desc)"); - filter.setOrderBy(null); - - result = PollenUtils.createQueryFindElementsByCollection( - participantPropertyProvider, listPropertyProvider, filter); - - log.debug("Query : " + result); - - Assert.assertEquals(result.fullQuery(), - "SELECT P FROM " + FavoriteParticipant.class.getName() + " P, " - + FavoriteList.class.getName() + " L" + - " WHERE P IN elements(L." + FavoriteList.FAVORITE_PARTICIPANT + - ") ORDER BY P." + TopiaEntity.TOPIA_CREATE_DATE + " DESC"); - - log.info("test3 : orderBy name and email desc"); - String orderBy = FavoriteParticipant.NAME + ", " + FavoriteParticipant.EMAIL + " desc"; - filter.setOrderBy(orderBy); - - result = PollenUtils.createQueryFindElementsByCollection( - participantPropertyProvider, listPropertyProvider, filter); - - log.debug("Query : " + result); - - Assert.assertEquals(result.fullQuery(), - "SELECT P FROM " + FavoriteParticipant.class.getName() + " P, " - + FavoriteList.class.getName() + " L" + - " WHERE P IN elements(L." + FavoriteList.FAVORITE_PARTICIPANT + - ") ORDER BY P." + FavoriteParticipant.NAME + ", P." + - FavoriteParticipant.EMAIL + " desc"); - - } - -} \ No newline at end of file Modified: trunk/pollen-business/src/test/java/org/chorem/pollen/service/ServiceVoteImplTest.java =================================================================== --- trunk/pollen-business/src/test/java/org/chorem/pollen/service/ServiceVoteImplTest.java 2010-05-28 08:40:01 UTC (rev 3019) +++ trunk/pollen-business/src/test/java/org/chorem/pollen/service/ServiceVoteImplTest.java 2010-05-28 13:21:09 UTC (rev 3020) @@ -4,17 +4,24 @@ import org.apache.commons.logging.LogFactory; import org.chorem.pollen.PollenBusinessException; import org.chorem.pollen.PollenDAOHelper; +import org.chorem.pollen.common.ChoiceType; +import org.chorem.pollen.common.VoteCountingType; +import org.chorem.pollen.entity.Choice; import org.chorem.pollen.entity.Poll; import org.chorem.pollen.entity.PollAccount; import org.chorem.pollen.entity.PollAccountDAO; +import org.chorem.pollen.entity.PollAccountImpl; import org.chorem.pollen.entity.UserAccount; +import org.chorem.pollen.entity.Vote; +import org.chorem.pollen.entity.VoteImpl; import org.chorem.pollen.test.AbstractServiceTest; import org.junit.Assert; import org.junit.Test; import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaException; -import java.util.Iterator; +import java.util.ArrayList; +import java.util.List; /** * Created: 27 mai 2010 @@ -32,27 +39,19 @@ } @Test - public void testCanVoteForGroupPoll() + public void testCanVoteForGroupPoll() throws PollenBusinessException, TopiaException { /** PREPARE DATA **/ UserAccount user = createUser(false); - Poll pollCreated = createGroupPoll("Poll", user, 3, "Group1", "Group2"); + Poll pollCreated = createGroupPoll("Poll", user, null, 3, "Group1", "Group2"); PollAccount group1 = pollCreated.getPollAccount().iterator().next(); PollAccount existingParticipant = group1.getChild().iterator().next(); // Reload the poll to really be unconnected (no loading of PollAccount) - TopiaContext transaction = beginTransaction(); - Poll poll = null; - try { - poll = PollenDAOHelper.getPollDAO(transaction). - findByTopiaId(pollCreated.getTopiaId()); - transaction.commitTransaction(); - } finally { - transaction.closeContext(); - } + Poll poll = loadEntity(pollCreated); /** EXEC METHOD **/ log.info("test 1 : participant found and can vote"); @@ -66,7 +65,7 @@ log.info("test 3 : existingParticipant can't vote, poll is closed"); poll.setClosed(true); - transaction = beginTransaction(); + TopiaContext transaction = beginTransaction(); try { PollenDAOHelper.getPollDAO(transaction).update(poll); transaction.commitTransaction(); @@ -77,4 +76,132 @@ result = serviceVote.canVote(poll, existingParticipant); Assert.assertFalse(result); } + + @Test + public void testSaveVoteForFreePoll() throws PollenBusinessException, TopiaException { + /** PREPARE DATA **/ + UserAccount user = createUser(false); + + Poll pollCreated = createFreePoll("Poll", user, VoteCountingType.NORMAL); + List<Choice> choices = + addChoicesToPoll(pollCreated, ChoiceType.TEXT, + "choice1", "choice2", "choice3"); + + Poll poll = loadEntity(pollCreated); + + // Instantiate new participant with votes + PollAccount participant = new PollAccountImpl(); + participant.setName("participant"); + participant.setEmail("participant@domain.org"); + participant.setUserAccount(user); + + Vote vote1 = new VoteImpl(); + vote1.setChoice(choices.get(0)); // choice1 + vote1.setVoteValue(1); // TRUE + participant.addChoiceVote(vote1); + + Vote vote2 = new VoteImpl(); + vote2.setChoice(choices.get(1)); // choice2 + vote2.setVoteValue(0); // FALSE + participant.addChoiceVote(vote2); + + Vote vote3 = new VoteImpl(); + vote3.setChoice(choices.get(2)); // choice3 + vote3.setVoteValue(1); // TRUE + participant.addChoiceVote(vote3); + + /** EXEC METHOD **/ + + log.info("test 1 : new participant with 2 votes with value to 1"); + + String uid = serviceVote.saveVote(poll, participant); + + TopiaContext transaction = beginTransaction(); + try { + + PollAccountDAO dao = PollenDAOHelper.getPollAccountDAO(transaction); + + PollAccount accountFound = dao.findByNaturalId(uid); + + Assert.assertNotNull(accountFound); + Assert.assertEquals(context.getCurrentDate(), accountFound.getVoteDate()); + Assert.assertEquals(2, accountFound.getChoiceVote().size()); + List<String> choicesExist = new ArrayList<String>(); + for (Vote curr : accountFound.getChoiceVote()) { + Assert.assertEquals(1, curr.getVoteValue(), 0); + choicesExist.add(curr.getChoice().getName()); + } + + Assert.assertTrue(choicesExist.contains("choice1")); + Assert.assertTrue(choicesExist.contains("choice3")); + + } finally { + transaction.closeContext(); + } + + log.info("test 2 : anonymous participant"); + participant.setAnonymous(true); + + uid = serviceVote.saveVote(poll, participant); + transaction = beginTransaction(); + try { + + PollAccountDAO dao = PollenDAOHelper.getPollAccountDAO(transaction); + + PollAccount accountFound = dao.findByNaturalId(uid); + Assert.assertTrue(accountFound.isAnonymous()); + Assert.assertNull(accountFound.getName()); + Assert.assertNull(accountFound.getUserAccount()); + Assert.assertNull(accountFound.getEmail()); + + // Update topiaId for next test + participant.setTopiaId(accountFound.getTopiaId()); + + } finally { + transaction.closeContext(); + } + + log.info("test 3 : update participant votes"); + + PollAccount participantExist = + loadEntity(participant, PollAccount.CHOICE_VOTE); + + for (Vote vote : participantExist.getChoiceVote()) { + // Update choice 1 with value 0 -> will be deleted + if (vote.getChoice().equals(choices.get(0))) { + vote.setVoteValue(0); + } + } + // Add choice2 + vote2 = new VoteImpl(); + vote2.setChoice(choices.get(1)); + vote2.setVoteValue(1); // TRUE + participantExist.addChoiceVote(vote2); + + uid = serviceVote.saveVote(poll, participantExist); + + transaction = beginTransaction(); + try { + + PollAccountDAO dao = PollenDAOHelper.getPollAccountDAO(transaction); + + PollAccount accountFound = dao.findByNaturalId(uid); + + Assert.assertEquals(context.getCurrentDate(), accountFound.getVoteDate()); + Assert.assertEquals(2, accountFound.getChoiceVote().size()); + List<String> choicesExist = new ArrayList<String>(); + for (Vote curr : accountFound.getChoiceVote()) { + choicesExist.add(curr.getChoice().getName()); + } + + Assert.assertTrue(choicesExist.contains("choice2")); + Assert.assertTrue(choicesExist.contains("choice3")); + + } finally { + transaction.closeContext(); + } + + + + } } Modified: trunk/pollen-business/src/test/java/org/chorem/pollen/test/AbstractServiceTest.java =================================================================== --- trunk/pollen-business/src/test/java/org/chorem/pollen/test/AbstractServiceTest.java 2010-05-28 08:40:01 UTC (rev 3019) +++ trunk/pollen-business/src/test/java/org/chorem/pollen/test/AbstractServiceTest.java 2010-05-28 13:21:09 UTC (rev 3020) @@ -6,7 +6,11 @@ import org.chorem.pollen.PollenContext; import org.chorem.pollen.PollenContextImpl; import org.chorem.pollen.PollenDAOHelper; +import org.chorem.pollen.common.ChoiceType; import org.chorem.pollen.common.PollType; +import org.chorem.pollen.common.VoteCountingType; +import org.chorem.pollen.entity.Choice; +import org.chorem.pollen.entity.ChoiceDAO; import org.chorem.pollen.entity.FavoriteList; import org.chorem.pollen.entity.FavoriteListDAO; import org.chorem.pollen.entity.FavoriteParticipant; @@ -35,8 +39,10 @@ import java.io.IOException; import java.io.InputStream; import java.util.Calendar; +import java.util.Collection; import java.util.Date; import java.util.GregorianCalendar; +import java.util.List; import java.util.Properties; /** @@ -45,10 +51,7 @@ * Created: 24 févr. 2010 * * @author fdesbois - * @version $Revision$ - * <p/> - * Mise a jour: $Date$ - * par : $Author$ + * @version $Id$ */ @Ignore public abstract class AbstractServiceTest { @@ -258,7 +261,8 @@ } protected Poll createPoll(TopiaContext transaction, - String title, UserAccount user, PollType type) + String title, UserAccount user, + PollType type, VoteCountingType voteCounting) throws TopiaException { PollAccountDAO accountDAO = @@ -276,12 +280,54 @@ poll.setCreator(creator); poll.setTitle(title); poll.setPollType(type); + if (voteCounting == null) { + voteCounting = VoteCountingType.NORMAL; + } + poll.setVoteCountingType(voteCounting); return poll; } + public Poll createFreePoll(String title, UserAccount user, VoteCountingType voteCounting) + throws TopiaException { + TopiaContext transaction = beginTransaction(); + try { + Poll poll = createPoll(transaction, title, user, PollType.FREE, voteCounting); + + transaction.commitTransaction(); + + return poll; + } finally { + transaction.closeContext(); + } + } + + public List<Choice> addChoicesToPoll(Poll poll, ChoiceType choiceType, String... choices) + throws TopiaException { + + TopiaContext transaction = beginTransaction(); + try { + ChoiceDAO dao = PollenDAOHelper.getChoiceDAO(transaction); + + poll.setChoiceType(choiceType); + + for (String choice : choices) { + Choice newChoice = dao.create(); + newChoice.setName(choice); + newChoice.setChoiceType(choiceType); + poll.addChoice(newChoice); + } + + transaction.commitTransaction(); + + return poll.getChoice(); + } finally { + transaction.closeContext(); + } + } + public Poll createGroupPoll(String title, - UserAccount user, + UserAccount user, VoteCountingType voteCounting, int nbParticipantsByGroup, String... groupNames) throws TopiaException { @@ -289,7 +335,7 @@ TopiaContext transaction = beginTransaction(); try { - Poll poll = createPoll(transaction, title, user, PollType.GROUP); + Poll poll = createPoll(transaction, title, user, PollType.GROUP, voteCounting); PollAccountDAO accountDAO = PollenDAOHelper.getPollAccountDAO(transaction); @@ -320,4 +366,27 @@ } } + public <E extends TopiaEntity> E loadEntity(E source, String... properties) + throws TopiaException { + + TopiaContext transaction = beginTransaction(); + try { + TopiaQuery query = new TopiaQuery(source.getClass()); + query.addLoad(properties). + addEquals(TopiaEntity.TOPIA_ID, source.getTopiaId()). + setMaxResults(1); + + E result = (E)query.executeToEntity(transaction, source.getClass()); + + if (result != null) { + return result; + } + + throw new NullPointerException("nothing to find... from " + + source.getTopiaId()); + } finally { + transaction.closeContext(); + } + } + } Deleted: trunk/pollen-business/src/test/java/org/chorem/pollen/test/TestData.java =================================================================== --- trunk/pollen-business/src/test/java/org/chorem/pollen/test/TestData.java 2010-05-28 08:40:01 UTC (rev 3019) +++ trunk/pollen-business/src/test/java/org/chorem/pollen/test/TestData.java 2010-05-28 13:21:09 UTC (rev 3020) @@ -1,40 +0,0 @@ -package org.chorem.pollen.test; - -import org.nuiton.topia.TopiaContext; - -/** - * TestData - * - * Created: 24 févr. 2010 - * - * @author fdesbois - * @version $Revision$ - * - * Mise a jour: $Date$ - * par : $Author$ - */ -public abstract class TestData { - - protected TopiaContext transaction; - - public void execute() throws Exception { - transaction = null; - try { - //transaction = beginTransaction(); - - test(); - - } catch (Exception eee) { - if (transaction != null) { - transaction.rollbackTransaction(); - } - throw eee; - } finally { - if (transaction != null) { - transaction.closeContext(); - } - } - } - - protected abstract void test() throws Exception; -}
participants (1)
-
fdesbois@users.chorem.org