Skip to content

Commit

Permalink
Updates to unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahsan Cheema committed Mar 22, 2024
1 parent 1bd2ac0 commit 761451a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// https://drewnoakes.com/code/exif/
//
#endregion
using JpegXmpWritePluginMDE.MetadataExtractor;
using JpegXmpWritePluginMDE.MetadataExtractor.Formats.Jpeg;
using MetadataExtractor.Formats.Jpeg;
using MetadataExtractor.IO;
Expand Down Expand Up @@ -93,14 +94,16 @@ public void TestUpdateJpegFragments_FailsOnUnknownMetadataObject()
[Fact]
public void TestWriteJpegMetadata()
{
var originalStream = TestDataUtil.OpenRead("Data/xmpWriting_PictureWithMicrosoftXmp.jpg");
IXmpMeta xmp = XmpMetaFactory.ParseFromString(File.ReadAllText("Data/xmpWriting_XmpContent.xmp"));
byte[] expectedResult = TestDataUtil.GetBytes("Data/xmpWriting_PictureWithMicrosoftXmpReencoded.jpg");
string writeToFile = TestDataUtil.GetFullTestFilePath("xmpWriting_PictureWithMicrosoftXmp.jpg");
string xmpToWrite = TestDataUtil.GetFullTestFilePath("xmpWriting_XmpContent.xmp");
string expectedOutputFile = TestDataUtil.GetFullTestFilePath("xmpWriting_PictureWithMicrosoftXmpReencoded.jpg");

var metadata_objects = new object[] { xmp };
var updatedStream = JpegMetadataWriter.WriteMetadata(originalStream, metadata_objects);
IXmpMeta xmp = XmpMetaFactory.ParseFromString(File.ReadAllText(xmpToWrite));
byte[] expectedResult = TestDataUtil.GetBytes(expectedOutputFile);

var actualResult = updatedStream.ToArray();
var metadata_objects = new object[] { xmp };
ImageMetadataWriter.WriteMetadata(writeToFile, metadata_objects);
byte[] actualResult = TestDataUtil.GetBytes(writeToFile);

Assert.True(actualResult.SequenceEqual(expectedResult));
}
Expand Down
14 changes: 9 additions & 5 deletions JpegXmpWritePluginMDE.Tests/ImageMetadataWriterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@ public sealed class ImageMetadataWriterTest
[Fact]
public void TestWriteImageMetadata()
{
var originalStream = TestDataUtil.OpenRead("Data/xmpWriting_PictureWithMicrosoftXmp.jpg");
IXmpMeta xmp = XmpMetaFactory.ParseFromString(File.ReadAllText("Data/xmpWriting_XmpContent.xmp"));
byte[] expectedResult = TestDataUtil.GetBytes("Data/xmpWriting_PictureWithMicrosoftXmpReencoded.jpg");
string writeToFile = TestDataUtil.CreateTestCopy("xmpWriting_PictureWithMicrosoftXmp.jpg");
string xmpToWrite = TestDataUtil.GetFullTestFilePath("xmpWriting_XmpContent.xmp");
string expectedOutputFile = TestDataUtil.GetFullTestFilePath("xmpWriting_PictureWithMicrosoftXmpReencoded.jpg");

IXmpMeta xmp = XmpMetaFactory.ParseFromString(File.ReadAllText(xmpToWrite));
byte[] expectedResult = TestDataUtil.GetBytes(expectedOutputFile);

var metadata_objects = new object[] { xmp };
var updatedStream = ImageMetadataWriter.WriteMetadata(originalStream, metadata_objects);
ImageMetadataWriter.WriteMetadata(writeToFile, metadata_objects);
byte[] actualResult = TestDataUtil.GetBytes(writeToFile);

var actualResult = updatedStream.ToArray();
TestDataUtil.DeleteTestFile(writeToFile);

Assert.True(actualResult.SequenceEqual(expectedResult));
}
Expand Down
40 changes: 40 additions & 0 deletions JpegXmpWritePluginMDE.Tests/TestDataUtil.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) Drew Noakes and contributors. All Rights Reserved. Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

using System.Reflection;

namespace JpegXmpWritePluginMDE.Tests
{
/// <summary>Utility functions for working with unit tests data files.</summary>
Expand All @@ -15,5 +17,43 @@ internal static class TestDataUtil
public static Stream OpenRead(string filePath) => new FileStream(GetPath(filePath), FileMode.Open, FileAccess.Read, FileShare.Read);

public static byte[] GetBytes(string filePath) => File.ReadAllBytes(GetPath(filePath));
/// <summary>
/// Gets path of the executing assembly
/// </summary>
/// <returns></returns>
public static string GetTestExecutionPath() => Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

Check warning on line 24 in JpegXmpWritePluginMDE.Tests/TestDataUtil.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 24 in JpegXmpWritePluginMDE.Tests/TestDataUtil.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
/// <summary>
/// Gets the path to the test file
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static string GetFullTestFilePath(string fileName)
{
return Path.Combine(GetTestExecutionPath(), "Data", fileName);
}
/// <summary>
/// Creates copy of a test file to avoid multiple unit tests accessing the same file for writing
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static string CreateTestCopy(string fileName)
{
string destinationFilePath = GetTestExecutionPath() + "\\" + "Data\\"+ fileName + Guid.NewGuid().ToString();
string originalFilePath = GetFullTestFilePath(fileName);
File.Copy(originalFilePath, destinationFilePath);
return destinationFilePath;
}
/// <summary>
/// Delete the test file
/// </summary>
/// <param name="fileName"></param>
public static void DeleteTestFile (string fileName)
{
if (File.Exists(fileName))
{
File.Delete(fileName);
}

}
}
}

0 comments on commit 761451a

Please sign in to comment.