KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > common > defragment > SlotDefragmentFixture


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.db4ounit.common.defragment;
22
23 import java.io.*;
24
25 import com.db4o.*;
26 import com.db4o.config.*;
27 import com.db4o.defragment.*;
28 import com.db4o.ext.*;
29 import com.db4o.query.*;
30
31 import db4ounit.*;
32
33 public class SlotDefragmentFixture {
34
35     public static final String JavaDoc PRIMITIVE_FIELDNAME = "_id";
36     public static final String JavaDoc WRAPPER_FIELDNAME = "_wrapper";
37     public static final String JavaDoc TYPEDOBJECT_FIELDNAME = "_next";
38     
39     public static class Data {
40         
41         public int _id;
42         public Integer JavaDoc _wrapper;
43         public Data _next;
44
45         public Data(int id,Data next) {
46             _id = id;
47             _wrapper=new Integer JavaDoc(id);
48             _next=next;
49         }
50     }
51
52     public static final int VALUE = 42;
53
54     public static DefragmentConfig defragConfig(boolean forceBackupDelete) {
55         DefragmentConfig defragConfig = new DefragmentConfig(SlotDefragmentConstants.FILENAME,SlotDefragmentConstants.BACKUPFILENAME);
56         defragConfig.forceBackupDelete(forceBackupDelete);
57         return defragConfig;
58     }
59
60     public static void createFile(String JavaDoc fileName) {
61         Configuration config = Db4o.newConfiguration();
62         ObjectContainer db=Db4o.openFile(config,fileName);
63         Data data=null;
64         for(int value=VALUE-1;value<=VALUE+1;value++) {
65             data=new Data(value,data);
66             db.set(data);
67         }
68         db.close();
69     }
70
71     public static void forceIndex() {
72         Configuration config=Db4o.newConfiguration();
73         config.objectClass(Data.class).objectField(PRIMITIVE_FIELDNAME).indexed(true);
74         config.objectClass(Data.class).objectField(WRAPPER_FIELDNAME).indexed(true);
75         config.objectClass(Data.class).objectField(TYPEDOBJECT_FIELDNAME).indexed(true);
76         ObjectContainer db=Db4o.openFile(config,SlotDefragmentConstants.FILENAME);
77         Assert.isTrue(db.ext().storedClass(Data.class).storedField(PRIMITIVE_FIELDNAME,Integer.TYPE).hasIndex());
78         Assert.isTrue(db.ext().storedClass(Data.class).storedField(WRAPPER_FIELDNAME,Integer JavaDoc.class).hasIndex());
79         Assert.isTrue(db.ext().storedClass(Data.class).storedField(TYPEDOBJECT_FIELDNAME,Data.class).hasIndex());
80         db.close();
81     }
82
83     public static void assertIndex(String JavaDoc fieldName) throws IOException {
84         forceIndex();
85         Defragment.defrag(SlotDefragmentConstants.FILENAME,SlotDefragmentConstants.BACKUPFILENAME);
86         ObjectContainer db=Db4o.openFile(Db4o.newConfiguration(),SlotDefragmentConstants.FILENAME);
87         Query query=db.query();
88         query.constrain(Data.class);
89         query.descend(fieldName).constrain(new Integer JavaDoc(VALUE));
90         ObjectSet result=query.execute();
91         Assert.areEqual(1,result.size());
92         db.close();
93     }
94
95     public static void assertDataClassKnown(boolean expected) {
96         ObjectContainer db=Db4o.openFile(Db4o.newConfiguration(),SlotDefragmentConstants.FILENAME);
97         try {
98             StoredClass storedClass=db.ext().storedClass(Data.class);
99             if(expected) {
100                 Assert.isNotNull(storedClass);
101             }
102             else {
103                 Assert.isNull(storedClass);
104             }
105         }
106         finally {
107             db.close();
108         }
109     }
110 }
111
Popular Tags