[fix][test] Fixed filename sanitation issue in OpenRocketSaverTest

This commit is contained in:
Daniel_M_Williams 2017-12-25 11:54:22 -05:00
parent b84de68575
commit 1fac8818b5

View File

@ -49,7 +49,7 @@ import com.google.inject.util.Modules;
public class OpenRocketSaverTest { public class OpenRocketSaverTest {
private OpenRocketSaver saver = new OpenRocketSaver(); private OpenRocketSaver saver = new OpenRocketSaver();
private static final String TMP_DIR = "./tmp/"; private static final File TMP_DIR = new File("./tmp/");
public static final String SIMULATION_EXTENSION_SCRIPT = "// Test < &\n// >\n// <![CDATA["; public static final String SIMULATION_EXTENSION_SCRIPT = "// Test < &\n// >\n// <![CDATA[";
@ -72,23 +72,19 @@ public class OpenRocketSaverTest {
injector = Guice.createInjector(Modules.override(applicationModule).with(dbOverrides), pluginModule); injector = Guice.createInjector(Modules.override(applicationModule).with(dbOverrides), pluginModule);
Application.setInjector(injector); Application.setInjector(injector);
File tmpDir = new File("./tmp"); if( !(TMP_DIR.exists() && TMP_DIR.isDirectory()) ){
if (!tmpDir.exists()) { boolean success = TMP_DIR.mkdirs();
boolean success = tmpDir.mkdirs();
if (!success) { if (!success) {
fail("Unable to create core/tmp dir needed for tests."); fail("Unable to create core/tmp dir needed for tests.");
} }
} }
} }
@After @After
public void deleteRocketFilesFromTemp() { public void deleteRocketFilesFromTemp() {
final String fileNameMatchStr = String.format("%s_.*\\.ork", this.getClass().getName()); final String fileNameMatchStr = String.format("%s_.*\\.ork", this.getClass().getName());
File directory = new File(TMP_DIR); File[] toBeDeleted = TMP_DIR.listFiles(new FileFilter() {
File[] toBeDeleted = directory.listFiles(new FileFilter() {
@Override @Override
public boolean accept(File theFile) { public boolean accept(File theFile) {
if (theFile.isFile()) { if (theFile.isFile()) {
@ -254,25 +250,24 @@ public class OpenRocketSaverTest {
} }
private File saveRocket(OpenRocketDocument rocketDoc, StorageOptions options) { private File saveRocket(OpenRocketDocument rocketDoc, StorageOptions options) {
String fileName = String.format(TMP_DIR + "%s_%s.ork", this.getClass().getName(), rocketDoc.getRocket().getName()); File file = null;
File file = new File(fileName);
OutputStream out = null; OutputStream out = null;
try { try {
file = File.createTempFile( TMP_DIR.getName(), ".ork");
out = new FileOutputStream(file); out = new FileOutputStream(file);
this.saver.save(out, rocketDoc, options); this.saver.save(out, rocketDoc, options);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
fail("FileNotFound saving file " + fileName + ": " + e.getMessage()); fail("FileNotFound saving temp file in: " + TMP_DIR.getName() + ": " + e.getMessage());
} catch (IOException e) { } catch (IOException e) {
fail("IOException saving file " + fileName + ": " + e.getMessage()); fail("IOException saving temp file in: " + TMP_DIR.getName() + ": " + e.getMessage());
} }finally {
try {
try { if (out != null) {
if (out != null) { out.close();
out.close(); }
} catch (IOException e) {
fail("Unable to close output stream for temp file in " + TMP_DIR.getName() + ": " + e.getMessage());
} }
} catch (IOException e) {
fail("Unable to close output stream for file " + fileName + ": " + e.getMessage());
} }
return file; return file;