Skip to content

Commit c6dc3c8

Browse files
committed
CronServiceTest: ported to Spock Framework.
Addressed to #151
1 parent 923b435 commit c6dc3c8

File tree

3 files changed

+128
-128
lines changed

3 files changed

+128
-128
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (C) 2009-2013 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.service
19+
20+
import spock.lang.Specification
21+
22+
import ru.mystamps.web.dao.UsersActivationDao
23+
import ru.mystamps.web.entity.UsersActivation
24+
import ru.mystamps.web.tests.DateUtils
25+
26+
public class CronServiceTest extends Specification {
27+
28+
private UsersActivationDao usersActivationDao = Mock()
29+
30+
private CronService service = new CronServiceImpl(usersActivationDao)
31+
32+
//
33+
// Tests for purgeUsersActivations()
34+
//
35+
36+
def "purgeUsersActivations() should get expired activations from dao"() {
37+
when:
38+
service.purgeUsersActivations()
39+
then:
40+
1 * usersActivationDao.findByCreatedAtLessThan(_ as Date) >> Collections.<UsersActivation>emptyList()
41+
}
42+
43+
def "purgeUsersActivations() should pass expired date to dao"() {
44+
when:
45+
service.purgeUsersActivations()
46+
then:
47+
1 * usersActivationDao.findByCreatedAtLessThan({ Date passedDate ->
48+
Calendar expectedDate = new GregorianCalendar()
49+
expectedDate.setTime(new Date())
50+
expectedDate.add(Calendar.DAY_OF_MONTH, -(CronService.PURGE_AFTER_DAYS))
51+
52+
assert DateUtils.roughlyEqual(passedDate, expectedDate.getTime())
53+
54+
return true
55+
}) >> Collections.<UsersActivation>emptyList()
56+
}
57+
58+
def "purgeUsersActivations() should throw exception when null activations was returned"() {
59+
given:
60+
usersActivationDao.findByCreatedAtLessThan(_ as Date) >> null
61+
when:
62+
service.purgeUsersActivations()
63+
then:
64+
thrown IllegalStateException
65+
}
66+
67+
def "purgeUsersActivations() should delete expired activations"() {
68+
given:
69+
List<UsersActivation> expectedActivations =
70+
Collections.singletonList(TestObjects.createUsersActivation())
71+
usersActivationDao.findByCreatedAtLessThan(_ as Date) >> expectedActivations
72+
when:
73+
service.purgeUsersActivations()
74+
then:
75+
1 * usersActivationDao.delete(expectedActivations)
76+
}
77+
78+
def "purgeUsersActivations() should do nothing if no activations"() {
79+
given:
80+
usersActivationDao.findByCreatedAtLessThan(_ as Date) >> Collections.<UsersActivation>emptyList()
81+
when:
82+
service.purgeUsersActivations()
83+
then:
84+
0 * usersActivationDao.delete(_ as Iterable)
85+
}
86+
87+
}
88+

src/test/java/ru/mystamps/web/service/CronServiceTest.java

Lines changed: 0 additions & 128 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (C) 2009-2013 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.tests;
19+
20+
import java.util.Date;
21+
22+
public final class DateUtils {
23+
24+
private static final long MAXIMUM_DIFFERENCE_IN_MILLIS = 10000L;
25+
26+
private DateUtils() {
27+
}
28+
29+
/**
30+
* Checks that the dates roughly equal.
31+
*
32+
* Both dates must differ from each other no more than 10 seconds to pass this check.
33+
*/
34+
public static boolean roughlyEqual(Date first, Date second) {
35+
return first != null
36+
&& second != null
37+
&& Math.abs(first.getTime() - second.getTime()) <= MAXIMUM_DIFFERENCE_IN_MILLIS;
38+
}
39+
40+
}

0 commit comments

Comments
 (0)