KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > common > assorted > AliasesTestCase


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.assorted;
22
23 import com.db4o.config.*;
24 import com.db4o.db4ounit.util.Strings;
25
26 import db4ounit.*;
27 import db4ounit.extensions.*;
28 import db4ounit.extensions.fixtures.*;
29
30
31 public class AliasesTestCase extends AbstractDb4oTestCase implements OptOutDefragSolo {
32     
33     public static void main(String JavaDoc[] args) {
34         new AliasesTestCase().runSolo();
35     }
36     
37     
38     private int id;
39     
40     private Alias alias;
41
42     
43     public static class AFoo{
44         public String JavaDoc foo;
45     }
46     
47     public static class ABar extends AFoo {
48         public String JavaDoc bar;
49     }
50     
51     public static class BFoo {
52         public String JavaDoc foo;
53     }
54     
55     public static class BBar extends BFoo {
56         public String JavaDoc bar;
57     }
58     
59     public static class CFoo{
60         public String JavaDoc foo;
61     }
62     
63     public static class CBar extends CFoo {
64         public String JavaDoc bar;
65     }
66     
67     protected void store(){
68         addACAlias();
69         CBar bar = new CBar();
70         bar.foo = "foo";
71         bar.bar = "bar";
72         store(bar);
73         id = (int)db().getID(bar);
74     }
75     
76     public void testAccessByChildClass() throws Exception JavaDoc{
77         addABAlias();
78         BBar bar = (BBar) retrieveOnlyInstance(BBar.class);
79         assertInstanceOK(bar);
80     }
81     
82     public void testAccessByParentClass() throws Exception JavaDoc{
83         addABAlias();
84         BBar bar = (BBar) retrieveOnlyInstance(BFoo.class);
85         assertInstanceOK(bar);
86     }
87     
88     public void testAccessById() throws Exception JavaDoc{
89         addABAlias();
90         BBar bar = (BBar) db().getByID(id);
91         db().activate(bar, 2);
92         assertInstanceOK(bar);
93     }
94     
95     public void testAccessWithoutAlias() throws Exception JavaDoc{
96         removeAlias();
97         ABar bar = (ABar) retrieveOnlyInstance(ABar.class);
98         assertInstanceOK(bar);
99     }
100     
101     private void assertInstanceOK (BBar bar) {
102         Assert.areEqual("foo", bar.foo);
103         Assert.areEqual("bar", bar.bar);
104     }
105     
106     private void assertInstanceOK (ABar bar) {
107         Assert.areEqual("foo", bar.foo);
108         Assert.areEqual("bar", bar.bar);
109     }
110     
111     private void addABAlias() throws Exception JavaDoc{
112         addAlias("A", "B");
113     }
114     
115     private void addACAlias(){
116         addAlias("A", "C");
117     }
118     
119     private void addAlias(String JavaDoc storedLetter, String JavaDoc runtimeLetter){
120         removeAlias();
121         alias = createAlias(storedLetter, runtimeLetter);
122         db().configure().addAlias(alias);
123     }
124     
125     private void removeAlias(){
126         if(alias != null){
127             db().configure().removeAlias(alias);
128             alias = null;
129         }
130     }
131     
132     private WildcardAlias createAlias(String JavaDoc storedLetter, String JavaDoc runtimeLetter){
133         String JavaDoc className = reflector().forObject(new ABar()).getName();
134         String JavaDoc storedPattern = Strings.replace(className, "ABar", storedLetter + "*");
135         String JavaDoc runtimePattern = Strings.replace(className, "ABar", runtimeLetter + "*");
136         return new WildcardAlias(storedPattern, runtimePattern);
137     }
138
139 }
140
Popular Tags