1 18 package net.sf.drftpd.master.usermanager; 19 20 import java.util.ArrayList ; 21 import java.util.Arrays ; 22 import java.util.Calendar ; 23 import java.util.Date ; 24 import java.util.Iterator ; 25 import java.util.List ; 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 42 public class AbstractUserTest extends TestCase { 43 44 private static class CM extends ConnectionManager { 45 public ArrayList events = new ArrayList (); 46 public void dispatchFtpEvent(Event event) { 47 events.add(event); 48 } 49 } 50 private static final Logger logger = 51 Logger.getLogger(AbstractUserTest.class); 52 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 eventNames, ArrayList events) { 61 logger.debug(events); 62 i1 : for (Iterator i1 = events.iterator(); i1.hasNext();) { 63 UserEvent event1 = (UserEvent) i1.next(); 64 String eventName2 = null; 65 for (Iterator i2 = eventNames.iterator(); i2.hasNext();) { 66 eventName2 = (String ) i2.next(); 67 if (event1.getCommand().equals(eventName2)) { 68 i2.remove(); 69 continue i1; 71 } 72 } 73 if (eventName2 == null) 74 throw new RuntimeException ( 75 "no eventNames to look for when looking for " 76 + event1.getCommand()); 77 throw new RuntimeException ( 78 eventName2 + " was expected but wasn't found in " + events); 79 } 80 if (eventNames.size() != 0) 81 throw new RuntimeException ( 82 "The events " 83 + eventNames 84 + " were not in the list of expected events (" 85 + events 86 + ")"); 87 } 88 private static void verifyEvents(List tmpEventNames, ArrayList events) { 89 verifyEvents(new ArrayList (tmpEventNames), events); 90 } 91 92 private CM _cm; 93 private Calendar _resetCal; 94 private DummyUser _user; 95 96 public AbstractUserTest(String 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 (_user.getLastReset())); 117 logger.debug("resetDay date " + _resetCal.getTime()); 118 _user.reset(_cm, _resetCal); 119 verifyEvents(Arrays.asList(new String [] { "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 (_user.getLastReset())); 128 logger.debug("resetMonth date " + _resetCal.getTime()); 129 _user.reset(_cm, _resetCal); 130 verifyEvents( 131 Arrays.asList( 132 new String [] { "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 (_user.getLastReset())); 153 logger.debug("resetWeek date " + _resetCal.getTime()); 154 _user.reset(_cm, _resetCal); 155 verifyEvents( 156 Arrays.asList(new String [] { "RESETDAY", "RESETWEEK" }), 157 _cm.events); 158 } 159 } 160 | Popular Tags |