Terms of Service | Privacy Policy | Cookie Policy

Commit 7d9593dd authored by Uwe Plonus's avatar Uwe Plonus

Started implementing tests for FileCodeData

parent e8b84225
Pipeline #425 failed with stages
in 18 minutes and 19 seconds
......@@ -76,13 +76,7 @@ public class FileCodeData implements CodeData {
*/
@Override
public OutputStream getOutput() throws IOException {
File folder = new File(config.getOutput().getFolder());
if (!folder.exists()) {
if (!folder.mkdirs()) {
throw new IOException(
String.format("Cannot create output folder '%s'.", folder.getAbsoluteFile()));
}
}
File folder = createOutputFolder();
return new FileOutputStream(new File(folder, config.getOutput().getFile().getName()));
}
......@@ -105,15 +99,25 @@ public class FileCodeData implements CodeData {
public OutputStream getOutputForIdent(final CodeType type, final String format, final String ident,
final String suffix)
throws IOException {
File folder = new File(config.getOutput().getFolder());
if (!folder.exists()) {
if (!folder.mkdirs()) {
throw new IOException(
String.format("Cannot create output folder '%s'.", folder.getAbsoluteFile()));
}
}
File folder = createOutputFolder();
return new FileOutputStream(new File(folder,
String.format("%s-%s-%s.%s", type.getType(), format, ident, suffix)));
}
/**
* <p>
* Create the output folder if it does not already exists.
* </p>
* @return the output folder.
* @throws IOException if the folder cannot be created.
*/
private File createOutputFolder() throws IOException {
File folder = new File(config.getOutput().getFolder());
if (!folder.exists() && !folder.mkdirs()) {
throw new IOException(
String.format("Cannot create output folder '%s'.", folder.getAbsoluteFile()));
}
return folder;
}
}
/*
* 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.codedata;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import org.sw4j.tool.barcode.random.config.CodeConfig;
import org.sw4j.tool.barcode.random.config.Config;
import org.sw4j.tool.barcode.random.config.FileConfig;
import org.sw4j.tool.barcode.random.config.OutputConfig;
import org.sw4j.tool.barcode.random.config.RandomConfig;
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.Test;
/**
*
* @author Uwe Plonus &lt;u.plonus@gmail.com&gt;
*/
public class FileCodeDataNGTest {
@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 {
}
@Test
public void getInputSuccess() throws IOException {
Config config = Config.builder()
.setInput(FileConfig.builder().setName("src/test/data/testdata.csv").build())
.setOutput(OutputConfig.builder()
.setFolder("target/output")
.setFile(FileConfig.builder().setName("output.csv").build()).build())
.setRandom(RandomConfig.builder()
.setSize(128)
.setCodes(Arrays.asList(CodeConfig.builder()
.setType("qrcode")
.setEncoding("hex")
.build()
))
.build())
.build();
FileCodeData codeData = new FileCodeData(config);
InputStream is = codeData.getInput();
assertNotNull(is, "Expected an InputStream to be created.");
}
@Test(expectedExceptions = FileNotFoundException.class)
public void getInputFails() throws IOException {
Config config = Config.builder()
.setInput(FileConfig.builder().setName("src/test/data/notexists.csv").build())
.setOutput(OutputConfig.builder()
.setFolder("target/output")
.setFile(FileConfig.builder().setName("output.csv").build()).build())
.setRandom(RandomConfig.builder()
.setSize(128)
.setCodes(Arrays.asList(CodeConfig.builder()
.setType("qrcode")
.setEncoding("hex")
.build()
))
.build())
.build();
FileCodeData codeData = new FileCodeData(config);
InputStream is = codeData.getInput();
fail("Expected a FileNotFoundException to be thrown.");
}
@Test
public void getOutputCreatesFolder() throws IOException {
Config config = Config.builder()
.setInput(FileConfig.builder().setName("src/test/data/testdata.csv").build())
.setOutput(OutputConfig.builder()
.setFolder("target/output")
.setFile(FileConfig.builder().setName("output.csv").build()).build())
.setRandom(RandomConfig.builder()
.setSize(128)
.setCodes(Arrays.asList(CodeConfig.builder()
.setType("qrcode")
.setEncoding("hex")
.build()
))
.build())
.build();
FileCodeData codeData = new FileCodeData(config);
codeData.getOutput();
File outputFolder = new File("target/output");
assertTrue(outputFolder.exists(), "OutputFolder should exist.");
assertTrue(outputFolder.isDirectory(), "OutputFolder should be folder.");
}
}
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