1 18 package net.sf.drftpd.event.listeners; 19 20 import java.util.Calendar ; 21 import java.util.Locale ; 22 import java.util.Properties ; 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 37 public class TrialTest extends TestCase { 38 41 private static final long RESETTIME = 1071519356421L; 42 private static final long TESTBYTES = Bytes.parseBytes("100m"); 43 public static TestSuite suite() { 44 return new TestSuite(TrialTest.class); 48 } 49 private Calendar cal; 50 private int period; 51 private Trial trial; 52 private DummyUser user; 53 54 public TrialTest(String 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 getJUnitCalendar() { 76 Calendar cal = Calendar.getInstance(); 77 cal.setTimeInMillis(RESETTIME); 78 CalendarUtils.ceilAllLessThanDay(cal); 79 return cal; 80 } 81 82 private Trial getJUnitTrial() throws Exception { 83 Properties p = new Properties (); 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 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 { 110 internalSetUp(); 112 action(Trial.PERIOD_DAILY); 113 assertUserNeither(); 114 } 115 116 private void internalTestUnique() throws Exception { 117 internalSetUp(); 118 cal.add(period, 1); 119 120 user = getJUnitUser(); 122 action(Trial.PERIOD_DAILY); 123 assertUserFailed(); 124 125 user = getJUnitUser(); 127 user.setUploadedBytes(TESTBYTES); 128 action(Trial.PERIOD_DAILY); 129 assertUserPassed(); 130 } 131 132 private void internalTestUniqueAfterUnique() throws Exception { 133 internalSetUp(); 134 cal.add(period, 1); 135 Calendar calOld = (Calendar ) cal.clone(); 136 cal.add(period, 1); 137 138 user = getJUnitUser(); 140 user.setLastReset(calOld.getTimeInMillis()); 141 action(Trial.PERIOD_DAILY); 142 assertUserFailed(); 143 144 user = getJUnitUser(); 146 user.setLastReset(calOld.getTimeInMillis()); 147 user.setUploadedBytes(TESTBYTES); 148 action(Trial.PERIOD_DAILY); 149 assertUserPassed(); 150 151 if (period != Trial.PERIOD_DAILY) { 153 user = getJUnitUser(); 154 action(Trial.PERIOD_DAILY); 155 assertUserNeither(); 156 } 157 } 158 159 private void internalTestRegular() throws Exception { 160 internalSetUp(); 161 cal.add(period, 2); 162 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 { 174 BasicConfigurator.configure(); 175 } 176 177 protected void tearDown() throws Exception { 178 } 179 180 public void testDayBeforeUnique() throws Exception { 181 period = Trial.PERIOD_DAILY; 182 internalTestBeforeUnique(); 183 } 184 185 public void testDayUnique() throws Exception { 186 period = Trial.PERIOD_DAILY; 187 internalTestUnique(); 188 } 189 190 public void testDayRegular() throws Exception { 191 period = Trial.PERIOD_DAILY; 192 internalTestRegular(); 193 } 194 195 public void testDayUniqueAfterUnique() throws Exception { 196 period = Trial.PERIOD_DAILY; 197 internalTestUniqueAfterUnique(); 198 } 199 200 public void testMonthBeforeUnique() throws Exception { 201 period = Trial.PERIOD_MONTHLY; 202 internalTestBeforeUnique(); 203 } 204 205 public void testMonthUnique() throws Exception { 206 period = Trial.PERIOD_MONTHLY; 207 internalTestUnique(); 208 } 209 210 public void testMonthRegular() throws Exception { 211 period = Trial.PERIOD_MONTHLY; 212 internalTestRegular(); 213 } 214 215 public void testMonthUniqueAfterUnique() throws Exception { 216 period = Trial.PERIOD_MONTHLY; 217 internalTestUniqueAfterUnique(); 218 } 219 220 public void testWeekBeforeUnique() throws Exception { 221 period = Trial.PERIOD_WEEKLY; 222 internalTestBeforeUnique(); 223 } 224 225 public void testWeekRegular() throws Exception { 226 period = Trial.PERIOD_WEEKLY; 227 internalTestRegular(); 228 } 229 230 public void testWeekUniqueAfterUnique() throws Exception { 231 period = Trial.PERIOD_WEEKLY; 232 internalTestUniqueAfterUnique(); 233 } 234 235 public void testWeekUnique() throws Exception { 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(Locale.FRANCE); 246 assertEquals(Calendar.SUNDAY, Trial.getCalendarForEndOfPeriod(Trial.PERIOD_WEEKLY).get(Calendar.DAY_OF_WEEK)); 247 } 248 252 private void internalSetUp() throws Exception { 253 trial = getJUnitTrial(); 254 cal = getJUnitCalendar(); 255 user = getJUnitUser(); 256 } 257 } 258 | Popular Tags |