001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.calendar.store;
019:
020: import java.io.File;
021: import java.util.Iterator;
022:
023: import junit.framework.TestCase;
024:
025: import org.columba.calendar.base.UUIDGenerator;
026: import org.columba.calendar.model.api.IComponent;
027: import org.columba.calendar.parser.XCSDocumentParser;
028: import org.columba.calendar.store.api.StoreException;
029: import org.jdom.Document;
030:
031: public class LocalXMLFileStoreTest extends TestCase {
032:
033: private File file;
034:
035: private LocalXMLFileStore storage;
036:
037: protected void setUp() throws Exception {
038: file = new File("test_calendar");
039:
040: storage = new LocalXMLFileStore(file);
041: }
042:
043: public void testAddGet() throws Exception {
044:
045: XCSDocumentParser model = new XCSDocumentParser(
046: IComponent.TYPE.EVENT);
047: String uuid = model.getId();
048: storage.save(uuid, model.getDocument());
049:
050: boolean exists = storage.exists(uuid);
051: assertTrue(exists);
052:
053: Document result = storage.load(uuid);
054: assertNotNull(result);
055:
056: }
057:
058: public void testIterator() throws Exception {
059: String uuid1 = new UUIDGenerator().newUUID();
060: XCSDocumentParser model1 = new XCSDocumentParser(
061: IComponent.TYPE.EVENT);
062: storage.save(uuid1, model1.getDocument());
063: String uuid2 = new UUIDGenerator().newUUID();
064: XCSDocumentParser model2 = new XCSDocumentParser(
065: IComponent.TYPE.EVENT);
066: storage.save(uuid2, model2.getDocument());
067:
068: Iterator it = storage.iterator();
069: Document result1 = (Document) it.next();
070: Document result2 = (Document) it.next();
071:
072: assertNotNull(result1);
073: assertNotNull(result2);
074: }
075:
076: public void testRemove() throws Exception {
077: String uuid = new UUIDGenerator().newUUID();
078: XCSDocumentParser model = new XCSDocumentParser(
079: IComponent.TYPE.EVENT);
080: storage.save(uuid, model.getDocument());
081:
082: storage.remove(uuid);
083:
084: try {
085: Document result = storage.load(uuid);
086: } catch (StoreException e) {
087: // this is the expected cases
088: return;
089: } catch (Exception e) {
090: fail("Expected StoreException, not " + e.getMessage());
091: }
092:
093: fail("Expected StoreException, got no exception");
094:
095: }
096:
097: protected void tearDown() throws Exception {
098:
099: // delete all data in directory
100: File[] list = file.listFiles();
101:
102: for (int i = 0; i < list.length; i++) {
103: list[i].delete();
104: }
105:
106: // delete folder
107: file.delete();
108:
109: }
110:
111: }
|