diff --git a/src/main/java/com/conveyal/datatools/manager/persistence/FeedStore.java b/src/main/java/com/conveyal/datatools/manager/persistence/FeedStore.java index af3fad226..8ef19f471 100644 --- a/src/main/java/com/conveyal/datatools/manager/persistence/FeedStore.java +++ b/src/main/java/com/conveyal/datatools/manager/persistence/FeedStore.java @@ -209,11 +209,12 @@ private void copyVersionToLatest(File version, FeedSource feedSource) throws IOE FileUtils.copyFile(version, latest, true); } - private File createTempFile (String name, InputStream in) throws IOException { - Path path = Files.createTempFile(name, null); + protected File createTempFile (String name, InputStream in) throws IOException { + // Create temp file in such a way that filename is preserved (no tmp suffix added). + final File tempFile = new File(new File(System.getProperty("java.io.tmpdir")), name); + LOG.info("Storing temp GTFS file at {}", tempFile.getAbsolutePath()); // FIXME: Figure out how to manage temp files created here. Currently, we just call deleteOnExit, but // this will only delete the file once the java process stops. - File tempFile = path.toFile(); tempFile.deleteOnExit(); ByteStreams.copy(in, new FileOutputStream(tempFile)); return tempFile; diff --git a/src/test/java/com/conveyal/datatools/TestUtils.java b/src/test/java/com/conveyal/datatools/TestUtils.java index 471540776..ba450405b 100644 --- a/src/test/java/com/conveyal/datatools/TestUtils.java +++ b/src/test/java/com/conveyal/datatools/TestUtils.java @@ -74,7 +74,7 @@ public static FeedVersion createFeedVersionFromGtfsZip(FeedSource source, String /** * Helper to get a File for the given file or folder that should be in the gtfs folder of the test resources */ - private static String getGtfsResourcePath(String gtfsFileName) { + public static String getGtfsResourcePath(String gtfsFileName) { return TestUtils.class.getResource("gtfs/" + gtfsFileName).getFile(); } diff --git a/src/test/java/com/conveyal/datatools/manager/persistence/FeedStoreTest.java b/src/test/java/com/conveyal/datatools/manager/persistence/FeedStoreTest.java new file mode 100644 index 000000000..a7106e2f0 --- /dev/null +++ b/src/test/java/com/conveyal/datatools/manager/persistence/FeedStoreTest.java @@ -0,0 +1,39 @@ +package com.conveyal.datatools.manager.persistence; + +import com.conveyal.datatools.DatatoolsTest; +import com.conveyal.datatools.UnitTest; +import com.conveyal.datatools.manager.models.FeedVersion; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; + +import static com.conveyal.datatools.TestUtils.getGtfsResourcePath; + +public class FeedStoreTest extends UnitTest { + private static final Logger LOG = LoggerFactory.getLogger(FeedStoreTest.class); + + @BeforeClass + public static void setUp() throws Exception { + DatatoolsTest.setUp(); + LOG.info("{} setup", FeedStoreTest.class.getSimpleName()); + } + + /** + * Verify that {@link FeedStore} will write a temp file with the input file name. + */ + @Test + public void canCreateTempGtfsFile() throws IOException { + final String gtfsFileName = "gtfs.zip"; + File gtfsFile = new File(getGtfsResourcePath("bart_new.zip")); + FileInputStream fileInputStream = new FileInputStream(gtfsFile); + File tempFile = FeedVersion.feedStore.createTempFile(gtfsFileName, fileInputStream); + LOG.info("Feed store wrote temp file to: {}", tempFile.getAbsolutePath()); + Assert.assertEquals(tempFile.getName(), gtfsFileName); + } +}