/mysql-connector-java-5.1.40/src/testsuite/regression/SubqueriesRegressionTest.java

https://gitlab.com/Grupo3/ProyectoED · Java · 175 lines · 103 code · 29 blank · 43 comment · 10 complexity · 1303363cd33d541ca4296cb80eefe17e MD5 · raw file

  1. /*
  2. Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
  3. The MySQL Connector/J is licensed under the terms of the GPLv2
  4. <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors.
  5. There are special exceptions to the terms and conditions of the GPLv2 as it is applied to
  6. this software, see the FOSS License Exception
  7. <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
  8. This program is free software; you can redistribute it and/or modify it under the terms
  9. of the GNU General Public License as published by the Free Software Foundation; version 2
  10. of the License.
  11. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  12. without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. See the GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License along with this
  15. program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth
  16. Floor, Boston, MA 02110-1301 USA
  17. */
  18. package testsuite.regression;
  19. import testsuite.BaseTestCase;
  20. /**
  21. * Tests SubQueries on MySQL > 4.1
  22. */
  23. public class SubqueriesRegressionTest extends BaseTestCase {
  24. private final static int REPETITIONS = 100;
  25. /**
  26. */
  27. public SubqueriesRegressionTest(String name) {
  28. super(name);
  29. }
  30. /*
  31. * (non-Javadoc)
  32. *
  33. * @see junit.framework.TestCase#setUp()
  34. */
  35. @Override
  36. public void setUp() throws Exception {
  37. super.setUp();
  38. createTables();
  39. }
  40. /*
  41. * (non-Javadoc)
  42. *
  43. * @see junit.framework.TestCase#tearDown()
  44. */
  45. @Override
  46. public void tearDown() throws Exception {
  47. super.tearDown();
  48. }
  49. /**
  50. * Runs all test cases in this test suite
  51. *
  52. * @param args
  53. */
  54. public static void main(String[] args) {
  55. junit.textui.TestRunner.run(SubqueriesRegressionTest.class);
  56. }
  57. public void testSubQuery1() throws Exception {
  58. if (versionMeetsMinimum(4, 1)) {
  59. for (int i = 0; i < REPETITIONS; i++) {
  60. this.rs = this.stmt.executeQuery(
  61. "select t3.colA from t3, t1 where t3.colA = 'bbbb' and t3.colB = t1.colA and exists (select 'X' from t2 where t2.colB = t1.colB)");
  62. assertTrue(this.rs.next());
  63. assertTrue("bbbb".equals(this.rs.getString(1)));
  64. assertTrue(!this.rs.next());
  65. }
  66. }
  67. }
  68. public void testSubQuery2() throws Exception {
  69. if (versionMeetsMinimum(4, 1)) {
  70. for (int i = 0; i < REPETITIONS; i++) {
  71. this.rs = this.stmt.executeQuery(
  72. "select t3.colA from t3, t1 where t3.colA = 'bbbb' and t3.colB = t1.colA and exists (select 'X' from t2 where t2.colB = 2)");
  73. assertTrue(this.rs.next());
  74. assertTrue("bbbb".equals(this.rs.getString(1)));
  75. assertTrue(!this.rs.next());
  76. }
  77. }
  78. }
  79. public void testSubQuery3() throws Exception {
  80. if (versionMeetsMinimum(4, 1)) {
  81. for (int i = 0; i < REPETITIONS; i++) {
  82. this.rs = this.stmt.executeQuery("select * from t1 where t1.colA = 'efgh' and exists (select 'X' from t2 where t2.colB = t1.colB)");
  83. assertTrue(this.rs.next());
  84. assertTrue("efgh".equals(this.rs.getString(1)));
  85. assertTrue("2".equals(this.rs.getString(2)));
  86. assertTrue(!this.rs.next());
  87. }
  88. }
  89. }
  90. public void testSubQuery4() throws Exception {
  91. // not really a subquery, but we want to have this in our testsuite
  92. if (versionMeetsMinimum(4, 1)) {
  93. for (int i = 0; i < REPETITIONS; i++) {
  94. this.rs = this.stmt.executeQuery("select colA, '' from t2 union select colA, colB from t3");
  95. assertTrue(this.rs.next());
  96. assertTrue("type1".equals(this.rs.getString(1)));
  97. assertTrue("".equals(this.rs.getString(2)));
  98. assertTrue(this.rs.next());
  99. assertTrue("type2".equals(this.rs.getString(1)));
  100. assertTrue("".equals(this.rs.getString(2)));
  101. assertTrue(this.rs.next());
  102. assertTrue("type3".equals(this.rs.getString(1)));
  103. assertTrue("".equals(this.rs.getString(2)));
  104. assertTrue(this.rs.next());
  105. assertTrue("aaaa".equals(this.rs.getString(1)));
  106. assertTrue("'" + this.rs.getString(2) + "' != expected of 'abcd'", "abcd".equals(this.rs.getString(2)));
  107. assertTrue(this.rs.next());
  108. assertTrue("bbbb".equals(this.rs.getString(1)));
  109. assertTrue("efgh".equals(this.rs.getString(2)));
  110. assertTrue(this.rs.next());
  111. assertTrue("cccc".equals(this.rs.getString(1)));
  112. assertTrue("'" + this.rs.getString(2) + "' != expected of 'ijkl'", "ijkl".equals(this.rs.getString(2)));
  113. assertTrue(!this.rs.next());
  114. }
  115. }
  116. }
  117. public void testSubQuery5() throws Exception {
  118. if (versionMeetsMinimum(4, 1)) {
  119. for (int i = 0; i < REPETITIONS; i++) {
  120. this.rs = this.stmt.executeQuery("select t1.colA from t1, t4 where t4.colA = t1.colA and exists (select 'X' from t2 where t2.colA = t4.colB)");
  121. assertTrue(this.rs.next());
  122. assertTrue("abcd".equals(this.rs.getString(1)));
  123. assertTrue(this.rs.next());
  124. assertTrue("efgh".equals(this.rs.getString(1)));
  125. assertTrue(this.rs.next());
  126. assertTrue("ijkl".equals(this.rs.getString(1)));
  127. assertTrue(!this.rs.next());
  128. }
  129. }
  130. }
  131. private void createTables() throws Exception {
  132. createTable("t1", "(colA varchar(10), colB decimal(3,0))");
  133. createTable("t2", "(colA varchar(10), colB varchar(10))");
  134. createTable("t3", "(colA varchar(10), colB varchar(10))");
  135. createTable("t4", "(colA varchar(10), colB varchar(10))");
  136. this.stmt.executeUpdate("insert into t1 values ('abcd', 1), ('efgh', 2), ('ijkl', 3)");
  137. this.stmt.executeUpdate("insert into t2 values ('type1', '1'), ('type2', '2'), ('type3', '3')");
  138. this.stmt.executeUpdate("insert into t3 values ('aaaa', 'abcd'), ('bbbb', 'efgh'), ('cccc', 'ijkl')");
  139. this.stmt.executeUpdate("insert into t4 values ('abcd', 'type1'), ('efgh', 'type2'), ('ijkl', 'type3')");
  140. }
  141. }