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.config;
022:
023: import com.db4o.*;
024: import com.db4o.config.*;
025: import com.db4o.foundation.*;
026:
027: import db4ounit.*;
028: import db4ounit.extensions.*;
029:
030: public class ObjectTranslatorTestCase extends AbstractDb4oTestCase {
031:
032: public static class Thing {
033: public String name;
034:
035: public Thing(String name) {
036: this .name = name;
037: }
038: }
039:
040: public static class ThingCounterTranslator implements
041: ObjectConstructor {
042:
043: private Hashtable4 _countCache = new Hashtable4();
044:
045: public void onActivate(ObjectContainer container,
046: Object applicationObject, Object storedObject) {
047: }
048:
049: public Object onStore(ObjectContainer container,
050: Object applicationObject) {
051: Thing t = (Thing) applicationObject;
052: addToCache(t);
053: return t.name;
054: }
055:
056: private void addToCache(Thing t) {
057: Object o = (Object) _countCache.get(t.name);
058: if (o == null)
059: o = new Integer(0);
060: _countCache.put(t.name, new Integer(((Integer) o)
061: .intValue() + 1));
062: }
063:
064: public int getCount(Thing t) {
065: Object o = (Integer) _countCache.get(t.name);
066: if (o == null)
067: return 0;
068: return ((Integer) o).intValue();
069: }
070:
071: public Object onInstantiate(ObjectContainer container,
072: Object storedObject) {
073: String name = (String) storedObject;
074: return new Thing(name);
075: }
076:
077: public Class storedClass() {
078: return String.class;
079: }
080: }
081:
082: private ThingCounterTranslator _trans;
083:
084: protected void configure(Configuration config) {
085: config.objectClass(Thing.class).translate(
086: _trans = new ThingCounterTranslator());
087: }
088:
089: protected void store() throws Exception {
090: db().set(new Thing("jbe"));
091: }
092:
093: public void _testTranslationCount() {
094: Thing t = (Thing) retrieveOnlyInstance(Thing.class);
095: Assert.isNotNull(t);
096: Assert.areEqual("jbe", t.name);
097: Assert.areEqual(1, _trans.getCount(t));
098: }
099:
100: public static void main(String[] args) {
101: new ObjectTranslatorTestCase().runSolo();
102: }
103: }
|