Skip to content

Commit

Permalink
Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbl committed Jun 11, 2022
1 parent 6568ffe commit ac9b18e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private static String formatAmountForCode(BigDecimal amount) {
}

// According to a letter from SIX dated August 5, 2020 only the major number (leading "02") should be checked
private static final Pattern VALID_VERSION = Pattern.compile("^02[0-9][0-9]$");
private static final Pattern VALID_VERSION = Pattern.compile("^02\\d\\d$");

/**
* Decodes the specified text and returns the bill data.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,30 +181,15 @@ static void normalizeDecodedBill(Bill bill) {
bill.getFormat().setLanguage(Language.DE); // fix language (not contained in text)
}

@Test
void decodeInvalidFormat1() {
QRBillValidationError err = assertThrows(QRBillValidationError.class, () -> QRBill.decodeQrCodeText("garbage"));
assertSingleError(err.getValidationResult(), ValidationConstants.KEY_DATA_STRUCTURE_INVALID, ValidationConstants.FIELD_QR_TYPE);
}

@Test
void decodeInvalidFormat2a() {
QRBillValidationError err = assertThrows(QRBillValidationError.class,
() -> QRBill.decodeQrCodeText("SPC\r\n0100\r\n\r\n\r\n"));
assertSingleError(err.getValidationResult(), ValidationConstants.KEY_DATA_STRUCTURE_INVALID, ValidationConstants.FIELD_QR_TYPE);
}

@Test
void decodeInvalidFormat2b() {
QRBillValidationError err = assertThrows(QRBillValidationError.class, () -> QRBill.decodeQrCodeText(
"SPC1\r\n0200\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"));
assertSingleError(err.getValidationResult(), ValidationConstants.KEY_DATA_STRUCTURE_INVALID, ValidationConstants.FIELD_QR_TYPE);
}

@Test
void decodeInvalidFormat3() {
QRBillValidationError err = assertThrows(QRBillValidationError.class, () -> QRBill.decodeQrCodeText(
"SPC1\r\n0200\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"));
@ParameterizedTest
@ValueSource(strings = {
"garbage",
"SPC\r\n0100\r\n\r\n\r\n",
"SPC1\r\n0200\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n",
"SPC1\r\n0200\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"
})
void invalidTest_keyDataStructureInvalidError(String qrText) {
QRBillValidationError err = assertThrows(QRBillValidationError.class, () -> QRBill.decodeQrCodeText(qrText));
assertSingleError(err.getValidationResult(), ValidationConstants.KEY_DATA_STRUCTURE_INVALID, ValidationConstants.FIELD_QR_TYPE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,12 @@ void invariantTest() {
assertEquals("abc", lines[0]);
}

@Test
void singleSpace() {
String[] lines = fontMetrics.splitLines(" ", 50, 10);
assertEquals(1, lines.length);
assertEquals("", lines[0]);
}

@Test
void manySpaces() {
String[] lines = fontMetrics.splitLines(" ", 50, 10);
assertEquals(1, lines.length);
assertEquals("", lines[0]);
}

@Test
void outsideASCIIRange() {
String[] lines = fontMetrics.splitLines("éà£$\uD83D\uDE03", 50, 10);
@ParameterizedTest
@ValueSource(strings = { " ", " ", "éà£$\uD83D\uDE03"})
void input_hasSingleLine(String input) {
String[] lines = fontMetrics.splitLines(input, 50, 10);
assertEquals(1, lines.length);
assertEquals("éà£$\uD83D\uDE03", lines[0]);
assertEquals(input.trim(), lines[0]);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -49,7 +50,8 @@ void saveAs_throwsException() throws IOException {
bill.getFormat().setOutputSize(OutputSize.QR_BILL_EXTRA_SPACE);
try (PDFCanvas canvas = new PDFCanvas(document, PDFCanvas.NEW_PAGE_AT_END)) {
QRBill.draw(bill, canvas);
assertThrows(IllegalStateException.class, () -> canvas.saveAs(Paths.get("some.pdf")));
Path path = Paths.get("some.pdf");
assertThrows(IllegalStateException.class, () -> canvas.saveAs(path));
}
}

Expand All @@ -63,7 +65,8 @@ void writeTo_throwsException() throws IOException {
bill.getFormat().setOutputSize(OutputSize.QR_BILL_EXTRA_SPACE);
try (PDFCanvas canvas = new PDFCanvas(stream)) {
QRBill.draw(bill, canvas);
assertThrows(IllegalStateException.class, () -> canvas.writeTo(new ByteArrayOutputStream()));
ByteArrayOutputStream os = new ByteArrayOutputStream();
assertThrows(IllegalStateException.class, () -> canvas.writeTo(os));
}
}
}
Expand Down

0 comments on commit ac9b18e

Please sign in to comment.