我正在开发一个大型遗留 Java 应用程序。它已经有一个广泛的现有框架来处理设备驱动程序。我需要为通过 JavaComm 连接到串行端口的设备创建一个新的设备驱动程序。
现有驱动程序只需在其configure()
方法中创建新的串行端口,然后从串行端口对象创建新的输入和输出流,然后将这些输入和输出流用于设备通信,但没有单元测试。
但是,我希望我的新类是可单元测试的,但不确定如果它要适合这个现有的框架,该框架将期望在configure()
方法中设置串行端口、输入和输出流,我该怎么做。
有任何想法吗?
public void configure()
{
super.configure();
if (isEmulated()) {
return;
}
if (isFaulty()) {
if (isOutOfService()) {
ZBopNotificationManager.getInstance().add(
new SystemNotice(SeverityCode.LEVEL3, getName(), getErrorMessage()));
}
return;
}
// ZP:
String portName = getSerialPort();
// String portName = "COM1";
try {
CommPortIdentifier id = CommPortIdentifier.getPortIdentifier(portName);
Trace.log(this, Trace.D, "Port name = " + id.getName());
port = (SerialPort) id.open(getName(), 1000);
} catch (NoSuchPortException nspe) {
report(SeverityCode.LEVEL3, getName(), "Bar Code Scanner is not connected to " + portName + " port, or the port does not exist.");
return;
} catch (PortInUseException piue) {
report(SeverityCode.LEVEL3, getName(), portName + " port is already in-use by some other device. Reason: " + piue.getMessage());
return;
}
try {
port.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
// this should not happen
port.close();
report(SeverityCode.LEVEL2, getName(), portName + " port configuration failed: " + e);
return;
}
try {
in = port.getInputStream();
out = port.getOutputStream();
} catch (IOException ioe) {
port.close();
report(SeverityCode.LEVEL3, getName(), portName + " port configuration failed: " + ioe.getMessage());
return;
}
meout(30);
// ... other init code omitted
}