JSFComponentTest.java
6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* License Agreement.
*
* Rich Faces - Natural Ajax for Java Server Faces (JSF)
*
* Copyright (C) 2007 Exadel, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.richfaces.component;
import org.ajax4jsf.tests.AbstractAjax4JsfTestCase;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.StringTokenizer;
/**
* Unit test for simple Component.
*/
public class JSFComponentTest extends AbstractAjax4JsfTestCase {
public JSFComponentTest(String testName) {
super(testName);
}
/**
* Checks if component properly calculates date ranges of displayed days
* in order to provide initial events.
*
* @throws IOException if file with test data cannot be read
* @see JSFComponentTest#doTestDisplayedDays(java.lang.String, java.lang.String)
*/
public void testDisplayedDaysForMonthViewCalculation() throws IOException {
doTestDisplayedDays("displayedDaysForMonthViewCalculationTestData", UISchedule.VIEW_MONTH);
}
/**
* Checks if component properly calculates date ranges of displayed days
* in order to provide initial events.
*
* @throws IOException if file with test data cannot be read
* @see JSFComponentTest#doTestDisplayedDays(java.lang.String, java.lang.String)
*/
public void testDisplayedDaysForWeekViewCalculation() throws IOException {
doTestDisplayedDays("displayedDaysForWeekViewCalculationTestData", UISchedule.VIEW_AGENDA_WEEK);
doTestDisplayedDays("displayedDaysForWeekViewCalculationTestData", UISchedule.VIEW_BASIC_WEEK);
}
/**
* Checks if component properly calculates date ranges of displayed days
* in order to provide initial events.
*
* @throws IOException if file with test data cannot be read
* @see JSFComponentTest#doTestDisplayedDays(java.lang.String, java.lang.String)
*/
public void testDisplayedDaysForDayViewCalculation() throws IOException {
doTestDisplayedDays("displayedDaysForDayViewCalculationTestData", UISchedule.VIEW_AGENDA_DAY);
doTestDisplayedDays("displayedDaysForDayViewCalculationTestData", UISchedule.VIEW_BASIC_DAY);
}
/**
* Checks if component properly calculates date ranges of displayed days
* in order to provide initial events.
*
* @param fileNameProperty name of file with test data
* @param view view name for which calculation should be tested
* @throws IOException if file with test data cannot be read
*/
private void doTestDisplayedDays(String fileNameProperty, String view) throws IOException {
Calendar calendar = Calendar.getInstance();
UISchedule schedule = (UISchedule) application.createComponent(UISchedule.COMPONENT_TYPE);
Calendar expectedFirstDisplayedDay, expectedLastDisplayedDay;
final String fileName = System.getProperty(fileNameProperty);
System.out.println("filename:" + fileName);
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line;
int lineNr = 0;
int firstDay;
boolean showWeekends;
StringTokenizer tokenizer;
try {
while ((line = reader.readLine()) != null) {
lineNr++;
System.out.print("line " + lineNr + ": " + line);
try {
tokenizer = new StringTokenizer(line, ",");
firstDay = Integer.parseInt(tokenizer.nextToken());
showWeekends = Integer.parseInt(tokenizer.nextToken()) == 1;
calendar.set(Calendar.YEAR, Integer.parseInt(tokenizer.nextToken()));
calendar.set(Calendar.MONTH, Integer.parseInt(tokenizer.nextToken()));
calendar.set(Calendar.DATE, Integer.parseInt(tokenizer.nextToken()));
Date initialDate = calendar.getTime();
calendar.set(Calendar.YEAR, Integer.parseInt(tokenizer.nextToken()));
calendar.set(Calendar.MONTH, Integer.parseInt(tokenizer.nextToken()));
calendar.set(Calendar.DATE, Integer.parseInt(tokenizer.nextToken()));
expectedFirstDisplayedDay = (Calendar) calendar.clone();
calendar.set(Calendar.YEAR, Integer.parseInt(tokenizer.nextToken()));
calendar.set(Calendar.MONTH, Integer.parseInt(tokenizer.nextToken()));
calendar.set(Calendar.DATE, Integer.parseInt(tokenizer.nextToken()));
expectedLastDisplayedDay = (Calendar) calendar.clone();
schedule.setShowWeekends(showWeekends);
schedule.setFirstDay(firstDay);
schedule.setDate(initialDate);
schedule.setView(view);
calendar.setTime(UISchedule.getFirstDisplayedDay(schedule));
System.out.print(" result: " + calendar.get(Calendar.YEAR) + "," + calendar.get(Calendar.MONTH) + "," + calendar.get(Calendar.DATE));
assert expectedFirstDisplayedDay.get(Calendar.YEAR) == calendar.get(Calendar.YEAR);
assert expectedFirstDisplayedDay.get(Calendar.MONTH) == calendar.get(Calendar.MONTH);
assert expectedFirstDisplayedDay.get(Calendar.DATE) == calendar.get(Calendar.DATE);
calendar.setTime(UISchedule.getLastDisplayedDate(schedule));
System.out.print("," + calendar.get(Calendar.YEAR) + "," + calendar.get(Calendar.MONTH) + "," + calendar.get(Calendar.DATE));
assert expectedLastDisplayedDay.get(Calendar.YEAR) == calendar.get(Calendar.YEAR);
assert expectedLastDisplayedDay.get(Calendar.MONTH) == calendar.get(Calendar.MONTH);
assert expectedLastDisplayedDay.get(Calendar.DATE) == calendar.get(Calendar.DATE);
} catch (Exception e) {
throw new RuntimeException("Exception during processing of line " + lineNr + " of test data. Line: " + line, e);
} finally {
System.out.println("");
}
}
} finally {
reader.close();
}
}
}