2 * CalendarSyncSourceTest.java
5 * Created on July 26, 2007, 5:44 PM
7 * GroupDAV connector for Funambol v6.5
8 * Copyright (C) 2007 Mathew McBride
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as
12 * published by the Free Software Foundation, either version 3 of the
13 * License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 package net
.bionicmessage
.funambol
.groupdav
.calendar
;
25 import com
.funambol
.framework
.engine
.SyncItem
;
26 import com
.funambol
.framework
.engine
.SyncItemImpl
;
27 import com
.funambol
.framework
.engine
.SyncItemKey
;
28 import com
.funambol
.framework
.engine
.source
.ContentType
;
29 import com
.funambol
.framework
.engine
.source
.SyncContext
;
30 import com
.funambol
.framework
.engine
.source
.SyncSourceInfo
;
31 import com
.funambol
.framework
.security
.Sync4jPrincipal
;
33 import java
.io
.FileInputStream
;
34 import java
.util
.Properties
;
35 import junit
.framework
.JUnit4TestAdapter
;
36 import net
.bionicmessage
.funambol
.framework
.Constants
;
37 import org
.junit
.After
;
38 import org
.junit
.AfterClass
;
39 import org
.junit
.Before
;
40 import org
.junit
.BeforeClass
;
41 import org
.junit
.Test
;
42 import static org
.junit
.Assert
.*;
43 import org
.apache
.commons
.codec
.binary
.Base64
;
49 public class CalendarSyncSourceTest
{
51 private String clientId
= "STATIC-TEST";
52 private String testPropertiesFile
= ".groupdavconnector_test_properties";
53 private CalendarSyncSource css
= null;
54 private SyncContext ctx
= null;
55 private Sync4jPrincipal spp
= null;
56 private Properties testProps
= null;
58 public CalendarSyncSourceTest() {
62 public static void setUpClass() throws Exception
{
66 public static void tearDownClass() throws Exception
{
70 public void setUp() throws Exception
{
71 // Load a properties file from the local user dir
72 String testPropsPath
= System
.getProperty("user.home") + System
.getProperty("file.separator") + testPropertiesFile
;
73 testProps
= new Properties();
74 File testPropsFile
= new File(testPropsPath
);
75 if (!testPropsFile
.exists()) {
76 System
.err
.println("CalendarSyncSourceTest not configured. Exiting");
77 System
.err
.println("Perhaps you should pass the -Dmaven.test.skip.exec=true flag to build?");
80 testProps
.load(new FileInputStream(testPropsFile
));
81 css
= new CalendarSyncSource();
82 String storePath
= System
.getProperty("java.io.tmpdir") + System
.getProperty("file.separator") + "calendarsyncsource-test";
83 css
.setConnectorProperties(testProps
);
84 String testUser
= testProps
.getProperty("test.user");
85 spp
= Sync4jPrincipal
.createPrincipal(testProps
.getProperty("test.user"), "testid");
86 String passString
= String
.format("%s:%s", testProps
.getProperty("test.user"),
87 testProps
.getProperty("test.password"));
88 String encodedPassString
= new String(Base64
.encodeBase64(passString
.getBytes()));
89 spp
.setEncodedUserPwd(encodedPassString
);
90 spp
.setUsername(testProps
.getProperty("test.user"));
91 ctx
= new SyncContext(spp
, 0, null, null, 0);
92 SyncSourceInfo ssInfo
= new SyncSourceInfo();
93 ContentType vcard
= new ContentType();
94 vcard
.setType("text/x-vcalendar");
95 vcard
.setVersion("1.0");
96 ContentType
[] supported
= new ContentType
[1];
98 ssInfo
.setSupportedTypes(supported
);
99 ssInfo
.setPreferred(0);
104 public void tearDown() throws Exception
{
107 /** The tests below are in a series of 'easiest' to 'i can't believe its not
110 public void beginSlowAndEnd() throws Exception
{
111 ctx
= new SyncContext(spp
, 201, null, null, 0);
117 public void beginSlowGetAllSyncItemKeysEnd() throws Exception
{
118 ctx
= new SyncContext(spp
, 201, null, null, 0);
120 SyncItemKey
[] all
= css
.getAllSyncItemKeys();
121 for (int i
= 0; i
< all
.length
; i
++) {
122 SyncItemKey syncItemKey
= all
[i
];
123 SyncItemKey
[] indiv
= new SyncItemKey
[1];
124 indiv
[0] = syncItemKey
;
125 System
.out
.printf("New/updated Key: %s\n", syncItemKey
.getKeyAsString()).flush();
126 css
.setOperationStatus("Add", 201, indiv
);
132 public void beginSlowGetAllSyncItemKeysGetItemsVcalEnd() throws Exception
{
133 ctx
= new SyncContext(spp
, 201, null, null, 0);
134 css
.setType("text/x-vcalendar"); // to fix later
136 SyncItemKey
[] all
= css
.getAllSyncItemKeys();
137 for (int i
= 0; i
< all
.length
; i
++) {
138 SyncItemKey syncItemKey
= all
[i
];
139 System
.out
.printf("New/updated Key: %s\n", syncItemKey
.getKeyAsString()).flush();
140 SyncItem si
= css
.getSyncItemFromId(syncItemKey
);
141 String contents
= new String(si
.getContent());
142 System
.out
.printf("Contents: %s\n------\n", contents
).flush();
148 public void beginNormalAddItem() throws Exception
{
149 ctx
= new SyncContext(spp
, 200, null, null, 0);
150 css
.setType("text/x-vcalendar");
152 File problemSample
= new File("doc/problem-samples/synthesis-quoted-printable-example.txt");
153 FileInputStream fis
= new FileInputStream(problemSample
);
154 byte[] data
= new byte[fis
.available()];
157 SyncItemImpl sync
= new SyncItemImpl(css
, "testitem-calsourcetest");
158 sync
.setContent(data
);
159 sync
.setType("text/x-vcalendar");
160 SyncItem result
= css
.addSyncItem(sync
);
161 byte[] newdata
= result
.getContent();
162 String resdata
= new String(newdata
);
163 String resuid
= result
.getKey().getKeyAsString();
164 System
.out
.printf("Returned from addSyncItem: %s\n, UID: %s\n", resdata
, resuid
).flush();
166 System
.setProperty("calsyncsourcetest.beginslowadditem.resuid", resuid
);
171 public void beginNormalDeleteItem() throws Exception
{
172 ctx
= new SyncContext(spp
, 200, null, null, 0);
173 css
.setType("text/x-vcalendar");
175 String uid
= System
.getProperty("calsyncsourcetest.beginslowadditem.resuid");
176 css
.removeSyncItem(new SyncItemKey(uid
), null, false);
177 System
.out
.printf("Removed item %s from server\n", uid
);
181 public void testNoSummary() throws Exception
{
182 ctx
= new SyncContext(spp
, 200, null, null, 0);
183 css
.setType("text/x-vcalendar");
185 File problemSample
= new File("doc/problem-samples/no-summary.vcs");
186 FileInputStream fis
= new FileInputStream(problemSample
);
187 byte[] data
= new byte[fis
.available()];
190 SyncItemImpl sync
= new SyncItemImpl(css
, "testitem-nosummary");
191 sync
.setContent(data
);
192 sync
.setType("text/x-vcalendar");
193 SyncItem result
= css
.addSyncItem(sync
);
194 byte[] newdata
= result
.getContent();
195 String resdata
= new String(newdata
);
196 String resuid
= result
.getKey().getKeyAsString();
197 System
.out
.printf("Returned from addSyncItem: %s\n, UID: %s\n", resdata
, resuid
).flush();
198 assertTrue(resdata
.contains("No summary entered"));
200 System
.setProperty("calsyncsourcetest.nosummary.resuid", resuid
);
204 public void beginNormalDeleteNoSummary() throws Exception
{
205 ctx
= new SyncContext(spp
, 200, null, null, 0);
206 css
.setType("text/x-vcalendar");
208 String uid
= System
.getProperty("calsyncsourcetest.nosummary.resuid");
209 css
.removeSyncItem(new SyncItemKey(uid
), null, false);
210 System
.out
.printf("Removed item %s from server\n", uid
);
213 public static junit
.framework
.Test
suite() {
214 return new JUnit4TestAdapter(CalendarSyncSourceTest
.class);