1 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 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 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 { 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 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 getDotnetDataFilePath() { 103 return buildTempPath("dotnet.yap"); 104 } 105 106 private String buildTempPath(String fname) { 107 return new File(getTempPath(), fname).getAbsolutePath(); 108 } 109 110 private String 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 executeAssembly(String assembly, String args) throws IOException { 120 String cmdLine = isLinux() 121 ? "mono " + assembly + " " + args 122 : assembly + " " + args; 123 return exec(cmdLine); 124 } 125 126 private String generateAssembly() throws IOException { 127 String 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 srcFile = buildTempPath("MyAssembly.cs"); 142 writeFile(srcFile, code); 143 String exePath = buildTempPath("MyAssembly.exe"); 144 145 File4.copy(db4odllPath(), buildTempPath("db4o.dll")); 146 String cmdLine = csharpCompiler() + " /target:exe /r:" + db4odllPath() + " /out:" + exePath + " " + srcFile; 147 exec(cmdLine); 148 return exePath; 149 } 150 151 private String 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 exec(String cmdLine) throws IOException { 160 Process p = Runtime.getRuntime().exec(cmdLine); 161 return readStdOut(p); 162 } 163 164 private String db4odllPath() throws IOException { 165 String 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 fname, String 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 readStdOut(Process p) throws IOException { 181 182 String lineSeparator = System.getProperty("line.separator"); 183 BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); 184 StringWriter writer = new StringWriter(); 185 String 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 expected) { 194 actual.reset(); 195 while (actual.hasNext()) { 196 Object next = actual.next(); 197 if (next.equals(expected)) 198 return; 199 } 200 Test.ensure(false); 201 } 202 203 public static void main(String [] args) { 204 Test.runSolo(ClassAliasesTestCase.class); 205 } 206 207 } 208 | Popular Tags |