Using try-with-resource

This commit is contained in:
Papick Garcia Taboada
2016-09-13 17:00:10 +02:00
parent c5311cad7c
commit 85a75da5ef
6 changed files with 252 additions and 198 deletions
@@ -13,46 +13,43 @@ import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.w3c.dom.Element;
import slash.navigation.base.BaseNavigationPosition;
import slash.navigation.gpx.binding11.WptType;
import com.sap.sailing.domain.common.tracking.GPSFix;
import com.sap.sailing.domain.trackfiles.TrackFileImportDeviceIdentifier;
import com.sap.sailing.server.trackfiles.impl.BaseRouteConverterGPSFixImporterImpl;
import com.sap.sailing.server.trackfiles.impl.RouteConverterGPSFixImporterImpl;
import slash.navigation.base.BaseNavigationPosition;
import slash.navigation.gpx.binding11.WptType;
public class RouteConverterGpx11ExtensionsTest {
private boolean extensionsFound = false;
@Test
public void areExtensionsRead() throws Exception {
String testFile = "/equine-extensions.gpx";
InputStream in = getClass().getResourceAsStream(testFile);
assert in != null : new Exception("Input file not found");
BaseRouteConverterGPSFixImporterImpl importer = spy(new RouteConverterGPSFixImporterImpl());
doAnswer(new Answer<GPSFix>() {
@Override
public GPSFix answer(InvocationOnMock invocation) throws Throwable {
BaseNavigationPosition p = (BaseNavigationPosition) invocation.getArguments()[0];
WptType waypoint = p.asGpxPosition().getOrigin(WptType.class);
final List<Object> extensions = waypoint.getExtensions().getAny();
for (Object o : extensions) {
Element el = (Element) o;
System.out.println(el.getLocalName() + " " + el.getTextContent());
extensionsFound = true;
try (InputStream in = getClass().getResourceAsStream(testFile)) {
assert in != null : new Exception("Input file not found");
BaseRouteConverterGPSFixImporterImpl importer = spy(new RouteConverterGPSFixImporterImpl());
doAnswer(new Answer<GPSFix>() {
@Override
public GPSFix answer(InvocationOnMock invocation) throws Throwable {
BaseNavigationPosition p = (BaseNavigationPosition) invocation.getArguments()[0];
WptType waypoint = p.asGpxPosition().getOrigin(WptType.class);
final List<Object> extensions = waypoint.getExtensions().getAny();
for (Object o : extensions) {
Element el = (Element) o;
System.out.println(el.getLocalName() + " " + el.getTextContent());
extensionsFound = true;
}
return (GPSFix) invocation.callRealMethod();
}
return (GPSFix) invocation.callRealMethod();
}
}).when(importer).convertToGPSFix(any(BaseNavigationPosition.class));
importer.importFixes(in, new com.sap.sailing.domain.trackimport.GPSFixImporter.Callback() {
@Override
public void addFix(GPSFix fix, TrackFileImportDeviceIdentifier device) {
}
}, false, "source");
assertTrue(extensionsFound);
}).when(importer).convertToGPSFix(any(BaseNavigationPosition.class));
importer.importFixes(in, new com.sap.sailing.domain.trackimport.GPSFixImporter.Callback() {
@Override
public void addFix(GPSFix fix, TrackFileImportDeviceIdentifier device) {
}
}, false, "source");
assertTrue(extensionsFound);
}
}
}
@@ -17,37 +17,39 @@ import com.sap.sailing.server.trackfiles.impl.RouteConverterGPSFixImporterImpl;
public class TrackFileImportTest {
private boolean callbackCalled = false;
@Before
public void setup() {
callbackCalled = false;
}
@Test
public void testGpx() throws IOException, FormatNotSupportedException {
InputStream in = getClass().getResourceAsStream("/Cardiff Race17 - COMPETITORS.gpx");
new RouteConverterGPSFixImporterImpl().importFixes(in, new Callback() {
@Override
public void addFix(GPSFix fix, TrackFileImportDeviceIdentifier device) {
if (fix instanceof GPSFixMoving) {
callbackCalled = true;
try (InputStream in = getClass().getResourceAsStream("/Cardiff Race17 - COMPETITORS.gpx")) {
new RouteConverterGPSFixImporterImpl().importFixes(in, new Callback() {
@Override
public void addFix(GPSFix fix, TrackFileImportDeviceIdentifier device) {
if (fix instanceof GPSFixMoving) {
callbackCalled = true;
}
}
}
}, false, "source");
assertTrue(callbackCalled);
}, false, "source");
assertTrue(callbackCalled);
}
}
@Test
public void testKmlOnlyLatLong() throws IOException, FormatNotSupportedException {
InputStream in = getClass().getResourceAsStream("/Cardiff Race22 - COMPETITORS.kml");
new RouteConverterGPSFixImporterImpl().importFixes(in, new Callback() {
@Override
public void addFix(GPSFix fix, TrackFileImportDeviceIdentifier device) {
if (fix instanceof GPSFix) {
callbackCalled = true;
try (InputStream in = getClass().getResourceAsStream("/Cardiff Race22 - COMPETITORS.kml")) {
new RouteConverterGPSFixImporterImpl().importFixes(in, new Callback() {
@Override
public void addFix(GPSFix fix, TrackFileImportDeviceIdentifier device) {
if (fix instanceof GPSFix) {
callbackCalled = true;
}
}
}
}, false, "source");
assertTrue(callbackCalled);
}, false, "source");
assertTrue(callbackCalled);
}
}
}
@@ -26,7 +26,6 @@ import com.tractrac.model.lib.api.event.CreateModelException;
import com.tractrac.subscription.lib.api.SubscriberInitializationException;
public class TrackedRacesExportTest extends OnlineTracTracBasedTest {
public TrackedRacesExportTest() throws MalformedURLException, URISyntaxException {
super();
}
@@ -37,35 +36,34 @@ public class TrackedRacesExportTest extends OnlineTracTracBasedTest {
}
@Before
public void setUp() throws URISyntaxException, IOException, InterruptedException, SubscriberInitializationException, CreateModelException {
URI storedUri = new URI("file:///"+new File("resources/event_20120905_erEuropean-Gold_fleet_-_race_1.mtb").getCanonicalPath().replace('\\', '/'));
super.setUp(new URL("file:///"+new File("resources/event_20120905_erEuropean-Gold_fleet_-_race_1.txt").getCanonicalPath()),
public void setUp() throws URISyntaxException, IOException, InterruptedException, SubscriberInitializationException,
CreateModelException {
URI storedUri = new URI("file:///" + new File("resources/event_20120905_erEuropean-Gold_fleet_-_race_1.mtb")
.getCanonicalPath().replace('\\', '/'));
super.setUp(
new URL("file:///"
+ new File("resources/event_20120905_erEuropean-Gold_fleet_-_race_1.txt").getCanonicalPath()),
/* liveUri */ null, /* storedUri */ storedUri,
new ReceiverType[] { ReceiverType.RACECOURSE, ReceiverType.RAWPOSITIONS, ReceiverType.MARKPASSINGS });
}
private byte[] getBytes(TrackFilesDataSource data, TrackFilesFormat format, TrackedRace race,
boolean dataBeforeAfter, boolean rawFixes) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(out);
TrackFileExporterImpl.INSTANCE.writeAllData(Collections.singletonList(data), format, Collections.singletonList(race),
true, true, zip);
zip.flush();
byte[] result = out.toByteArray();
out.close();
return result;
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
ZipOutputStream zip = new ZipOutputStream(out);
TrackFileExporterImpl.INSTANCE.writeAllData(Collections.singletonList(data), format,
Collections.singletonList(race), true, true, zip);
zip.flush();
byte[] result = out.toByteArray();
return result;
}
}
@Test
public void doAllFormatsWork() throws IOException, FormatNotSupportedException {
TrackedRace race = getTrackedRace();
for (TrackFilesFormat format : TrackFilesFormat.values()) {
byte[] data = getBytes(TrackFilesDataSource.COMPETITORS, format, race, true, true);
assertTrue(data.length > 4000);
}
}
@@ -73,10 +71,8 @@ public class TrackedRacesExportTest extends OnlineTracTracBasedTest {
@Test
public void doAllDataSourcesWork() throws IOException, FormatNotSupportedException {
TrackedRace race = getTrackedRace();
for (TrackFilesDataSource source : TrackFilesDataSource.values()) {
byte[] data = getBytes(source, TrackFilesFormat.Gpx11, race, true, true);
assertTrue(data.length > 40);
}
}