001: /*
002: * This file is part of DrFTPD, Distributed FTP Daemon.
003: *
004: * DrFTPD is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * DrFTPD is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with DrFTPD; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.drftpd.plugins;
019:
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022:
023: import net.sf.drftpd.event.UserEvent;
024: import net.sf.drftpd.util.CalendarUtils;
025:
026: import org.apache.log4j.BasicConfigurator;
027: import org.apache.log4j.Logger;
028:
029: import org.drftpd.Bytes;
030: import org.drftpd.tests.DummyUser;
031:
032: import java.util.Calendar;
033: import java.util.Locale;
034: import java.util.Properties;
035:
036: /**
037: * @author mog
038: * @version $Id: TrialTest.java 1513 2006-10-13 22:41:08Z tdsoul $
039: */
040: public class TrialTest extends TestCase {
041: /**
042: * Mon Dec 15 23:59:59 CET 2003
043: */
044: private static final long RESETTIME = 1071519356421L;
045: private static final long TESTBYTES = Bytes.parseBytes("100m");
046: private Calendar cal;
047: private int period;
048: private Trial trial;
049: private DummyUser user;
050:
051: public TrialTest(String fName) {
052: super (fName);
053: }
054:
055: public static TestSuite suite() {
056: //TestSuite suite = new TestSuite();
057: //suite.addTest(new TrialTest("testMonth"));
058: //return suite;
059: return new TestSuite(TrialTest.class);
060: }
061:
062: private void action(int period) {
063: trial.actionPerformed(getUserEvent(period));
064: }
065:
066: private void action() {
067: action(period);
068: }
069:
070: private void assertUserFailed() {
071: assertTrue(user.getGroups().toString(), user
072: .isMemberOf("FAiLED"));
073: assertFalse(user.getGroups().toString(), user
074: .isMemberOf("PASSED"));
075: }
076:
077: private void assertUserNeither() {
078: assertEquals(user.getGroups().toString(), 0, user.getGroups()
079: .size());
080: }
081:
082: private void assertUserPassed() {
083: assertTrue(user.getGroups().toString(), user
084: .isMemberOf("PASSED"));
085: assertFalse(user.getGroups().toString(), user
086: .isMemberOf("FAiLED"));
087: }
088:
089: private Calendar getJUnitCalendar() {
090: Calendar cal = Calendar.getInstance();
091: cal.setTimeInMillis(RESETTIME);
092: CalendarUtils.ceilAllLessThanDay(cal);
093:
094: return cal;
095: }
096:
097: private Trial getJUnitTrial() throws Exception {
098: Properties p = new Properties();
099: p.setProperty("1.period", Trial.getPeriodName2(period));
100: p.setProperty("1.fail", "chgrp FAiLED");
101: p.setProperty("1.pass", "chgrp PASSED");
102: p.setProperty("1.quota", "" + TESTBYTES);
103: p.setProperty("1.name", Trial.getPeriodName(period));
104: p.setProperty("1.perm", "*");
105:
106: Trial trial = new Trial();
107: trial.reload(p);
108:
109: return trial;
110: }
111:
112: /**
113: * Returns a fresh user object.
114: * @return a fresh user object.
115: */
116: private DummyUser getJUnitUser() {
117: DummyUser user = new DummyUser("junit", RESETTIME);
118: user.setLastReset(cal.getTimeInMillis());
119:
120: return user;
121: }
122:
123: private UserEvent getUserEvent(int period) {
124: return new UserEvent(user, getCommandFromPeriod(period), cal
125: .getTimeInMillis());
126: }
127:
128: private void internalTestBeforeUnique() throws Exception {
129: //before unique period is over
130: internalSetUp();
131: action(Trial.PERIOD_DAILY);
132: assertUserNeither();
133: }
134:
135: private void internalTestUnique() throws Exception {
136: internalSetUp();
137: cal.add(period, 1);
138:
139: //fail
140: user = getJUnitUser();
141: action(Trial.PERIOD_DAILY);
142: assertUserFailed();
143:
144: //pass
145: user = getJUnitUser();
146: user.setUploadedBytes(TESTBYTES);
147: action(Trial.PERIOD_DAILY);
148: assertUserPassed();
149: }
150:
151: private void internalTestUniqueAfterUnique() throws Exception {
152: internalSetUp();
153: cal.add(period, 1);
154:
155: Calendar calOld = (Calendar) cal.clone();
156: cal.add(period, 1);
157:
158: //fail
159: user = getJUnitUser();
160: user.setLastReset(calOld.getTimeInMillis());
161: action(Trial.PERIOD_DAILY);
162: assertUserFailed();
163:
164: //pass
165: user = getJUnitUser();
166: user.setLastReset(calOld.getTimeInMillis());
167: user.setUploadedBytes(TESTBYTES);
168: action(Trial.PERIOD_DAILY);
169: assertUserPassed();
170:
171: //neither (regular fail for daily reset)
172: if (period != Trial.PERIOD_DAILY) {
173: user = getJUnitUser();
174: action(Trial.PERIOD_DAILY);
175: assertUserNeither();
176: }
177: }
178:
179: private void internalTestRegular() throws Exception {
180: internalSetUp();
181: cal.add(period, 2);
182:
183: //pass real period
184: user = getJUnitUser();
185: user.setUploadedBytesForTrialPeriod(period, TESTBYTES);
186: action();
187: assertUserPassed();
188:
189: user = getJUnitUser();
190: action();
191: assertUserFailed();
192: }
193:
194: protected void setUp() throws Exception {
195: BasicConfigurator.configure();
196: }
197:
198: protected void tearDown() throws Exception {
199: }
200:
201: public void testDayBeforeUnique() throws Exception {
202: period = Trial.PERIOD_DAILY;
203: internalTestBeforeUnique();
204: }
205:
206: public void testDayUnique() throws Exception {
207: period = Trial.PERIOD_DAILY;
208: internalTestUnique();
209: }
210:
211: public void testDayRegular() throws Exception {
212: period = Trial.PERIOD_DAILY;
213: internalTestRegular();
214: }
215:
216: public void testDayUniqueAfterUnique() throws Exception {
217: period = Trial.PERIOD_DAILY;
218: internalTestUniqueAfterUnique();
219: }
220:
221: public void testMonthBeforeUnique() throws Exception {
222: period = Trial.PERIOD_MONTHLY;
223: internalTestBeforeUnique();
224: }
225:
226: public void testMonthUnique() throws Exception {
227: period = Trial.PERIOD_MONTHLY;
228: internalTestUnique();
229: }
230:
231: public void testMonthRegular() throws Exception {
232: period = Trial.PERIOD_MONTHLY;
233: internalTestRegular();
234: }
235:
236: public void testMonthUniqueAfterUnique() throws Exception {
237: period = Trial.PERIOD_MONTHLY;
238: internalTestUniqueAfterUnique();
239: }
240:
241: public void testWeekBeforeUnique() throws Exception {
242: period = Trial.PERIOD_WEEKLY;
243: internalTestBeforeUnique();
244: }
245:
246: public void testWeekRegular() throws Exception {
247: period = Trial.PERIOD_WEEKLY;
248: internalTestRegular();
249: }
250:
251: public void testWeekUniqueAfterUnique() throws Exception {
252: period = Trial.PERIOD_WEEKLY;
253: internalTestUniqueAfterUnique();
254: }
255:
256: public void testWeekUnique() throws Exception {
257: period = Trial.PERIOD_WEEKLY;
258: internalTestUnique();
259: }
260:
261: public void testGetCalendarEndOfWeek() {
262: Locale.setDefault(Locale.ENGLISH);
263: assertEquals(Calendar.SATURDAY, Trial
264: .getCalendarForEndOfPeriod(Trial.PERIOD_WEEKLY).get(
265: Calendar.DAY_OF_WEEK));
266:
267: //Locale.setDefault(new Locale("sv", "SE"));
268: Locale.setDefault(Locale.FRANCE);
269: assertEquals(Calendar.SUNDAY, Trial.getCalendarForEndOfPeriod(
270: Trial.PERIOD_WEEKLY).get(Calendar.DAY_OF_WEEK));
271: }
272:
273: /**
274: * Sets up trial, cal and user.
275: * period must be set before internalSetUp() is called because of Limit period.
276: */
277: private void internalSetUp() throws Exception {
278: trial = getJUnitTrial();
279: cal = getJUnitCalendar();
280: Logger.getLogger(TrialTest.class).debug(
281: "cal = " + cal.getTime());
282: user = getJUnitUser();
283: }
284:
285: public static String getCommandFromPeriod(int period) {
286: switch (period) {
287: case Trial.PERIOD_DAILY:
288: return "RESETDAY";
289:
290: case Trial.PERIOD_MONTHLY:
291: return "RESETMONTH";
292:
293: case Trial.PERIOD_WEEKLY:
294: return "RESETWEEK";
295:
296: default:
297: throw new RuntimeException();
298: }
299: }
300: }
|