KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > master > usermanager > AbstractUserTest


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.master.usermanager;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Arrays JavaDoc;
22 import java.util.Calendar JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import junit.framework.TestCase;
28 import junit.framework.TestSuite;
29 import net.sf.drftpd.event.Event;
30 import net.sf.drftpd.event.UserEvent;
31 import net.sf.drftpd.master.ConnectionManager;
32 import net.sf.drftpd.util.CalendarUtils;
33
34 import org.apache.log4j.BasicConfigurator;
35 import org.apache.log4j.Logger;
36 import org.drftpd.tests.DummyUser;
37
38 /**
39  * @author mog
40  * @version $Id: AbstractUserTest.java,v 1.1 2004/06/01 15:40:31 mog Exp $
41  */

42 public class AbstractUserTest extends TestCase {
43
44     private static class CM extends ConnectionManager {
45         public ArrayList JavaDoc events = new ArrayList JavaDoc();
46         public void dispatchFtpEvent(Event event) {
47             events.add(event);
48         }
49     }
50     private static final Logger logger =
51         Logger.getLogger(AbstractUserTest.class);
52     /**
53      * Mon Dec 15 23:59:59 CET 2003
54      */

55     private static final long RESETTIME = 1071519356421L;
56
57     public static TestSuite suite() {
58         return new TestSuite(AbstractUserTest.class);
59     }
60     private static void verifyEvents(ArrayList JavaDoc eventNames, ArrayList JavaDoc events) {
61         logger.debug(events);
62         i1 : for (Iterator JavaDoc i1 = events.iterator(); i1.hasNext();) {
63             UserEvent event1 = (UserEvent) i1.next();
64             String JavaDoc eventName2 = null;
65             for (Iterator JavaDoc i2 = eventNames.iterator(); i2.hasNext();) {
66                 eventName2 = (String JavaDoc) i2.next();
67                 if (event1.getCommand().equals(eventName2)) {
68                     i2.remove();
69                     //if(eventNames.size() == 0) return;
70
continue i1;
71                 }
72             }
73             if (eventName2 == null)
74                 throw new RuntimeException JavaDoc(
75                     "no eventNames to look for when looking for "
76                         + event1.getCommand());
77             throw new RuntimeException JavaDoc(
78                 eventName2 + " was expected but wasn't found in " + events);
79         }
80         if (eventNames.size() != 0)
81             throw new RuntimeException JavaDoc(
82                 "The events "
83                     + eventNames
84                     + " were not in the list of expected events ("
85                     + events
86                     + ")");
87     }
88     private static void verifyEvents(List JavaDoc tmpEventNames, ArrayList JavaDoc events) {
89         verifyEvents(new ArrayList JavaDoc(tmpEventNames), events);
90     }
91
92     private CM _cm;
93     private Calendar JavaDoc _resetCal;
94     private DummyUser _user;
95
96     public AbstractUserTest(String JavaDoc fName) {
97         super(fName);
98     }
99     public void resetSetUp() {
100         _cm = new CM();
101
102         _user = new DummyUser("junit");
103         _user.setLastReset(RESETTIME - 10000);
104
105         _resetCal = Calendar.getInstance();
106         _resetCal.setTimeInMillis(RESETTIME);
107     }
108     public void setUp() {
109         BasicConfigurator.configure();
110     }
111
112     public void testResetDay() throws UserFileException {
113         resetSetUp();
114         _resetCal.add(Calendar.DAY_OF_MONTH, 1);
115         CalendarUtils.floorAllLessThanDay(_resetCal);
116         logger.debug("resetDay lastreset " + new Date JavaDoc(_user.getLastReset()));
117         logger.debug("resetDay date " + _resetCal.getTime());
118         _user.reset(_cm, _resetCal);
119         verifyEvents(Arrays.asList(new String JavaDoc[] { "RESETDAY" }), _cm.events);
120     }
121
122     public void testResetMonth() throws UserFileException {
123         resetSetUp();
124         _resetCal.add(Calendar.MONTH, 1);
125         CalendarUtils.floorDayOfMonth(_resetCal);
126         CalendarUtils.floorAllLessThanDay(_resetCal);
127         logger.debug("resetMonth lastreset " + new Date JavaDoc(_user.getLastReset()));
128         logger.debug("resetMonth date " + _resetCal.getTime());
129         _user.reset(_cm, _resetCal);
130         verifyEvents(
131             Arrays.asList(
132                 new String JavaDoc[] { "RESETDAY", "RESETWEEK", "RESETMONTH" }),
133             _cm.events);
134     }
135     public void testResetNone1() throws UserFileException {
136         resetSetUp();
137         CalendarUtils.ceilAllLessThanDay(_resetCal);
138         _user.reset(_cm, _resetCal);
139     }
140
141     public void testResetNone2() throws UserFileException {
142         resetSetUp();
143         CalendarUtils.floorAllLessThanDay(_resetCal);
144         _user.reset(_cm, _resetCal);
145     }
146
147     public void testResetWeek() throws UserFileException {
148         resetSetUp();
149         _resetCal.add(Calendar.WEEK_OF_MONTH, 1);
150         CalendarUtils.floorDayOfWeek(_resetCal);
151         CalendarUtils.floorAllLessThanDay(_resetCal);
152         logger.debug("resetWeek lastreset " + new Date JavaDoc(_user.getLastReset()));
153         logger.debug("resetWeek date " + _resetCal.getTime());
154         _user.reset(_cm, _resetCal);
155         verifyEvents(
156             Arrays.asList(new String JavaDoc[] { "RESETDAY", "RESETWEEK" }),
157             _cm.events);
158     }
159 }
160
Popular Tags