KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > jre12 > assorted > TranslatorStoredClassesTestCase


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.jre12.assorted;
22
23 import java.io.*;
24 import java.math.*;
25
26 import com.db4o.*;
27 import com.db4o.config.*;
28
29 import db4ounit.*;
30
31 public class TranslatorStoredClassesTestCase implements TestCase {
32
33     private final static String JavaDoc FILENAME="translator.yap";
34     
35     public static class DataRawChild implements Serializable {
36         public int _id;
37
38         public DataRawChild(int id) {
39             _id=id;
40         }
41     }
42
43     public static class DataRawParent {
44         public DataRawChild _child;
45
46         public DataRawParent(int id) {
47             _child=new DataRawChild(id);
48         }
49     }
50
51     public static class DataBigDecimal {
52         public BigDecimal _bd;
53
54         public DataBigDecimal(int id) {
55             _bd=new BigDecimal(String.valueOf(id));
56         }
57     }
58     
59     public void testBigDecimal() {
60         assertStoredClassesAfterTranslator(BigDecimal.class,new DataBigDecimal(42));
61     }
62
63     public void testRaw() {
64         assertStoredClassesAfterTranslator(DataRawChild.class,new DataRawParent(42));
65     }
66
67     public void assertStoredClassesAfterTranslator(Class JavaDoc translated,Object JavaDoc data) {
68         createFile(translated,data);
69         check(translated);
70     }
71
72     private static void createFile(Class JavaDoc translated,Object JavaDoc data) {
73         new File(FILENAME).delete();
74         ObjectContainer server = db(translated,new TSerializable());
75         server.set(data);
76         server.close();
77     }
78
79     private static void check(Class JavaDoc translated) {
80         ObjectContainer db=db(translated,null);
81         db.ext().storedClasses();
82         db.close();
83     }
84
85     private static ObjectContainer db(Class JavaDoc translated,ObjectTranslator translator) {
86         Configuration config=Db4o.newConfiguration();
87         config.objectClass(translated).translate(translator);
88         return Db4o.openFile(config,FILENAME);
89     }
90
91 }
92
Popular Tags