diff options
| author | gschwaer <gero@kaiser-schwaericke.de> | 2021-07-29 23:28:21 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-29 23:28:21 +0300 |
| commit | d2027d0e0f210d35feda20902315e89dedf311ad (patch) | |
| tree | 5641c469d8dbbba65776b1e4e6d3651cb0651479 /core/src/test/java | |
| parent | 0d089156288f91cd87eec1550ad4183b226915b0 (diff) | |
| download | zxing-d2027d0e0f210d35feda20902315e89dedf311ad.tar.xz | |
Introduce hint to force specific code set for Code-128 encoding (#1411)
* Introduce hint to force specific code set for Code-128 encoding
* Address review comments
* Make character check more readable; address review comments
* Address review comments
Co-authored-by: Gero Schwäricke <gero@schwaericke.email>
Diffstat (limited to 'core/src/test/java')
| -rw-r--r-- | core/src/test/java/com/google/zxing/oned/Code128WriterTestCase.java | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/core/src/test/java/com/google/zxing/oned/Code128WriterTestCase.java b/core/src/test/java/com/google/zxing/oned/Code128WriterTestCase.java index a12d44de1..9e7ca8f5f 100644 --- a/core/src/test/java/com/google/zxing/oned/Code128WriterTestCase.java +++ b/core/src/test/java/com/google/zxing/oned/Code128WriterTestCase.java @@ -22,12 +22,16 @@ import org.junit.Before; import org.junit.Test; import com.google.zxing.BarcodeFormat; +import com.google.zxing.EncodeHintType; import com.google.zxing.Result; import com.google.zxing.Writer; import com.google.zxing.WriterException; import com.google.zxing.common.BitArray; import com.google.zxing.common.BitMatrix; +import java.util.Map; +import java.util.EnumMap; + /** * Tests {@link Code128Writer}. */ @@ -151,4 +155,82 @@ public class Code128WriterTestCase extends Assert { String actualRoundtripResultText = rtResult.getText(); assertEquals(toEncode, actualRoundtripResultText); } + + @Test(expected = IllegalArgumentException.class) + public void testEncodeWithForcedCodeSetFailureCodeSetABadCharacter() throws Exception { + // Lower case characters should not be accepted when the code set is forced to A. + String toEncode = "ASDFx0123"; + + Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class); + hints.put(EncodeHintType.FORCE_CODE_SET, "A"); + BitMatrix result = writer.encode(toEncode, BarcodeFormat.CODE_128, 0, 0, hints); + } + + @Test(expected = IllegalArgumentException.class) + public void testEncodeWithForcedCodeSetFailureCodeSetBBadCharacter() throws Exception { + String toEncode = "ASdf\00123"; // \0 (ascii value 0) + // Characters with ASCII value below 32 should not be accepted when the code set is forced to B. + + Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class); + hints.put(EncodeHintType.FORCE_CODE_SET, "B"); + BitMatrix result = writer.encode(toEncode, BarcodeFormat.CODE_128, 0, 0, hints); + } + + @Test(expected = IllegalArgumentException.class) + public void testEncodeWithForcedCodeSetFailureCodeSetCBadCharactersNonNum() throws Exception { + String toEncode = "123a5678"; + // Non-digit characters should not be accepted when the code set is forced to C. + + Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class); + hints.put(EncodeHintType.FORCE_CODE_SET, "C"); + BitMatrix result = writer.encode(toEncode, BarcodeFormat.CODE_128, 0, 0, hints); + } + + @Test(expected = IllegalArgumentException.class) + public void testEncodeWithForcedCodeSetFailureCodeSetCBadCharactersFncCode() throws Exception { + String toEncode = "123\u00f2a678"; + // Function codes other than 1 should not be accepted when the code set is forced to C. + + Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class); + hints.put(EncodeHintType.FORCE_CODE_SET, "C"); + BitMatrix result = writer.encode(toEncode, BarcodeFormat.CODE_128, 0, 0, hints); + } + + @Test(expected = IllegalArgumentException.class) + public void testEncodeWithForcedCodeSetFailureCodeSetCWrongAmountOfDigits() throws Exception { + String toEncode = "123456789"; + // An uneven amount of digits should not be accepted when the code set is forced to C. + + Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class); + hints.put(EncodeHintType.FORCE_CODE_SET, "C"); + BitMatrix result = writer.encode(toEncode, BarcodeFormat.CODE_128, 0, 0, hints); + } + + @Test + public void testEncodeWithForcedCodeSetFailureCodeSetA() throws Exception { + String toEncode = "AB123"; + // would default to B "A" "B" "1" "2" "3" check digit 10 + String expected = QUIET_SPACE + START_CODE_A + "10100011000" + "10001011000" + "10011100110" + "11001110010" + "11001011100" + "11001000100" + STOP + QUIET_SPACE; + + Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class); + hints.put(EncodeHintType.FORCE_CODE_SET, "A"); + BitMatrix result = writer.encode(toEncode, BarcodeFormat.CODE_128, 0, 0, hints); + + String actual = BitMatrixTestCase.matrixToString(result); + assertEquals(expected, actual); + } + + @Test + public void testEncodeWithForcedCodeSetFailureCodeSetB() throws Exception { + String toEncode = "1234"; + // would default to C "1" "2" "3" "4" check digit 88 + String expected = QUIET_SPACE + START_CODE_B + "10011100110" + "11001110010" + "11001011100" + "11001001110" + "11110010010" + STOP + QUIET_SPACE; + + Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class); + hints.put(EncodeHintType.FORCE_CODE_SET, "B"); + BitMatrix result = writer.encode(toEncode, BarcodeFormat.CODE_128, 0, 0, hints); + + String actual = BitMatrixTestCase.matrixToString(result); + assertEquals(expected, actual); + } } |
