Example usage for java.sql SQLXML getBinaryStream

List of usage examples for java.sql SQLXML getBinaryStream

Introduction

In this page you can find the example usage for java.sql SQLXML getBinaryStream.

Prototype

InputStream getBinaryStream() throws SQLException;

Source Link

Document

Retrieves the XML value designated by this SQLXML instance as a stream.

Usage

From source file:org.jboss.bqt.client.results.xml.XMLCompareResults.java

/**
 * Compare actual results, identifiers and types with expected. <br>
 * <strong>Note </strong>: result list are expected to match element for
 * element.</br>//from w ww  .j  ava  2  s.c om
 * 
 * @param actualResults
 * @param actualDatatypes
 * @param actualIdentifiers
 * @param expectedResults
 * @param expectedDatatypes
 * @param expectedIdentifiers
 * @param eMsg
 * @throws QueryTestFailedException
 *             If comparison fails.
 */
private static void compareResultSets(final List actualResults, final List actualDatatypes,
        final List actualIdentifiers, final List expectedResults, final List expectedDatatypes,
        final List expectedIdentifiers, final String eMsg) throws QueryTestFailedException {
    // Compare column names and types
    compareIdentifiers(actualIdentifiers, expectedIdentifiers, actualDatatypes, expectedDatatypes);

    // Walk through records and compare actual against expected
    final int actualRowCount = actualResults.size();
    final int expectedRowCount = expectedResults.size();
    final int actualColumnCount = actualIdentifiers.size();

    // Check for less records than in expected results
    if (actualRowCount < expectedRowCount) {
        throw new QueryTestFailedException(eMsg + "Expected " + expectedRowCount + //$NON-NLS-1$
                " records but received only " + actualRowCount); //$NON-NLS-1$
    } else if (actualRowCount > expectedRowCount) {
        // Check also for more records than expected
        throw new QueryTestFailedException(eMsg + "Expected " + expectedRowCount + //$NON-NLS-1$
                " records but received " + actualRowCount); //$NON-NLS-1$
    }

    // DEBUG:
    // debugOut.println("================== Compariing Rows ===================");

    // Loop through rows
    for (int row = 0; row < actualRowCount; row++) {

        // Get actual record
        final List actualRecord = (List) actualResults.get(row);

        // Get expected record
        final List expectedRecord = (List) expectedResults.get(row);

        // DEBUG:
        // debugOut.println("Row: " + (row + 1));
        // debugOut.println(" expectedRecord: " + expectedRecord);
        // debugOut.println(" actualRecord: " + actualRecord);
        // Loop through columns
        // Compare actual elements with expected elements column by column
        // in this row
        for (int col = 0; col < actualColumnCount; col++) {
            // Get actual value
            Object actualValue = actualRecord.get(col);
            // Get expected value
            Object expectedValue = expectedRecord.get(col);

            // DEBUG:
            // debugOut.println(" Col: " +(col +1) + ": expectedValue:[" +
            // expectedValue + "] actualValue:[" + actualValue +
            // "]");

            // Compare these values
            if ((expectedValue == null && actualValue != null)
                    || (actualValue == null && expectedValue != null)) {
                // Compare nulls
                throw new QueryTestFailedException(eMsg + "Value mismatch at row " + (row + 1) //$NON-NLS-1$
                        + " and column " + (col + 1) //$NON-NLS-1$
                        + ": expected = [" //$NON-NLS-1$
                        + (expectedValue != null ? expectedValue : "null") + "], actual = [" //$NON-NLS-1$
                        + (actualValue != null ? actualValue : "null") + "]"); //$NON-NLS-1$

            }

            if (expectedValue == null && actualValue == null) {
                continue;
            }

            if (actualValue instanceof Blob || actualValue instanceof Clob || actualValue instanceof SQLXML) {

                if (actualValue instanceof Clob) {
                    Clob c = (Clob) actualValue;
                    try {
                        actualValue = ObjectConverterUtil.convertToString(c.getAsciiStream());

                    } catch (Throwable e) {
                        // TODO Auto-generated catch block
                        throw new QueryTestFailedException(e);
                    }
                } else if (actualValue instanceof Blob) {
                    Blob b = (Blob) actualValue;
                    try {
                        byte[] ba = ObjectConverterUtil.convertToByteArray(b.getBinaryStream());

                        actualValue = String.valueOf(ba.length);

                        // actualValue =
                        // ObjectConverterUtil.convertToString(b.getBinaryStream());

                    } catch (Throwable e) {
                        // TODO Auto-generated catch block
                        throw new QueryTestFailedException(e);
                    }
                } else if (actualValue instanceof SQLXML) {
                    SQLXML s = (SQLXML) actualValue;
                    try {
                        actualValue = ObjectConverterUtil.convertToString(s.getBinaryStream());

                    } catch (Throwable e) {
                        // TODO Auto-generated catch block
                        throw new QueryTestFailedException(e);
                    }
                }

                if (!(expectedValue instanceof String)) {
                    expectedValue = expectedValue.toString();
                }
            }

            // Compare values with equals
            if (!expectedValue.equals(actualValue)) {
                // DEBUG:

                if (expectedValue instanceof java.sql.Date) {
                    expectedValue = expectedValue.toString();
                    actualValue = actualValue.toString();

                } else if (expectedValue instanceof java.sql.Time) {
                    expectedValue = expectedValue.toString();
                    actualValue = actualValue.toString();

                }

                if (expectedValue instanceof String) {
                    final String expectedString = (String) expectedValue;

                    if (!(actualValue instanceof String)) {
                        throw new QueryTestFailedException(eMsg + "Value (types) mismatch at row " + (row + 1) //$NON-NLS-1$
                                + " and column " + (col + 1) //$NON-NLS-1$
                                + ": expected = [" //$NON-NLS-1$
                                + expectedValue + ", (String) ], actual = [" //$NON-NLS-1$
                                + actualValue + ", (" + actualValue.getClass().getName() + ") ]"); //$NON-NLS-1$
                    }

                    // Check for String difference
                    assertStringsMatch(expectedString, (String) actualValue, (row + 1), (col + 1), eMsg);

                } else {

                    throw new QueryTestFailedException(eMsg + "Value mismatch at row " + (row + 1) //$NON-NLS-1$
                            + " and column " + (col + 1) //$NON-NLS-1$
                            + ": expected = [" //$NON-NLS-1$
                            + expectedValue + "], actual = [" //$NON-NLS-1$
                            + actualValue + "]"); //$NON-NLS-1$

                }
            }

        } // end loop through columns
    } // end loop through rows
}

From source file:org.jboss.bqt.client.xml.XMLQueryVisitationStrategy.java

/**
 * Produce an XML message for an instance of the Object.
 * <br>/*from ww w  .j a  v  a 2s .c  o  m*/
 * @param object the instance for which the message is to be produced.
 * @param parent the XML element that is to be the parent of the produced XML message.
 * @return the root element of the XML segment that was produced.
 * @exception JDOMException if there is an error producing the message.
 * @throws SQLException 
 */
private Element produceObject(Object object, Element parent) throws JDOMException, SQLException {

    // ----------------------
    // Create the Object element ...
    // ----------------------
    Element objectElement = new Element(TagNames.Elements.OBJECT);

    String result = null;
    if (object instanceof Blob || object instanceof Clob || object instanceof SQLXML) {

        if (object instanceof Clob) {
            Clob c = (Clob) object;
            try {
                result = ObjectConverterUtil.convertToString(c.getAsciiStream());

            } catch (Throwable e) {
                // TODO Auto-generated catch block
                throw new SQLException(e);
            }
        } else if (object instanceof Blob) {
            Blob b = (Blob) object;
            try {
                byte[] ba = ObjectConverterUtil.convertToByteArray(b.getBinaryStream());

                result = String.valueOf(ba.length);

            } catch (Throwable e) {
                // TODO Auto-generated catch block
                throw new SQLException(e);
            }
        } else if (object instanceof SQLXML) {

            SQLXML s = (SQLXML) object;
            try {
                result = ObjectConverterUtil.convertToString(s.getBinaryStream());

            } catch (Throwable e) {
                // TODO Auto-generated catch block
                throw new SQLException(e);
            }
        }
    } else {
        result = object.toString();
    }

    objectElement.setText(result);

    if (parent != null) {
        objectElement = parent.addContent(objectElement);
    }

    return objectElement;
}