Terms of Service | Privacy Policy | Cookie Policy

Verified Commit 32eca135 authored by Uwe Plonus's avatar Uwe Plonus
Browse files

Added test for creation of csv output file

parent 7a35ba79
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -118,7 +118,7 @@ public class CodeGenerator {
                            encodingParameters.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
                            BitMatrix matrix = codeWriter.encode(codeUrl, BarcodeFormat.QR_CODE,
                                    code.getWidth(), code.getHeight());
                            MatrixToImageWriter.writeToStream(matrix, "png", os);
                            MatrixToImageWriter.writeToStream(matrix, code.getFiletype(), os);
                        } catch (IOException | WriterException exc) {
                            logger.log(Level.WARNING,
                                    String.format("Writing of Code 'type: %s / encoding: %s / ident: %s' failed.",
+1 −1
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<project xmlns='http://maven.apache.org/DECORATION/1.6.0'
         xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
         xsi:schemaLocation='http://maven.apache.org/DECORATION/1.6.0 http://maven.apache.org/xsd/decoration-1.6.0.xsd'
         name="Random QRCode Generator">
         name="Random Barcode Generator">

  <skin>
    <groupId>org.apache.maven.skins</groupId>
+44 −0
Original line number Diff line number Diff line
@@ -181,4 +181,48 @@ public class CodeGeneratorNGTest {
                "Expected only the header 'ident,hex' to be written.");
    }

    @Test
    public void createCsvOutput() throws IOException {
        InputStream is = new ByteArrayInputStream("01\n".getBytes(Charset.forName("UTF-8")));
        OutputStream os = new ByteArrayOutputStream();
        CodeConfig codeConfig = CodeConfig.builder()
                .setType("qrcode")
                .setEncoding("hex")
                .setUrl("https://example.org/{code}")
                .build();
        RandomConfig config = new RandomConfig(128, Arrays.asList(codeConfig), Arrays.asList("base58{10}"));
        TestCodeData codeData = new TestCodeData(is, os);

        CodeGenerator generator = new CodeGenerator(config, codeData);

        generator.createCodes();

        ByteArrayOutputStream baos = (ByteArrayOutputStream)codeData.getOutput();
        String generatedCsv = new String(baos.toByteArray(), Charset.forName("UTF-8"));
        assertTrue(generatedCsv.matches("ident,hex,base58\\{10\\}\\R01,\"[0-9a-f]{32}\",[1-9A-HJ-NP-Za-km-z]{10}\\R") ||
                generatedCsv.matches("ident,base58\\{10\\},hex\\R01,[1-9A-HJ-NP-Za-km-z]{10},\"[0-9a-f]{32}\"\\R"),
                "Expected the csv to be created.");
    }

    @Test
    public void createCodeOutput() throws IOException {
        InputStream is = new ByteArrayInputStream("01\n".getBytes(Charset.forName("UTF-8")));
        OutputStream os = new ByteArrayOutputStream();
        CodeConfig codeConfig = CodeConfig.builder()
                .setType("qrcode")
                .setEncoding("hex")
                .setUrl("https://example.org/{code}")
                .build();
        RandomConfig config = new RandomConfig(128, Arrays.asList(codeConfig), Arrays.asList("base58{10}"));
        TestCodeData codeData = new TestCodeData(is, os);

        CodeGenerator generator = new CodeGenerator(config, codeData);

        generator.createCodes();

        ByteArrayOutputStream baos = (ByteArrayOutputStream)codeData.getOutput();
        String generatedCsv = new String(baos.toByteArray(), Charset.forName("UTF-8"));
        assertEquals(codeData.getOutputs().size(), 1, "Expected 1 barcode to be created.");
    }

}
+15 −1
Original line number Diff line number Diff line
@@ -16,9 +16,13 @@
 */
package org.sw4j.tool.barcode.random.support;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.sw4j.tool.barcode.random.codedata.CodeData;

/**
@@ -31,9 +35,12 @@ public class TestCodeData implements CodeData {

    private final OutputStream output;

    private final Map<String, ByteArrayOutputStream> outputs;

    public TestCodeData(InputStream input, OutputStream output) {
        this.input = input;
        this.output = output;
        this.outputs = new HashMap<>();
    }

    @Override
@@ -48,7 +55,14 @@ public class TestCodeData implements CodeData {

    @Override
    public OutputStream getOutputForIdent(String type, String format, String ident, String suffix) throws IOException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        String key = String.format("%s-%s-%s.suffix", type, format, ident, suffix);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        outputs.put(key, os);
        return os;
    }

    public Map<String, ByteArrayOutputStream> getOutputs() {
        return Collections.unmodifiableMap(outputs);
    }

}