PageRenderTime 136ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/cayenne-3.0.1/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/dbsync/SchemaUpdateStrategyTest.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 353 lines | 287 code | 48 blank | 18 comment | 14 complexity | eb9d98420496f0c943d9396ece33c5c4 MD5 | raw file
  1. /*****************************************************************
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. ****************************************************************/
  19. package org.apache.cayenne.access.dbsync;
  20. import java.sql.Connection;
  21. import java.sql.ResultSet;
  22. import java.sql.SQLException;
  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.Collections;
  26. import java.util.HashMap;
  27. import java.util.Iterator;
  28. import java.util.List;
  29. import java.util.Map;
  30. import org.apache.cayenne.CayenneRuntimeException;
  31. import org.apache.cayenne.access.DataNode;
  32. import org.apache.cayenne.access.MockOperationObserver;
  33. import org.apache.cayenne.access.dbsync.CreateIfNoSchemaStrategy;
  34. import org.apache.cayenne.access.dbsync.SchemaUpdateStrategy;
  35. import org.apache.cayenne.access.dbsync.ThrowOnPartialOrCreateSchemaStrategy;
  36. import org.apache.cayenne.access.dbsync.ThrowOnPartialSchemaStrategy;
  37. import org.apache.cayenne.dba.JdbcAdapter;
  38. import org.apache.cayenne.map.DataMap;
  39. import org.apache.cayenne.map.DbEntity;
  40. import org.apache.cayenne.map.EntityResolver;
  41. import org.apache.cayenne.map.MapLoader;
  42. import org.apache.cayenne.query.Query;
  43. import org.apache.cayenne.query.SQLTemplate;
  44. import org.apache.cayenne.unit.CayenneCase;
  45. import org.apache.cayenne.unit.CayenneResources;
  46. import org.xml.sax.InputSource;
  47. public class SchemaUpdateStrategyTest extends CayenneCase {
  48. private DataMap dataMap;
  49. public DataMap getDataMap() {
  50. if (dataMap == null) {
  51. MapLoader mapLoader = new MapLoader();
  52. dataMap = mapLoader.loadDataMap(getMapXml("sus-map.map.xml"));
  53. }
  54. return dataMap;
  55. }
  56. private InputSource getMapXml(String mapName) {
  57. return new InputSource(getClass().getClassLoader().getResourceAsStream(mapName));
  58. }
  59. public void testDBGeneratorStrategy() throws Exception {
  60. String template = "SELECT #result('id' 'int') FROM SUS1";
  61. SQLTemplate query = new SQLTemplate(Object.class, template);
  62. DataMap map = getDataMap();
  63. assertNotNull(map);
  64. DataNode dataNode = createDataNode(map);
  65. int sizeDB = getNameTablesInDB(dataNode).size();
  66. MockOperationObserver observer = new MockOperationObserver();
  67. try {
  68. generateDBWithDBGeneratorStrategy(dataNode, query, observer);
  69. int sizeDB2 = getNameTablesInDB(dataNode).size();
  70. assertEquals(2, sizeDB2 - sizeDB);
  71. dataNode.performQueries(Collections.singletonList((Query) query), observer);
  72. int sizeDB3 = getNameTablesInDB(dataNode).size();
  73. assertEquals(sizeDB2, sizeDB3);
  74. }
  75. finally {
  76. DataNode dataNode2 = createDataNode(map);
  77. dataNode2.setSchemaUpdateStrategy((SchemaUpdateStrategy) Class.forName(
  78. dataNode2.getSchemaUpdateStrategyName()).newInstance());
  79. dropTables(map, dataNode2, observer);
  80. }
  81. assertEquals(getNameTablesInDB(dataNode).size(), sizeDB);
  82. }
  83. public void testThrowOnPartialStrategyTableNoExist() throws Exception {
  84. String template = "SELECT #result('ARTIST_ID' 'int') FROM ARTIST ORDER BY ARTIST_ID";
  85. SQLTemplate query = new SQLTemplate(Object.class, template);
  86. DataMap map = getDataMap();
  87. assertNotNull(map);
  88. MockOperationObserver observer = new MockOperationObserver();
  89. DataNode dataNode = createDataNode(map);
  90. setStrategy(ThrowOnPartialSchemaStrategy.class.getName(), dataNode);
  91. try {
  92. dataNode.performQueries(Collections.singletonList((Query) query), observer);
  93. }
  94. catch (CayenneRuntimeException e) {
  95. assertNotNull(e);
  96. }
  97. try {
  98. dataNode.performQueries(Collections.singletonList((Query) query), observer);
  99. }
  100. catch (CayenneRuntimeException e) {
  101. assertNotNull(e);
  102. }
  103. }
  104. public void testThrowOnPartialStrategyTableExist() throws Exception {
  105. tableExistfForThrowOnPartialAndMixStrategy(ThrowOnPartialSchemaStrategy.class
  106. .getName());
  107. }
  108. public void testThrowOnPartialStrategyWithOneTable() throws Exception {
  109. withOneTableForThrowOnPartialAndMixStrategy(ThrowOnPartialSchemaStrategy.class
  110. .getName());
  111. }
  112. public void testMixedStrategyTableNoExist() throws Exception {
  113. String template = "SELECT #result('id' 'int') FROM SUS1";
  114. SQLTemplate query = new SQLTemplate(Object.class, template);
  115. DataMap map = getDataMap();
  116. assertNotNull(map);
  117. DataNode dataNode = createDataNode(map);
  118. int sizeDB = getNameTablesInDB(dataNode).size();
  119. MockOperationObserver observer = new MockOperationObserver();
  120. setStrategy(ThrowOnPartialOrCreateSchemaStrategy.class.getName(), dataNode);
  121. try {
  122. dataNode.performQueries(Collections.singletonList((Query) query), observer);
  123. Map<String, Boolean> nameTables = getNameTablesInDB(dataNode);
  124. assertTrue(nameTables.get("sus1") != null || nameTables.get("SUS1") != null);
  125. int sizeDB2 = getNameTablesInDB(dataNode).size();
  126. assertEquals(2, sizeDB2 - sizeDB);
  127. dataNode.performQueries(Collections.singletonList((Query) query), observer);
  128. int sizeDB3 = getNameTablesInDB(dataNode).size();
  129. assertEquals(sizeDB2, sizeDB3);
  130. }
  131. finally {
  132. DataNode dataNode2 = createDataNode(map);
  133. dataNode2.setSchemaUpdateStrategy((SchemaUpdateStrategy) Class.forName(
  134. dataNode2.getSchemaUpdateStrategyName()).newInstance());
  135. dropTables(map, dataNode2, observer);
  136. }
  137. assertEquals(getNameTablesInDB(dataNode).size(), sizeDB);
  138. }
  139. public void testMixedStrategyTableExist() throws Exception {
  140. tableExistfForThrowOnPartialAndMixStrategy(ThrowOnPartialOrCreateSchemaStrategy.class
  141. .getName());
  142. }
  143. public void testMixedStrategyWithOneTable() throws Exception {
  144. withOneTableForThrowOnPartialAndMixStrategy(ThrowOnPartialOrCreateSchemaStrategy.class
  145. .getName());
  146. };
  147. public void testNoStandartSchema() {
  148. String template = "SELECT #result('ARTIST_ID' 'int') FROM ARTIST ORDER BY ARTIST_ID";
  149. SQLTemplate query = new SQLTemplate(Object.class, template);
  150. MockOperationObserver observer = new MockOperationObserver();
  151. DataMap map = getDataMap();
  152. assertNotNull(map);
  153. DataNode dataNode = createDataNode(map);
  154. setStrategy(TestSchemaUpdateStrategy.class.getName(), dataNode);
  155. dataNode.performQueries(Collections.singletonList((Query) query), observer);
  156. assertTrue(dataNode.getSchemaUpdateStrategy() instanceof TestSchemaUpdateStrategy);
  157. }
  158. private void withOneTableForThrowOnPartialAndMixStrategy(String strategy) {
  159. DbEntity entity = null;
  160. String template = "SELECT #result('ARTIST_ID' 'int') FROM ARTIST ORDER BY ARTIST_ID";
  161. SQLTemplate query = new SQLTemplate(Object.class, template);
  162. DataMap map = getDataMap();
  163. assertNotNull(map);
  164. MockOperationObserver observer = new MockOperationObserver();
  165. DataNode dataNode = createDataNode(map);
  166. DataNode dataNode2 = createDataNode(map);
  167. try {
  168. int sizeDB = getNameTablesInDB(dataNode).size();
  169. entity = createOneTable(dataNode);
  170. int sizeDB2 = getNameTablesInDB(dataNode).size();
  171. assertEquals(1, sizeDB2 - sizeDB);
  172. setStrategy(strategy, dataNode2);
  173. dataNode2.performQueries(Collections.singletonList((Query) query), observer);
  174. }
  175. catch (CayenneRuntimeException e) {
  176. assertNotNull(e);
  177. }
  178. try {
  179. dataNode2.performQueries(Collections.singletonList((Query) query), observer);
  180. }
  181. catch (CayenneRuntimeException e) {
  182. assertNotNull(e);
  183. }
  184. finally {
  185. if (entity != null) {
  186. Collection<String> template2 = dataNode.getAdapter().dropTableStatements(
  187. entity);
  188. Iterator<String> it = template2.iterator();
  189. List<Query> list = new ArrayList<Query>();
  190. while (it.hasNext()) {
  191. SQLTemplate q = new SQLTemplate(Object.class, it.next());
  192. list.add(q);
  193. }
  194. dataNode.performQueries(list, observer);
  195. }
  196. }
  197. }
  198. private void tableExistfForThrowOnPartialAndMixStrategy(String strategy)
  199. throws Exception {
  200. String template = "SELECT #result('ARTIST_ID' 'int') FROM ARTIST ORDER BY ARTIST_ID";
  201. SQLTemplate query = new SQLTemplate(Object.class, template);
  202. DataMap map = getDataMap();
  203. assertNotNull(map);
  204. MockOperationObserver observer = new MockOperationObserver();
  205. DataNode dataNode = createDataNode(map);
  206. int sizeDB = getNameTablesInDB(dataNode).size();
  207. generateDBWithDBGeneratorStrategy(dataNode, query, observer);
  208. int sizeDB2 = getNameTablesInDB(dataNode).size();
  209. assertEquals(2, sizeDB2 - sizeDB);
  210. try {
  211. DataNode dataNode2 = createDataNode(map);
  212. setStrategy(strategy, dataNode2);
  213. dataNode2.performQueries(Collections.singletonList((Query) query), observer);
  214. }
  215. finally {
  216. dropTables(map, dataNode, observer);
  217. }
  218. }
  219. private DbEntity createOneTable(DataNode dataNode) {
  220. DataMap map = getDataMap();
  221. Collection<DbEntity> ent = map.getDbEntities();
  222. DbEntity entity = ent.iterator().next();
  223. String template = dataNode.getAdapter().createTable(entity);
  224. SQLTemplate query = new SQLTemplate(Object.class, template);
  225. MockOperationObserver observer = new MockOperationObserver();
  226. setStrategy(null, dataNode);
  227. dataNode.performQueries(Collections.singletonList((Query) query), observer);
  228. return entity;
  229. }
  230. private DataNode createDataNode(DataMap map) {
  231. JdbcAdapter adapter = (JdbcAdapter) getAccessStackAdapter().getAdapter();
  232. Collection<DataMap> colection = new ArrayList<DataMap>();
  233. colection.add(map);
  234. DataNode dataNode = new DataNode();
  235. dataNode.setDataMaps(colection);
  236. dataNode.setAdapter(adapter);
  237. dataNode.setDataSource(CayenneResources.getResources().getDataSource());
  238. dataNode.setDataSourceFactory(getNode().getDataSourceFactory());
  239. dataNode.setSchemaUpdateStrategyName(getNode().getSchemaUpdateStrategyName());
  240. dataNode.setEntityResolver(new EntityResolver(colection));
  241. return dataNode;
  242. }
  243. private void generateDBWithDBGeneratorStrategy(
  244. DataNode dataNode,
  245. SQLTemplate query,
  246. MockOperationObserver observer) {
  247. setStrategy(CreateIfNoSchemaStrategy.class.getName(), dataNode);
  248. dataNode.performQueries(Collections.singletonList((Query) query), observer);
  249. Map<String, Boolean> nameTables = getNameTablesInDB(dataNode);
  250. assertTrue(nameTables.get("sus1") != null || nameTables.get("SUS1") != null);
  251. }
  252. private void setStrategy(String name, DataNode dataNode) {
  253. dataNode.setSchemaUpdateStrategyName(name);
  254. try {
  255. dataNode.setSchemaUpdateStrategy((SchemaUpdateStrategy) Class.forName(
  256. dataNode.getSchemaUpdateStrategyName()).newInstance());
  257. }
  258. catch (Exception e) {
  259. throw new CayenneRuntimeException(e);
  260. }
  261. }
  262. private void dropTables(DataMap map, DataNode dataNode, MockOperationObserver observer) {
  263. Collection<DbEntity> ent = map.getDbEntities();
  264. Iterator<DbEntity> iterator = ent.iterator();
  265. while (iterator.hasNext()) {
  266. Collection<String> collectionDrop = dataNode
  267. .getAdapter()
  268. .dropTableStatements(iterator.next());
  269. for (String s : collectionDrop) {
  270. SQLTemplate queryDrop = new SQLTemplate(Object.class, s);
  271. dataNode.performQueries(
  272. Collections.singletonList((Query) queryDrop),
  273. observer);
  274. }
  275. }
  276. }
  277. private Map<String, Boolean> getNameTablesInDB(DataNode dataNode) {
  278. String tableLabel = dataNode.getAdapter().tableTypeForTable();
  279. Connection con = null;
  280. Map<String, Boolean> nameTables = new HashMap<String, Boolean>();
  281. try {
  282. con = dataNode.getDataSource().getConnection();
  283. ResultSet rs = con.getMetaData().getTables(null, null, "%", new String[] {
  284. tableLabel
  285. });
  286. while (rs.next()) {
  287. String name = rs.getString("TABLE_NAME");
  288. nameTables.put(name, false);
  289. }
  290. rs.close();
  291. }
  292. catch (SQLException e) {
  293. throw new CayenneRuntimeException(e);
  294. }
  295. finally {
  296. try {
  297. con.close();
  298. }
  299. catch (SQLException e) {
  300. throw new CayenneRuntimeException(e);
  301. }
  302. }
  303. return nameTables;
  304. }
  305. }