diff --git a/src/main/java/com/target/devicemanager/components/printer/PrinterDevice.java b/src/main/java/com/target/devicemanager/components/printer/PrinterDevice.java index e8f23e3..398fb26 100644 --- a/src/main/java/com/target/devicemanager/components/printer/PrinterDevice.java +++ b/src/main/java/com/target/devicemanager/components/printer/PrinterDevice.java @@ -165,14 +165,14 @@ private void enable() throws JposException { public Void printContent(List contents, int printerStation) throws JposException, PrinterException { LOGGER.debug("printContent()"); if(tryLock()) { - if (contents == null || contents.isEmpty()) { - LOGGER.debug("Receipt contents are empty"); - return null; - } - enable(); POSPrinter printer; synchronized (printer = dynamicPrinter.getDevice()) { try { + if (contents == null || contents.isEmpty()) { + LOGGER.debug("Receipt contents are empty"); + throw new PrinterException(PrinterError.INVALID_FORMAT); + } + enable(); if (printerStation != PrinterStationType.CHECK_PRINTER.getValue() && (wasPaperEmpty || paperEmptyCheck())) { // Throw JPOS extended error JPOS_EPTR_REC_EMPTY throw new JposException(114, 203); diff --git a/src/test/java/com/target/devicemanager/components/printer/PrinterDeviceTest.java b/src/test/java/com/target/devicemanager/components/printer/PrinterDeviceTest.java index cbd8bf0..2dde931 100644 --- a/src/test/java/com/target/devicemanager/components/printer/PrinterDeviceTest.java +++ b/src/test/java/com/target/devicemanager/components/printer/PrinterDeviceTest.java @@ -357,12 +357,19 @@ public void printContent_WhenContentsNull() throws JposException, PrinterExcepti //arrange //act - printerDevice.printContent(null, 0); - + try { + printerDevice.printContent(null, 0); + } //assert - verify(mockDynamicPrinter, never()).getDevice(); - verify(mockPrinter, never()).transactionPrint(anyInt(), anyInt()); - verify(mockPrinter, never()).clearOutput(); + catch (PrinterException printerException) { + assert(printerException.getDeviceError().equals(PrinterError.INVALID_FORMAT)); + verify(mockDynamicPrinter, times(1)).getDevice(); + verify(mockPrinter, never()).transactionPrint(anyInt(), anyInt()); + verify(mockPrinter, times(1)).clearOutput(); + return; + } catch (JposException jposException) { + fail("Expected PrinterException, got JposException"); + } } @Test @@ -371,12 +378,19 @@ public void printContent_WhenContentsEmpty() throws JposException, PrinterExcept List contents = new ArrayList<>(); //act - printerDevice.printContent(contents, 0); - + try { + printerDevice.printContent(contents, 0); + } //assert - verify(mockDynamicPrinter, never()).getDevice(); - verify(mockPrinter, never()).transactionPrint(anyInt(), anyInt()); - verify(mockPrinter, never()).clearOutput(); + catch (PrinterException printerException) { + assert(printerException.getDeviceError().equals(PrinterError.INVALID_FORMAT)); + verify(mockDynamicPrinter, times(1)).getDevice(); + verify(mockPrinter, never()).transactionPrint(anyInt(), anyInt()); + verify(mockPrinter, times(1)).clearOutput(); + return; + } catch (JposException jposException) { + fail("Expected PrinterException, got JposException"); + } } @Test @@ -399,9 +413,10 @@ public String toString() { //assert catch (JposException jposException) { - verify(mockDynamicPrinter, never()).getDevice(); + assert(jposException.getErrorCode() == JposConst.JPOS_E_OFFLINE); + verify(mockDynamicPrinter, times(1)).getDevice(); verify(mockPrinter, never()).transactionPrint(anyInt(), anyInt()); - verify(mockPrinter, never()).clearOutput(); + verify(mockPrinter, times(1)).clearOutput(); return; } catch (PrinterException printerException) { fail("Expected JposException, got PrinterException");