01: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
02:
03: This file is part of the db4o open source object database.
04:
05: db4o is free software; you can redistribute it and/or modify it under
06: the terms of version 2 of the GNU General Public License as published
07: by the Free Software Foundation and as clarified by db4objects' GPL
08: interpretation policy, available at
09: 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.test;
22:
23: import com.db4o.*;
24: import com.db4o.ext.*;
25: import com.db4o.query.*;
26:
27: public class UuidAware {
28:
29: public String name;
30:
31: private transient long uuidLongPart;
32:
33: private transient byte[] uuidSignaturePart;
34:
35: public UuidAware() {
36:
37: }
38:
39: public UuidAware(String name) {
40: this .name = name;
41: }
42:
43: public void configure() {
44: Db4o.configure().objectClass(this ).generateUUIDs(true);
45: }
46:
47: public void store() {
48: Test.objectContainer().set(new UuidAware("one"));
49: Test.objectContainer().set(new UuidAware("two"));
50: }
51:
52: public void test() {
53: ExtObjectContainer oc = Test.objectContainer();
54:
55: UuidAware ua = queryName("one");
56: ua.checkUUID(oc);
57:
58: ua = queryName("two");
59: ua.checkUUID(oc);
60: }
61:
62: private UuidAware queryName(String name) {
63: Query q = Test.query();
64: q.constrain(getClass());
65: q.descend(name);
66: return (UuidAware) q.execute().next();
67: }
68:
69: private void checkUUID(ExtObjectContainer oc) {
70: Db4oUUID uuid = new Db4oUUID(uuidLongPart, uuidSignaturePart);
71: Test.ensure(oc.getByUUID(uuid) == this );
72: }
73:
74: public void objectOnActivate(ObjectContainer oc) {
75: setUuidFields(oc);
76: }
77:
78: public void objectOnNew(ObjectContainer oc) {
79: setUuidFields(oc);
80: }
81:
82: private void setUuidFields(ObjectContainer oc) {
83: ObjectInfo info = oc.ext().getObjectInfo(this );
84: Db4oUUID uuid = info.getUUID();
85: uuidLongPart = uuid.getLongPart();
86: uuidSignaturePart = uuid.getSignaturePart();
87: }
88:
89: }
|