1
0
Fork 0
mirror of https://github.com/graphhopper/jsprit.git synced 2020-01-24 07:45:05 +01:00

Add VrpXMLWrite method that returns OutputStream

Add test to check that two outputs are the same
This commit is contained in:
Heinrich Filter 2017-06-27 23:12:32 +02:00
parent bf40087b22
commit bab1f7d6a8
2 changed files with 54 additions and 18 deletions

View file

@ -41,9 +41,7 @@ import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@ -94,8 +92,36 @@ public class VrpXMLWriter {
public void write(String filename) {
if (!filename.endsWith(".xml")) filename += ".xml";
log.info("write vrp: " + filename);
XMLConf xmlConfig = createXMLConfiguration();
try {
xmlConfig.setFileName(filename);
Writer out = new FileWriter(filename);
XMLSerializer serializer = new XMLSerializer(out, createOutputFormat());
serializer.serialize(xmlConfig.getDocument());
out.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public OutputStream write() {
XMLConf xmlConfig = createXMLConfiguration();
OutputStream out = new ByteArrayOutputStream();
try {
XMLSerializer serializer = new XMLSerializer(out, createOutputFormat());
serializer.serialize(xmlConfig.getDocument());
out.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
return out;
}
private XMLConf createXMLConfiguration() {
XMLConf xmlConfig = new XMLConf();
xmlConfig.setFileName(filename);
xmlConfig.setRootElementName("problem");
xmlConfig.setAttributeSplittingDisabled(true);
xmlConfig.setDelimiterParsingDisabled(true);
@ -123,10 +149,6 @@ public class VrpXMLWriter {
writeSolutions(xmlConfig);
OutputFormat format = new OutputFormat();
format.setIndenting(true);
format.setIndent(5);
try {
Document document = xmlConfig.createDoc();
@ -138,17 +160,14 @@ public class VrpXMLWriter {
} catch (ConfigurationException e) {
throw new RuntimeException(e);
}
return xmlConfig;
}
try {
Writer out = new FileWriter(filename);
XMLSerializer serializer = new XMLSerializer(out, format);
serializer.serialize(xmlConfig.getDocument());
out.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
private OutputFormat createOutputFormat() {
OutputFormat format = new OutputFormat();
format.setIndenting(true);
format.setIndent(5);
return format;
}
private void writeInitialRoutes(XMLConf xmlConfig) {

View file

@ -34,6 +34,10 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@ -621,6 +625,19 @@ public class VrpXMLWriterTest {
assertEquals("2", Solutions.bestOf(solutionsToRead).getUnassignedJobs().iterator().next().getId());
}
@Test
public void outputStreamAndFileContentsAreEqual() throws IOException {
VehicleRoutingProblem.Builder builder = twoVehicleTypesAndImpls();
VehicleRoutingProblem vrp = builder.build();
new VrpXMLWriter(vrp, null).write(infileName);
String outputStringFromFile = new String(Files.readAllBytes(Paths.get(infileName)));
String outputStringFromStream = new VrpXMLWriter(vrp, null).write().toString();
assertEquals(outputStringFromFile, outputStringFromStream);
}
private VehicleRoutingProblem writeAndRereadXml(VehicleRoutingProblem vrp) {
new VrpXMLWriter(vrp, null).write(infileName);