KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > event > listeners > TrialTest


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD 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  * DrFTPD 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 DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package net.sf.drftpd.event.listeners;
19
20 import java.util.Calendar JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import junit.framework.TestCase;
25 import junit.framework.TestSuite;
26 import net.sf.drftpd.Bytes;
27 import net.sf.drftpd.event.UserEvent;
28 import net.sf.drftpd.util.CalendarUtils;
29
30 import org.apache.log4j.BasicConfigurator;
31 import org.drftpd.tests.DummyUser;
32
33 /**
34  * @author mog
35  * @version $Id: TrialTest.java,v 1.7.2.1 2004/06/27 22:22:48 mog Exp $
36  */

37 public class TrialTest extends TestCase {
38     /**
39      * Mon Dec 15 23:59:59 CET 2003
40      */

41     private static final long RESETTIME = 1071519356421L;
42     private static final long TESTBYTES = Bytes.parseBytes("100m");
43     public static TestSuite suite() {
44         //TestSuite suite = new TestSuite();
45
//suite.addTest(new TrialTest("testMonth"));
46
//return suite;
47
return new TestSuite(TrialTest.class);
48     }
49     private Calendar JavaDoc cal;
50     private int period;
51     private Trial trial;
52     private DummyUser user;
53
54     public TrialTest(String JavaDoc fName) {
55         super(fName);
56     }
57
58     private void action(int period) {
59         trial.actionPerformed(getUserEvent(period));
60     }
61     private void action() {
62         action(period);
63     }
64     private void assertUserFailed() {
65         assertTrue(user.getGroups().toString(), user.isMemberOf("FAiLED"));
66         assertFalse(user.getGroups().toString(), user.isMemberOf("PASSED"));
67     }
68     private void assertUserNeither() {
69         assertEquals(user.getGroups().toString(), 0, user.getGroups().size());
70     }
71     private void assertUserPassed() {
72         assertTrue(user.getGroups().toString(), user.isMemberOf("PASSED"));
73         assertFalse(user.getGroups().toString(), user.isMemberOf("FAiLED"));
74     }
75     private Calendar JavaDoc getJUnitCalendar() {
76         Calendar JavaDoc cal = Calendar.getInstance();
77         cal.setTimeInMillis(RESETTIME);
78         CalendarUtils.ceilAllLessThanDay(cal);
79         return cal;
80     }
81
82     private Trial getJUnitTrial() throws Exception JavaDoc {
83         Properties JavaDoc p = new Properties JavaDoc();
84         p.setProperty("1.period",Trial.getPeriodName2(period));
85         p.setProperty("1.fail", "chgrp FAiLED");
86         p.setProperty("1.pass", "chgrp PASSED");
87         p.setProperty("1.quota", ""+TESTBYTES);
88         p.setProperty("1.name", Trial.getPeriodName(period));
89         p.setProperty("1.perm","*");
90         Trial trial = new Trial();
91         trial.reload(p);
92         return trial;
93     }
94
95     /**
96      * Returns a fresh user object.
97      * @return a fresh user object.
98      */

99     private DummyUser getJUnitUser() {
100         DummyUser user = new DummyUser("junit", RESETTIME);
101         user.setLastReset(cal.getTimeInMillis());
102         return user;
103     }
104
105     private UserEvent getUserEvent(int period) {
106         return new UserEvent(user, period, cal.getTimeInMillis());
107     }
108
109     private void internalTestBeforeUnique() throws Exception JavaDoc {
110         //before unique period is over
111
internalSetUp();
112         action(Trial.PERIOD_DAILY);
113         assertUserNeither();
114     }
115
116     private void internalTestUnique() throws Exception JavaDoc {
117         internalSetUp();
118         cal.add(period, 1);
119
120         //fail
121
user = getJUnitUser();
122         action(Trial.PERIOD_DAILY);
123         assertUserFailed();
124
125         //pass
126
user = getJUnitUser();
127         user.setUploadedBytes(TESTBYTES);
128         action(Trial.PERIOD_DAILY);
129         assertUserPassed();
130     }
131
132     private void internalTestUniqueAfterUnique() throws Exception JavaDoc {
133         internalSetUp();
134         cal.add(period, 1);
135         Calendar JavaDoc calOld = (Calendar JavaDoc) cal.clone();
136         cal.add(period, 1);
137
138         //fail
139
user = getJUnitUser();
140         user.setLastReset(calOld.getTimeInMillis());
141         action(Trial.PERIOD_DAILY);
142         assertUserFailed();
143
144         //pass
145
user = getJUnitUser();
146         user.setLastReset(calOld.getTimeInMillis());
147         user.setUploadedBytes(TESTBYTES);
148         action(Trial.PERIOD_DAILY);
149         assertUserPassed();
150
151         //neither (regular fail for daily reset)
152
if (period != Trial.PERIOD_DAILY) {
153             user = getJUnitUser();
154             action(Trial.PERIOD_DAILY);
155             assertUserNeither();
156         }
157     }
158
159     private void internalTestRegular() throws Exception JavaDoc {
160         internalSetUp();
161         cal.add(period, 2);
162         //pass real period
163
user = getJUnitUser();
164         user.setUploadedBytesPeriod(period, TESTBYTES);
165         action();
166         assertUserPassed();
167
168         user = getJUnitUser();
169         action();
170         assertUserFailed();
171     }
172
173     protected void setUp() throws Exception JavaDoc {
174         BasicConfigurator.configure();
175     }
176
177     protected void tearDown() throws Exception JavaDoc {
178     }
179
180     public void testDayBeforeUnique() throws Exception JavaDoc {
181         period = Trial.PERIOD_DAILY;
182         internalTestBeforeUnique();
183     }
184
185     public void testDayUnique() throws Exception JavaDoc {
186         period = Trial.PERIOD_DAILY;
187         internalTestUnique();
188     }
189
190     public void testDayRegular() throws Exception JavaDoc {
191         period = Trial.PERIOD_DAILY;
192         internalTestRegular();
193     }
194
195     public void testDayUniqueAfterUnique() throws Exception JavaDoc {
196         period = Trial.PERIOD_DAILY;
197         internalTestUniqueAfterUnique();
198     }
199
200     public void testMonthBeforeUnique() throws Exception JavaDoc {
201         period = Trial.PERIOD_MONTHLY;
202         internalTestBeforeUnique();
203     }
204
205     public void testMonthUnique() throws Exception JavaDoc {
206         period = Trial.PERIOD_MONTHLY;
207         internalTestUnique();
208     }
209
210     public void testMonthRegular() throws Exception JavaDoc {
211         period = Trial.PERIOD_MONTHLY;
212         internalTestRegular();
213     }
214
215     public void testMonthUniqueAfterUnique() throws Exception JavaDoc {
216         period = Trial.PERIOD_MONTHLY;
217         internalTestUniqueAfterUnique();
218     }
219
220     public void testWeekBeforeUnique() throws Exception JavaDoc {
221         period = Trial.PERIOD_WEEKLY;
222         internalTestBeforeUnique();
223     }
224
225     public void testWeekRegular() throws Exception JavaDoc {
226         period = Trial.PERIOD_WEEKLY;
227         internalTestRegular();
228     }
229
230     public void testWeekUniqueAfterUnique() throws Exception JavaDoc {
231         period = Trial.PERIOD_WEEKLY;
232         internalTestUniqueAfterUnique();
233     }
234
235     public void testWeekUnique() throws Exception JavaDoc {
236         period = Trial.PERIOD_WEEKLY;
237         internalTestUnique();
238     }
239
240     public void testGetCalendarEndOfWeek() {
241         Locale.setDefault(Locale.ENGLISH);
242         assertEquals(Calendar.SATURDAY, Trial.getCalendarForEndOfPeriod(Trial.PERIOD_WEEKLY).get(Calendar.DAY_OF_WEEK));
243
244         //Locale.setDefault(new Locale("sv", "SE"));
245
Locale.setDefault(Locale.FRANCE);
246         assertEquals(Calendar.SUNDAY, Trial.getCalendarForEndOfPeriod(Trial.PERIOD_WEEKLY).get(Calendar.DAY_OF_WEEK));
247     }
248     /**
249      * Sets up trial, cal and user.
250      * period must be set before internalSetUp() is called because of Limit period.
251      */

252     private void internalSetUp() throws Exception JavaDoc {
253         trial = getJUnitTrial();
254         cal = getJUnitCalendar();
255         user = getJUnitUser();
256     }
257 }
258
Popular Tags