Skip to content

Commit

Permalink
updated unit test cases so that build doesn't generate un-committed f…
Browse files Browse the repository at this point in the history
…iles
  • Loading branch information
amit2103 committed Jul 6, 2020
1 parent c6eba53 commit 2303832
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,5 @@ transaction.log
*-shell.log

apache-cxf/cxf-aegis/baeldung.xml

libraries-2/*.db
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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";
Expand All @@ -34,12 +39,18 @@ public void createGraph() throws IOException {
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,59 @@
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"));
}


}
;

};
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -25,12 +26,19 @@

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
Expand All @@ -53,9 +61,9 @@ public void givenYamlObject_FileWritten() throws JsonGenerationException, JsonMa
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@

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();
Expand Down

0 comments on commit 2303832

Please sign in to comment.