001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.pluto.testsuite;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021:
022: /**
023: * Static class that provides utility methods for Pluto testsuite.
024: * @since 2006-02-13
025: */
026: public class TestUtils {
027:
028: /** Logger. */
029: private static final Log LOG = LogFactory.getLog(TestUtils.class);
030:
031: // Private Constructor -----------------------------------------------------
032:
033: /**
034: * Private method that prevents external instantiation.
035: */
036: private TestUtils() {
037: // Do nothing.
038: }
039:
040: // Public Static Methods ---------------------------------------------------
041:
042: /**
043: * Sets the test result return code to <code>FAILED</code>, and reports
044: * assertion details by specifying values got and values expected.
045: * @param valueName the name of the values to assert.
046: * @param valueGot the values got.
047: * @param valueExpected the values expected.
048: * @param result the test result.
049: */
050: public static void failOnAssertion(String valuesName,
051: String[] valuesGot, String[] valuesExpected,
052: TestResult result) {
053: failOnAssertion(valuesName, arrayToString(valuesGot),
054: arrayToString(valuesExpected), result);
055: }
056:
057: /**
058: * Sets the test result return code to <code>FAILED</code>, and reports
059: * assertion details by specifying value got and value expected.
060: * @param valueName the name of the value to assert.
061: * @param valueGot the value object got.
062: * @param valueExpected the value object expected.
063: * @param result the test result.
064: */
065: public static void failOnAssertion(String valueName,
066: Object valueGot, Object valueExpected, TestResult result) {
067: StringBuffer buffer = new StringBuffer();
068: buffer.append("Assertion failed: ");
069: buffer.append("got ").append(valueName).append(": ").append(
070: valueGot);
071: buffer.append(", expected: ").append(valueExpected);
072: result.setReturnCode(TestResult.FAILED);
073: result.setResultMessage(buffer.toString());
074: }
075:
076: /**
077: * Sets the test result return code to <code>FAILED</code>, and reports
078: * exception details.
079: * @param message the error message.
080: * @param cause the cause exception.
081: * @param result the test result.
082: */
083: public static void failOnException(String message, Throwable cause,
084: TestResult result) {
085: // Construct error message.
086: StringBuffer buffer = new StringBuffer();
087: if (message != null) {
088: buffer.append(message);
089: } else {
090: buffer.append("Exception occurred.");
091: }
092: buffer.append(" Cause (").append(cause.getClass().getName())
093: .append("): ");
094: buffer.append(cause.getMessage());
095:
096: // Log error message.
097: if (LOG.isErrorEnabled()) {
098: LOG.error(buffer.toString(), cause);
099: }
100:
101: // Set error message to test result.
102: result.setReturnCode(TestResult.FAILED);
103: result.setResultMessage(buffer.toString());
104: }
105:
106: // Private Static Methods --------------------------------------------------
107:
108: /**
109: * Converts a string array to a string.
110: * @param values the string array to convert.
111: * @return a string representing the values in the string array.
112: */
113: private static String arrayToString(String[] values) {
114: StringBuffer buffer = new StringBuffer();
115: if (values == null) {
116: buffer.append("null");
117: } else {
118: buffer.append("{");
119: for (int i = 0; i < values.length; i++) {
120: buffer.append(values[i]);
121: if (i < values.length - 1) {
122: buffer.append(",");
123: }
124: }
125: buffer.append("}");
126: }
127: return buffer.toString();
128: }
129:
130: }
|