From a0a4d8ff8082d09c237b0b7f906f71356cac02f9 Mon Sep 17 00:00:00 2001 From: Sean Owen Date: Tue, 24 Jul 2018 17:59:52 -0500 Subject: [PATCH] A few more tests and fix handling of data: URIs in command line decoder --- .../zxing/client/j2se/DecodeWorker.java | 9 +++- .../j2se/CommandLineEncoderTestCase.java | 51 +++++++++++++++++++ .../j2se/CommandLineRunnerTestCase.java | 31 +++++++++++ .../client/j2se/DecodeWorkerTestCase.java | 2 +- 4 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 javase/src/test/java/com/google/zxing/client/j2se/CommandLineEncoderTestCase.java create mode 100644 javase/src/test/java/com/google/zxing/client/j2se/CommandLineRunnerTestCase.java diff --git a/javase/src/main/java/com/google/zxing/client/j2se/DecodeWorker.java b/javase/src/main/java/com/google/zxing/client/j2se/DecodeWorker.java index acacf4477..04d19244d 100644 --- a/javase/src/main/java/com/google/zxing/client/j2se/DecodeWorker.java +++ b/javase/src/main/java/com/google/zxing/client/j2se/DecodeWorker.java @@ -93,8 +93,13 @@ final class DecodeWorker implements Callable { inputFileName = inputPath.getFileName().toString(); } else { outDir = Paths.get(".").toRealPath(); - String[] pathElements = input.getPath().split("/"); - inputFileName = pathElements[pathElements.length - 1]; + String path = input.getPath(); + if (path == null) { + inputFileName = "input"; + } else { + String[] pathElements = path.split("/"); + inputFileName = pathElements[pathElements.length - 1]; + } } // Replace/add extension diff --git a/javase/src/test/java/com/google/zxing/client/j2se/CommandLineEncoderTestCase.java b/javase/src/test/java/com/google/zxing/client/j2se/CommandLineEncoderTestCase.java new file mode 100644 index 000000000..3b8e608c1 --- /dev/null +++ b/javase/src/test/java/com/google/zxing/client/j2se/CommandLineEncoderTestCase.java @@ -0,0 +1,51 @@ +/* + * Copyright 2018 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.zxing.client.j2se; + +import org.junit.Assert; +import org.junit.Test; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; + +public final class CommandLineEncoderTestCase extends Assert { + + @Test + public void testEncode() throws Exception { + File out = File.createTempFile("qrcode", ".png"); + out.deleteOnExit(); + String[] args = { + "--barcode_format", "QR_CODE", + "--image_format", "PNG", + "--output", out.toString(), + "--width", "1", "--height", "1", + "--error-correction-level", "L", + "HELLO" }; + CommandLineEncoder.main(args); + + Path outPath = out.toPath(); + assertTrue(Files.exists(outPath)); + assertTrue(Files.size(outPath) > 0); + BufferedImage image = ImageIO.read(out); + assertEquals(33, image.getHeight()); + assertEquals(33, image.getWidth()); + } + +} diff --git a/javase/src/test/java/com/google/zxing/client/j2se/CommandLineRunnerTestCase.java b/javase/src/test/java/com/google/zxing/client/j2se/CommandLineRunnerTestCase.java new file mode 100644 index 000000000..d43df2e8d --- /dev/null +++ b/javase/src/test/java/com/google/zxing/client/j2se/CommandLineRunnerTestCase.java @@ -0,0 +1,31 @@ +/* + * Copyright 2018 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.zxing.client.j2se; + +import org.junit.Assert; +import org.junit.Test; + +public final class CommandLineRunnerTestCase extends Assert { + + @Test + public void testCommandLineRunner() throws Exception { + String[] args = { "--pure_barcode", DecodeWorkerTestCase.IMAGE_DATA_URI }; + // Not a lot to do here but make sure it runs + CommandLineRunner.main(args); + } + +} diff --git a/javase/src/test/java/com/google/zxing/client/j2se/DecodeWorkerTestCase.java b/javase/src/test/java/com/google/zxing/client/j2se/DecodeWorkerTestCase.java index 83d9b959e..66ecd0061 100644 --- a/javase/src/test/java/com/google/zxing/client/j2se/DecodeWorkerTestCase.java +++ b/javase/src/test/java/com/google/zxing/client/j2se/DecodeWorkerTestCase.java @@ -30,7 +30,7 @@ import java.util.Queue; */ public final class DecodeWorkerTestCase extends Assert { - private static final String IMAGE_DATA_URI = + static final String IMAGE_DATA_URI = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACEAAAAhAQAAAAB/n//CAAAAkklEQVR42mP4DwQNDJjkB4" + "E77A0M369N/d7A8CV6rjiQjPMFkWG1QPL7RVGg%2BAfREKCa/5/vA9V/nFSQ3sDwb7/KdiDJqX4dSH4pXN/A8DfyDVD2" + "988HQPUfPVaqA0XKz%2BgD9bIk1AP1fgwvB7KlS9VBdqXbA82PT9AH2fiaH2SXGdDM71fDgeIfhIvKsbkTTAIAKYVr0N" +