Skip to content

Commit

Permalink
[DAQ-105] create parent dir for nexus file if it doesn't exist
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Dickie <matthew.dickie@diamond.ac.uk>
  • Loading branch information
mpdickie committed May 23, 2016
1 parent 23ae97e commit f075e76
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class DefaultNexusFileBuilderTest {

private static String filePath;

private static String fileInSubDirPath;

private NexusFileBuilder nexusFileBuilder;

@BeforeClass
Expand All @@ -41,6 +43,7 @@ public static void setUpBeforeClass() throws Exception {
DefaultNexusFileBuilderTest.class.getSimpleName());
TestUtils.makeScratchDirectory(testScratchDirectoryName);
filePath = testScratchDirectoryName + fileName;
fileInSubDirPath = testScratchDirectoryName + "subdir/" + filePath;
}

@Before
Expand All @@ -65,6 +68,24 @@ public void testCreateAndOpenFile() throws NexusException {
assertThat(entry.getTitleScalar(), equalTo("test"));
}

@Test
public void testCreateAndOpenFileInSubDir() throws NexusException {
ServiceHolder.setNexusFileFactory(new NexusFileFactoryHDF5());
NexusFileBuilder nexusSubdirFileBuilder = new DefaultNexusFileBuilder(fileInSubDirPath);
NexusEntryBuilder nexusEntryBuilder = nexusSubdirFileBuilder.newEntry();

nexusEntryBuilder.getNXentry().setTitleScalar("test");
nexusSubdirFileBuilder.createFile();

TreeFile nexusFile = NexusTestUtils.loadNexusFile(fileInSubDirPath, true);
assertThat(nexusFile, notNullValue());
NXroot root = (NXroot) nexusFile.getGroupNode();
assertThat(nexusFile.getGroupNode(), notNullValue());
NXentry entry = root.getEntry();
assertThat(entry, notNullValue());
assertThat(entry.getTitleScalar(), equalTo("test"));
}

@Test
public void testGetNexusTree() {
TreeFile nexusFile = nexusFileBuilder.getNexusTree();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

package org.eclipse.dawnsci.nexus.builder.impl;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -127,6 +128,12 @@ public NexusScanFile createFile() throws NexusException {

final String filename = treeFile.getFilename();

// create the parent dir if it doesn't exist
File parentDir = new File(filename).getParentFile();
if (!parentDir.exists()) {
parentDir.mkdirs();
}

// create and open the nexus file
final INexusFileFactory nexusFileFactory = ServiceHolder.getNexusFileFactory();
try (NexusFile nexusFile = nexusFileFactory.newNexusFile(filename, true)) {
Expand Down

0 comments on commit f075e76

Please sign in to comment.