📄 testutils.java
字号:
/*
* OPIAM Suite
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package opiam.admin.faare.unittests;
import org.apache.log4j.Logger;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
/**
* This class contains the common methods used for the tests.
*
*/
public class TestUtils
{
/** Message prefix. */
private static final String PREFIX = " TEST - ";
/** Class message prefix. */
private static final String PREFIX_CLASS = " TEST - class : ";
/** Beginning message. */
private static final String BEGIN_TEST = " BEGIN CLASS TEST ";
/** End message. */
private static final String END_TEST = " END CLASS TEST ";
/** Beginning method message. */
private static final String BEGIN_METHOD = PREFIX + " method : ";
/** End method message. */
private static final String END_METHOD = PREFIX + " End ";
/** Success result message. */
private static final String RESULT_SUCCESS = " ** RESULT => Test OK ** ";
/** Failure result message. */
private static final String RESULT_FAILURE = " ** RESULT => Test NOT OK ** ";
/** Error result message. */
private static final String RESULT_ERROR = " ** RESULT => Unexpected exception ** ";
/** Indicator result message. */
private static final String RESULT_INDICATOR = " ** --------------------- ** ";
/** Indicator next message. */
public static final String NEXT = "\n";
/** Indicator empty message. */
public static final String EMPTY = "";
/** Limit size of the line. */
private static final int LINE_SIZE = 80;
/** Instance of logger. */
private static Logger _logger = Logger.getLogger(TestUtils.class);
/**
* Constructor that defines the logger.
* @param log Logger.
*/
public TestUtils(Logger log)
{
if (log != null)
{
_logger = log;
}
}
/**
* This method displays the test message.
*
* @param msg Message to display.
*/
private void display(String msg)
{
_logger.warn(centerLine(msg));
}
/**
* Formats the line to center the message.
*
* @param line String to format.
*
* @return Formatted line.
*/
private String centerLine(String line)
{
/* int rab = LINE_SIZE - line.length();
int frontSpace = rab / 2;
int backSpace = rab - frontSpace;
StringBuffer fLine = new StringBuffer();
for (int i = 0; i < frontSpace; i++)
{
fLine.append(' ');
}
fLine.append(line);
for (int i = 0; i < backSpace; i++)
{
fLine.append(' ');
}
return fLine.toString();
*/
return line;
}
/**
* Displays message with the test prefix.
*
* @param msg Message to display.
*/
public void displayMsg(String msg)
{
int ind = msg.indexOf(NEXT);
int indLeft = 0;
while (ind >= 0)
{
display(PREFIX + msg.substring(indLeft, ind).trim());
indLeft = ind + NEXT.length();
ind = msg.indexOf(NEXT, indLeft);
}
display(PREFIX + msg.substring(indLeft));
}
/**
* Displays a success message.
*
* @param msg Description to display with the message.
*/
public void displayResultSuccess(String msg)
{
if (msg != null)
{
displayMsg(RESULT_INDICATOR);
displayMsg(RESULT_SUCCESS);
displayMsg(msg);
displayMsg(RESULT_INDICATOR);
}
else
{
displayMsg(RESULT_SUCCESS);
}
TestResult.getInstance().addSuccess(msg);
}
/**
* Displays a failure message.
*
* @param msg Description to display with the message.
*/
public void displayResultFailure(String msg)
{
if (msg != null)
{
displayMsg(RESULT_INDICATOR);
displayMsg(RESULT_FAILURE);
displayMsg(msg);
displayMsg(RESULT_INDICATOR);
}
else
{
displayMsg(RESULT_FAILURE);
}
TestResult.getInstance().addFailure(msg);
}
/**
* Displays an error message when an unexpected exception occurs.
*
* @param msg Description to display with the message.
*/
public void displayResultError(String msg)
{
if (msg != null)
{
displayMsg(RESULT_INDICATOR);
displayMsg(RESULT_ERROR);
displayMsg(msg);
displayMsg(RESULT_INDICATOR);
}
else
{
displayMsg(RESULT_ERROR);
}
TestResult.getInstance().addErrors(msg);
}
/**
* Displays the header for the class test.
*
* @param header Message to display.
*/
public void displayHeaderClass(String header)
{
display("");
display(" <<<<<<<<<<<<<<< " + BEGIN_TEST + " >>>>>>>>>>>>>>> ");
if (header != null)
{
display(PREFIX_CLASS + header);
}
display("");
TestResult.getInstance().beginTestClass(header);
}
/**
* Displays the footer for the class test.
*
* @param footer Message to display.
*/
public void displayFooterClass(String footer)
{
if (footer != null)
{
display(PREFIX + footer);
}
display("");
display(" >>>>>>>>>>>>>>> " + END_TEST + " <<<<<<<<<<<<<<< ");
display("");
TestResult.getInstance().endTestClass();
}
/**
* Displays the header for the method test.
*
* @param header Message to display.
*/
public void displayHeaderMethod(String header)
{
display("");
display(" ========== " + BEGIN_METHOD + header + " ========== ");
display("");
TestResult.getInstance().beginTestMethod(header);
}
/**
* Displays the footer for the method test.
*
* @param footer Message to display.
*/
public void displayFooterMethod(String footer)
{
display("");
display(" ========== " + END_METHOD + footer + " ========== ");
display("");
TestResult.getInstance().endTestMethod();
}
/**
* Compares two values and displays the result.
*
* @param refValue The reference value.
* @param cmpValue The comparaison value.
*
* @return true if equal, false if not equal
*/
public boolean compareValues(String refValue, String cmpValue)
{
displayMsg("Reference value : " + refValue);
displayMsg("Comparaison value : " + cmpValue);
if (refValue.equals(cmpValue))
{
displayResultSuccess(null);
return true;
}
else
{
displayResultFailure(null);
return false;
}
}
/**
* Displays the tests results for the methods of a given class.
*
* @param tClass The given class.
*/
private void displayMethodsStatement(TestClass tClass)
{
display(" --------------------------------------- ");
display(" ----- METHODS : SUCCESSFULLY TESTS ---- ");
display(" --------------------------------------- ");
display(NEXT);
ArrayList methodsList = tClass.getMethodsSuccess();
for (int j = 0; j < methodsList.size(); j++)
{
TestMethod tMethod = (TestMethod) methodsList.get(j);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -