|
31 | 31 | import com.graphhopper.jsprit.core.util.Solutions;
|
32 | 32 | import com.graphhopper.jsprit.io.util.TestUtils;
|
33 | 33 | import org.junit.Assert;
|
34 |
| -import org.junit.Before; |
35 | 34 | import org.junit.Test;
|
36 | 35 |
|
| 36 | +import java.io.ByteArrayInputStream; |
| 37 | +import java.io.ByteArrayOutputStream; |
37 | 38 | import java.io.IOException;
|
38 |
| -import java.io.OutputStream; |
39 |
| -import java.nio.file.Files; |
40 |
| -import java.nio.file.Paths; |
41 | 39 | import java.util.ArrayList;
|
42 | 40 | import java.util.Collection;
|
43 | 41 | import java.util.List;
|
|
46 | 44 |
|
47 | 45 | public class VrpXMLWriterTest {
|
48 | 46 |
|
49 |
| - private String infileName; |
50 |
| - |
51 |
| - @Before |
52 |
| - public void doBefore() { |
53 |
| - infileName = "src/test/resources/infiniteWriterV2Test.xml"; |
54 |
| - } |
55 |
| - |
56 |
| - @Test |
57 |
| - public void whenWritingInfiniteVrp_itWritesCorrectly() { |
58 |
| - VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance(); |
59 |
| - builder.setFleetSize(VehicleRoutingProblem.FleetSize.INFINITE); |
60 |
| - VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("vehType").addCapacityDimension(0, 20).build(); |
61 |
| - VehicleImpl vehicle = VehicleImpl.Builder.newInstance("myVehicle").setStartLocation(TestUtils.loc("loc")).setType(type).build(); |
62 |
| - builder.addVehicle(vehicle); |
63 |
| - VehicleRoutingProblem vrp = builder.build(); |
64 |
| - new VrpXMLWriter(vrp, null).write(infileName); |
65 |
| - } |
66 |
| - |
67 |
| - @Test |
68 |
| - public void whenWritingFiniteVrp_itWritesAndReadsCorrectly() { |
69 |
| - VehicleRoutingProblem.Builder builder = twoVehicleTypesAndImpls(); |
70 |
| - builder.setFleetSize(VehicleRoutingProblem.FleetSize.FINITE); |
71 |
| - |
72 |
| - VehicleRoutingProblem vrp = builder.build(); |
73 |
| - writeAndRereadXml(vrp); |
74 |
| - } |
75 |
| - |
76 |
| - |
77 | 47 | @Test
|
78 | 48 | public void whenWritingServices_itWritesThemCorrectly() {
|
79 | 49 | VehicleRoutingProblem.Builder builder = twoVehicleTypesAndImpls();
|
@@ -630,28 +600,32 @@ public void outputStreamAndFileContentsAreEqual() throws IOException {
|
630 | 600 | VehicleRoutingProblem.Builder builder = twoVehicleTypesAndImpls();
|
631 | 601 | VehicleRoutingProblem vrp = builder.build();
|
632 | 602 |
|
633 |
| - new VrpXMLWriter(vrp, null).write(infileName); |
634 |
| - String outputStringFromFile = new String(Files.readAllBytes(Paths.get(infileName))); |
| 603 | + VrpXMLWriter vrpXMLWriter = new VrpXMLWriter(vrp, null); |
| 604 | + ByteArrayOutputStream os = (ByteArrayOutputStream) vrpXMLWriter.write(); |
| 605 | + |
| 606 | + String outputStringFromFile = new String(os.toByteArray()); |
635 | 607 | String outputStringFromStream = new VrpXMLWriter(vrp, null).write().toString();
|
636 | 608 |
|
637 | 609 | assertEquals(outputStringFromFile, outputStringFromStream);
|
638 | 610 |
|
639 | 611 | }
|
640 | 612 |
|
641 | 613 | private VehicleRoutingProblem writeAndRereadXml(VehicleRoutingProblem vrp) {
|
642 |
| - new VrpXMLWriter(vrp, null).write(infileName); |
643 |
| - |
| 614 | + VrpXMLWriter vrpXMLWriter = new VrpXMLWriter(vrp, null); |
| 615 | + ByteArrayOutputStream os = (ByteArrayOutputStream) vrpXMLWriter.write(); |
| 616 | + ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()); |
644 | 617 | VehicleRoutingProblem.Builder vrpToReadBuilder = VehicleRoutingProblem.Builder.newInstance();
|
645 |
| - new VrpXMLReader(vrpToReadBuilder, null).read(infileName); |
| 618 | + new VrpXMLReader(vrpToReadBuilder, null).read(is); |
646 | 619 | return vrpToReadBuilder.build();
|
647 | 620 | }
|
648 | 621 |
|
649 | 622 | private List<VehicleRoutingProblemSolution> writeAndRereadXmlWithSolutions(VehicleRoutingProblem vrp, List<VehicleRoutingProblemSolution> solutions) {
|
650 |
| - new VrpXMLWriter(vrp, solutions).write(infileName); |
651 |
| - |
| 623 | + VrpXMLWriter vrpXMLWriter = new VrpXMLWriter(vrp, solutions); |
| 624 | + ByteArrayOutputStream os = (ByteArrayOutputStream) vrpXMLWriter.write(); |
| 625 | + ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()); |
652 | 626 | VehicleRoutingProblem.Builder vrpToReadBuilder = VehicleRoutingProblem.Builder.newInstance();
|
653 | 627 | List<VehicleRoutingProblemSolution> solutionsToRead = new ArrayList<VehicleRoutingProblemSolution>();
|
654 |
| - new VrpXMLReader(vrpToReadBuilder, solutionsToRead).read(infileName); |
| 628 | + new VrpXMLReader(vrpToReadBuilder, solutionsToRead).read(is); |
655 | 629 | return solutionsToRead;
|
656 | 630 | }
|
657 | 631 |
|
|
0 commit comments