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

📄 testutil.java

📁 hsqldb是100%java实现的数据库,是一个开放源代码的JAVA数据库 l 具有标准的SQL语法和JAVA接口 l HSQLDB可以自由使用和分发 l 非常简洁和快速的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        try {            aStatement.execute(getSql());        } catch (Exception x) {            message = x.getMessage();            return false;        }        return true;    }    /**     * Checks that the type code letter is valid     * @param aCode type code to validate.     * @return true if the type code is valid, otherwise false.     */    protected static boolean isValidCode(char aCode) {        /* Allowed values for test codes are:         * (note that UPPERCASE codes, while valid are only processed if the         * system property IgnoreCodeCase has been set to true)         *         * 'u' ('U') - update         * 'c' ('C') - count         * 'e' ('E') - exception         * 'r' ('R') - results         * 's' ('S') - silent         * 'd'       - display   (No reason to use upper-case).         * ' ' - not a test         */        char testChar = Character.toLowerCase(aCode);        switch (testChar) {            case ' ' :            case 'r' :            case 'e' :            case 'c' :            case 'u' :            case 's' :            case 'd' :                return true;        }        return false;    }}/** Represents a ParsedSection for a ResultSet test */class ResultSetParsedSection extends ParsedSection {    private String delim = System.getProperty("TestUtilFieldDelimiter", ",");    private String[] expectedRows = null;    /**     * constructs a new instance of ResultSetParsedSection, interpreting     * the supplied results as one or more lines of delimited field values     * @param lines String[]     */    protected ResultSetParsedSection(String[] lines) {        super(lines);        type = 'r';        //now we'll populate the expectedResults array        expectedRows = new String[(resEndRow + 1)];        for (int i = 0; i <= resEndRow; i++) {            int skip = StringUtil.skipSpaces(lines[i], 0);            expectedRows[i] = lines[i].substring(skip);        }    }    protected String getResultString() {        StringBuffer printVal = new StringBuffer();        for (int i = 0; i < getExpectedRows().length; i++) {            printVal.append(getExpectedRows()[i]).append("\n");        }        return printVal.toString();    }    protected boolean test(Statement aStatement) {        try {            try {                //execute the SQL                aStatement.execute(getSql());            } catch (SQLException s) {                throw new Exception(                    "Expected a ResultSet, but got the error: "                    + s.getMessage());            }            //check that update count != -1            if (aStatement.getUpdateCount() != -1) {                throw new Exception(                    "Expected a ResultSet, but got an update count of "                    + aStatement.getUpdateCount());            }            //iterate over the ResultSet            ResultSet results = aStatement.getResultSet();            int       count   = 0;            while (results.next()) {                if (count < getExpectedRows().length) {//                    String[] expectedFields = getExpectedRows()[count].split(delim);                    String[] expectedFields =                        StringUtil.split(getExpectedRows()[count], delim);                    //check that we have the number of columns expected...                    if (results.getMetaData().getColumnCount()                            == expectedFields.length) {                        //...and if so, check that the column values are as expected...                        int j = 0;                        for (int i = 0; i < expectedFields.length; i++) {                            j = i + 1;                            String actual = results.getString(j);                            //...including null values...                            if (actual == null) {    //..then we have a null                                //...check to see if we were expecting it...                                if (!expectedFields[i].equalsIgnoreCase(                                        "NULL")) {                                    throw new Exception(                                        "Expected row " + count                                        + " of the ResultSet to contain:\n"                                        + getExpectedRows()[count]                                        + "\nbut field " + j                                        + " contained NULL");                                }                            } else if (!actual.equals(expectedFields[i])) {                                //then the results are different                                throw new Exception(                                    "Expected row " + (count + 1)                                    + " of the ResultSet to contain:\n"                                    + getExpectedRows()[count]                                    + "\nbut field " + j + " contained "                                    + results.getString(j));                            }                        }                    } else {                        //we have the wrong number of columns                        throw new Exception(                            "Expected the ResultSet to contain "                            + expectedFields.length                            + " fields, but it contained "                            + results.getMetaData().getColumnCount()                            + " fields.");                    }                }                count++;            }            //check that we got as many rows as expected            if (count != getExpectedRows().length) {                //we don't have the expected number of rows                throw new Exception("Expected the ResultSet to contain "                                    + getExpectedRows().length                                    + " rows, but it contained " + count                                    + " rows.");            }        } catch (Exception x) {            message = x.getMessage();            return false;        }        return true;    }    private String[] getExpectedRows() {        return expectedRows;    }}/** Represents a ParsedSection for an update test */class UpdateParsedSection extends ParsedSection {    //expected update count    int countWeWant;    protected UpdateParsedSection(String[] lines) {        super(lines);        type        = 'u';        countWeWant = Integer.parseInt(lines[0]);    }    protected String getResultString() {        return Integer.toString(getCountWeWant());    }    private int getCountWeWant() {        return countWeWant;    }    protected boolean test(Statement aStatement) {        try {            try {                //execute the SQL                aStatement.execute(getSql());            } catch (SQLException s) {                throw new Exception("Expected an update count of "                                    + getCountWeWant()                                    + ", but got the error: "                                    + s.getMessage());            }            if (aStatement.getUpdateCount() != getCountWeWant()) {                throw new Exception("Expected an update count of "                                    + getCountWeWant()                                    + ", but got an update count of "                                    + aStatement.getUpdateCount() + ".");            }        } catch (Exception x) {            message = x.getMessage();            return false;        }        return true;    }}/** Represents a ParsedSection for silent execution */class SilentParsedSection extends ParsedSection {    protected SilentParsedSection(String[] lines) {        super(lines);        type = 's';    }    protected String getResultString() {        return null;    }    protected boolean test(Statement aStatement) {        try {            aStatement.execute(getSql());        } catch (Exception x) {}        return true;    }}/** Represents a ParsedSection for a count test */class CountParsedSection extends ParsedSection {    //expected row count    private int countWeWant;    protected CountParsedSection(String[] lines) {        super(lines);        type        = 'c';        countWeWant = Integer.parseInt(lines[0]);    }    protected String getResultString() {        return Integer.toString(getCountWeWant());    }    private int getCountWeWant() {        return countWeWant;    }    protected boolean test(Statement aStatement) {        try {            //execute the SQL            try {                aStatement.execute(getSql());            } catch (SQLException s) {                throw new Exception("Expected a ResultSet containing "                                    + getCountWeWant()                                    + " rows, but got the error: "                                    + s.getMessage());            }            //check that update count != -1            if (aStatement.getUpdateCount() != -1) {                throw new Exception(                    "Expected a ResultSet, but got an update count of "                    + aStatement.getUpdateCount());            }            //iterate over the ResultSet            ResultSet results = aStatement.getResultSet();            int       count   = 0;            while (results.next()) {                count++;            }            //check that we got as many rows as expected            if (count != getCountWeWant()) {                //we don't have the expected number of rows                throw new Exception("Expected the ResultSet to contain "                                    + getCountWeWant()                                    + " rows, but it contained " + count                                    + " rows.");            }        } catch (Exception x) {            message = x.getMessage();            return false;        }        return true;    }}/** Represents a ParsedSection for an Exception test */class ExceptionParsedSection extends ParsedSection {    protected ExceptionParsedSection(String[] lines) {        super(lines);        type = 'e';    }    protected String getResultString() {        return "SQLException";    }    protected boolean test(Statement aStatement) {        try {            aStatement.execute(getSql());        } catch (SQLException sqlX) {            return true;        } catch (Exception x) {            message = x.getMessage();            return false;        }        return false;    }}/** Represents a ParsedSection for a section with blank type */class BlankParsedSection extends ParsedSection {    protected BlankParsedSection(String[] lines) {        super(lines);        type = ' ';    }    protected String getResultString() {        return "No result specified for this section";    }}/** Represents a ParsedSection that is to be ignored */class IgnoreParsedSection extends ParsedSection {    protected IgnoreParsedSection(String[] inLines, char aType) {        /* Extremely ambiguous to use input parameter of same exact         * variable name as the superclass member "lines".         * Therefore, renaming to inLines. */        // Inefficient to parse this into SQL when we aren't going to use        // it as SQL.  Should probably just be removed to use the         // super() constructor.        super(inLines);        type = aType;    }    protected String getResultString() {        return "This section, of type '" + getType() + "' was ignored";    }}/** Represents a Section to be Displayed, not executed */class DisplaySection extends ParsedSection {    protected DisplaySection(String[] inLines) {        /* Can't user the super constructor, since it does funny things when         * constructing the SQL Buffer, which we don't need. */        lines = inLines;        int firstSlash = lines[0].indexOf('/');        lines[0] = lines[0].substring(firstSlash + 1);    }    protected String getResultString() {        StringBuffer sb = new StringBuffer();        for (int i = 0; i < lines.length; i++) {            if (i > 0) {                sb.append('\n');            }            sb.append("+ " + lines[i]);        }        return sb.toString();    }}

⌨️ 快捷键说明

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