updated unit test cases so that build doesn't generate un-committed files

This commit is contained in:
amit.pandey 2020-07-06 13:57:22 +05:30
parent a593cfc3ef
commit 9de9692083
5 changed files with 71 additions and 28 deletions

2
.gitignore vendored
View File

@ -85,3 +85,5 @@ transaction.log
*-shell.log
apache-cxf/cxf-aegis/baeldung.xml
libraries-2/*.db

View File

@ -1,16 +1,21 @@
package com.baeldung.jgrapht;
import static org.junit.Assert.assertTrue;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.jgrapht.ext.JGraphXAdapter;
import org.jgrapht.graph.DefaultDirectedGraph;
import org.jgrapht.graph.DefaultEdge;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.mxgraph.layout.mxCircleLayout;
import com.mxgraph.layout.mxIGraphLayout;
import com.mxgraph.util.mxCellRenderer;
@ -20,7 +25,7 @@ public class GraphImageGenerationUnitTest {
@Before
public void createGraph() throws IOException {
File imgFile = new File("src/test/resources/graph.png");
File imgFile = new File("src/test/resources/graph1.png");
imgFile.createNewFile();
g = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
String x1 = "x1";
@ -34,12 +39,18 @@ public class GraphImageGenerationUnitTest {
g.addEdge(x3, x1);
}
@After
public void cleanup() {
File imgFile = new File("src/test/resources/graph1.png");
imgFile.deleteOnExit();
}
@Test
public void givenAdaptedGraph_whenWriteBufferedImage_ThenFileShouldExist() throws IOException {
JGraphXAdapter<String, DefaultEdge> graphAdapter = new JGraphXAdapter<String, DefaultEdge>(g);
mxIGraphLayout layout = new mxCircleLayout(graphAdapter);
layout.execute(graphAdapter.getDefaultParent());
File imgFile = new File("src/test/resources/graph.png");
File imgFile = new File("src/test/resources/graph1.png");
BufferedImage image = mxCellRenderer.createBufferedImage(graphAdapter, null, 2, Color.WHITE, true, null);
ImageIO.write(image, "PNG", imgFile);
assertTrue(imgFile.exists());

View File

@ -7,48 +7,59 @@ import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.google.common.io.Files;
public class CsvUnitTest {
private File csvFromJson;
private File jsonFromCsv;
private File formattedCsvFromJson;
@Before
public void setup() {
csvFromJson = new File("src/main/resources/csv/csvFromJson.csv");
jsonFromCsv = new File("src/main/resources/csv/jsonFromCsv.json");
formattedCsvFromJson = new File("src/main/resources/csv/formattedCsvFromJson.csv");
}
@After
public void cleanup() {
csvFromJson.deleteOnExit();
jsonFromCsv.deleteOnExit();
formattedCsvFromJson.deleteOnExit();
}
@Test
public void givenJsonInput_thenWriteCsv() throws JsonParseException, JsonMappingException, IOException {
JsonCsvConverter.JsonToCsv(new File("src/main/resources/csv/orderLines.json"),
new File("src/main/resources/csv/csvFromJson.csv"));
assertEquals(readFile("src/main/resources/csv/csvFromJson.csv"),
readFile("src/test/resources/csv/expectedCsvFromJson.csv"));
JsonCsvConverter.JsonToCsv(new File("src/main/resources/csv/orderLines.json"), csvFromJson);
assertEquals(readFile(csvFromJson.getAbsolutePath()), readFile("src/test/resources/csv/expectedCsvFromJson.csv"));
}
@Test
public void givenCsvInput_thenWritesJson() throws JsonParseException, JsonMappingException, IOException {
JsonCsvConverter.csvToJson(new File("src/main/resources/csv/orderLines.csv"),
new File("src/main/resources/csv/jsonFromCsv.json"));
assertEquals(readFile("src/main/resources/csv/jsonFromCsv.json"),
readFile("src/test/resources/csv/expectedJsonFromCsv.json"));
JsonCsvConverter.csvToJson(new File("src/main/resources/csv/orderLines.csv"), jsonFromCsv);
assertEquals(readFile(jsonFromCsv.getAbsolutePath()), readFile("src/test/resources/csv/expectedJsonFromCsv.json"));
}
@Test
public void givenJsonInput_thenWriteFormattedCsvOutput() throws JsonParseException, JsonMappingException, IOException {
JsonCsvConverter.JsonToFormattedCsv(new File("src/main/resources/csv/orderLines.json"),
new File("src/main/resources/csv/formattedCsvFromJson.csv"));
JsonCsvConverter.JsonToFormattedCsv(new File("src/main/resources/csv/orderLines.json"), formattedCsvFromJson);
assertEquals(readFile(formattedCsvFromJson.getAbsolutePath()), readFile("src/test/resources/csv/expectedFormattedCsvFromJson.csv"));
assertEquals(readFile("src/main/resources/csv/formattedCsvFromJson.csv"),
readFile("src/test/resources/csv/expectedFormattedCsvFromJson.csv"));
}
private List<String> readFile(String filename) throws IOException {
return Files.readLines(new File(filename), Charset.forName("utf-8"));
}
}
;
};

View File

@ -12,6 +12,7 @@ import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@ -25,12 +26,19 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature;
public class YamlUnitTest {
private ObjectMapper mapper;
private File orderOutput;
@Before
public void setup() {
mapper = new ObjectMapper(new YAMLFactory().disable(Feature.WRITE_DOC_START_MARKER));
mapper.findAndRegisterModules();
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
orderOutput = new File("src/test/resources/yaml/orderOutput.yaml");
}
@After
public void cleanup() {
orderOutput.deleteOnExit();
}
@Test
@ -53,9 +61,9 @@ public class YamlUnitTest {
LocalDate.parse("2019-04-18", DateTimeFormatter.ISO_DATE),
"Customer, Jane",
lines);
mapper.writeValue(new File("src/test/resources/yaml/orderOutput.yaml"), order);
mapper.writeValue(orderOutput, order);
File outputYaml = new File("src/test/resources/yaml/orderOutput.yaml");
File outputYaml = new File(orderOutput.getAbsolutePath());
assertTrue(outputYaml.exists());
}
}

View File

@ -2,15 +2,26 @@ package com.baeldung.univocity;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.junit.After;
import org.junit.Test;
import com.baeldung.univocity.model.Product;
public class ParsingServiceUnitTest {
@After
public void cleanup() {
File csvFile = new File("src/test/resources/outputProductList.csv");
csvFile.deleteOnExit();
File textFile = new File("src/test/resources/outputProductList.txt");
textFile.deleteOnExit();
}
@Test
public void givenCsvFile_thenParsedResultsShouldBeReturned() {
ParsingService parsingService = new ParsingService();