Terms of Service | Privacy Policy | Cookie Policy

Commit 6e6d3c70 authored by Uwe Plonus's avatar Uwe Plonus

Added test for different output formats

parent 4bdeead4
Pipeline #435 passed with stages
in 12 minutes and 53 seconds
......@@ -170,4 +170,10 @@ $h4 Filetypes
Currently supported filetypes:
* bmp
* gif
* jpeg
* pcx
* png
* pnm
* tiff
/*
* Copyright (C) 2019 sw4j.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sw4j.tool.barcode.random.generator;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import org.sw4j.tool.barcode.random.config.CodeConfig;
import org.sw4j.tool.barcode.random.support.PredictableRandom;
import org.sw4j.tool.barcode.random.support.TestCodeData;
import static org.testng.Assert.*;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
*
* @author Uwe Plonus &lt;u.plonus@gmail.com&gt;
*/
public class BarCodeWriterNGTest {
@BeforeClass
public static void setUpClass() throws Exception {
}
@AfterClass
public static void tearDownClass() throws Exception {
}
@BeforeMethod
public void setUpMethod() throws Exception {
}
@AfterMethod
public void tearDownMethod() throws Exception {
}
@DataProvider
public String[] fileTypes() {
return new String[]{
"bmp",
"gif",
"jpeg",
"pcx",
"png",
"pnm",
"tiff",
};
}
@Test(dataProvider = "fileTypes")
public void generateQrCodeNoUrlWithDefaultSize(String fileType) throws IOException, ReaderException {
CodeConfig codeConfig = CodeConfig.builder()
.setType("qrcode")
.setEncoding("hex")
.setFiletype(fileType)
.build();
TestCodeData codeData = new TestCodeData(null, null);
BarcodeWriter barcodeWriter = new BarcodeWriter(codeConfig, codeData);
RandomIdent randomIdent = new RandomIdent("0000", 128,
new PredictableRandom(new byte[]{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}),
Collections.singleton("hex"));
barcodeWriter.accept(randomIdent);
ByteArrayOutputStream baos = codeData.getOutputs().get(String.format("qrcode-hex-0000.%s", fileType));
assertNotNull(baos, "Expected an OutputStream to be returned.");
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ImageInputStream iis = ImageIO.createImageInputStream(bais);
assertNotNull(iis, "Expected an ImageInputStream to be created.");
Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(fileType);
assertTrue(readers.hasNext(), String.format("Expected the %s image to be readable by Java.", fileType));
ImageReader ir = readers.next();
assertNotNull(ir, "Expected an image reader to be returned.");
ir.setInput(iis);
assertEquals(ir.getWidth(0), 200, "Expected the image to be 200px wide.");
assertEquals(ir.getHeight(0), 200, "Expected the image to be 200px high");
LuminanceSource source = new BufferedImageLuminanceSource(ir.read(0));
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap image = new BinaryBitmap(binarizer);
QRCodeReader qrCodeReader = new QRCodeReader();
Result barcodeContent = qrCodeReader.decode(image);
assertEquals(barcodeContent.getText(), "00000000000000000000000000000000",
"Expected the result to be encoded in the QR Code.");
}
}
......@@ -44,7 +44,7 @@ import org.testng.annotations.Test;
/**
*
* @author uwe
* @author Uwe Plonus &lt;u.plonus@gmail.com&gt;
*/
public class CodeGeneratorNGTest {
......
......@@ -57,7 +57,7 @@ public class TestCodeData implements CodeData {
@Override
public OutputStream getOutputForIdent(CodeType type, String format, String ident, String suffix)
throws IOException {
String key = String.format("%s-%s-%s.suffix", type.getType(), format, ident, suffix);
String key = String.format("%s-%s-%s.%s", type.getType(), format, ident, suffix);
ByteArrayOutputStream os = new ByteArrayOutputStream();
outputs.put(key, os);
return os;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment