001: /*
002: *
003: * JMoney - A Personal Finance Manager
004: * Copyright (c) 2005 Johann Gyger <jgyger@users.sf.net>
005: *
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020: *
021: */
022: package net.sf.jmoney.test;
023:
024: import java.io.File;
025: import java.io.IOException;
026: import java.net.URL;
027:
028: import javax.xml.transform.TransformerConfigurationException;
029:
030: import junit.framework.TestCase;
031: import net.sf.jmoney.model2.BankAccountInfo;
032: import net.sf.jmoney.model2.TransactionInfo;
033: import net.sf.jmoney.model2.BankAccount;
034: import net.sf.jmoney.model2.Entry;
035: import net.sf.jmoney.model2.Session;
036: import net.sf.jmoney.model2.Transaction;
037: import net.sf.jmoney.serializeddatastore.SessionManager;
038: import net.sf.jmoney.serializeddatastore.formats.JMoneyXmlFormat;
039:
040: import org.eclipse.core.runtime.CoreException;
041: import org.eclipse.core.runtime.FileLocator;
042: import org.eclipse.core.runtime.Platform;
043: import org.osgi.framework.Bundle;
044: import org.osgi.framework.BundleException;
045: import org.xml.sax.SAXException;
046:
047: /**
048: * Tests regarding opening JMoney session files.
049: *
050: * @author Johann Gyger
051: */
052: public class OpenSessionTest extends TestCase {
053:
054: @Override
055: public void setUp() {
056: Bundle bundle = Platform.getBundle("net.sf.jmoney.test");
057: try {
058: bundle.start();
059: } catch (BundleException e) {
060: // TODO Auto-generated catch block
061: e.printStackTrace();
062: }
063: }
064:
065: @Override
066: public void tearDown() {
067: Bundle bundle = Platform.getBundle("net.sf.jmoney.test");
068: try {
069: bundle.stop();
070: } catch (BundleException e) {
071: // TODO Auto-generated catch block
072: e.printStackTrace();
073: }
074: }
075:
076: /**
077: * Test if a JMoney file with the properties below can be opened:
078: * - Old format (version 0.4.5 or prior)
079: * - Empty (minimal file contents)
080: * - Uncompressed
081: *
082: * @throws CoreException
083: * @throws IOException
084: */
085: public void testOldEmptyUncompressed() throws IOException,
086: CoreException {
087: File file = getSessionFile("old_empty_session.xml");
088: JMoneyXmlFormat reader = new JMoneyXmlFormat();
089: SessionManager manager = new SessionManager(
090: "net.sf.jmoney.serializeddatastore.xmlFormat", reader,
091: file);
092: reader.readSessionQuietly(file, manager, null);
093: assertNotNull(manager);
094: assertNotNull(manager.getSession());
095: }
096:
097: /**
098: * Test if a JMoney file with the properties below can be opened:
099: * - Old format (version 0.4.5 or prior)
100: * - Empty (minimal file contents)
101: * - Compressed
102: *
103: * @throws CoreException
104: * @throws IOException
105: */
106: public void testOldEmptyCompressed() throws IOException,
107: CoreException {
108: File file = getSessionFile("old_empty_session.jmx");
109: JMoneyXmlFormat reader = new JMoneyXmlFormat();
110: SessionManager manager = new SessionManager(
111: "net.sf.jmoney.serializeddatastore.jmxFormat", reader,
112: file);
113: reader.readSessionQuietly(file, manager, null);
114: assertNotNull(manager);
115: assertNotNull(manager.getSession());
116: }
117:
118: /**
119: * Test if a JMoney file with the properties below can be opened:
120: * - New format
121: * - Empty (minimal file contents)
122: * - Uncompressed
123: *
124: * @throws CoreException
125: * @throws IOException
126: */
127: public void testNewEmptyUncompressed() throws IOException,
128: CoreException {
129: File file = getSessionFile("new_empty_session.xml");
130: JMoneyXmlFormat reader = new JMoneyXmlFormat();
131: SessionManager manager = new SessionManager(
132: "net.sf.jmoney.serializeddatastore.xmlFormat", reader,
133: file);
134: reader.readSessionQuietly(file, manager, null);
135: assertNotNull(manager);
136: assertNotNull(manager.getSession());
137: }
138:
139: /**
140: * Test if a JMoney file with the properties below can be opened:
141: * - New format
142: * - Empty (minimal file contents)
143: * - Compressed
144: *
145: * @throws CoreException
146: * @throws IOException
147: */
148: public void testNewEmptyCompressed() throws IOException,
149: CoreException {
150: File file = getSessionFile("new_empty_session.jmx");
151: JMoneyXmlFormat reader = new JMoneyXmlFormat();
152: SessionManager manager = new SessionManager(
153: "net.sf.jmoney.serializeddatastore.jmxFormat", reader,
154: file);
155: reader.readSessionQuietly(file, manager, null);
156: assertNotNull(manager);
157: assertNotNull(manager.getSession());
158: }
159:
160: /**
161: * Test if a JMoney file with the properties below can be opened:
162: * - New format
163: * - Empty (minimal file contents)
164: * - Compressed
165: *
166: * @throws CoreException
167: * @throws IOException
168: */
169: public void testOpenUpdateSaveCycle() throws IOException,
170: CoreException {
171: File inputFile = getSessionFile("new_empty_session.jmx");
172: File outputFile = new File("test1.xml");
173:
174: JMoneyXmlFormat reader = new JMoneyXmlFormat();
175: SessionManager manager = new SessionManager(
176: JMoneyXmlFormat.ID_FILE_FORMAT, reader, inputFile);
177: reader.readSessionQuietly(inputFile, manager, null);
178: assertNotNull(manager);
179: assertNotNull(manager.getSession());
180:
181: // Make some updates
182: Session session = manager.getSession();
183:
184: BankAccount account1 = session.getAccountCollection()
185: .createNewElement(BankAccountInfo.getPropertySet());
186: BankAccount account2 = session.getAccountCollection()
187: .createNewElement(BankAccountInfo.getPropertySet());
188:
189: account1.setName("My Checking Account");
190: account2.setName("My Savings Account");
191:
192: Transaction trans = session.getTransactionCollection()
193: .createNewElement(TransactionInfo.getPropertySet());
194: Entry entry1 = trans.getEntryCollection().createEntry();
195: Entry entry2 = trans.getEntryCollection().createEntry();
196:
197: entry1.setAccount(account1);
198: entry2.setAccount(account2);
199: entry1.setAmount(1234);
200: entry2.setAmount(-1234);
201: entry1.setMemo("transfer from savings");
202: entry2.setMemo("transfer to checking");
203:
204: try {
205: reader.writeSessionQuietly(manager, outputFile, null);
206: } catch (TransformerConfigurationException e) {
207: e.printStackTrace();
208: fail(e.getLocalizedMessage());
209: } catch (SAXException e) {
210: e.printStackTrace();
211: fail(e.getLocalizedMessage());
212: }
213:
214: // Read the file back in
215: SessionManager manager2 = new SessionManager(
216: JMoneyXmlFormat.ID_FILE_FORMAT, reader, inputFile);
217: reader.readSessionQuietly(inputFile, manager2, null);
218: assertNotNull(manager2);
219: assertNotNull(manager2.getSession());
220: Session session2 = manager2.getSession();
221: assertEquals(1, session2.getCommodityCollection().size());
222: assertEquals(2, session2.getAccountCollection().size());
223: assertEquals(1, session2.getTransactionCollection().size());
224: }
225:
226: protected File getSessionFile(String filename) throws IOException {
227: Bundle bundle = Platform.getBundle("net.sf.jmoney.test");
228: URL url = bundle.getEntry("resources/" + filename);
229: return new File(FileLocator.toFileURL(url).getFile());
230: }
231:
232: }
|