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.assorted;
022:
023: import com.db4o.config.*;
024: import com.db4o.db4ounit.util.Strings;
025:
026: import db4ounit.*;
027: import db4ounit.extensions.*;
028: import db4ounit.extensions.fixtures.*;
029:
030: public class AliasesTestCase extends AbstractDb4oTestCase implements
031: OptOutDefragSolo {
032:
033: public static void main(String[] args) {
034: new AliasesTestCase().runSolo();
035: }
036:
037: private int id;
038:
039: private Alias alias;
040:
041: public static class AFoo {
042: public String foo;
043: }
044:
045: public static class ABar extends AFoo {
046: public String bar;
047: }
048:
049: public static class BFoo {
050: public String foo;
051: }
052:
053: public static class BBar extends BFoo {
054: public String bar;
055: }
056:
057: public static class CFoo {
058: public String foo;
059: }
060:
061: public static class CBar extends CFoo {
062: public String bar;
063: }
064:
065: protected void store() {
066: addACAlias();
067: CBar bar = new CBar();
068: bar.foo = "foo";
069: bar.bar = "bar";
070: store(bar);
071: id = (int) db().getID(bar);
072: }
073:
074: public void testAccessByChildClass() throws Exception {
075: addABAlias();
076: BBar bar = (BBar) retrieveOnlyInstance(BBar.class);
077: assertInstanceOK(bar);
078: }
079:
080: public void testAccessByParentClass() throws Exception {
081: addABAlias();
082: BBar bar = (BBar) retrieveOnlyInstance(BFoo.class);
083: assertInstanceOK(bar);
084: }
085:
086: public void testAccessById() throws Exception {
087: addABAlias();
088: BBar bar = (BBar) db().getByID(id);
089: db().activate(bar, 2);
090: assertInstanceOK(bar);
091: }
092:
093: public void testAccessWithoutAlias() throws Exception {
094: removeAlias();
095: ABar bar = (ABar) retrieveOnlyInstance(ABar.class);
096: assertInstanceOK(bar);
097: }
098:
099: private void assertInstanceOK(BBar bar) {
100: Assert.areEqual("foo", bar.foo);
101: Assert.areEqual("bar", bar.bar);
102: }
103:
104: private void assertInstanceOK(ABar bar) {
105: Assert.areEqual("foo", bar.foo);
106: Assert.areEqual("bar", bar.bar);
107: }
108:
109: private void addABAlias() throws Exception {
110: addAlias("A", "B");
111: }
112:
113: private void addACAlias() {
114: addAlias("A", "C");
115: }
116:
117: private void addAlias(String storedLetter, String runtimeLetter) {
118: removeAlias();
119: alias = createAlias(storedLetter, runtimeLetter);
120: fixture().configureAtRuntime(new RuntimeConfigureAction() {
121: public void apply(Configuration config) {
122: config.addAlias(alias);
123: }
124: });
125: }
126:
127: private void removeAlias() {
128: if (alias != null) {
129: fixture().configureAtRuntime(new RuntimeConfigureAction() {
130: public void apply(Configuration config) {
131: config.removeAlias(alias);
132: }
133: });
134: alias = null;
135: }
136: }
137:
138: private WildcardAlias createAlias(String storedLetter,
139: String runtimeLetter) {
140: String className = reflector().forObject(new ABar()).getName();
141: String storedPattern = Strings.replace(className, "ABar",
142: storedLetter + "*");
143: String runtimePattern = Strings.replace(className, "ABar",
144: runtimeLetter + "*");
145: return new WildcardAlias(storedPattern, runtimePattern);
146: }
147:
148: }
|