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.test.aliases;
022:
023: import java.io.*;
024:
025: import com.db4o.*;
026: import com.db4o.config.*;
027: import com.db4o.db4ounit.util.IOServices;
028: import com.db4o.foundation.io.File4;
029: import com.db4o.test.Test;
030:
031: public class ClassAliasesTestCase {
032:
033: public void testTypeAlias() {
034:
035: cleanUp();
036:
037: ObjectContainer container = Test.objectContainer();
038:
039: container.set(new Person1("Homer Simpson"));
040: container.set(new Person1("John Cleese"));
041:
042: container = Test.reOpen();
043: container.ext().configure().addAlias(
044: // Person1 instances should be read as Person2 objects
045: new TypeAlias("com.db4o.test.aliases.Person1",
046: "com.db4o.test.aliases.Person2"));
047:
048: assertData(container);
049: }
050:
051: public void testAliasedTypeIsStoredCorrectly() {
052:
053: cleanUp();
054:
055: ObjectContainer container = Test.objectContainer();
056: container.ext().configure().addAlias(
057: // Person1 instances should be read as Person2 objects
058: new TypeAlias("com.db4o.test.aliases.Person1",
059: "com.db4o.test.aliases.Person2"));
060:
061: container.set(new Person2("Homer Simpson"));
062: container.set(new Person2("John Cleese"));
063:
064: container = Test.reOpen();
065: assertData(container);
066:
067: }
068:
069: private void cleanUp() {
070: Test.reOpen();
071: Test.deleteAllInstances(Person1.class);
072: }
073:
074: public void testAccessingDotnetFromJava() throws Exception {
075: generateDotnetData();
076: ObjectContainer container = openDotnetDataFile();
077: container.ext().configure().addAlias(
078: new WildcardAlias(
079: "com.db4o.test.aliases.*, MyAssembly",
080: "com.db4o.test.aliases.*"));
081: // new TypeAlias(
082: // Person2.class.getName() + ", MyAssembly",
083: // Person2.class.getName()));
084: try {
085: assertData(container);
086: } finally {
087: container.close();
088: }
089: }
090:
091: private void assertData(ObjectContainer container) {
092: ObjectSet os = container.query(Person2.class);
093:
094: Test.ensureEquals(2, os.size());
095: ensureContains(os, new Person2("Homer Simpson"));
096: ensureContains(os, new Person2("John Cleese"));
097: }
098:
099: private ObjectContainer openDotnetDataFile() {
100: return Db4o.openFile(getDotnetDataFilePath());
101: }
102:
103: private String getDotnetDataFilePath() {
104: return buildTempPath("dotnet.yap");
105: }
106:
107: private String buildTempPath(String fname) {
108: return IOServices.buildTempPath(fname);
109: }
110:
111: private void generateDotnetData() throws IOException {
112: new File(getDotnetDataFilePath()).delete();
113: executeAssembly(generateAssembly(), getDotnetDataFilePath());
114: }
115:
116: private String executeAssembly(String assembly, String args)
117: throws IOException {
118: String cmdLine = isLinux() ? "mono " + assembly + " " + args
119: : assembly + " " + args;
120: return exec(cmdLine);
121: }
122:
123: private String generateAssembly() throws IOException {
124: String code = "namespace com.db4o.test.aliases {"
125: + "class Person2 { string _name; public Person2(string name) { _name = name; }}"
126: + "class Program {"
127: + "static void Main(string[] args) {"
128: + "string fname = args[0];"
129: + "using (ObjectContainer container = Db4o.OpenFile(fname)) {"
130: + "container.Set(new Person2(\"Homer Simpson\"));"
131: + "container.Set(new Person2(\"John Cleese\"));" + "}"
132: + "System.Console.WriteLine(\"success\");" + "}" + "}"
133: + "}";
134:
135: String srcFile = buildTempPath("MyAssembly.cs");
136: writeFile(srcFile, code);
137: String exePath = buildTempPath("MyAssembly.exe");
138:
139: File4.copy(db4odllPath(), buildTempPath("db4o.dll"));
140: String cmdLine = csharpCompiler() + " /target:exe /r:"
141: + db4odllPath() + " /out:" + exePath + " " + srcFile;
142: exec(cmdLine);
143: return exePath;
144: }
145:
146: private String csharpCompiler() {
147: return isLinux() ? "mcs" : "csc";
148: }
149:
150: private boolean isLinux() {
151: return System.getProperty("os.name").toLowerCase().indexOf(
152: "linux") != -1;
153: }
154:
155: private String exec(String cmdLine) throws IOException {
156: Process p = Runtime.getRuntime().exec(cmdLine);
157: return readStdOut(p);
158: }
159:
160: private String db4odllPath() throws IOException {
161: String path = isLinux() ? "../db4obuild/dist/mono/dll/db4o.dll"
162: : "../db4obuild/dist/dll/net/db4o.dll";
163: return new File(path).getCanonicalPath();
164: }
165:
166: private void writeFile(String fname, String contents)
167: throws IOException {
168: FileWriter writer = new FileWriter(fname);
169: try {
170: writer.write(contents);
171: } finally {
172: writer.close();
173: }
174: }
175:
176: private String readStdOut(Process p) throws IOException {
177:
178: String lineSeparator = System.getProperty("line.separator");
179: BufferedReader reader = new BufferedReader(
180: new InputStreamReader(p.getInputStream()));
181: StringWriter writer = new StringWriter();
182: String line = null;
183: while (null != (line = reader.readLine())) {
184: writer.write(line);
185: writer.write(lineSeparator);
186: }
187: return writer.toString();
188: }
189:
190: private void ensureContains(ObjectSet actual, Object expected) {
191: actual.reset();
192: while (actual.hasNext()) {
193: Object next = actual.next();
194: if (next.equals(expected))
195: return;
196: }
197: Test.ensure(false);
198: }
199:
200: public static void main(String[] args) {
201: Test.runSolo(ClassAliasesTestCase.class);
202: }
203:
204: }
|