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.");
            }
        }
Ejemplo n.º 3
0
        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.");
        }
        /// <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);
        }