abdullah
11-01-2004, 02:37 PM
assalamu 'alaikum,
i wrote a class which is supposed to be run as a thread in the main application. what it does is basically calculate the time when it needs to wake up and play an audio file. it does this, based on system time.
i have no idea of how i can write a junit test (to test this class, even if it was run as the main class.. or is that a bad idea?) ... if anyone can point me in the right direction that will be great.
the other alternative is to monitor it for 2 days, get the bugs, and rectify.. and wait for another 2 days..but that will take a very long time.. and i want to get this done as soon as possible.
just in case my question is not clear, i have attached the source code for the whole class. i would paste the code here, but the file is 230 lines.
if anyone can help me with the algorithm, that will be great too.
thanks in advance..
EDIT:
i thought i would post the run() method here..if any1 doesnt want to view the attachment
public void run() {
java.util.Date date = new java.util.Date();
//if times stored are not for today, then update them
if (sdate.getDate() != date.getDate() || sdate.getMonth() != date.getMonth() || sdate.getYear() != date.getYear()) {
updatePrayerTimes(date);
}
int ph,pm,ps;
//need an algorithm for this...
if (time == -1) {
int dh = date.getHours();
int dm = date.getMinutes();
int ds = date.getSeconds();
int j = -1;
for (int i=0; i<6; i++) {
if (i == 0)
j = 5;
else
j = i-1;
if (i == 1)
continue;
if (i == 2) {
if (ptlist[2].getHour() > ptlist[3].getHour()) { //this means dhur starts in pm
if (ptlist[2].getHour() == 12)
ph = 12;
else
ph = 12 + ptlist[2].getHour();
} else {
ph = ptlist[2].getHour();
}
pm = ptlist[2].getHour();
} else {
ph = ptlist[i].getHour();
pm = ptlist[i].getMinute();
}
if (dh < ph) {
time = j;
break;
}
if (dh == ph && dm < pm) {
time = j;
break;
}
if (dh == ph && dm == pm && ds < ptlist[0].getSecond()) {
time = j;
break;
}
/*
if (dh < ph)
time = i;
if (dh == ph && dm < pm)
time = i;
if (dh == ph && dm == pm && ds < ptlist[0].getSecond())
time = i;
*/
}
}
System.out.println("TIME OUT OF FOR: "+time+"");
while (true) {
int j, tot;
java.util.Date d2 = new java.util.Date();
if (time == 5)
j = 0;
else
j = time+1;
System.out.println("TIME IN WHILE: "+time+"");
System.out.println("J IN WHILE: "+j+"");
//get the time difference between now and the time for next prayer
if (time == 5) { //then fajr will be next day
int month = date.getMonth();
int d = date.getDate();
if (month == 2) { //TODO: correct the algorithm for leap years
tot = 28;
} else if ((month % 2) == 0) {
tot = 31;
} else {
tot = 30;
}
if (date.getDate() == tot) {
d2.setMonth(month+1);
d2.setDate(1);
} else {
d2.setDate(d+1);
}
}
updatePrayerTimes(d2);
System.out.println("D2 :"+d2.toString());
d2.setHours(ptlist[j].getHour());
d2.setMinutes(ptlist[j].getMinute());
d2.setSeconds(ptlist[j].getSecond());
long diff = d2.getTime() - date.getTime();
//now that we have time difference, we play the adhan and we sleep
playAdhan();
try {
Thread.sleep(diff + 1000); //sleep for 1 second more than required, to be safe
} catch (Exception e) {
System.out.println(e.toString());
}
time = j;
}
}
i wrote a class which is supposed to be run as a thread in the main application. what it does is basically calculate the time when it needs to wake up and play an audio file. it does this, based on system time.
i have no idea of how i can write a junit test (to test this class, even if it was run as the main class.. or is that a bad idea?) ... if anyone can point me in the right direction that will be great.
the other alternative is to monitor it for 2 days, get the bugs, and rectify.. and wait for another 2 days..but that will take a very long time.. and i want to get this done as soon as possible.
just in case my question is not clear, i have attached the source code for the whole class. i would paste the code here, but the file is 230 lines.
if anyone can help me with the algorithm, that will be great too.
thanks in advance..
EDIT:
i thought i would post the run() method here..if any1 doesnt want to view the attachment
public void run() {
java.util.Date date = new java.util.Date();
//if times stored are not for today, then update them
if (sdate.getDate() != date.getDate() || sdate.getMonth() != date.getMonth() || sdate.getYear() != date.getYear()) {
updatePrayerTimes(date);
}
int ph,pm,ps;
//need an algorithm for this...
if (time == -1) {
int dh = date.getHours();
int dm = date.getMinutes();
int ds = date.getSeconds();
int j = -1;
for (int i=0; i<6; i++) {
if (i == 0)
j = 5;
else
j = i-1;
if (i == 1)
continue;
if (i == 2) {
if (ptlist[2].getHour() > ptlist[3].getHour()) { //this means dhur starts in pm
if (ptlist[2].getHour() == 12)
ph = 12;
else
ph = 12 + ptlist[2].getHour();
} else {
ph = ptlist[2].getHour();
}
pm = ptlist[2].getHour();
} else {
ph = ptlist[i].getHour();
pm = ptlist[i].getMinute();
}
if (dh < ph) {
time = j;
break;
}
if (dh == ph && dm < pm) {
time = j;
break;
}
if (dh == ph && dm == pm && ds < ptlist[0].getSecond()) {
time = j;
break;
}
/*
if (dh < ph)
time = i;
if (dh == ph && dm < pm)
time = i;
if (dh == ph && dm == pm && ds < ptlist[0].getSecond())
time = i;
*/
}
}
System.out.println("TIME OUT OF FOR: "+time+"");
while (true) {
int j, tot;
java.util.Date d2 = new java.util.Date();
if (time == 5)
j = 0;
else
j = time+1;
System.out.println("TIME IN WHILE: "+time+"");
System.out.println("J IN WHILE: "+j+"");
//get the time difference between now and the time for next prayer
if (time == 5) { //then fajr will be next day
int month = date.getMonth();
int d = date.getDate();
if (month == 2) { //TODO: correct the algorithm for leap years
tot = 28;
} else if ((month % 2) == 0) {
tot = 31;
} else {
tot = 30;
}
if (date.getDate() == tot) {
d2.setMonth(month+1);
d2.setDate(1);
} else {
d2.setDate(d+1);
}
}
updatePrayerTimes(d2);
System.out.println("D2 :"+d2.toString());
d2.setHours(ptlist[j].getHour());
d2.setMinutes(ptlist[j].getMinute());
d2.setSeconds(ptlist[j].getSecond());
long diff = d2.getTime() - date.getTime();
//now that we have time difference, we play the adhan and we sleep
playAdhan();
try {
Thread.sleep(diff + 1000); //sleep for 1 second more than required, to be safe
} catch (Exception e) {
System.out.println(e.toString());
}
time = j;
}
}