⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testbase.java

📁 非常棒的java数据库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        }
        if (!a.equals(b)) {
            for (int i = 0; i < a.length(); i++) {
                String s = a.substring(0, i);
                if (!b.startsWith(s)) {
                    a = a.substring(0, i) + "<*>" + a.substring(i);
                    break;
                }
            }
            error("string a: " + a + " (" + a.length() + ") b: " + b + " (" + b.length() + ")");
        }
    }

    protected void checkFalse(String a, String b) throws Exception {
        if (a.equals(b)) {
            error("string false a: " + a + " b: " + b);
        }
    }

    protected void check(long a, long b) throws Exception {
        if (a != b) {
            error("long a: " + a + " b: " + b);
        }
    }

    protected void checkSmaller(long a, long b) throws Exception {
        if (a >= b) {
            error("a: " + a + " is not smaller than b: " + b);
        }
    }

    protected void checkContains(String result, String contains) throws Exception {
        if (result.indexOf(contains) < 0) {
            error(result + " does not contain: " + contains);
        }
    }

    protected void check(double a, double b) throws Exception {
        if (a != b) {
            error("double a: " + a + " b: " + b);
        }
    }

    protected void check(float a, float b) throws Exception {
        if (a != b) {
            error("float a: " + a + " b: " + b);
        }
    }

    protected void check(boolean a, boolean b) throws Exception {
        if (a != b) {
            error("boolean a: " + a + " b: " + b);
        }
    }

    protected void check(boolean value) throws Exception {
        if (!value) {
            error("expected: true got: false");
        }
    }

    protected void checkFalse(boolean value) throws Exception {
        if (value) {
            error("expected: false got: true");
        }
    }

    protected void checkResultRowCount(ResultSet rs, int expected) throws Exception {
        int i = 0;
        while (rs.next()) {
            i++;
        }
        check(i, expected);
    }

    protected void checkSingleValue(Statement stat, String sql, int value) throws Exception {
        ResultSet rs = stat.executeQuery(sql);
        check(rs.next());
        check(rs.getInt(1), value);
        checkFalse(rs.next());
    }

    protected void testResultSetMeta(ResultSet rs, int columnCount, String[] labels, int[] datatypes, int[] precision,
            int[] scale) throws Exception {
        ResultSetMetaData meta = rs.getMetaData();
        int cc = meta.getColumnCount();
        if (cc != columnCount) {
            error("result set contains " + cc + " columns not " + columnCount);
        }
        for (int i = 0; i < columnCount; i++) {
            if (labels != null) {
                String l = meta.getColumnLabel(i + 1);
                if (!labels[i].equals(l)) {
                    error("column label " + i + " is " + l + " not " + labels[i]);
                }
            }
            if (datatypes != null) {
                int t = meta.getColumnType(i + 1);
                if (datatypes[i] != t) {
                    error("column datatype " + i + " is " + t + " not " + datatypes[i] + " (prec="
                            + meta.getPrecision(i + 1) + " scale=" + meta.getScale(i + 1) + ")");
                }
                String typeName = meta.getColumnTypeName(i + 1);
                String className = meta.getColumnClassName(i + 1);
                switch (t) {
                case Types.INTEGER:
                    check(typeName, "INTEGER");
                    check(className, "java.lang.Integer");
                    break;
                case Types.VARCHAR:
                    check(typeName, "VARCHAR");
                    check(className, "java.lang.String");
                    break;
                case Types.SMALLINT:
                    check(typeName, "SMALLINT");
                    check(className, "java.lang.Short");
                    break;
                case Types.TIMESTAMP:
                    check(typeName, "TIMESTAMP");
                    check(className, "java.sql.Timestamp");
                    break;
                case Types.DECIMAL:
                    check(typeName, "DECIMAL");
                    check(className, "java.math.BigDecimal");
                    break;
                default:
                }
            }
            if (precision != null) {
                int p = meta.getPrecision(i + 1);
                if (precision[i] != p) {
                    error("column precision " + i + " is " + p + " not " + precision[i]);
                }
            }
            if (scale != null) {
                int s = meta.getScale(i + 1);
                if (scale[i] != s) {
                    error("column scale " + i + " is " + s + " not " + scale[i]);
                }
            }

        }
    }

    protected void testResultSetOrdered(ResultSet rs, String[][] data) throws Exception {
        testResultSet(true, rs, data);
    }

    void testResultSetUnordered(ResultSet rs, String[][] data) throws Exception {
        testResultSet(false, rs, data);
    }

    void testResultSet(boolean ordered, ResultSet rs, String[][] data) throws Exception {
        int len = rs.getMetaData().getColumnCount();
        int rows = data.length;
        if (rows == 0) {
            // special case: no rows
            if (rs.next()) {
                error("testResultSet expected rowCount:" + rows + " got:0");
            }
        }
        int len2 = data[0].length;
        if (len < len2) {
            error("testResultSet expected columnCount:" + len2 + " got:" + len);
        }
        for (int i = 0; i < rows; i++) {
            if (!rs.next()) {
                error("testResultSet expected rowCount:" + rows + " got:" + i);
            }
            String[] row = getData(rs, len);
            if (ordered) {
                String[] good = data[i];
                if (!testRow(good, row, good.length)) {
                    error("testResultSet row not equal, got:\n" + formatRow(row) + "\n" + formatRow(good));
                }
            } else {
                boolean found = false;
                for (int j = 0; j < rows; j++) {
                    String[] good = data[i];
                    if (testRow(good, row, good.length)) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    error("testResultSet no match for row:" + formatRow(row));
                }
            }
        }
        if (rs.next()) {
            String[] row = getData(rs, len);
            error("testResultSet expected rowcount:" + rows + " got:>=" + (rows + 1) + " data:" + formatRow(row));
        }
    }

    boolean testRow(String[] a, String[] b, int len) {
        for (int i = 0; i < len; i++) {
            String sa = a[i];
            String sb = b[i];
            if (sa == null || sb == null) {
                if (sa != sb) {
                    return false;
                }
            } else {
                if (!sa.equals(sb)) {
                    return false;
                }
            }
        }
        return true;
    }

    String[] getData(ResultSet rs, int len) throws SQLException {
        String[] data = new String[len];
        for (int i = 0; i < len; i++) {
            data[i] = rs.getString(i + 1);
            // just check if it works
            rs.getObject(i + 1);
        }
        return data;
    }

    String formatRow(String[] row) {
        String sb = "";
        for (int i = 0; i < row.length; i++) {
            sb += "{" + row[i] + "}";
        }
        return "{" + sb + "}";
    }

    protected void crash(Connection conn) throws Exception {
        ((JdbcConnection) conn).setPowerOffCount(1);
        try {
            conn.createStatement().execute("SET WRITE_DELAY 0");
            conn.createStatement().execute("CREATE TABLE TEST_A(ID INT)");
            error("should be crashed already");
        } catch (SQLException e) {
            // expected
        }
        try {
            conn.close();
        } catch (SQLException e) {
            // ignore
        }
    }

    protected String readString(Reader reader) throws Exception {
        if (reader == null) {
            return null;
        }
        StringBuffer buffer = new StringBuffer();
        try {
            while (true) {
                int c = reader.read();
                if (c == -1) {
                    break;
                }
                buffer.append((char) c);
            }
            return buffer.toString();
        } catch (Exception e) {
            check(false);
            return null;
        }
    }

    protected void checkNotGeneralException(SQLException e) throws Exception {
        if (e != null && e.getSQLState().startsWith("HY000")) {
            TestBase.logError("Unexpected General error", e);
        }
    }

    protected void check(Integer a, Integer b) throws Exception {
        if (a == null || b == null) {
            check(a == b);
        } else {
            check(a.intValue(), b.intValue());
        }
    }

    protected void compareDatabases(Statement stat1, Statement stat2) throws Exception {
        ResultSet rs1 = stat1.executeQuery("SCRIPT NOPASSWORDS");
        ResultSet rs2 = stat2.executeQuery("SCRIPT NOPASSWORDS");
        ArrayList list1 = new ArrayList();
        ArrayList list2 = new ArrayList();
        while (rs1.next()) {
            check(rs2.next());
            String s1 = rs1.getString(1);
            list1.add(s1);
            String s2 = rs2.getString(1);
            list2.add(s2);
        }
        for (int i = 0; i < list1.size(); i++) {
            String s = (String) list1.get(i);
            if (!list2.remove(s)) {
                error("not found: " + s);
            }
        }
        check(list2.size(), 0);
        checkFalse(rs2.next());
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -