Example #1
0
        public void Validate()
        {
            // Load the sample file data
            file = new AFPFile(testFilePath, false);

            // Ensure it validates
            Assert.IsTrue(file.EncodeData().Any());

            // Remove the first container information from all fields it contains
            Container c = file.Fields.First(f => f.LowestLevelContainer != null).LowestLevelContainer;

            foreach (DataStructure s in c.Structures)
            {
                s.Containers.Remove(c);
            }

            // it should no longer validate
            Assert.IsFalse(file.EncodeData().Any());

            // Reload data
            file = new AFPFile(testFilePath, false);

            // Surround the file with BPF/EPF tags
            file.AddField(StructuredField.New <BPF>(), 0);
            file.AddField(StructuredField.New <EPF>(), file.Fields.Count);

            // Check it still validates (container info should have been updated)
            Assert.IsTrue(file.EncodeData().Any());

            // Try adding a TLE field to the top of the file
            TLE newTLE = StructuredField.New <TLE>();

            file.AddField(newTLE, 0);

            // Ensure it does not validate
            Assert.IsFalse(file.EncodeData().Any());

            // Delete that field, and add it in a place where it is allowed to be (in this case, after a BPG)
            file.DeleteField(newTLE);
            int newIndex = 0;

            for (int i = 0; i < file.Fields.Count; i++)
            {
                if (file.Fields[i] is BPG)
                {
                    newIndex = i + 1; break;
                }
            }
            file.AddField(newTLE, newIndex);

            Assert.IsTrue(file.EncodeData().Any());
        }
Example #2
0
        public void DeleteFieldsSuccessfully()
        {
            // Load file (ignore data)
            file = new AFPFile(testFilePath, false);

            // Delete all presentation text containers
            List <StructuredField> textFields = file.Fields.Where(f => f.LowestLevelContainer != null &&
                                                                  f.LowestLevelContainer.Structures[0].GetType() == typeof(BPT)).ToList();

            Assert.IsTrue(textFields.Any());
            foreach (StructuredField f in textFields)
            {
                file.DeleteField(f);
            }

            // Make sure they are gone
            Assert.IsFalse(file.Fields.Any(f => f.LowestLevelContainer != null && f.LowestLevelContainer.Structures[0].GetType() == typeof(BPT)));
        }
Example #3
0
        public void UpdateContainerInfo_WhenFieldIsAddedOrDeleted()
        {
            // Load a file (ignore data), check the first NOP field
            file = new AFPFile(testFilePath, false);
            NOP foundNOP = file.Fields.OfType <NOP>().First();

            // Get the container of this field for future assertions, and delete the field
            Container NOPContainer = foundNOP.LowestLevelContainer;

            file.DeleteField(foundNOP);

            // Ensure that no containers have the deleted structure
            foreach (Container c in file.Fields.Select(f => f.LowestLevelContainer).Distinct())
            {
                Assert.IsFalse(c.Structures.Contains(foundNOP));
            }

            // Create a new NOP field and insert it after the first detected field with a container
            NOP newNOP = StructuredField.New <NOP>();

            // Store the insert index
            int insertIndex = 0;

            for (int i = 0; i < file.Fields.Count; i++)
            {
                if (file.Fields[i].LowestLevelContainer != null)
                {
                    insertIndex = i + 1;
                    break;
                }
            }
            file.AddField(newNOP, insertIndex);

            // Ensure the new field has the expected container
            Assert.AreEqual(NOPContainer, newNOP.LowestLevelContainer);
        }