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
@@ -4,11 +4,15 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Collection;
import org.apache.commons.fileupload.FileItem;
import org.junit.Test;
import com.sap.sailing.domain.common.tracking.GPSFix;
@@ -30,32 +34,33 @@ public class GPSFixImportTest {
@Test
public void testReReadingEOFFromBufferedInputStream() throws IOException {
InputStream in = getClass().getResourceAsStream("/Cardiff Race17 - COMPETITORS.gpx");
BufferedInputStreamWithPublicBuffer bis = new BufferedInputStreamWithPublicBuffer(in);
bis.mark(10*1024*1024); // the file has ~2MB, so 10MB is much more than twice the size, so should never be required
int size = 0;
while (bis.read() != -1) {
size++;
try (InputStream in = getClass().getResourceAsStream("/Cardiff Race17 - COMPETITORS.gpx");
BufferedInputStreamWithPublicBuffer bis = new BufferedInputStreamWithPublicBuffer(in)) {
bis.mark(10 * 1024 * 1024); // the file has ~2MB, so 10MB is much more than twice the size, so should never
// be required
int size = 0;
while (bis.read() != -1) {
size++;
}
bis.reset();
int secondSize = 0;
while (bis.read() != -1) {
secondSize++;
}
assertEquals(size, secondSize);
assertTrue(bis.getBuffer().length < 8 * 1024 * 1024);
bis.reset();
int thirdSize = 0;
while (bis.read() != -1) {
thirdSize++;
}
assertEquals(size, thirdSize);
assertTrue(bis.getBuffer().length < 8 * 1024 * 1024);
// Now try to read EOF multiple times
for (int i = 1; i < 1000; i++) {
assertEquals(-1, bis.read());
}
}
bis.reset();
int secondSize = 0;
while (bis.read() != -1) {
secondSize++;
}
assertEquals(size, secondSize);
assertTrue(bis.getBuffer().length < 8*1024*1024);
bis.reset();
int thirdSize = 0;
while (bis.read() != -1) {
thirdSize++;
}
assertEquals(size, thirdSize);
assertTrue(bis.getBuffer().length < 8*1024*1024);
// Now try to read EOF multiple times
for (int i=1; i<1000; i++) {
assertEquals(-1, bis.read());
}
bis.close();
}
@Test
@@ -73,8 +78,81 @@ public class GPSFixImportTest {
public void storeFix(GPSFix fix, DeviceIdentifier deviceIdentifier) {
}
};
InputStream in = getClass().getResourceAsStream("/Cardiff Race17 - COMPETITORS.gpx");
servlet.importFiles(Arrays.asList(new Pair<>("test.gpx", in)), new AlwaysFailingGPSFixImporter(-1));
FileItem fi = new FileItem() {
private static final long serialVersionUID = 1L;
@Override
public void write(File file) throws Exception {
}
@Override
public void setFormField(boolean state) {
}
@Override
public void setFieldName(String name) {
}
@Override
public boolean isInMemory() {
return false;
}
@Override
public boolean isFormField() {
return false;
}
@Override
public String getString(String encoding) throws UnsupportedEncodingException {
return null;
}
@Override
public String getString() {
return null;
}
@Override
public long getSize() {
return 0;
}
@Override
public OutputStream getOutputStream() throws IOException {
return null;
}
@Override
public String getName() {
return "Cardiff Race17 - COMPETITORS.gpx";
}
@Override
public InputStream getInputStream() throws IOException {
return getClass().getResourceAsStream("/" + getName());
}
@Override
public String getFieldName() {
return null;
}
@Override
public String getContentType() {
return null;
}
@Override
public byte[] get() {
return null;
}
@Override
public void delete() {
}
};
servlet.importFiles(Arrays.asList(new Pair<>("test.gpx", fi)), new AlwaysFailingGPSFixImporter(-1));
// getting to here without errors is good enough
}
}
@@ -63,12 +63,6 @@ public class SensorDataImportServlet extends AbstractFileUploadServlet {
for (Pair<String, FileItem> file : files) {
final String requestedImporterName = file.getA();
final FileItem fi = file.getB();
BufferedInputStream in = new BufferedInputStream(fi.getInputStream()) {
@Override
public void close() throws IOException {
// prevent importers from closing this stream
}
};
DoubleVectorFixImporter importerToUse = null;
for (DoubleVectorFixImporter candidate : availableImporters) {
if (candidate.getType().equals(requestedImporterName)) {
@@ -77,43 +71,37 @@ public class SensorDataImportServlet extends AbstractFileUploadServlet {
}
}
if (importerToUse == null) {
in.close();
throw new RuntimeException("Sensor importer not found");
}
in.mark(READ_BUFFER_SIZE);
try {
in.reset();
} catch (IOException e1) {
logger.log(Level.SEVERE, "Could not reset stream", e1);
}
logger.log(Level.INFO,
"Going to import sensor data file with importer " + importerToUse.getClass().getSimpleName());
try {
importerToUse.importFixes(in, new DoubleVectorFixImporter.Callback() {
@Override
public void addFixes(Iterable<DoubleVectorFix> fixes, TrackFileImportDeviceIdentifier device) {
deviceIds.add(device);
storeFixes(fixes, device);
TimePoint earliestFixSoFarFromCurrentDevice = from.get(device);
TimePoint latestFixSoFarFromCurrentDevice = to.get(device);
for (DoubleVectorFix fix : fixes) {
if (earliestFixSoFarFromCurrentDevice == null
|| earliestFixSoFarFromCurrentDevice.after(fix.getTimePoint())) {
earliestFixSoFarFromCurrentDevice = fix.getTimePoint();
from.put(device, earliestFixSoFarFromCurrentDevice);
}
if (latestFixSoFarFromCurrentDevice == null
|| latestFixSoFarFromCurrentDevice.before(fix.getTimePoint())) {
latestFixSoFarFromCurrentDevice = fix.getTimePoint();
to.put(device, latestFixSoFarFromCurrentDevice);
try (BufferedInputStream in = new BufferedInputStream(fi.getInputStream())) {
try {
importerToUse.importFixes(in, new DoubleVectorFixImporter.Callback() {
@Override
public void addFixes(Iterable<DoubleVectorFix> fixes, TrackFileImportDeviceIdentifier device) {
deviceIds.add(device);
storeFixes(fixes, device);
TimePoint earliestFixSoFarFromCurrentDevice = from.get(device);
TimePoint latestFixSoFarFromCurrentDevice = to.get(device);
for (DoubleVectorFix fix : fixes) {
if (earliestFixSoFarFromCurrentDevice == null
|| earliestFixSoFarFromCurrentDevice.after(fix.getTimePoint())) {
earliestFixSoFarFromCurrentDevice = fix.getTimePoint();
from.put(device, earliestFixSoFarFromCurrentDevice);
}
if (latestFixSoFarFromCurrentDevice == null
|| latestFixSoFarFromCurrentDevice.before(fix.getTimePoint())) {
latestFixSoFarFromCurrentDevice = fix.getTimePoint();
to.put(device, latestFixSoFarFromCurrentDevice);
}
}
}
}
}, fi.getName(), requestedImporterName);
logger.log(Level.INFO, "Successfully imported file " + requestedImporterName);
} catch (FormatNotSupportedException e) {
logger.log(Level.INFO, "Failed to import file " + requestedImporterName);
}, fi.getName(), requestedImporterName);
logger.log(Level.INFO, "Successfully imported file " + requestedImporterName);
} catch (FormatNotSupportedException e) {
logger.log(Level.INFO, "Failed to import file " + requestedImporterName);
}
}
}
return deviceIds;
@@ -2,18 +2,17 @@ package com.sap.sailing.server.gateway.trackfiles.impl;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -42,7 +41,8 @@ import com.sap.sse.common.Util.Pair;
* The available importers are tried one by one in the following order, until the first one is found that does not fail
* with an {@link FormatNotSupportedException}:
* <ul>
* <li>If the type of a {@link #PREFERRED_IMPORTER preferred importer} is transmitted, this is the first that is used.</li>
* <li>If the type of a {@link #PREFERRED_IMPORTER preferred importer} is transmitted, this is the first that is used.
* </li>
* <li>Then the importers registered for a matching {@link GPSFixImporter#FILE_EXTENSION_PROPERTY file extension} are
* used.</li>
* <li>If all this fails, all other available importers are used.</li>
@@ -55,8 +55,6 @@ public class TrackFilesImportServlet extends AbstractFileUploadServlet {
public static final String PREFERRED_IMPORTER = "preferredImporter";
private static final long serialVersionUID = 1120226743039934620L;
private static final Logger logger = Logger.getLogger(TrackFilesImportServlet.class.getName());
private static final int READ_BUFFER_SIZE = 1024 * 1024 * 1024;
public void storeFix(GPSFix fix, DeviceIdentifier deviceIdentifier) {
try {
@@ -84,69 +82,67 @@ public class TrackFilesImportServlet extends AbstractFileUploadServlet {
}
return result;
}
Iterable<TrackFileImportDeviceIdentifier> importFiles(Iterable<Pair<String, InputStream>> files, GPSFixImporter preferredImporter)
throws IOException {
protected Iterable<TrackFileImportDeviceIdentifier> importFiles(Iterable<Pair<String, FileItem>> files,
GPSFixImporter preferredImporter) throws IOException {
final Set<TrackFileImportDeviceIdentifier> deviceIds = new HashSet<>();
final Map<DeviceIdentifier, TimePoint> from = new HashMap<>();
final Map<DeviceIdentifier, TimePoint> to = new HashMap<>();
for (Pair<String, InputStream> file : files) {
final String fileName = file.getA();
for (Pair<String, FileItem> pair : files) {
final String fileName = pair.getA();
final FileItem fileItem = pair.getB();
String fileExt = null;
if (fileName.contains(".")) {
fileExt = fileName.substring(fileName.lastIndexOf(".") + 1);
}
Collection<GPSFixImporter> importersToTry = new LinkedHashSet<>();
Set<GPSFixImporter> importersToTry = new LinkedHashSet<>();
if (preferredImporter != null) {
importersToTry.add(preferredImporter);
}
importersToTry.addAll(getGPSFixImporters(fileExt));
importersToTry.addAll(getGPSFixImporters(null));
BufferedInputStream in = new BufferedInputStream(file.getB()) {
@Override
public void close() throws IOException {
//prevent importers from closing this stream
}
};
in.mark(READ_BUFFER_SIZE);
boolean done = false;
Iterator<GPSFixImporter> iter = importersToTry.iterator();
while (iter.hasNext() && ! done) {
try {
in.reset();
} catch (IOException e1) {
e1.printStackTrace();
}
boolean failed = false;
GPSFixImporter importer = iter.next();
logger.log(Level.INFO, "Trying to import file " + fileName + " with importer " + importer.getType());
try {
importer.importFixes(in, new Callback() {
@Override
public void addFix(GPSFix fix, TrackFileImportDeviceIdentifier device) {
deviceIds.add(device);
storeFix(fix, device);
TimePoint earliestFixSoFarFromCurrentDevice = from.get(device);
if (earliestFixSoFarFromCurrentDevice == null || earliestFixSoFarFromCurrentDevice.after(fix.getTimePoint())) {
earliestFixSoFarFromCurrentDevice = fix.getTimePoint();
from.put(device, earliestFixSoFarFromCurrentDevice);
}
TimePoint latestFixSoFarFromCurrentDevice = to.get(device);
if (latestFixSoFarFromCurrentDevice == null || latestFixSoFarFromCurrentDevice.before(fix.getTimePoint())) {
latestFixSoFarFromCurrentDevice = fix.getTimePoint();
to.put(device, latestFixSoFarFromCurrentDevice);
}
}
}, true, fileName);
} catch (FormatNotSupportedException e) {
failed = true;
logger.log(Level.INFO,
"System knows " +
importersToTry.size() +
" importers: "+
importersToTry.stream().map(i -> i.getType()).collect(Collectors.joining(", "))
);
parsersLoop: for (GPSFixImporter importer : importersToTry) {
boolean succeeded = false;
logger.log(Level.INFO, "Trying to import file " + fileName + " with importer " + importer.getType());
try (BufferedInputStream in = new BufferedInputStream(fileItem.getInputStream())) {
try {
importer.importFixes(in, new Callback() {
@Override
public void addFix(GPSFix fix, TrackFileImportDeviceIdentifier device) {
deviceIds.add(device);
storeFix(fix, device);
TimePoint earliestFixSoFarFromCurrentDevice = from.get(device);
if (earliestFixSoFarFromCurrentDevice == null
|| earliestFixSoFarFromCurrentDevice.after(fix.getTimePoint())) {
earliestFixSoFarFromCurrentDevice = fix.getTimePoint();
from.put(device, earliestFixSoFarFromCurrentDevice);
}
TimePoint latestFixSoFarFromCurrentDevice = to.get(device);
if (latestFixSoFarFromCurrentDevice == null
|| latestFixSoFarFromCurrentDevice.before(fix.getTimePoint())) {
latestFixSoFarFromCurrentDevice = fix.getTimePoint();
to.put(device, latestFixSoFarFromCurrentDevice);
}
}
}, true, fileName);
succeeded = true;
} catch (Exception e) {
logger.log(Level.INFO, "Failed with " + e.getClass().getSimpleName()
+ " while importing file using " + importer.getType());
}
}
if (! failed) {
done = true;
logger.log(Level.INFO, "Successfully imported file " + fileName);
if (succeeded) {
logger.log(Level.INFO, "Successfully imported file " + fileName + " using " + importer.getType());
break parsersLoop;
}
}
}
@@ -156,10 +152,10 @@ public class TrackFilesImportServlet extends AbstractFileUploadServlet {
@Override
protected void process(List<FileItem> fileItems, HttpServletRequest req, HttpServletResponse resp) throws IOException {
String prefImporterType = null;
List<Pair<String, InputStream>> files = new ArrayList<>();
List<Pair<String, FileItem>> files = new ArrayList<>();
for (FileItem item : fileItems) {
if (!item.isFormField())
files.add(new Pair<String, InputStream>(item.getName(), item.getInputStream()));
files.add(new Pair<String, FileItem>(item.getName(), item));
else {
if (item.getFieldName() != null && item.getFieldName().equals(PREFERRED_IMPORTER)) {
prefImporterType = item.getString();
@@ -167,18 +163,15 @@ public class TrackFilesImportServlet extends AbstractFileUploadServlet {
}
}
GPSFixImporter preferredImporter = null;
if (prefImporterType != null && ! prefImporterType.isEmpty()) {
preferredImporter = getServiceFinderFactory().createServiceFinder(GPSFixImporter.class).
findService(prefImporterType);
if (prefImporterType != null && !prefImporterType.isEmpty()) {
preferredImporter = getServiceFinderFactory().createServiceFinder(GPSFixImporter.class)
.findService(prefImporterType);
}
final Iterable<TrackFileImportDeviceIdentifier> mappingList = importFiles(files, preferredImporter);
//setJsonResponseHeader(resp);
//DO NOT set a JSON response header. This causes the browser to wrap the response in a
//<pre> tag when uploading from GWT, as this is an AJAX-request inside an iFrame.
// setJsonResponseHeader(resp);
// DO NOT set a JSON response header. This causes the browser to wrap the response in a
// <pre> tag when uploading from GWT, as this is an AJAX-request inside an iFrame.
resp.setContentType("text/html");
for (TrackFileImportDeviceIdentifier mapping : mappingList) {
String stringRep = mapping.getId().toString();
resp.getWriter().println(stringRep);
@@ -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);
}
}