r3499 - in trunk: . pollen-persistence pollen-persistence/src/main/java/org/chorem/pollen/business/persistence pollen-persistence/src/test/java/org/chorem/pollen/business/persistence
Author: tchemit Date: 2012-06-17 15:12:38 +0200 (Sun, 17 Jun 2012) New Revision: 3499 Url: http://chorem.org/repositories/revision/pollen/3499 Log: - fixes #615: Cannot add choices if no begin date is set - improve some code - add tests on Poll Added: trunk/pollen-persistence/src/test/java/org/chorem/pollen/business/persistence/PollTest.java Modified: trunk/pollen-persistence/pom.xml trunk/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollImpl.java trunk/pom.xml Modified: trunk/pollen-persistence/pom.xml =================================================================== --- trunk/pollen-persistence/pom.xml 2012-06-16 21:57:35 UTC (rev 3498) +++ trunk/pollen-persistence/pom.xml 2012-06-17 13:12:38 UTC (rev 3499) @@ -61,7 +61,13 @@ <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + </dependency> + + <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </dependency> Modified: trunk/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollImpl.java =================================================================== --- trunk/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollImpl.java 2012-06-16 21:57:35 UTC (rev 3498) +++ trunk/pollen-persistence/src/main/java/org/chorem/pollen/business/persistence/PollImpl.java 2012-06-17 13:12:38 UTC (rev 3499) @@ -116,7 +116,9 @@ @Override public boolean isRunning(Date currentDate) { - return !isClosed() && isStarted(currentDate) && !isFinished(currentDate); + return !isClosed() && + isStarted(currentDate) && + !isFinished(currentDate); } @Override @@ -126,52 +128,62 @@ @Override public boolean isAddChoiceStarted(Date currentDate) { - return choiceAddAllowed && (beginChoiceDate == null || beginChoiceDate.before(currentDate)); + return isChoiceAddAllowed() && + (beginChoiceDate == null || beginChoiceDate.before(currentDate)); } @Override public boolean isAddChoiceRunning(Date currentDate) { - return !isClosed() && !isFinished(currentDate) && isAddChoiceStarted(currentDate) && !isAddChoiceFinished(currentDate); + return isChoiceAddAllowed() && + !isClosed() && + !isFinished(currentDate) && + isAddChoiceStarted(currentDate) && + !isAddChoiceFinished(currentDate); } @Override public boolean isAddChoiceFinished(Date currentDate) { - return !choiceAddAllowed || (endChoiceDate != null && endChoiceDate.after(currentDate)); + return !isChoiceAddAllowed() || + (endChoiceDate != null && endChoiceDate.before(currentDate)); } @Override public PreventRule getPreventRuleByScope(String scope) { Preconditions.checkNotNull(scope); + PreventRule rule = null; for (PreventRule value : getPreventRule()) { if (scope.equals(value.getScope())) { - return value; + rule = value; + break; } } - return null; + return rule; } @Override public PersonToList getPersonToListByVote(Vote vote) { Preconditions.checkNotNull(vote); - PersonToList result; + PollAccount pollAccount = vote.getPollAccount(); + + PersonToList personToList = null; for (VotingList votingList : getVotingList()) { - result = votingList.getPollAccountPersonToList(vote.getPollAccount()); + personToList = votingList.getPollAccountPersonToList(pollAccount); - if (result != null) { - return result; + if (personToList != null) { + break; } } - return null; + return personToList; } @Override public List<PollAccount> getPollAccounts(boolean withNoVote) { - List<PollAccount> result = Lists.newLinkedList(); + List<PollAccount> pollAccounts = Lists.newLinkedList(); for (VotingList votingList : getVotingList()) { @@ -183,7 +195,7 @@ continue; } - result.add(personToList.getPollAccount()); + pollAccounts.add(personToList.getPollAccount()); // // if (!withNoVote || !personToList.isHasVoted()) { // @@ -191,7 +203,7 @@ // } } } - return result; + return pollAccounts; } } //PollImpl Added: trunk/pollen-persistence/src/test/java/org/chorem/pollen/business/persistence/PollTest.java =================================================================== --- trunk/pollen-persistence/src/test/java/org/chorem/pollen/business/persistence/PollTest.java (rev 0) +++ trunk/pollen-persistence/src/test/java/org/chorem/pollen/business/persistence/PollTest.java 2012-06-17 13:12:38 UTC (rev 3499) @@ -0,0 +1,385 @@ +/* + * #%L + * Pollen :: Persistence + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin, Tony Chemit + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package org.chorem.pollen.business.persistence; + +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; +import org.nuiton.util.DateUtil; + +import java.util.Date; + +/** + * Tests the {@link PollImpl} methods. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.4 + */ +public class PollTest { + + Date yesterday() { + return DateUtil.createDate(1, 1, 2012); + } + + Date today() { + return DateUtil.createDate(2, 1, 2012); + } + + Date tomorrow() { + return DateUtil.createDate(3, 1, 2012); + } + + @Test + public void testIsStarted() throws Exception { + + Poll poll = new PollImpl(); + + // no begin date, started + Assert.assertTrue(poll.isStarted(today())); + + // begin date yesterday, started + poll.setBeginDate(yesterday()); + Assert.assertTrue(poll.isStarted(today())); + + // begin date tomorrow, not started + poll.setBeginDate(tomorrow()); + Assert.assertFalse(poll.isStarted(today())); + } + + @Test + public void testIsFinished() throws Exception { + + Poll poll = new PollImpl(); + + // no end date, not finished + Assert.assertFalse(poll.isFinished(today())); + + // end date tomorrow, not finished + poll.setEndDate(tomorrow()); + Assert.assertFalse(poll.isFinished(today())); + + // end date yesterday, not finished + poll.setEndDate(yesterday()); + Assert.assertTrue(poll.isFinished(today())); + } + + @Test + public void testIsAddChoiceStarted() throws Exception { + + Poll poll = new PollImpl(); + + // -- no choiceAddAllowed -- // + + // no choiceAddAllowed, not choice started + Assert.assertFalse(poll.isAddChoiceStarted(today())); + + // begin add choice yesterday, no choiceAddAllowed, not choice started + poll.setBeginChoiceDate(yesterday()); + Assert.assertFalse(poll.isAddChoiceStarted(today())); + + // begin add choice tomorrow, no choiceAddAllowed, not choice started + poll.setBeginChoiceDate(tomorrow()); + Assert.assertFalse(poll.isAddChoiceStarted(today())); + + // -- choiceAddAllowed -- // + + poll.setChoiceAddAllowed(true); + + // no begin choice date, started + poll.setBeginChoiceDate(null); + Assert.assertTrue(poll.isAddChoiceStarted(today())); + + // begin add choice yesterday, started + poll.setBeginChoiceDate(yesterday()); + Assert.assertTrue(poll.isAddChoiceStarted(today())); + + // begin add choice tomorrow, not started + poll.setBeginChoiceDate(tomorrow()); + Assert.assertFalse(poll.isAddChoiceStarted(today())); + } + + @Test + public void testIsAddChoiceFinished() throws Exception { + + Poll poll = new PollImpl(); + + // -- no choiceAddAllowed -- // + + // no choiceAddAllowed, add choice finished + Assert.assertTrue(poll.isAddChoiceFinished(today())); + + // no end choice date, no choiceAddAllowed, add choice finished + Assert.assertTrue(poll.isAddChoiceFinished(today())); + + // end add choice tomorrow, no choiceAddAllowed, add choice finished + poll.setEndChoiceDate(tomorrow()); + Assert.assertTrue(poll.isAddChoiceFinished(today())); + + // end add choice yesterday, no choiceAddAllowed, add choice finished + poll.setEndChoiceDate(yesterday()); + Assert.assertTrue(poll.isAddChoiceFinished(today())); + + // -- choiceAddAllowed -- // + poll.setChoiceAddAllowed(true); + + // no end choice date, not finished + poll.setEndChoiceDate(null); + Assert.assertFalse(poll.isAddChoiceFinished(today())); + + // end add choice tomorrow, not finished + poll.setEndChoiceDate(tomorrow()); + Assert.assertFalse(poll.isAddChoiceFinished(today())); + + // end add choice yesterday, finished + poll.setEndChoiceDate(yesterday()); + Assert.assertTrue(poll.isAddChoiceFinished(today())); + } + + @Test + public void testIsRunning() throws Exception { + + Poll poll = new PollImpl(); + + // -- closed -- // + // -- closed -- // + // -- closed -- // + + // closed, not running + poll.setClosed(true); + Assert.assertFalse(poll.isRunning(today())); + + // -- No end date -- // + + // no begin date, no end date, closed, not running + poll.setBeginDate(null); + poll.setEndDate(null); + Assert.assertFalse(poll.isRunning(today())); + + // begin date yesterday, no end date, closed, not running + poll.setBeginDate(yesterday()); + Assert.assertFalse(poll.isRunning(today())); + + // begin date tomorrow, no end date, closed, not running + poll.setBeginDate(tomorrow()); + Assert.assertFalse(poll.isRunning(today())); + + // -- end date yesterday -- // + + poll.setBeginDate(null); + poll.setEndDate(yesterday()); + + // no begin date, end date yesterday, closed, not running + Assert.assertFalse(poll.isRunning(today())); + + // begin date yesterday, end date yesterday, closed, not running + poll.setBeginDate(yesterday()); + Assert.assertFalse(poll.isRunning(today())); + + // begin date tomorrow, end date yesterday, closed, not running + poll.setBeginDate(tomorrow()); + Assert.assertFalse(poll.isRunning(today())); + + // -- end date tomorrow -- // + + poll.setBeginDate(null); + poll.setEndDate(tomorrow()); + + // no begin date, end date tomorrow, running + Assert.assertFalse(poll.isRunning(today())); + + // begin date yesterday, end date tomorrow, running + poll.setBeginDate(yesterday()); + Assert.assertFalse(poll.isRunning(today())); + + // begin date tomorrow, end date tomorrow, not running (not started) + poll.setBeginDate(tomorrow()); + Assert.assertFalse(poll.isRunning(today())); + + // -- not closed -- // + // -- not closed -- // + // -- not closed -- // + + poll.setClosed(false); + + // must be started but not finished + + // -- No end date -- // + + // no begin date, no end date, running + poll.setBeginDate(null); + poll.setEndDate(null); + Assert.assertTrue(poll.isRunning(today())); + + // begin date yesterday, no end date, running + poll.setBeginDate(yesterday()); + Assert.assertTrue(poll.isRunning(today())); + + // begin date tomorrow, no end date, not running (not started) + poll.setBeginDate(tomorrow()); + Assert.assertFalse(poll.isRunning(today())); + + // -- end date yesterday -- // + + poll.setBeginDate(null); + poll.setEndDate(yesterday()); + + // no begin date, end date yesterday, not running (finished) + Assert.assertFalse(poll.isRunning(today())); + + // begin date yesterday, end date yesterday, not running (finished) + poll.setBeginDate(yesterday()); + Assert.assertFalse(poll.isRunning(today())); + + // begin date tomorrow, end date yesterday, not running (finished) + poll.setBeginDate(tomorrow()); + Assert.assertFalse(poll.isRunning(today())); + + // -- end date tomorrow -- // + + poll.setBeginDate(null); + poll.setEndDate(tomorrow()); + + // no begin date, end date tomorrow, running + Assert.assertTrue(poll.isRunning(today())); + + // begin date yesterday, end date tomorrow, running + poll.setBeginDate(yesterday()); + Assert.assertTrue(poll.isRunning(today())); + + // begin date tomorrow, end date tomorrow, not running (not started) + poll.setBeginDate(tomorrow()); + Assert.assertFalse(poll.isRunning(today())); + } + + @Test + public void testIsAddChoiceRunning() throws Exception { + + // says that poll is not closed and is not finished + // only begin choice date and end choice date have a meaning + + PollImpl poll = Mockito.mock(PollImpl.class); + Mockito.when(poll.isClosed()).thenReturn(false); + Mockito.when(poll.isFinished(Mockito.any(Date.class))).thenReturn(false); + + Mockito.when(poll.isChoiceAddAllowed()).thenCallRealMethod(); + Mockito.when(poll.isAddChoiceRunning(Mockito.any(Date.class))).thenCallRealMethod(); + Mockito.when(poll.isAddChoiceStarted(Mockito.any(Date.class))).thenCallRealMethod(); + Mockito.when(poll.isAddChoiceFinished(Mockito.any(Date.class))).thenCallRealMethod(); + + Mockito.doCallRealMethod().when(poll).setChoiceAddAllowed(Mockito.anyBoolean()); + Mockito.doCallRealMethod().when(poll).setBeginChoiceDate(Mockito.any(Date.class)); + Mockito.doCallRealMethod().when(poll).setEndChoiceDate(Mockito.any(Date.class)); + + // -- choiceAddAllowed -- // + // -- choiceAddAllowed -- // + // -- choiceAddAllowed -- // + + poll.setChoiceAddAllowed(true); + + // -- no end choice date (choice not finished) -- // + + poll.setEndChoiceDate(null); + + // no begin date (choice running) + poll.setBeginChoiceDate(null); + Assert.assertTrue(poll.isAddChoiceRunning(today())); + + // begin date yesterday (choice running) + poll.setBeginChoiceDate(yesterday()); + Assert.assertTrue(poll.isAddChoiceRunning(today())); + + // begin date tomorrow (choice not running) + poll.setBeginChoiceDate(tomorrow()); + Assert.assertFalse(poll.isAddChoiceRunning(today())); + + // -- end choice date yesterday (choice finished) -- // + + poll.setEndChoiceDate(yesterday()); + + // no begin date (choice not running) (choice finished) + poll.setBeginChoiceDate(null); + Assert.assertFalse(poll.isAddChoiceRunning(today())); + + // begin date yesterday (choice not running) (choice finished) + poll.setBeginChoiceDate(yesterday()); + Assert.assertFalse(poll.isAddChoiceRunning(today())); + + // begin date tomorrow (choice not running) (choice finished) + poll.setBeginChoiceDate(tomorrow()); + Assert.assertFalse(poll.isAddChoiceRunning(today())); + + // -- end choice date tomorrow (choice not finished) -- // + + poll.setEndChoiceDate(tomorrow()); + + // no begin date (choice running) + poll.setBeginChoiceDate(null); + Assert.assertTrue(poll.isAddChoiceRunning(today())); + + // begin date yesterday (choice running) + poll.setBeginChoiceDate(yesterday()); + Assert.assertTrue(poll.isAddChoiceRunning(today())); + + // begin date tomorrow (choice not running) + poll.setBeginChoiceDate(tomorrow()); + Assert.assertFalse(poll.isAddChoiceRunning(today())); + + // -- not choiceAddAllowed (so all is false) -- // + // -- not choiceAddAllowed (so all is false) -- // + // -- not choiceAddAllowed (so all is false) -- // + + poll.setChoiceAddAllowed(false); + + // -- no end choice date (choice not finished) -- // + + poll.setEndChoiceDate(null); + poll.setBeginChoiceDate(null); + Assert.assertFalse(poll.isAddChoiceRunning(today())); + + poll.setBeginChoiceDate(yesterday()); + Assert.assertFalse(poll.isAddChoiceRunning(today())); + + poll.setBeginChoiceDate(tomorrow()); + Assert.assertFalse(poll.isAddChoiceRunning(today())); + + poll.setEndChoiceDate(yesterday()); + poll.setBeginChoiceDate(null); + Assert.assertFalse(poll.isAddChoiceRunning(today())); + + poll.setBeginChoiceDate(yesterday()); + Assert.assertFalse(poll.isAddChoiceRunning(today())); + + poll.setBeginChoiceDate(tomorrow()); + Assert.assertFalse(poll.isAddChoiceRunning(today())); + + poll.setEndChoiceDate(tomorrow()); + poll.setBeginChoiceDate(null); + Assert.assertFalse(poll.isAddChoiceRunning(today())); + + poll.setBeginChoiceDate(yesterday()); + Assert.assertFalse(poll.isAddChoiceRunning(today())); + + poll.setBeginChoiceDate(tomorrow()); + Assert.assertFalse(poll.isAddChoiceRunning(today())); + } +} Property changes on: trunk/pollen-persistence/src/test/java/org/chorem/pollen/business/persistence/PollTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2012-06-16 21:57:35 UTC (rev 3498) +++ trunk/pom.xml 2012-06-17 13:12:38 UTC (rev 3499) @@ -193,6 +193,7 @@ <jettyVersion>${jettyPluginVersion}</jettyVersion> <hibernateVersion>3.6.10.Final</hibernateVersion> <seleniumVersion>2.21.0</seleniumVersion> + <mockitoVersion>1.9.0</mockitoVersion> <pollenI18nBundle>pollen-i18n</pollenI18nBundle> <!-- license to use --> @@ -419,6 +420,13 @@ <version>1.1.3</version> </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <version>${mockitoVersion}</version> + <scope>test</scope> + </dependency> + <!-- Selenium --> <dependency> <groupId>org.seleniumhq.selenium</groupId>
participants (1)
-
tchemit@users.chorem.org