KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > test > aliases > ClassAliasesTestCase


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.test.aliases;
22
23 import java.io.*;
24
25 import com.db4o.*;
26 import com.db4o.config.*;
27 import com.db4o.foundation.io.*;
28 import com.db4o.test.Test;
29
30 public class ClassAliasesTestCase {
31
32     public void testTypeAlias() {
33         
34         cleanUp();
35         
36         ObjectContainer container = Test.objectContainer();
37
38         container.set(new Person1("Homer Simpson"));
39         container.set(new Person1("John Cleese"));
40         
41         container = Test.reOpen();
42         container.ext().configure().addAlias(
43                 // Person1 instances should be read as Person2 objects
44
new TypeAlias("com.db4o.test.aliases.Person1",
45                         "com.db4o.test.aliases.Person2"));
46         
47         assertData(container);
48     }
49     
50     public void testAliasedTypeIsStoredCorrectly() {
51         
52         cleanUp();
53         
54         ObjectContainer container = Test.objectContainer();
55         container.ext().configure().addAlias(
56                 // Person1 instances should be read as Person2 objects
57
new TypeAlias("com.db4o.test.aliases.Person1",
58                         "com.db4o.test.aliases.Person2"));
59         
60         container.set(new Person2("Homer Simpson"));
61         container.set(new Person2("John Cleese"));
62         
63         container = Test.reOpen();
64         assertData(container);
65         
66     }
67
68     private void cleanUp() {
69         Test.reOpen();
70         Test.deleteAllInstances(Person1.class);
71     }
72     
73     public void testAccessingDotnetFromJava() throws Exception JavaDoc {
74         generateDotnetData();
75         ObjectContainer container = openDotnetDataFile();
76         container.ext().configure().addAlias(
77                 new WildcardAlias(
78                         "com.db4o.test.aliases.*, MyAssembly",
79                         "com.db4o.test.aliases.*"));
80 // new TypeAlias(
81
// Person2.class.getName() + ", MyAssembly",
82
// Person2.class.getName()));
83
try {
84             assertData(container);
85         } finally {
86             container.close();
87         }
88     }
89     
90     private void assertData(ObjectContainer container) {
91         ObjectSet os = container.query(Person2.class);
92         
93         Test.ensureEquals(2, os.size());
94         ensureContains(os, new Person2("Homer Simpson"));
95         ensureContains(os, new Person2("John Cleese"));
96     }
97
98     private ObjectContainer openDotnetDataFile() {
99         return Db4o.openFile(getDotnetDataFilePath());
100     }
101
102     private String JavaDoc getDotnetDataFilePath() {
103         return buildTempPath("dotnet.yap");
104     }
105
106     private String JavaDoc buildTempPath(String JavaDoc fname) {
107         return new File(getTempPath(), fname).getAbsolutePath();
108     }
109
110     private String JavaDoc getTempPath() {
111         return System.getProperty("java.io.tmpdir");
112     }
113
114     private void generateDotnetData() throws IOException {
115         new File(getDotnetDataFilePath()).delete();
116         executeAssembly(generateAssembly(), getDotnetDataFilePath());
117     }
118
119     private String JavaDoc executeAssembly(String JavaDoc assembly, String JavaDoc args) throws IOException {
120         String JavaDoc cmdLine = isLinux()
121             ? "mono " + assembly + " " + args
122             : assembly + " " + args;
123         return exec(cmdLine);
124     }
125
126     private String JavaDoc generateAssembly() throws IOException {
127         String JavaDoc code = "namespace com.db4o.test.aliases {" +
128             "class Person2 { string _name; public Person2(string name) { _name = name; }}" +
129             "class Program {" +
130                 "static void Main(string[] args) {" +
131                     "string fname = args[0];" +
132                     "using (ObjectContainer container = Db4o.OpenFile(fname)) {" +
133                         "container.Set(new Person2(\"Homer Simpson\"));" +
134                         "container.Set(new Person2(\"John Cleese\"));" +
135                     "}" +
136                     "System.Console.WriteLine(\"success\");" +
137                 "}" +
138             "}" +
139         "}";
140         
141         String JavaDoc srcFile = buildTempPath("MyAssembly.cs");
142         writeFile(srcFile, code);
143         String JavaDoc exePath = buildTempPath("MyAssembly.exe");
144
145         File4.copy(db4odllPath(), buildTempPath("db4o.dll"));
146         String JavaDoc cmdLine = csharpCompiler() + " /target:exe /r:" + db4odllPath() + " /out:" + exePath + " " + srcFile;
147         exec(cmdLine);
148         return exePath;
149     }
150
151     private String JavaDoc csharpCompiler() {
152         return isLinux() ? "mcs" : "csc";
153     }
154
155     private boolean isLinux() {
156         return System.getProperty("os.name").toLowerCase().indexOf("linux") != -1;
157     }
158
159     private String JavaDoc exec(String JavaDoc cmdLine) throws IOException {
160         Process JavaDoc p = Runtime.getRuntime().exec(cmdLine);
161         return readStdOut(p);
162     }
163
164     private String JavaDoc db4odllPath() throws IOException {
165         String JavaDoc path = isLinux()
166             ? "../db4obuild/dist/mono/dll/db4o.dll"
167             : "../db4obuild/dist/dll/net/db4o.dll";
168         return new File(path).getCanonicalPath();
169     }
170
171     private void writeFile(String JavaDoc fname, String JavaDoc contents) throws IOException {
172         FileWriter writer = new FileWriter(fname);
173         try {
174             writer.write(contents);
175         } finally {
176             writer.close();
177         }
178     }
179
180     private String JavaDoc readStdOut(Process JavaDoc p) throws IOException {
181         
182         String JavaDoc lineSeparator = System.getProperty("line.separator");
183         BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
184         StringWriter writer = new StringWriter();
185         String JavaDoc line = null;
186         while (null != (line = reader.readLine())) {
187             writer.write(line);
188             writer.write(lineSeparator);
189         }
190         return writer.toString();
191     }
192
193     private void ensureContains(ObjectSet actual, Object JavaDoc expected) {
194         actual.reset();
195         while (actual.hasNext()) {
196             Object JavaDoc next = actual.next();
197             if (next.equals(expected))
198                 return;
199         }
200         Test.ensure(false);
201     }
202     
203     public static void main(String JavaDoc[] args) {
204         Test.runSolo(ClassAliasesTestCase.class);
205     }
206
207 }
208
Popular Tags