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.common.freespace;
022:
023: import db4ounit.*;
024:
025: public class FileSizeTestCase extends FreespaceManagerTestCaseBase {
026:
027: private static final int ITERATIONS = 100;
028:
029: public static void main(String[] args) {
030: new FileSizeTestCase().runEmbeddedClientServer();
031: }
032:
033: public void testConsistentSizeOnRollback() {
034: storeSomeItems();
035: produceSomeFreeSpace();
036: assertConsistentSize(new Runnable() {
037: public void run() {
038: store(new Item());
039: db().rollback();
040: }
041: });
042: }
043:
044: public void testConsistentSizeOnCommit() {
045: storeSomeItems();
046: db().commit();
047: assertConsistentSize(new Runnable() {
048: public void run() {
049: db().commit();
050: }
051: });
052: }
053:
054: public void testConsistentSizeOnUpdate() {
055: storeSomeItems();
056: produceSomeFreeSpace();
057: final Item item = new Item();
058: store(item);
059: db().commit();
060: assertConsistentSize(new Runnable() {
061: public void run() {
062: store(item);
063: db().commit();
064: }
065: });
066: }
067:
068: public void testConsistentSizeOnReopen() throws Exception {
069: db().commit();
070: reopen();
071: assertConsistentSize(new Runnable() {
072: public void run() {
073: try {
074: reopen();
075: } catch (Exception e) {
076: e.printStackTrace();
077: }
078: }
079: });
080: }
081:
082: public void testConsistentSizeOnUpdateAndReopen() throws Exception {
083: produceSomeFreeSpace();
084: store(new Item());
085: db().commit();
086: assertConsistentSize(new Runnable() {
087: public void run() {
088: store(retrieveOnlyInstance(Item.class));
089: db().commit();
090: try {
091: reopen();
092: } catch (Exception e) {
093: e.printStackTrace();
094: }
095: }
096: });
097: }
098:
099: public void assertConsistentSize(Runnable runnable) {
100: for (int i = 0; i < 10; i++) {
101: runnable.run();
102: }
103: int originalFileSize = databaseFileSize();
104: for (int i = 0; i < ITERATIONS; i++) {
105: runnable.run();
106: }
107: Assert.areEqual(originalFileSize, databaseFileSize());
108: }
109:
110: }
|