001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.db4ounit.jre12.blobs;
022:
023: import java.io.*;
024:
025: import com.db4o.config.*;
026: import com.db4o.ext.*;
027: import com.db4o.foundation.io.*;
028: import com.db4o.types.*;
029:
030: import db4ounit.*;
031: import db4ounit.extensions.*;
032: import db4ounit.extensions.fixtures.*;
033: import db4ounit.extensions.util.*;
034:
035: public class ExternalBlobsTestCase extends AbstractDb4oTestCase
036: implements OptOutDefragSolo {
037:
038: public static void main(String[] args) {
039: new ExternalBlobsTestCase().runClientServer();
040: }
041:
042: private static final String BLOB_PATH = Path4.combine(Path4
043: .getTempPath(), "db4oTestBlobs");
044: private static final String BLOB_FILE_IN = BLOB_PATH
045: + "/regressionBlobIn.txt";
046: private static final String BLOB_FILE_OUT = BLOB_PATH
047: + "/regressionBlobOut.txt";
048:
049: private static class Data {
050: private Blob _blob;
051:
052: public Data() {
053: _blob = null;
054: }
055:
056: public Blob blob() {
057: return _blob;
058: }
059: }
060:
061: protected void db4oSetupBeforeStore() throws Exception {
062: deleteFiles();
063: File4.mkdirs(BLOB_PATH);
064: }
065:
066: protected void db4oTearDownAfterClean() throws Exception {
067: deleteFiles();
068: }
069:
070: protected void configure(Configuration config) throws IOException {
071: config.setBlobPath(BLOB_PATH);
072: }
073:
074: protected void store() throws Exception {
075: store(new Data());
076: }
077:
078: public void test() throws Exception {
079: Data data = (Data) retrieveOnlyInstance(Data.class);
080: Assert.isTrue(new File(BLOB_PATH).exists());
081: char[] chout = new char[] { 'H', 'i', ' ', 'f', 'o', 'l', 'k',
082: 's' };
083: FileWriter fw = new FileWriter(BLOB_FILE_IN);
084: fw.write(chout);
085: fw.flush();
086: fw.close();
087: data.blob().readFrom(new File(BLOB_FILE_IN));
088: double status = data.blob().getStatus();
089: while (status > Status.COMPLETED) {
090: Thread.sleep(50);
091: status = data.blob().getStatus();
092: }
093:
094: data.blob().writeTo(new File(BLOB_FILE_OUT));
095: status = data.blob().getStatus();
096: while (status > Status.COMPLETED) {
097: Thread.sleep(50);
098: status = data.blob().getStatus();
099: }
100: File resultingFile = new File(BLOB_FILE_OUT);
101: Assert.isTrue(resultingFile.exists());
102:
103: FileReader fr = new FileReader(resultingFile);
104: char[] chin = new char[chout.length];
105: fr.read(chin);
106: fr.close();
107: ArrayAssert.areEqual(chout, chin);
108:
109: Assert.areEqual(Status.COMPLETED, data.blob().getStatus());
110: data.blob().deleteFile();
111: Assert.areEqual(Status.UNUSED, data.blob().getStatus());
112: }
113:
114: private void deleteFiles() throws IOException {
115: IOUtil.deleteDir(BLOB_PATH);
116: }
117: }
|