Merge pull request #9647 from amit2103/JAVA-2028

updated unit test cases so that build doesn't generate un-committed f…
This commit is contained in:
Josh Cummings 2020-07-06 07:14:59 -06:00 committed by GitHub
commit 5eee8fb91d
5 changed files with 71 additions and 28 deletions

2
.gitignore vendored
View File

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

View File

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

View File

@ -7,48 +7,59 @@ import java.io.IOException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.List; import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonMappingException;
import com.google.common.io.Files; import com.google.common.io.Files;
public class CsvUnitTest { 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 @Test
public void givenJsonInput_thenWriteCsv() throws JsonParseException, JsonMappingException, IOException { public void givenJsonInput_thenWriteCsv() throws JsonParseException, JsonMappingException, IOException {
JsonCsvConverter.JsonToCsv(new File("src/main/resources/csv/orderLines.json"), JsonCsvConverter.JsonToCsv(new File("src/main/resources/csv/orderLines.json"), csvFromJson);
new File("src/main/resources/csv/csvFromJson.csv"));
assertEquals(readFile(csvFromJson.getAbsolutePath()), readFile("src/test/resources/csv/expectedCsvFromJson.csv"));
assertEquals(readFile("src/main/resources/csv/csvFromJson.csv"),
readFile("src/test/resources/csv/expectedCsvFromJson.csv"));
} }
@Test @Test
public void givenCsvInput_thenWritesJson() throws JsonParseException, JsonMappingException, IOException { public void givenCsvInput_thenWritesJson() throws JsonParseException, JsonMappingException, IOException {
JsonCsvConverter.csvToJson(new File("src/main/resources/csv/orderLines.csv"), JsonCsvConverter.csvToJson(new File("src/main/resources/csv/orderLines.csv"), jsonFromCsv);
new File("src/main/resources/csv/jsonFromCsv.json"));
assertEquals(readFile(jsonFromCsv.getAbsolutePath()), readFile("src/test/resources/csv/expectedJsonFromCsv.json"));
assertEquals(readFile("src/main/resources/csv/jsonFromCsv.json"),
readFile("src/test/resources/csv/expectedJsonFromCsv.json"));
} }
@Test @Test
public void givenJsonInput_thenWriteFormattedCsvOutput() throws JsonParseException, JsonMappingException, IOException { public void givenJsonInput_thenWriteFormattedCsvOutput() throws JsonParseException, JsonMappingException, IOException {
JsonCsvConverter.JsonToFormattedCsv(new File("src/main/resources/csv/orderLines.json"), JsonCsvConverter.JsonToFormattedCsv(new File("src/main/resources/csv/orderLines.json"), formattedCsvFromJson);
new File("src/main/resources/csv/formattedCsvFromJson.csv"));
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 { private List<String> readFile(String filename) throws IOException {
return Files.readLines(new File(filename), Charset.forName("utf-8")); 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.ArrayList;
import java.util.List; import java.util.List;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -25,12 +26,19 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature;
public class YamlUnitTest { public class YamlUnitTest {
private ObjectMapper mapper; private ObjectMapper mapper;
private File orderOutput;
@Before @Before
public void setup() { public void setup() {
mapper = new ObjectMapper(new YAMLFactory().disable(Feature.WRITE_DOC_START_MARKER)); mapper = new ObjectMapper(new YAMLFactory().disable(Feature.WRITE_DOC_START_MARKER));
mapper.findAndRegisterModules(); mapper.findAndRegisterModules();
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
orderOutput = new File("src/test/resources/yaml/orderOutput.yaml");
}
@After
public void cleanup() {
orderOutput.deleteOnExit();
} }
@Test @Test
@ -53,9 +61,9 @@ public class YamlUnitTest {
LocalDate.parse("2019-04-18", DateTimeFormatter.ISO_DATE), LocalDate.parse("2019-04-18", DateTimeFormatter.ISO_DATE),
"Customer, Jane", "Customer, Jane",
lines); 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()); assertTrue(outputYaml.exists());
} }
} }

View File

@ -2,15 +2,26 @@ package com.baeldung.univocity;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import com.baeldung.univocity.model.Product; import com.baeldung.univocity.model.Product;
public class ParsingServiceUnitTest { 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 @Test
public void givenCsvFile_thenParsedResultsShouldBeReturned() { public void givenCsvFile_thenParsedResultsShouldBeReturned() {
ParsingService parsingService = new ParsingService(); ParsingService parsingService = new ParsingService();