Skip to content

Commit

Permalink
Exchanged use of FileInfo and DirectoryInfo. Issue fo-dicom#69
Browse files Browse the repository at this point in the history
  • Loading branch information
anders9ustafsson committed Sep 1, 2015
1 parent 9c788b0 commit 72aad33
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 9 deletions.
1 change: 1 addition & 0 deletions DICOM [Unit Tests]/DICOM [Unit Tests].csproj
Expand Up @@ -88,6 +88,7 @@
<Compile Include="Media\DicomFileScannerTest.cs" />
<Compile Include="Network\PDUTest.cs" />
<Compile Include="Logging\MessageNameFormatToOrdinalFormatTests.cs" />
<Compile Include="Printing\FilmBoxTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
13 changes: 13 additions & 0 deletions DICOM [Unit Tests]/Network/PDUTest.cs
Expand Up @@ -35,5 +35,18 @@ public void Write_AeWithNonAsciiCharacters_ShouldBeAsciified()

Assert.NotEqual(notExpected, actual);
}

[Fact]
public void Save_ToNonExistingDirectory_Succeeds()
{
var path = @".\Test Data\PDU Test";
var name = Path.Combine(path, "assoc.pdu");
if (Directory.Exists(path)) Directory.Delete(path, true);

var pdu = new RawPDU(0x01);
pdu.Save(name);

Assert.True(File.Exists(name));
}
}
}
54 changes: 54 additions & 0 deletions DICOM [Unit Tests]/Printing/FilmBoxTest.cs
@@ -0,0 +1,54 @@
// Copyright (c) 2012-2015 fo-dicom contributors.
// Licensed under the Microsoft Public License (MS-PL).

namespace Dicom.Printing
{
using System.IO;

using Xunit;

public class FilmBoxTest
{
#region Unit tests

[Fact]
public void Save_BasicFilmBox_CreatesRelevantFilesAndFolders()
{
var path = @".\Test Data\Film Box Test 1";
if (Directory.Exists(path)) Directory.Delete(path, true);
Directory.CreateDirectory(path);

var session = new FilmSession(DicomUID.BasicFilmSessionSOPClass);
var box = new FilmBox(session, null, DicomTransferSyntax.ImplicitVRLittleEndian);
box.BasicImageBoxes.Add(new ImageBox(box, DicomUID.BasicGrayscaleImageBoxSOPClass, null));
box.Save(path);

Assert.True(File.Exists(Path.Combine(path, "FilmBox.dcm")));
Assert.True(File.Exists(Path.Combine(path, "FilmBox.txt")));
Assert.True(Directory.Exists(Path.Combine(path, "Images")));
Assert.True(Directory.GetFiles(Path.Combine(path, "Images")).Length > 0);
}

[Fact]
public void Load_BasicFilmBox_ExpectedSopClassFound()
{
var path = @".\Test Data\Film Box Test 2";
if (Directory.Exists(path)) Directory.Delete(path, true);
Directory.CreateDirectory(path);

var expected = DicomUID.Generate();

var session = new FilmSession(DicomUID.BasicFilmSessionSOPClass);
var box = new FilmBox(session, expected, DicomTransferSyntax.ImplicitVRLittleEndian);
box.BasicImageBoxes.Add(new ImageBox(box, DicomUID.BasicGrayscaleImageBoxSOPClass, null));
box.Save(path);

var loaded = FilmBox.Load(session, path);
var actual = loaded.SOPInstanceUID;
Assert.Equal(expected, actual);
Assert.True(loaded.BasicImageBoxes.Count > 0);
}

#endregion
}
}
8 changes: 4 additions & 4 deletions DICOM/Network/PDU.cs
Expand Up @@ -114,16 +114,16 @@ public void WritePDU(Stream s)
/// Saves PDU to file
/// </summary>
/// <param name="file">Filename</param>
public void Save(String file)
public void Save(string file)
{
FileInfo f = new FileInfo(file);
DirectoryInfo d = f.Directory;
var f = IOManager.CreateFileReference(file);
var d = f.Directory;

if (!d.Exists)
{
d.Create();
}
using (FileStream fs = f.OpenWrite())
using (var fs = f.OpenWrite())
{
WritePDU(fs);
}
Expand Down
12 changes: 7 additions & 5 deletions DICOM/Printing/FilmBox.cs
Expand Up @@ -10,6 +10,8 @@

namespace Dicom.Printing
{
using Dicom.IO;

/// <summary>
/// Basic film box
/// </summary>
Expand Down Expand Up @@ -864,12 +866,12 @@ public void Save(string filmBoxFolder)

System.IO.File.WriteAllText(filmBoxTextFile, this.WriteToString());

var imageBoxFolderInfo = new System.IO.DirectoryInfo(string.Format(@"{0}\Images", filmBoxFolder));
var imageBoxFolderInfo = IOManager.CreateDirectoryReference(string.Format(@"{0}\Images", filmBoxFolder));
imageBoxFolderInfo.Create();
for (int i = 0; i < this.BasicImageBoxes.Count; i++)
{
var imageBox = this.BasicImageBoxes[i];
imageBox.Save(string.Format(@"{0}\I{1:000000}", imageBoxFolderInfo.FullName, i + 1));
imageBox.Save(string.Format(@"{0}\I{1:000000}", imageBoxFolderInfo.Name, i + 1));
}
}

Expand All @@ -882,10 +884,10 @@ public static FilmBox Load(FilmSession filmSession, string filmBoxFolder)

var filmBox = new FilmBox(filmSession, file.FileMetaInfo.MediaStorageSOPInstanceUID, file.Dataset);

var imagesFolder = new System.IO.DirectoryInfo(string.Format(@"{0}\Images", filmBoxFolder));
foreach (var image in imagesFolder.EnumerateFiles("*.dcm"))
var imagesFolder = IOManager.CreateDirectoryReference(string.Format(@"{0}\Images", filmBoxFolder));
foreach (var image in imagesFolder.EnumerateFileNames("*.dcm"))
{
var imageBox = ImageBox.Load(filmBox, image.FullName);
var imageBox = ImageBox.Load(filmBox, image);

filmBox.BasicImageBoxes.Add(imageBox);
}
Expand Down

0 comments on commit 72aad33

Please sign in to comment.