Updated object store tests and added tests for search UIDs
[jgroupdav.git] / src / test / java / CalDAVExtensions.java
blob840bfee6d3468b25f24852fbea1d47205b121631
1 /*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
6 import java.io.File;
7 import java.io.FileInputStream;
8 import java.net.URL;
9 import java.util.ArrayList;
10 import java.util.HashMap;
11 import java.util.Iterator;
12 import java.util.List;
13 import java.util.Properties;
14 import net.bionicmessage.objects.MultipleSourceICalendarObjectStore;
15 import net.bionicmessage.objects.StoreConstants;
16 import net.fortuna.ical4j.data.CalendarBuilder;
17 import net.fortuna.ical4j.model.Calendar;
18 import net.fortuna.ical4j.model.ComponentList;
19 import net.fortuna.ical4j.model.component.CalendarComponent;
20 import net.fortuna.ical4j.model.component.VEvent;
21 import net.fortuna.ical4j.model.property.DtEnd;
22 import net.fortuna.ical4j.model.property.DtStart;
23 import net.fortuna.ical4j.model.property.Summary;
24 import net.fortuna.ical4j.model.property.Uid;
25 import net.fortuna.ical4j.model.property.Version;
26 import org.junit.After;
27 import org.junit.AfterClass;
28 import org.junit.Before;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import static org.junit.Assert.*;
33 /**
35 * @author matt
37 public class CalDAVExtensions {
39 public static final String TEST_PROPERTIES_FILE = ".jgroupdav_caldav_test";
40 private static Properties testProperties = null;
41 public static Calendar testDataCalendar = null;
42 protected HashMap<String, DtEnd> testData = null;
44 public CalDAVExtensions() {
47 @BeforeClass
48 public static void setUpClass() throws Exception {
49 String property_file_location = String.format("%s%s%s",
50 System.getProperty("user.home"),
51 File.separator,
52 TEST_PROPERTIES_FILE);
53 FileInputStream property_stream =
54 new FileInputStream(property_file_location);
55 testProperties = new Properties();
56 testProperties.load(property_stream);
57 File testFile = new File("additional_tests/test_data.ics");
58 FileInputStream testCalStream = new FileInputStream(testFile);
59 CalendarBuilder cbuild = new CalendarBuilder();
60 testDataCalendar = cbuild.build(testCalStream);
63 @AfterClass
64 public static void tearDownClass() throws Exception {
67 @Before
68 public void setUp() {
71 @After
72 public void tearDown() {
75 //@Test
76 public void testDateRangeSearch() throws Exception {
77 testData = new HashMap();
78 MultipleSourceICalendarObjectStore obtrack =
79 new MultipleSourceICalendarObjectStore(testProperties.getProperty("store.location"),
80 MultipleSourceICalendarObjectStore.OPTION_PURGE);
81 obtrack.setProperties(testProperties);
82 obtrack.loadSourcesFromProps();
83 obtrack.connect_storedpass();
84 obtrack.startSync();
85 // Are our testFiles already there?
86 ComponentList componentList = testDataCalendar.getComponents();
87 Iterator it = componentList.iterator();
88 while (it.hasNext()) {
89 VEvent event = (VEvent) it.next();
90 DtStart start = event.getStartDate();
91 DtEnd end = event.getEndDate();
92 Summary summary = event.getSummary();
93 Uid uid = event.getUid();
94 String existingUid = obtrack.searchUids(summary.getValue(),
95 start.getDate().getTime() / 1000, end.getDate().getTime() / 1000);
96 if (existingUid == null) {
97 Calendar component = new Calendar();
98 component.getProperties().add(Version.VERSION_2_0);
99 component.getComponents().add(event);
100 existingUid = obtrack.addObject("default", uid.getValue(),
101 summary.getValue(),
102 component.toString(),
103 start.getDate().getTime() / 1000,
104 end.getDate().getTime() / 1000);
106 testData.put(existingUid, end);
108 String serverHost = testProperties.getProperty("store.server");
109 URL srvURL = new URL(serverHost);
110 Iterator<String> uids = testData.keySet().iterator();
111 while (uids.hasNext()) {
112 String uid = uids.next();
113 DtEnd dt = testData.get(uid);
114 DtEnd dtEndPlusOne = (DtEnd) dt.copy();
115 java.util.Calendar manipulator = java.util.Calendar.getInstance();
116 manipulator.setTime(dtEndPlusOne.getDate());
117 manipulator.add(java.util.Calendar.WEEK_OF_MONTH, 1);
118 dtEndPlusOne.getDate().setTime(manipulator.getTimeInMillis());
119 List urlsForThisDtEnd = obtrack._listURLsByTimeRange("default", "VCALENDAR", dt, dtEndPlusOne);
120 obtrack.deleteObject(uid);
122 obtrack.close();
125 @Test
126 public void testStartSyncDateRange() throws Exception {
127 testData = new HashMap();
128 MultipleSourceICalendarObjectStore obtrack =
129 new MultipleSourceICalendarObjectStore(testProperties.getProperty("store.location"),
130 MultipleSourceICalendarObjectStore.OPTION_PURGE);
131 obtrack.setProperties(testProperties);
132 obtrack.loadSourcesFromProps();
133 obtrack.connect_storedpass();
134 obtrack.startSync();
135 // Are our testFiles already there?
136 ComponentList componentList = testDataCalendar.getComponents();
137 Iterator it = componentList.iterator();
138 while (it.hasNext()) {
139 VEvent event = (VEvent) it.next();
140 DtStart start = event.getStartDate();
141 DtEnd end = event.getEndDate();
142 Summary summary = event.getSummary();
143 Uid uid = event.getUid();
144 String existingUid = obtrack.searchUids(summary.getValue(),
145 start.getDate().getTime() / 1000, end.getDate().getTime() / 1000);
146 if (existingUid == null) {
147 Calendar component = new Calendar();
148 component.getProperties().add(Version.VERSION_2_0);
149 component.getComponents().add(event);
150 existingUid = obtrack.addObject("default", uid.getValue(),
151 summary.getValue(),
152 component.toString(),
153 start.getDate().getTime() / 1000,
154 end.getDate().getTime() / 1000);
156 testData.put(existingUid, end);
158 String serverHost = testProperties.getProperty("store.server");
159 URL srvURL = new URL(serverHost);
160 Iterator<String> uids = testData.keySet().iterator();
161 obtrack.close();
162 obtrack = null; // get rid of it
163 obtrack =
164 new MultipleSourceICalendarObjectStore(testProperties.getProperty("store.location"),
165 MultipleSourceICalendarObjectStore.OPTION_PURGE);
166 testProperties.setProperty(StoreConstants.CALDAV_COMPARE_COMPONENT, "VEVENT");
167 testProperties.setProperty(StoreConstants.CALDAV_COMPARE_PROPERTY, "DTSTART");
168 testProperties.setProperty(StoreConstants.CALDAV_COMPARE_START, "20080514T120000Z");
169 testProperties.setProperty(StoreConstants.CALDAV_COMPARE_END, "20080525T120000Z");
170 obtrack.setProperties(testProperties);
171 obtrack.loadSourcesFromProps();
172 obtrack.connect_storedpass();
173 obtrack.startSync();
174 boolean doesHave5May = true;
175 List itemsOnServer = obtrack.getAddedToStoreUIDS();
176 Iterator itsIt = itemsOnServer.iterator();
177 while (itsIt.hasNext()) {
178 String uid = (String) itsIt.next();
179 Calendar cal = obtrack.getObjectFromStore(uid);
180 VEvent ve = (VEvent) cal.getComponents().get(0);
181 if (ve.getSummary().getValue().contains(" 5th of may")) {
182 doesHave5May = false;
183 System.out.println("5th of may event: "+ve.getUid().getValue());
186 obtrack.close();
187 testProperties.remove(StoreConstants.CALDAV_COMPARE_COMPONENT);
188 testProperties.remove(StoreConstants.CALDAV_COMPARE_PROPERTY);
189 testProperties.remove(StoreConstants.CALDAV_COMPARE_START);
190 testProperties.remove(StoreConstants.CALDAV_COMPARE_END);
191 obtrack =
192 new MultipleSourceICalendarObjectStore(testProperties.getProperty("store.location"),
193 MultipleSourceICalendarObjectStore.OPTION_PURGE);
194 testProperties.setProperty(StoreConstants.CALDAV_COMPARE_COMPONENT, "VEVENT");
195 testProperties.setProperty(StoreConstants.CALDAV_COMPARE_PROPERTY, "DTSTART");
196 testProperties.setProperty(StoreConstants.CALDAV_COMPARE_START, "20080514T120000Z");
197 testProperties.setProperty(StoreConstants.CALDAV_COMPARE_END, "20080525T120000Z");
198 obtrack.setProperties(testProperties);
199 obtrack.loadSourcesFromProps();
200 obtrack.connect_storedpass();
201 obtrack.startSync();
202 while (uids.hasNext()) {
203 String uid = uids.next();
204 obtrack.deleteObject(uid);
206 obtrack.close();
207 assertFalse("CalDAV date discovery malfunction", doesHave5May);