public void Process_An_Valid_Package()
        {
            StreamResourceInfo info = App.GetResourceStream(new Uri(Config.ResourcePath_Valid30Package, UriKind.Relative));
            Stream zipStream = info.Stream;

            // Create a package inspector for the package
            PackageInspector inspector = new PackageInspector();
            inspector.ProcessPackage(zipStream);

            Assert.IsTrue(inspector.IsValid && inspector.Package != null);
        }
        /// <summary>
        /// Helper method to process an invalid package 
        /// </summary>
        /// <param name="stream"></param>
        private void ProcessInvalidPackage(Stream zipStream)
        {
            try
            {
                // Create a package inspector for the package
                PackageInspector inspector = new PackageInspector();
                inspector.ProcessPackage(zipStream);

                Assert.Fail("No exception was raised when an invalid stream was processed.");
            }
            catch (InvalidPackageException)
            {
                // Expected result
                Assert.IsTrue(true);
            }
            catch
            {
                Assert.Fail("Unexpected Exception when processing an invalid package.");
            }
        }
        public void Load_Valid_Book_Into_FileSystem()
        {
            StreamResourceInfo info = App.GetResourceStream(new Uri(Config.ResourcePath_Valid30Package, UriKind.Relative));
            Stream zipStream = info.Stream;

            // Create a package inspector for the package
            PackageInspector inspector = new PackageInspector();
            inspector.ProcessPackage(zipStream);

            // Used by subsequent tests
            BookId = inspector.Package.BookId;

            IDirectory directory = Context.LoadZipPackage(inspector, false);

            // Verify that all the files have been saved
            int fileCount = inspector.Package.FileCount;
            int actualFileCount = FileCount(directory);

            // Can't actually use this as some of the opf's have unreliable lists of files
            //Assert.IsTrue(actualFileCount == fileCount, "Number of files in the zip package does not match the number of files loaded into FileSystem.");
            Assert.IsTrue(actualFileCount > 0, "No files have been loaded.");
        }
        public void Access_Properties_Without_Processing()
        {
            // Create a package inspector for the package
            PackageInspector inspector = new PackageInspector();

            try
            {
                // Attempt to access the IsValid property
                if (inspector.IsValid)
                {
                    // Should never get here because IsValid should raise an exception
                    Assert.Fail("PackageInspector.IsValid should raise an exception if called without processing the stream first.");
                }
            }
            catch (InvalidOperationException)
            {
                Assert.IsTrue(true);
            }
            catch
            {
                Assert.Fail("Unexpected exception");
            }
        }
 public void Process_From_An_Null_Stream()
 {
     // Create a package inspector for the package
     PackageInspector inspector = new PackageInspector();
     ProcessInvalidPackage(null);
 }
        /// <summary>
        /// Initiates the processing of loading a book (from a zip stream) into the FileSystem, by loading and creating
        /// the book package and passing that off to the datacontext to load the rest of the book.
        /// </summary>
        /// <param name="zipStream">FileStream of the zip file to load the book from.</param>
        private void LoadBook(FileStream zipStream)
        {
            // The DataContext is instatiated with a IFileSystem implementation that will determine
            // where the books are stored to and retrieved from. For example, IsolatedStorage/ServerStorage
            DataContext store = new DataContext(base.MainState.BookFileSystem);

            // Listen to the progress the store DataContext makes
            store.ProgressChanged += store_ProgressChanged;

            IPackageInspector inspector;

            // To ensure the FileStream is property disposed wrap it in
            // a using statement.
            using (FileStream fileStream = zipStream)
            {
                inspector = new PackageInspector();

                // The inspector will pass on any progress updates to this event. We subscribe to
                // it from the View to be able to update the display.
                inspector.ProgressChanged += inspector_ProgressChanged;

                // Process the package - this will perform an initial parse over the zip file
                // to derive some basic information like its version, the bookId and Filecount.
                // This method will raise some ProgressChanged events for us to update the UI.
                inspector.ProcessPackage(fileStream);

                // Check the file was OK.
                if(!inspector.IsValid)
                {
                    // TODO: Could be enhanced to return all the errors.
                    // While there could actally be more than one error in ValidationErrors
                    // we are only going to notify of the first.
                    throw new InvalidPackageException(inspector.ValidationErrors[0]);
                }
                else
                {
                    // Save the book files to the store - don't bother replacing if the
                    // book is already there. Again this will take some time and will raise
                    // ProgressChanged events for us to update the UI with progress.
                    store.LoadZipPackage(inspector, false);
                }
            }

            // Set the CurrentBook on View
            SetCurrentBook(inspector.Package);
        }