PageRenderTime 28ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/derby-10.9.1.0/db-derby-10.9.1.0-src/java/testing/org/apache/derbyTesting/functionTests/tests/replicationTests/ReplicationRun_Local_StateTest_part2.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 393 lines | 265 code | 55 blank | 73 comment | 0 complexity | 4be3f16e784379acd58f4b9a9faad2d6 MD5 | raw file
  1. /*
  2. Derby - Class org.apache.derbyTesting.functionTests.tests.replicationTests.ReplicationRun_Local_StateTest_part2
  3. Licensed to the Apache Software Foundation (ASF) under one or more
  4. contributor license agreements. See the NOTICE file distributed with
  5. this work for additional information regarding copyright ownership.
  6. The ASF licenses this file to You under the Apache License, Version 2.0
  7. (the "License"); you may not use this file except in compliance with
  8. the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. */
  16. package org.apache.derbyTesting.functionTests.tests.replicationTests;
  17. import java.sql.Connection;
  18. import java.sql.DriverManager;
  19. import java.sql.ResultSet;
  20. import java.sql.SQLException;
  21. import java.sql.Statement;
  22. import junit.framework.Test;
  23. import junit.framework.TestSuite;
  24. import org.apache.derbyTesting.junit.SecurityManagerSetup;
  25. /**
  26. * Run a replication test on localhost
  27. * by using default values for master and slave hosts,
  28. * and master and slave ports.
  29. *
  30. */
  31. public class ReplicationRun_Local_StateTest_part2 extends ReplicationRun
  32. {
  33. /**
  34. * Creates a new instance of ReplicationRun_Local
  35. * @param testcaseName Identifying the test.
  36. */
  37. public ReplicationRun_Local_StateTest_part2(String testcaseName)
  38. {
  39. super(testcaseName);
  40. }
  41. public static Test suite()
  42. {
  43. TestSuite suite = new TestSuite("ReplicationRun_Local_StateTest_part2 Suite");
  44. suite.addTestSuite( ReplicationRun_Local_StateTest_part2.class );
  45. return SecurityManagerSetup.noSecurityManager(suite);
  46. }
  47. /**
  48. * Verify that correct response to replication "commands":
  49. * startSlave, startMaster, stopSlave, stopMaster and failOver,
  50. * are given when the replicating database is in the following states:
  51. * Failover has been performed and
  52. * - slave db has not been shut down,
  53. * - slave db has been shut down,
  54. * - slave server has been stopped,
  55. * - master db has been shut down,
  56. * - master server has been stopped.
  57. * @throws java.lang.Exception
  58. */
  59. public void testReplication_Local_StateTest_part2()
  60. throws Exception
  61. {
  62. cleanAllTestHosts();
  63. initEnvironment();
  64. initMaster(masterServerHost,
  65. replicatedDb);
  66. startServer(masterJvmVersion, derbyMasterVersion,
  67. masterServerHost,
  68. ALL_INTERFACES,
  69. masterServerPort,
  70. masterDbSubPath);
  71. startServer(slaveJvmVersion, derbySlaveVersion,
  72. slaveServerHost,
  73. ALL_INTERFACES,
  74. slaveServerPort,
  75. slaveDbSubPath);
  76. startServerMonitor(slaveServerHost);
  77. bootMasterDatabase(jvmVersion,
  78. masterDatabasePath +FS+ masterDbSubPath,
  79. replicatedDb,
  80. masterServerHost,
  81. masterServerPort,
  82. null // bootLoad, // The "test" to start when booting db.
  83. );
  84. initSlave(slaveServerHost,
  85. jvmVersion,
  86. replicatedDb);
  87. startSlave(jvmVersion, replicatedDb,
  88. slaveServerHost,
  89. slaveServerPort,
  90. slaveServerHost,
  91. slaveReplPort,
  92. testClientHost);
  93. startMaster(jvmVersion, replicatedDb,
  94. masterServerHost,
  95. masterServerPort,
  96. masterServerHost,
  97. slaveServerPort,
  98. slaveServerHost,
  99. slaveReplPort);
  100. // Run a "load" on the master to make sure there
  101. // has been replication to slave.
  102. replicationTest = "org.apache.derbyTesting.functionTests.tests.replicationTests.ReplicationTestRun";
  103. util.DEBUG("replicationTest: " + replicationTest);
  104. replicationVerify = "org.apache.derbyTesting.functionTests.tests.replicationTests.ReplicationTestRun_Verify";
  105. util.DEBUG("replicationVerify: " + replicationVerify);
  106. runTest(replicationTest, // Returns immediatly if replicationTest is null.
  107. jvmVersion,
  108. testClientHost,
  109. masterServerHost, masterServerPort,
  110. replicatedDb);
  111. // Check that this is gone after failover!
  112. Connection mConn = getConnection(masterServerHost, masterServerPort,
  113. masterDatabasePath, masterDbSubPath, replicatedDb);
  114. failOver(jvmVersion,
  115. masterDatabasePath, masterDbSubPath, replicatedDb,
  116. masterServerHost, // Where the master db is run.
  117. masterServerPort,
  118. testClientHost);
  119. connectPing(slaveDatabasePath+FS+slaveDbSubPath+FS+replicatedDb,
  120. slaveServerHost,slaveServerPort,
  121. testClientHost);
  122. verifySlave();
  123. // We should verify the master as well, at least to see that we still can connect.
  124. verifyMaster();
  125. _testPreStoppedSlave(mConn);
  126. shutdownDb(jvmVersion,
  127. slaveServerHost, slaveServerPort,
  128. slaveDatabasePath+FS+slaveDbSubPath, replicatedDb,
  129. testClientHost);
  130. _testPostStoppedSlave();
  131. // _testPreStoppedSlaveServer();
  132. stopServer(jvmVersion, derbyVersion,
  133. slaveServerHost, slaveServerPort);
  134. _testPostStoppedSlaveServer();
  135. // _testPreStoppedMaster();
  136. // NOTE:
  137. // If stopMaster is "connect 'jdbc:...:dbname;shutdown=true';",
  138. // this method call will fail because failover has already shutdown
  139. // the master database
  140. shutdownDb(jvmVersion,
  141. masterServerHost, masterServerPort,
  142. masterDatabasePath+FS+masterDbSubPath, replicatedDb,
  143. testClientHost);
  144. _testPostStoppedMaster();
  145. // _testPreStoppedMasterServer();
  146. stopServer(jvmVersion, derbyVersion,
  147. masterServerHost, masterServerPort);
  148. _testPostStoppedServer();
  149. }
  150. private void _testPreStoppedSlave(Connection mConn)
  151. throws Exception
  152. {
  153. util.DEBUG("_testPreStoppedSlave");
  154. /*
  155. * Future test case; should probably be done in a separate test
  156. * - let Derby instances change roles:
  157. * - (M) copy db from slave location to master location
  158. * - (M) start old master in slave mode
  159. * - (S) start old slave in master mode
  160. *
  161. */
  162. // Tests against slave:
  163. assertException(
  164. _startSlave(slaveServerHost,slaveServerPort,
  165. slaveDatabasePath, replicatedDb,
  166. slaveReplPort),
  167. "XRE09");
  168. assertException(
  169. stopSlave(slaveServerHost,slaveServerPort,
  170. slaveDatabasePath, replicatedDb,
  171. true),
  172. "XRE40");
  173. assertException(
  174. _failOver(slaveServerHost,slaveServerPort,
  175. slaveDatabasePath, slaveDbSubPath, replicatedDb),
  176. "XRE07");
  177. // Tests against master:
  178. assertException(
  179. _startMaster(masterServerHost,masterServerPort,
  180. masterDatabasePath, replicatedDb,
  181. slaveServerHost,slaveReplPort),
  182. "XRE04");
  183. assertException(
  184. _failOver(masterServerHost,masterServerPort,
  185. masterDatabasePath, masterDbSubPath, replicatedDb),
  186. "XRE07");
  187. // connect / show tables
  188. assertException(
  189. _executeQuery(mConn, "select count(*) from SYS.SYSTABLES"),
  190. "08006"); // Thats's just how it is...
  191. assertException(
  192. _executeQuery(mConn, "select count(*) from SYS.SYSTABLES"),
  193. "08003"); // Thats's just how it is...
  194. // Tests against slave:
  195. Connection sConn = getConnection(slaveServerHost, slaveServerPort,
  196. slaveDatabasePath, slaveDbSubPath, replicatedDb); // OK
  197. assertException(
  198. _executeQuery(sConn, "select count(*) from SYS.SYSTABLES"),
  199. null); // null: Should be OK
  200. sConn.close();
  201. }
  202. private void _testPostStoppedSlave()
  203. {
  204. util.DEBUG("_testPostStoppedSlave");
  205. /* No value-adding suggestions here exept that calling startSlave
  206. * should hang now.
  207. */
  208. util.DEBUG("_testPostStoppedSlave Not yet implemented."
  209. + " No value-adding suggestions here"
  210. + " exept that calling startSlave should hang now.");
  211. }
  212. private void _testPostStoppedSlaveServer()
  213. {
  214. /* No value-adding suggestions here */
  215. util.DEBUG("_testPostStoppedSlaveServer Not yet implemented."
  216. + " No value-adding suggestions here.");
  217. }
  218. private void _testPostStoppedMaster()
  219. {
  220. /* No value-adding suggestions here since the stopMaster method will
  221. * not do anything when called after failover
  222. */
  223. util.DEBUG("_testPostStoppedMaster Not yet implemented."
  224. + "No value-adding suggestions here since the stopMaster method"
  225. + " will not do anything when called after failover");
  226. }
  227. private void _testPostStoppedServer()
  228. {
  229. /* No value-adding suggestions here */
  230. util.DEBUG("_testPostStoppedServer Not yet implemented."
  231. + " No value-adding suggestions here.");
  232. }
  233. SQLException _startSlave(String slaveServerHost, int slaveServerPort,
  234. String slaveDatabasePath, String replicatedDb,
  235. int slaveReplPort)
  236. {
  237. String db = slaveDatabasePath +FS+ReplicationRun.slaveDbSubPath +FS+ replicatedDb;
  238. String connectionURL = "jdbc:derby:"
  239. + "//" + slaveServerHost + ":" + slaveServerPort + "/"
  240. + db
  241. + ";startSlave=true"
  242. + ";slaveHost=" + slaveServerHost
  243. + ";slavePort=" + slaveReplPort;
  244. util.DEBUG(connectionURL);
  245. try
  246. {
  247. Connection conn = DriverManager.getConnection(connectionURL);
  248. util.DEBUG("startSlave Unexpectedly connected as: " + connectionURL);
  249. return new SQLException("startSlave Unexpectedly connected");
  250. }
  251. catch (SQLException se)
  252. {
  253. return se;
  254. }
  255. }
  256. SQLException _failOver(String serverHost, int serverPort,
  257. String databasePath, String dbSubPath, String replicatedDb)
  258. {
  259. String db = databasePath +FS+dbSubPath +FS+ replicatedDb;
  260. String connectionURL = "jdbc:derby:"
  261. + "//" + serverHost + ":" + serverPort + "/"
  262. + db
  263. + ";failover=true";
  264. util.DEBUG(connectionURL);
  265. try
  266. {
  267. Connection conn = DriverManager.getConnection(connectionURL);
  268. util.DEBUG("failOver Unexpectedly connected as: " + connectionURL);
  269. return new SQLException("failOver Unexpectedly connected");
  270. }
  271. catch (SQLException se)
  272. {
  273. return se;
  274. }
  275. }
  276. SQLException _startMaster(String masterServerHost, int masterServerPort,
  277. String databasePath, String replicatedDb,
  278. String slaveServerHost,
  279. int slaveReplPort)
  280. {
  281. String db = databasePath +FS+ReplicationRun.masterDbSubPath +FS+ replicatedDb;
  282. String connectionURL = "jdbc:derby:"
  283. + "//" + masterServerHost + ":" + masterServerPort + "/"
  284. + db
  285. + ";startMaster=true"
  286. + ";slaveHost=" + slaveServerHost
  287. + ";slavePort=" + slaveReplPort;
  288. util.DEBUG(connectionURL);
  289. try
  290. {
  291. Connection conn = DriverManager.getConnection(connectionURL);
  292. util.DEBUG("startMaster Unexpectedly connected as: " + connectionURL);
  293. return new SQLException("startMaster Unexpectedly connected");
  294. }
  295. catch (SQLException se)
  296. {
  297. return se;
  298. }
  299. }
  300. SQLException connectTo(String serverHost, int serverPort,
  301. String databasePath, String dbSubPath, String replicatedDb)
  302. {
  303. String db = databasePath +FS+dbSubPath +FS+ replicatedDb;
  304. String connectionURL = "jdbc:derby:"
  305. + "//" + serverHost + ":" + serverPort + "/"
  306. + db;
  307. util.DEBUG(connectionURL);
  308. try
  309. {
  310. Connection conn = DriverManager.getConnection(connectionURL);
  311. util.DEBUG("connectTo Unexpectedly connected as: " + connectionURL);
  312. return new SQLException("connectTo Unexpectedly connected");
  313. }
  314. catch (SQLException se)
  315. {
  316. return se;
  317. }
  318. }
  319. SQLException _executeQuery(Connection conn, String query)
  320. {
  321. util.DEBUG("executeQuery: " + query);
  322. try
  323. {
  324. Statement s = conn.createStatement();
  325. ResultSet rs = s.executeQuery(query);
  326. return null;
  327. }
  328. catch (SQLException se)
  329. {
  330. return se;
  331. }
  332. }
  333. }