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 net
.bionicmessage
.funambol
.groupdav
.calendar
.CalendarSyncSource
;
38 import org
.junit
.After
;
39 import org
.junit
.AfterClass
;
40 import org
.junit
.Before
;
41 import org
.junit
.BeforeClass
;
42 import org
.junit
.Test
;
43 import static org
.junit
.Assert
.*;
44 import org
.apache
.commons
.codec
.binary
.Base64
;
50 public class CalendarSyncSourceTest
{
52 private String clientId
= "STATIC-TEST";
53 private String testPropertiesFile
= ".groupdavconnector_test_properties";
54 private CalendarSyncSource css
= null;
55 private SyncContext ctx
= null;
56 private Sync4jPrincipal spp
= null;
57 private Properties testProps
= null;
59 public CalendarSyncSourceTest() {
63 public static void setUpClass() throws Exception
{
67 public static void tearDownClass() throws Exception
{
71 public void setUp() throws Exception
{
72 // Load a properties file from the local user dir
73 String testPropsPath
= System
.getProperty("user.home") + System
.getProperty("file.separator") + testPropertiesFile
;
74 testProps
= new Properties();
75 testProps
.load(new FileInputStream(new File(testPropsPath
)));
76 css
= new CalendarSyncSource();
77 String storePath
= System
.getProperty("java.io.tmpdir") + System
.getProperty("file.separator") + "calendarsyncsource-test";
78 Properties props
= new Properties();
79 props
.setProperty(Constants
.STOREDIR_PATH
, storePath
);
80 props
.setProperty(Constants
.SOURCE_LOCATION_BASE
+ "default", testProps
.getProperty("connector.sources.default"));
81 props
.setProperty(Constants
.SERVER_HOST
, testProps
.getProperty("connector.serverhost"));
82 css
.setConnectorProperties(props
);
83 String testUser
= testProps
.getProperty("test.user");
84 spp
= Sync4jPrincipal
.createPrincipal(testProps
.getProperty("test.user"), "testid");
85 String passString
= String
.format("%s:%s", testProps
.getProperty("test.user"),
86 testProps
.getProperty("test.password"));
87 String encodedPassString
= new String(Base64
.encodeBase64(passString
.getBytes()));
88 spp
.setEncodedUserPwd(encodedPassString
);
89 spp
.setUsername(testProps
.getProperty("test.user"));
90 ctx
= new SyncContext(spp
, 0, null, null, 0);
91 SyncSourceInfo ssInfo
= new SyncSourceInfo();
92 ContentType vcard
= new ContentType();
93 vcard
.setType("text/x-vcalendar");
94 vcard
.setVersion("1.0");
95 ContentType
[] supported
= new ContentType
[1];
97 ssInfo
.setSupportedTypes(supported
);
98 ssInfo
.setPreferred(0);
103 public void tearDown() throws Exception
{
106 /** The tests below are in a series of 'easiest' to 'i can't believe its not
109 public void beginSlowAndEnd() throws Exception
{
110 ctx
= new SyncContext(spp
, 201, null, null, 0);
116 public void beginSlowGetAllSyncItemKeysEnd() throws Exception
{
117 ctx
= new SyncContext(spp
, 201, null, null, 0);
119 SyncItemKey
[] all
= css
.getAllSyncItemKeys();
120 for (int i
= 0; i
< all
.length
; i
++) {
121 SyncItemKey syncItemKey
= all
[i
];
122 SyncItemKey
[] indiv
= new SyncItemKey
[1];
123 indiv
[0] = syncItemKey
;
124 System
.out
.printf("New/updated Key: %s\n", syncItemKey
.getKeyAsString()).flush();
125 css
.setOperationStatus("Add", 201, indiv
);
131 public void beginSlowGetAllSyncItemKeysGetItemsVcalEnd() throws Exception
{
132 ctx
= new SyncContext(spp
, 201, null, null, 0);
133 css
.setType("text/x-vcalendar"); // to fix later
135 SyncItemKey
[] all
= css
.getAllSyncItemKeys();
136 for (int i
= 0; i
< all
.length
; i
++) {
137 SyncItemKey syncItemKey
= all
[i
];
138 System
.out
.printf("New/updated Key: %s\n", syncItemKey
.getKeyAsString()).flush();
139 SyncItem si
= css
.getSyncItemFromId(syncItemKey
);
140 String contents
= new String(si
.getContent());
141 System
.out
.printf("Contents: %s\n------\n", contents
).flush();
147 public void beginNormalAddItem() throws Exception
{
148 ctx
= new SyncContext(spp
, 200, null, null, 0);
149 css
.setType("text/x-vcalendar");
151 File problemSample
= new File("doc/problem-samples/synthesis-quoted-printable-example.txt");
152 FileInputStream fis
= new FileInputStream(problemSample
);
153 byte[] data
= new byte[fis
.available()];
156 SyncItemImpl sync
= new SyncItemImpl(css
, "testitem-calsourcetest");
157 sync
.setContent(data
);
158 sync
.setType("text/x-vcalendar");
159 SyncItem result
= css
.addSyncItem(sync
);
160 byte[] newdata
= result
.getContent();
161 String resdata
= new String(newdata
);
162 String resuid
= result
.getKey().getKeyAsString();
163 System
.out
.printf("Returned from addSyncItem: %s\n, UID: %s\n", resdata
, resuid
).flush();
165 System
.setProperty("calsyncsourcetest.beginslowadditem.resuid", resuid
);
170 public void beginNormalDeleteItem() throws Exception
{
171 ctx
= new SyncContext(spp
, 200, null, null, 0);
172 css
.setType("text/x-vcalendar");
174 String uid
= System
.getProperty("calsyncsourcetest.beginslowadditem.resuid");
175 css
.removeSyncItem(new SyncItemKey(uid
), null, false);
176 System
.out
.printf("Removed item %s from server\n", uid
);
180 public static junit
.framework
.Test
suite() {
181 return new JUnit4TestAdapter(CalendarSyncSourceTest
.class);