[f-spot] Add tests for reading broken sidecar fiels and broken metadata



commit 9518beafc530f43a946f752ff8eeb5aaef5ce6b0
Author: Mike Gemünde <mike gemuende de>
Date:   Thu Jul 22 13:05:29 2010 +0200

    Add tests for reading broken sidecar fiels and broken metadata
    
    The tests should ensure that broken sidecar files or broken image files
    do cause metadata reading to fail. The new tests in MetadataTest.cs
    are very similar to the ones in SidecarXmpExtensionsTests.cs, but they
    use different utility methods, which also should be tested.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=624794

 src/Utils/Makefile.am                        |    3 +-
 src/Utils/Tests/ImageTestHelper.cs           |   11 +++
 src/Utils/Tests/MetadataTest.cs              |  111 ++++++++++++++++++++++++++
 src/Utils/Tests/SidecarXmpExtensionsTests.cs |   57 +++++++++----
 tests/data/taglib-sample-broken.jpg          |  Bin 0 -> 41894 bytes
 tests/data/taglib-sample-broken.xmp          |   13 +++
 6 files changed, 177 insertions(+), 18 deletions(-)
---
diff --git a/src/Utils/Makefile.am b/src/Utils/Makefile.am
index 191d979..199e806 100644
--- a/src/Utils/Makefile.am
+++ b/src/Utils/Makefile.am
@@ -8,7 +8,7 @@ SOURCES = \
 	ConsoleCrayon.cs \
 	FileExtensions.cs \
 	GdkUtils.cs \
-    GIOTagLibFileAbstraction.cs \
+	GIOTagLibFileAbstraction.cs \
 	GtkUtil.cs \
 	HashUtils.cs \
 	Metadata.cs \
@@ -23,6 +23,7 @@ SOURCES = \
 	XdgThumbnailSpec.cs \
 	Tests/GIOTagLibFileAbstractionTests.cs \
 	Tests/ImageTestHelper.cs \
+	Tests/MetadataTest.cs \
 	Tests/SafeUriTests.cs \
 	Tests/SidecarXmpExtensionsTests.cs \
 	Tests/XdgThumbnailSpecTests.cs
diff --git a/src/Utils/Tests/ImageTestHelper.cs b/src/Utils/Tests/ImageTestHelper.cs
index 7e3f29e..4bcbc1b 100644
--- a/src/Utils/Tests/ImageTestHelper.cs
+++ b/src/Utils/Tests/ImageTestHelper.cs
@@ -20,6 +20,17 @@ namespace FSpot.Utils.Tests
             return uri2;
         }
 
+        public static SafeUri CopySidecarToTest (SafeUri uri, string filename)
+        {
+            var target = uri.ReplaceExtension (".xmp");
+
+            var orig_uri = new SafeUri (Environment.CurrentDirectory + "/../tests/data/" + filename);
+            var file = GLib.FileFactory.NewForUri (orig_uri);
+            var file2 = GLib.FileFactory.NewForUri (target);
+            file.Copy (file2, GLib.FileCopyFlags.Overwrite, null, null);
+            return target;
+        }
+
         public static void DeleteTempFile (SafeUri uri)
         {
             var file = GLib.FileFactory.NewForUri (uri);
diff --git a/src/Utils/Tests/MetadataTest.cs b/src/Utils/Tests/MetadataTest.cs
new file mode 100644
index 0000000..f5ee8ae
--- /dev/null
+++ b/src/Utils/Tests/MetadataTest.cs
@@ -0,0 +1,111 @@
+#if ENABLE_TESTS
+using NUnit.Framework;
+using System;
+using Hyena;
+using TagLib;
+using TagLib.IFD;
+using TagLib.IFD.Entries;
+using TagLib.IFD.Tags;
+using TagLib.Xmp;
+
+namespace FSpot.Utils.Tests
+{
+    [TestFixture]
+    public class MetadataTest
+    {
+        [SetUp]
+        public void Initialize () {
+            GLib.GType.Init ();
+        }
+
+        [Test]
+        public void ValidateWithoutSidecar ()
+        {
+            // Tests the file in its original state
+            var uri = ImageTestHelper.CreateTempFile ("taglib-sample.jpg");
+
+            var file = Metadata.Parse (uri);
+            Assert.IsNotNull (file);
+
+            XmpTag xmp = file.GetTag (TagTypes.XMP) as XmpTag;
+            // Xmp.MicrosoftPhoto_1_.DateAcquired (XmpText/20) "2009-08-04T20:42:36Z"
+            {
+                var node = xmp.NodeTree;
+                node = node.GetChild (XmpTag.MS_PHOTO_NS, "DateAcquired");
+                Assert.IsNotNull (node);
+                Assert.AreEqual ("2009-08-04T20:42:36Z", node.Value);
+                Assert.AreEqual (XmpNodeType.Simple, node.Type);
+                Assert.AreEqual (0, node.Children.Count);
+            }
+
+            Assert.AreEqual (new string [] { "Kirche Sulzbach" }, file.ImageTag.Keywords);
+
+            ImageTestHelper.DeleteTempFile (uri);
+        }
+
+        [Test]
+        public void ValidateWithSidecar ()
+        {
+            // Tests the file with a sidecar
+            var uri = ImageTestHelper.CreateTempFile ("taglib-sample.jpg");
+            var sidecar_uri = ImageTestHelper.CopySidecarToTest (uri, "taglib-sample.xmp");
+
+            var file = Metadata.Parse (uri);
+            Assert.IsNotNull (file);
+
+            XmpTag xmp = file.GetTag (TagTypes.XMP) as XmpTag;
+            // Xmp.MicrosoftPhoto_1_.DateAcquired (XmpText/20) "2009-08-04T20:42:36Z"
+            {
+                var node = xmp.NodeTree;
+                node = node.GetChild (XmpTag.MS_PHOTO_NS, "DateAcquired");
+                Assert.IsNull (node);
+            }
+
+            Assert.AreEqual (new string [] { "F-Spot", "metadata", "test" }, file.ImageTag.Keywords);
+
+            ImageTestHelper.DeleteTempFile (uri);
+            ImageTestHelper.DeleteTempFile (sidecar_uri);
+        }
+
+        [Test]
+        public void ValidateWithBrokenSidecar ()
+        {
+            // Tests the file with a sidecar
+            var uri = ImageTestHelper.CreateTempFile ("taglib-sample.jpg");
+            var sidecar_uri = ImageTestHelper.CopySidecarToTest (uri, "taglib-sample-broken.xmp");
+
+            var file = Metadata.Parse (uri);
+            Assert.IsNotNull (file);
+
+            XmpTag xmp = file.GetTag (TagTypes.XMP) as XmpTag;
+            // Xmp.MicrosoftPhoto_1_.DateAcquired (XmpText/20) "2009-08-04T20:42:36Z"
+            {
+                var node = xmp.NodeTree;
+                node = node.GetChild (XmpTag.MS_PHOTO_NS, "DateAcquired");
+                Assert.AreEqual ("2009-08-04T20:42:36Z", node.Value);
+                Assert.AreEqual (XmpNodeType.Simple, node.Type);
+                Assert.AreEqual (0, node.Children.Count);
+            }
+
+            Assert.AreEqual (new string [] { "Kirche Sulzbach" }, file.ImageTag.Keywords);
+
+            ImageTestHelper.DeleteTempFile (uri);
+            ImageTestHelper.DeleteTempFile (sidecar_uri);
+        }
+
+        [Test]
+        public void ValidateWithBrokenMetadata ()
+        {
+            // Tests the file with a sidecar
+            var uri = ImageTestHelper.CreateTempFile ("taglib-sample-broken.jpg");
+            var sidecar_uri = ImageTestHelper.CopySidecarToTest (uri, "taglib-sample-broken.xmp");
+
+            var file = Metadata.Parse (uri);
+            Assert.IsNull (file);
+
+            ImageTestHelper.DeleteTempFile (uri);
+            ImageTestHelper.DeleteTempFile (sidecar_uri);
+        }
+    }
+}
+#endif
diff --git a/src/Utils/Tests/SidecarXmpExtensionsTests.cs b/src/Utils/Tests/SidecarXmpExtensionsTests.cs
index fdda372..ba43dd5 100644
--- a/src/Utils/Tests/SidecarXmpExtensionsTests.cs
+++ b/src/Utils/Tests/SidecarXmpExtensionsTests.cs
@@ -26,7 +26,7 @@ namespace FSpot.Utils.Tests
             var res = new GIOTagLibFileAbstraction () { Uri = uri };
 
             var file = File.Create (res) as TagLib.Image.File;
-            Assert.IsTrue (file != null);
+            Assert.IsNotNull (file);
 
             XmpTag xmp = file.GetTag (TagTypes.XMP) as XmpTag;
             // Xmp.MicrosoftPhoto_1_.DateAcquired (XmpText/20) "2009-08-04T20:42:36Z"
@@ -50,14 +50,15 @@ namespace FSpot.Utils.Tests
             // Tests the file with a sidecar
             var uri = ImageTestHelper.CreateTempFile ("taglib-sample.jpg");
             var res = new GIOTagLibFileAbstraction () { Uri = uri };
-            var sidecar_uri = CopySidecarToTest (uri);
+            var sidecar_uri = ImageTestHelper.CopySidecarToTest (uri, "taglib-sample.xmp");
             var sidecar_res = new GIOTagLibFileAbstraction () { Uri = sidecar_uri };
 
             var file = File.Create (res) as TagLib.Image.File;
-            Assert.IsTrue (file != null);
+            Assert.IsNotNull (file);
 
             // Override by sidecar file
-            file.ParseXmpSidecar (sidecar_res);
+            bool success = file.ParseXmpSidecar (sidecar_res);
+            Assert.IsTrue (success);
 
             XmpTag xmp = file.GetTag (TagTypes.XMP) as XmpTag;
             // Xmp.MicrosoftPhoto_1_.DateAcquired (XmpText/20) "2009-08-04T20:42:36Z"
@@ -74,6 +75,38 @@ namespace FSpot.Utils.Tests
         }
 
         [Test]
+        public void ValidateWithBrokenSidecar ()
+        {
+            // Tests the file with a sidecar
+            var uri = ImageTestHelper.CreateTempFile ("taglib-sample.jpg");
+            var res = new GIOTagLibFileAbstraction () { Uri = uri };
+            var sidecar_uri = ImageTestHelper.CopySidecarToTest (uri, "taglib-sample-broken.xmp");
+            var sidecar_res = new GIOTagLibFileAbstraction () { Uri = sidecar_uri };
+
+            var file = File.Create (res) as TagLib.Image.File;
+            Assert.IsNotNull (file);
+
+            // Override by sidecar file should fail
+            bool success = file.ParseXmpSidecar (sidecar_res);
+            Assert.IsFalse (success);
+
+            XmpTag xmp = file.GetTag (TagTypes.XMP) as XmpTag;
+            // Xmp.MicrosoftPhoto_1_.DateAcquired (XmpText/20) "2009-08-04T20:42:36Z"
+            {
+                var node = xmp.NodeTree;
+                node = node.GetChild (XmpTag.MS_PHOTO_NS, "DateAcquired");
+                Assert.AreEqual ("2009-08-04T20:42:36Z", node.Value);
+                Assert.AreEqual (XmpNodeType.Simple, node.Type);
+                Assert.AreEqual (0, node.Children.Count);
+            }
+
+            Assert.AreEqual (new string [] { "Kirche Sulzbach" }, file.ImageTag.Keywords);
+
+            ImageTestHelper.DeleteTempFile (uri);
+            ImageTestHelper.DeleteTempFile (sidecar_uri);
+        }
+
+        [Test]
         public void TestSidecarWrite ()
         {
             var uri = ImageTestHelper.CreateTempFile ("taglib-sample.jpg");
@@ -86,12 +119,13 @@ namespace FSpot.Utils.Tests
             Assert.IsFalse (sidecar_file.Exists);
 
             var file = File.Create (res) as TagLib.Image.File;
-            Assert.IsTrue (file != null);
+            Assert.IsNotNull (file);
 
             file.ImageTag.Keywords = new string [] { "Kirche Aarschot" };
 
             // Validate writing of the sidecar
-            file.SaveXmpSidecar (sidecar_res);
+            bool success = file.SaveXmpSidecar (sidecar_res);
+            Assert.IsTrue (success);
             Assert.IsTrue (sidecar_file.Exists);
 
             var target = "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\"><rdf:RDF xm"
@@ -127,17 +161,6 @@ namespace FSpot.Utils.Tests
             ImageTestHelper.DeleteTempFile (uri);
             ImageTestHelper.DeleteTempFile (sidecar_uri);
         }
-
-        SafeUri CopySidecarToTest (SafeUri uri)
-        {
-            var target = uri.ReplaceExtension (".xmp");
-
-            var orig_uri = new SafeUri (Environment.CurrentDirectory + "/../tests/data/taglib-sample.xmp");
-            var file = GLib.FileFactory.NewForUri (orig_uri);
-            var file2 = GLib.FileFactory.NewForUri (target);
-            file.Copy (file2, GLib.FileCopyFlags.Overwrite, null, null);
-            return target;
-        }
     }
 }
 #endif
diff --git a/tests/data/taglib-sample-broken.jpg b/tests/data/taglib-sample-broken.jpg
new file mode 100644
index 0000000..15944eb
Binary files /dev/null and b/tests/data/taglib-sample-broken.jpg differ
diff --git a/tests/data/taglib-sample-broken.xmp b/tests/data/taglib-sample-broken.xmp
new file mode 100644
index 0000000..919f2d8
--- /dev/null
+++ b/tests/data/taglib-sample-broken.xmp
@@ -0,0 +1,13 @@
+<x:xmpmeta xmlns:x="adobe:ns:meta/">
+    <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";>
+        <rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/";>
+            <dc:subject>
+
+                    <rdf:li>F-Spot</rdf:li>
+                    <rdf:li>metadata</rdf:li>
+                    <rdf:li>test</rdf:li>
+                </rdf:Bag>
+            </dc:subject>
+        </rdf:Description>
+    </rdf:RDF>
+</x:xmpmeta>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]