2 * Copyright 2000-2016 Vaadin Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
16 package com.vaadin.v7.tests.server.component.calendar;
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertTrue;
21 import java.util.Date;
22 import java.util.List;
24 import org.junit.Before;
25 import org.junit.Test;
27 import com.vaadin.v7.data.Container.Indexed;
28 import com.vaadin.v7.data.Container.Sortable;
29 import com.vaadin.v7.data.Item;
30 import com.vaadin.v7.data.Property;
31 import com.vaadin.v7.data.util.BeanItemContainer;
32 import com.vaadin.v7.data.util.IndexedContainer;
33 import com.vaadin.v7.ui.Calendar;
34 import com.vaadin.v7.ui.components.calendar.ContainerEventProvider;
35 import com.vaadin.v7.ui.components.calendar.event.BasicEvent;
36 import com.vaadin.v7.ui.components.calendar.event.CalendarEvent;
38 public class ContainerDataSourceTest {
40 private Calendar calendar;
44 calendar = new Calendar();
48 * Tests adding a bean item container to the Calendar
51 public void testWithBeanItemContainer() {
53 // Create a container to use as a datasource
54 Indexed container = createTestBeanItemContainer();
57 calendar.setContainerDataSource(container);
59 // Start and end dates to query for
60 java.util.Calendar cal = java.util.Calendar.getInstance();
61 cal.setTime(((CalendarEvent) container.getIdByIndex(0)).getStart());
62 Date start = cal.getTime();
63 cal.add(java.util.Calendar.MONTH, 1);
64 Date end = cal.getTime();
66 // Test the all events are returned
67 List<CalendarEvent> events = calendar.getEventProvider()
68 .getEvents(start, end);
69 assertEquals(container.size(), events.size());
71 // Test that a certain range is returned
72 cal.setTime(((CalendarEvent) container.getIdByIndex(6)).getStart());
74 events = calendar.getEventProvider().getEvents(start, end);
75 assertEquals(6, events.size());
79 * This tests tests that if you give the Calendar an unsorted (== not sorted
80 * by starting date) container then the calendar should gracefully handle
81 * it. In this case the size of the container will be wrong. The test is
82 * exactly the same as {@link #testWithBeanItemContainer()} except that the
83 * beans has been intentionally sorted by caption instead of date.
86 public void testWithUnsortedBeanItemContainer() {
87 // Create a container to use as a datasource
88 Indexed container = createTestBeanItemContainer();
90 // Make the container sorted by caption
91 ((Sortable) container).sort(new Object[] { "caption" },
92 new boolean[] { true });
95 calendar.setContainerDataSource(container);
97 // Start and end dates to query for
98 java.util.Calendar cal = java.util.Calendar.getInstance();
99 cal.setTime(((CalendarEvent) container.getIdByIndex(0)).getStart());
100 Date start = cal.getTime();
101 cal.add(java.util.Calendar.MONTH, 1);
102 Date end = cal.getTime();
104 // Test the all events are returned
105 List<CalendarEvent> events = calendar.getEventProvider()
106 .getEvents(start, end);
107 assertEquals(container.size(), events.size());
109 // Test that a certain range is returned
110 cal.setTime(((CalendarEvent) container.getIdByIndex(6)).getStart());
112 events = calendar.getEventProvider().getEvents(start, end);
114 // The events size is 1 since the getEvents returns the wrong range
115 assertEquals(1, events.size());
119 * Tests adding a Indexed container to the Calendar
122 public void testWithIndexedContainer() {
124 // Create a container to use as a datasource
125 Indexed container = createTestIndexedContainer();
128 calendar.setContainerDataSource(container, "testCaption",
129 "testDescription", "testStartDate", "testEndDate", null);
131 // Start and end dates to query for
132 java.util.Calendar cal = java.util.Calendar.getInstance();
133 cal.setTime((Date) container.getItem(container.getIdByIndex(0))
134 .getItemProperty("testStartDate").getValue());
135 Date start = cal.getTime();
136 cal.add(java.util.Calendar.MONTH, 1);
137 Date end = cal.getTime();
139 // Test the all events are returned
140 List<CalendarEvent> events = calendar.getEventProvider()
141 .getEvents(start, end);
142 assertEquals(container.size(), events.size());
144 // Check that event values are present
145 CalendarEvent e = events.get(0);
146 assertEquals("Test 1", e.getCaption());
147 assertEquals("Description 1", e.getDescription());
148 assertTrue(e.getStart().compareTo(start) == 0);
150 // Test that a certain range is returned
151 cal.setTime((Date) container.getItem(container.getIdByIndex(6))
152 .getItemProperty("testStartDate").getValue());
154 events = calendar.getEventProvider().getEvents(start, end);
155 assertEquals(6, events.size());
159 public void testNullLimitsBeanItemContainer() {
160 // Create a container to use as a datasource
161 Indexed container = createTestBeanItemContainer();
163 // Start and end dates to query for
164 java.util.Calendar cal = java.util.Calendar.getInstance();
165 cal.setTime(((CalendarEvent) container.getIdByIndex(0)).getStart());
166 Date start = cal.getTime();
167 cal.add(java.util.Calendar.MONTH, 1);
168 Date end = cal.getTime();
171 calendar.setContainerDataSource(container);
173 // Test null start time
174 List<CalendarEvent> events = calendar.getEventProvider().getEvents(null,
176 assertEquals(container.size(), events.size());
178 // Test null end time
179 events = calendar.getEventProvider().getEvents(start, null);
180 assertEquals(container.size(), events.size());
182 // Test both null times
183 events = calendar.getEventProvider().getEvents(null, null);
184 assertEquals(container.size(), events.size());
188 public void testNullLimitsIndexedContainer() {
189 // Create a container to use as a datasource
190 Indexed container = createTestIndexedContainer();
192 // Start and end dates to query for
193 java.util.Calendar cal = java.util.Calendar.getInstance();
194 cal.setTime((Date) container.getItem(container.getIdByIndex(0))
195 .getItemProperty("testStartDate").getValue());
196 Date start = cal.getTime();
197 cal.add(java.util.Calendar.MONTH, 1);
198 Date end = cal.getTime();
201 calendar.setContainerDataSource(container, "testCaption",
202 "testDescription", "testStartDate", "testEndDate", null);
204 // Test null start time
205 List<CalendarEvent> events = calendar.getEventProvider().getEvents(null,
207 assertEquals(container.size(), events.size());
209 // Test null end time
210 events = calendar.getEventProvider().getEvents(start, null);
211 assertEquals(container.size(), events.size());
213 // Test both null times
214 events = calendar.getEventProvider().getEvents(null, null);
215 assertEquals(container.size(), events.size());
219 * Tests the addEvent convenience method with the default event provider
222 public void testAddEventConvinienceMethod() {
224 // Start and end dates to query for
225 java.util.Calendar cal = java.util.Calendar.getInstance();
226 Date start = cal.getTime();
227 cal.add(java.util.Calendar.MONTH, 1);
228 Date end = cal.getTime();
231 assertEquals(0, calendar.getEvents(start, end).size());
234 BasicEvent event = new BasicEvent("Test", "Test", start);
235 calendar.addEvent(event);
237 // Ensure event exists
238 List<CalendarEvent> events = calendar.getEvents(start, end);
239 assertEquals(1, events.size());
240 assertEquals(events.get(0).getCaption(), event.getCaption());
241 assertEquals(events.get(0).getDescription(), event.getDescription());
242 assertEquals(events.get(0).getStart(), event.getStart());
246 * Test the removeEvent convenience method with the default event provider
249 public void testRemoveEventConvinienceMethod() {
251 // Start and end dates to query for
252 java.util.Calendar cal = java.util.Calendar.getInstance();
253 Date start = cal.getTime();
254 cal.add(java.util.Calendar.MONTH, 1);
255 Date end = cal.getTime();
258 assertEquals(0, calendar.getEvents(start, end).size());
261 CalendarEvent event = new BasicEvent("Test", "Test", start);
262 calendar.addEvent(event);
264 // Ensure event exists
265 assertEquals(1, calendar.getEvents(start, end).size());
268 calendar.removeEvent(event);
271 assertEquals(0, calendar.getEvents(start, end).size());
275 public void testAddEventConvinienceMethodWithCustomEventProvider() {
277 // Use a container data source
278 calendar.setEventProvider(new ContainerEventProvider(
279 new BeanItemContainer<BasicEvent>(BasicEvent.class)));
281 // Start and end dates to query for
282 java.util.Calendar cal = java.util.Calendar.getInstance();
283 Date start = cal.getTime();
284 cal.add(java.util.Calendar.MONTH, 1);
285 Date end = cal.getTime();
288 assertEquals(0, calendar.getEvents(start, end).size());
291 BasicEvent event = new BasicEvent("Test", "Test", start);
292 calendar.addEvent(event);
294 // Ensure event exists
295 List<CalendarEvent> events = calendar.getEvents(start, end);
296 assertEquals(1, events.size());
297 assertEquals(events.get(0).getCaption(), event.getCaption());
298 assertEquals(events.get(0).getDescription(), event.getDescription());
299 assertEquals(events.get(0).getStart(), event.getStart());
303 public void testRemoveEventConvinienceMethodWithCustomEventProvider() {
305 // Use a container data source
306 calendar.setEventProvider(new ContainerEventProvider(
307 new BeanItemContainer<BasicEvent>(BasicEvent.class)));
309 // Start and end dates to query for
310 java.util.Calendar cal = java.util.Calendar.getInstance();
311 Date start = cal.getTime();
312 cal.add(java.util.Calendar.MONTH, 1);
313 Date end = cal.getTime();
316 assertEquals(0, calendar.getEvents(start, end).size());
319 BasicEvent event = new BasicEvent("Test", "Test", start);
320 calendar.addEvent(event);
322 // Ensure event exists
323 List<CalendarEvent> events = calendar.getEvents(start, end);
324 assertEquals(1, events.size());
327 calendar.removeEvent(event);
330 assertEquals(0, calendar.getEvents(start, end).size());
334 public void testStyleNamePropertyRetrieved() {
335 IndexedContainer ic = (IndexedContainer) createTestIndexedContainer();
336 ic.addContainerProperty("testStyleName", String.class, "");
337 for (int i = 0; i < 10; i++) {
338 Item item = ic.getItem(ic.getIdByIndex(i));
339 @SuppressWarnings("unchecked")
340 Property<String> itemProperty = item
341 .getItemProperty("testStyleName");
342 itemProperty.setValue("testStyle");
345 ContainerEventProvider provider = new ContainerEventProvider(ic);
346 provider.setCaptionProperty("testCaption");
347 provider.setDescriptionProperty("testDescription");
348 provider.setStartDateProperty("testStartDate");
349 provider.setEndDateProperty("testEndDate");
350 provider.setStyleNameProperty("testStyleName");
352 calendar.setEventProvider(provider);
353 java.util.Calendar cal = java.util.Calendar.getInstance();
354 Date now = cal.getTime();
355 cal.add(java.util.Calendar.DAY_OF_MONTH, 20);
356 Date then = cal.getTime();
357 List<CalendarEvent> events = calendar.getEventProvider().getEvents(now,
359 for (CalendarEvent ce : events) {
360 assertEquals("testStyle", ce.getStyleName());
364 private static Indexed createTestBeanItemContainer() {
365 BeanItemContainer<CalendarEvent> eventContainer = new BeanItemContainer<CalendarEvent>(
366 CalendarEvent.class);
367 java.util.Calendar cal = java.util.Calendar.getInstance();
368 for (int i = 1; i <= 10; i++) {
369 eventContainer.addBean(new BasicEvent("Test " + i,
370 "Description " + i, cal.getTime()));
371 cal.add(java.util.Calendar.DAY_OF_MONTH, 2);
373 return eventContainer;
376 private static Indexed createTestIndexedContainer() {
377 IndexedContainer container = new IndexedContainer();
378 container.addContainerProperty("testCaption", String.class, "");
379 container.addContainerProperty("testDescription", String.class, "");
380 container.addContainerProperty("testStartDate", Date.class, null);
381 container.addContainerProperty("testEndDate", Date.class, null);
383 java.util.Calendar cal = java.util.Calendar.getInstance();
384 for (int i = 1; i <= 10; i++) {
385 Item item = container.getItem(container.addItem());
386 item.getItemProperty("testCaption").setValue("Test " + i);
387 item.getItemProperty("testDescription")
388 .setValue("Description " + i);
389 item.getItemProperty("testStartDate").setValue(cal.getTime());
390 item.getItemProperty("testEndDate").setValue(cal.getTime());
391 cal.add(java.util.Calendar.DAY_OF_MONTH, 2);