001: package test.org.mandarax.jdbc;
002:
003: /*
004: * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: import java.sql.*;
022: import java.util.*;
023:
024: import org.mandarax.jdbc.server.PseudoColumns;
025:
026: import test.org.mandarax.testsupport.TestUtils;
027:
028: /**
029: * Test case based on the memory kb from the test.org.mandarax.util.resultsetfilters package.
030: * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
031: * @version 3.3.2 <29 December 2004>
032: * @since 3.0
033: */
034:
035: public class QueryTestCase5 extends AbstractQueryTestCase implements
036: Constants {
037:
038: private String name;
039: private Object[][] expectedResults;
040: private List expectedResultsAsStringList = null;
041: private boolean ignoreOrderOfResults = false;
042:
043: /**
044: * Constructor.
045: * @param name the name of this test case
046: * @param orderByConditions the order by conditions
047: * @param expected the expected results
048: */
049: public QueryTestCase5(String sql, Object[][] expected) {
050: this (sql, expected, false);
051: }
052:
053: /**
054: * Constructor.
055: * @param name the name of this test case
056: * @param orderByConditions the order by conditions
057: * @param expected the expected results
058: * @param ignoreOrderOfResults if true, the order of the results is ignored
059: */
060: public QueryTestCase5(String sql, Object[][] expected,
061: boolean ignoreOrderOfResults) {
062: super ("jdbc:mandarax:ref:"
063: + test.org.mandarax.jdbc.TestKnowledgeBaseRef.class
064: .getName(), sql);
065: this .sql = sql;
066: this .expectedResults = expected;
067: this .ignoreOrderOfResults = ignoreOrderOfResults;
068: }
069:
070: /**
071: * Set up the test case.
072: */
073: public void setUp() throws Exception {
074: super .setUp();
075: // convert array to sorted list in order to facilitate comparing
076: // and debugging
077: expectedResultsAsStringList = (List) getPrefContainer();
078: for (int i = 0; i < expectedResults.length; i++) {
079: expectedResultsAsStringList.add(TestUtils.record2string(
080: expectedResults[i], df));
081: }
082: }
083:
084: /**
085: * Run the test case.
086: */
087: public void test() throws Exception {
088:
089: Connection connection = java.sql.DriverManager
090: .getConnection(url);
091: Statement stmnt = connection.createStatement();
092: java.sql.ResultSet rs = stmnt.executeQuery(sql);
093: List computedResultsAsStringList = (List) TestUtils.asStrings(
094: rs, getPrefContainer(), df,
095: PseudoColumns.PSEUDO_COLUMN_PATTERN_DEF);
096:
097: if (expectedResultsAsStringList.size() != computedResultsAsStringList
098: .size())
099: assertTrue(false);
100:
101: // normalize order if necessary
102: if (this .ignoreOrderOfResults) {
103: Collections.sort(expectedResultsAsStringList);
104: Collections.sort(computedResultsAsStringList);
105: }
106:
107: Iterator exIter = expectedResultsAsStringList.iterator();
108: Iterator compIter = computedResultsAsStringList.iterator();
109:
110: while (exIter.hasNext() && compIter.hasNext()) {
111: String nextExpected = exIter.next().toString();
112: String nextComputed = compIter.next().toString();
113: if (!nextExpected.equals(nextComputed)) {
114: assertTrue(false);
115: }
116: }
117: connection.close();
118: assertTrue(true);
119: }
120:
121: /**
122: * Get the container used to organise results.
123: * The container selected will determine whether the order of the results is retained etc.
124: */
125: protected List getPrefContainer() {
126: return new ArrayList();
127: }
128:
129: /**
130: * Convert this object to a string.
131: * @return the string representation of this object
132: */
133: public String toString() {
134: return "Testing results returned by SQL statement: " + sql;
135: }
136:
137: }
|