Lines 10-35
Link Here
|
10 |
*/ |
10 |
*/ |
11 |
package org.eclipse.emf.cdo.tests.db; |
11 |
package org.eclipse.emf.cdo.tests.db; |
12 |
|
12 |
|
13 |
import org.eclipse.emf.cdo.server.IRepository; |
13 |
import org.eclipse.emf.cdo.common.CDOCommonRepository.IDGenerationLocation; |
14 |
import org.eclipse.emf.cdo.server.db.mapping.IMappingStrategy; |
|
|
15 |
|
16 |
import org.eclipse.net4j.db.DBUtil; |
17 |
import org.eclipse.net4j.db.IDBAdapter; |
18 |
import org.eclipse.net4j.db.h2.H2Adapter; |
19 |
import org.eclipse.net4j.util.WrappedException; |
20 |
import org.eclipse.net4j.util.io.IOUtil; |
21 |
import org.eclipse.net4j.util.io.TMPUtil; |
22 |
|
23 |
import org.h2.jdbcx.JdbcDataSource; |
24 |
|
25 |
import javax.sql.DataSource; |
26 |
|
27 |
import java.io.File; |
28 |
import java.sql.Connection; |
29 |
import java.sql.Statement; |
30 |
import java.util.ArrayList; |
31 |
import java.util.HashMap; |
32 |
import java.util.Map; |
33 |
|
14 |
|
34 |
import junit.framework.Test; |
15 |
import junit.framework.Test; |
35 |
import junit.framework.TestSuite; |
16 |
import junit.framework.TestSuite; |
Lines 47-279
Link Here
|
47 |
@Override |
28 |
@Override |
48 |
protected void initConfigSuites(TestSuite parent) |
29 |
protected void initConfigSuites(TestSuite parent) |
49 |
{ |
30 |
{ |
50 |
addScenario(parent, COMBINED, H2Branching.ReusableFolder.INSTANCE, JVM, NATIVE); |
31 |
addScenario(parent, COMBINED, new H2Config(true, true, false, false, IDGenerationLocation.STORE), JVM, NATIVE); |
51 |
addScenario(parent, COMBINED, H2Branching.ReusableFolder.RANGE_INSTANCE, JVM, NATIVE); |
32 |
addScenario(parent, COMBINED, new H2Config(true, true, true, false, IDGenerationLocation.STORE), JVM, NATIVE); |
52 |
addScenario(parent, COMBINED, H2Branching.ReusableFolder.CopyOnBranch.INSTANCE, JVM, NATIVE); |
33 |
addScenario(parent, COMBINED, new H2Config(true, true, true, true, IDGenerationLocation.STORE), JVM, NATIVE); |
53 |
} |
|
|
54 |
|
55 |
@Override |
56 |
protected boolean hasAuditSupport() |
57 |
{ |
58 |
return true; |
59 |
} |
60 |
|
61 |
@Override |
62 |
protected boolean hasBranchingSupport() |
63 |
{ |
64 |
return true; |
65 |
} |
66 |
|
67 |
/** |
68 |
* @author Eike Stepper |
69 |
*/ |
70 |
public static class H2Branching extends DBStoreRepositoryConfig |
71 |
{ |
72 |
private static final long serialVersionUID = 1L; |
73 |
|
74 |
public static final H2Branching INSTANCE = new H2Branching("DBStore: H2 (branching)", |
75 |
"org.eclipse.emf.cdo.server.internal.db.mapping.horizontal.HorizontalBranchingMappingStrategy"); |
76 |
|
77 |
protected transient File dbFolder; |
78 |
|
79 |
private String mappingStrategy; |
80 |
|
81 |
public H2Branching(String name, String mappingStrategy) |
82 |
{ |
83 |
super(name); |
84 |
this.mappingStrategy = mappingStrategy; |
85 |
} |
86 |
|
87 |
@Override |
88 |
protected void initRepositoryProperties(Map<String, String> props) |
89 |
{ |
90 |
super.initRepositoryProperties(props); |
91 |
props.put(IRepository.Props.SUPPORTING_AUDITS, "true"); |
92 |
props.put(IRepository.Props.SUPPORTING_BRANCHES, "true"); |
93 |
} |
94 |
|
95 |
@SuppressWarnings("unchecked") |
96 |
@Override |
97 |
protected IMappingStrategy createMappingStrategy() |
98 |
{ |
99 |
try |
100 |
{ |
101 |
Class<IMappingStrategy> clazz = (Class<IMappingStrategy>)Class.forName(mappingStrategy); |
102 |
return clazz.newInstance(); |
103 |
} |
104 |
catch (Exception ex) |
105 |
{ |
106 |
throw WrappedException.wrap(ex); |
107 |
} |
108 |
} |
109 |
|
110 |
@Override |
111 |
protected IDBAdapter createDBAdapter() |
112 |
{ |
113 |
return new H2Adapter(); |
114 |
} |
115 |
|
116 |
@Override |
117 |
protected DataSource createDataSource(String repoName) |
118 |
{ |
119 |
if (dbFolder == null) |
120 |
{ |
121 |
dbFolder = createDBFolder(); |
122 |
tearDownClean(); |
123 |
} |
124 |
|
125 |
JdbcDataSource dataSource = new JdbcDataSource(); |
126 |
dataSource.setURL("jdbc:h2:" + dbFolder.getAbsolutePath() + "/h2test;SCHEMA=" + repoName); |
127 |
|
128 |
return dataSource; |
129 |
} |
130 |
|
131 |
protected void tearDownClean() |
132 |
{ |
133 |
IOUtil.delete(dbFolder); |
134 |
} |
135 |
|
136 |
protected File createDBFolder() |
137 |
{ |
138 |
return TMPUtil.createTempFolder("h2_", "_test"); |
139 |
} |
140 |
|
141 |
/** |
142 |
* @author Eike Stepper |
143 |
*/ |
144 |
public static class ReusableFolder extends H2Branching |
145 |
{ |
146 |
private static final long serialVersionUID = 1L; |
147 |
|
148 |
public static final ReusableFolder INSTANCE = new ReusableFolder("DBStore: H2 (branching)", |
149 |
"org.eclipse.emf.cdo.server.internal.db.mapping.horizontal.HorizontalBranchingMappingStrategy"); |
150 |
|
151 |
public static final ReusableFolder RANGE_INSTANCE = new ReusableFolder("DBStore: H2 (branching, range-based)", |
152 |
"org.eclipse.emf.cdo.server.internal.db.mapping.horizontal.HorizontalBranchingMappingStrategyWithRanges"); |
153 |
|
154 |
private static File reusableFolder; |
155 |
|
156 |
private static JdbcDataSource defaultDataSource; |
157 |
|
158 |
private transient ArrayList<String> repoNames = new ArrayList<String>(); |
159 |
|
160 |
public ReusableFolder(String name, String mappingStrategy) |
161 |
{ |
162 |
super(name, mappingStrategy); |
163 |
} |
164 |
|
165 |
@Override |
166 |
protected DataSource createDataSource(String repoName) |
167 |
{ |
168 |
if (reusableFolder == null) |
169 |
{ |
170 |
reusableFolder = createDBFolder(); |
171 |
IOUtil.delete(reusableFolder); |
172 |
} |
173 |
|
174 |
dbFolder = reusableFolder; |
175 |
if (defaultDataSource == null) |
176 |
{ |
177 |
defaultDataSource = new JdbcDataSource(); |
178 |
defaultDataSource.setURL("jdbc:h2:" + dbFolder.getAbsolutePath() + "/h2test"); |
179 |
} |
180 |
|
181 |
Connection conn = null; |
182 |
Statement stmt = null; |
183 |
|
184 |
try |
185 |
{ |
186 |
conn = defaultDataSource.getConnection(); |
187 |
stmt = conn.createStatement(); |
188 |
|
189 |
if (!isRestarting()) |
190 |
{ |
191 |
stmt.execute("DROP SCHEMA IF EXISTS " + repoName); |
192 |
} |
193 |
|
194 |
stmt.execute("CREATE SCHEMA IF NOT EXISTS " + repoName); |
195 |
|
196 |
/* |
197 |
* final WebServer webServer = new WebServer(); webServer.init(new String[] { "-webPort", "7778" }); |
198 |
* webServer.start(); System.out.println("----------------------------------"); |
199 |
* System.out.println("----------------------------------"); System.out.println(webServer.addSession(conn)); |
200 |
* System.out.println("----------------------------------"); |
201 |
* System.out.println("----------------------------------"); new Thread() { |
202 |
* @Override public void run() { webServer.listen(); } }.start(); |
203 |
*/ |
204 |
} |
205 |
catch (Exception ex) |
206 |
{ |
207 |
ex.printStackTrace(); |
208 |
} |
209 |
finally |
210 |
{ |
211 |
DBUtil.close(stmt); |
212 |
DBUtil.close(conn); |
213 |
} |
214 |
|
215 |
JdbcDataSource dataSource = new JdbcDataSource(); |
216 |
dataSource.setURL("jdbc:h2:" + dbFolder.getAbsolutePath() + "/h2test;SCHEMA=" + repoName); |
217 |
return dataSource; |
218 |
} |
219 |
|
220 |
@Override |
221 |
protected void tearDownClean() |
222 |
{ |
223 |
for (String repoName : repoNames) |
224 |
{ |
225 |
tearDownClean(repoName); |
226 |
} |
227 |
} |
228 |
|
229 |
protected void tearDownClean(String repoName) |
230 |
{ |
231 |
reusableFolder.deleteOnExit(); |
232 |
Connection connection = null; |
233 |
Statement stmt = null; |
234 |
|
235 |
try |
236 |
{ |
237 |
connection = defaultDataSource.getConnection(); |
238 |
stmt = connection.createStatement(); |
239 |
stmt.execute("DROP SCHEMA " + repoName); |
240 |
} |
241 |
catch (Exception ex) |
242 |
{ |
243 |
throw WrappedException.wrap(ex); |
244 |
} |
245 |
finally |
246 |
{ |
247 |
DBUtil.close(stmt); |
248 |
DBUtil.close(connection); |
249 |
} |
250 |
} |
251 |
|
252 |
public static class CopyOnBranch extends ReusableFolder |
253 |
{ |
254 |
private static final long serialVersionUID = 1L; |
255 |
|
256 |
public static final ReusableFolder INSTANCE = new CopyOnBranch( |
257 |
"DBStore: H2 (branching, range-based, copyOnBranch)", |
258 |
"org.eclipse.emf.cdo.server.internal.db.mapping.horizontal.HorizontalBranchingMappingStrategyWithRanges"); |
259 |
|
260 |
public CopyOnBranch(String name, String ms) |
261 |
{ |
262 |
super(name, ms); |
263 |
} |
264 |
|
265 |
@Override |
266 |
protected IMappingStrategy createMappingStrategy() |
267 |
{ |
268 |
IMappingStrategy ms = super.createMappingStrategy(); |
269 |
|
270 |
Map<String, String> properties = new HashMap<String, String>(); |
271 |
properties.put("copyOnBranch", "true"); |
272 |
ms.setProperties(properties); |
273 |
|
274 |
return ms; |
275 |
} |
276 |
} |
277 |
} |
278 |
} |
34 |
} |
279 |
} |
35 |
} |