Пример #1
0
        public void XNameAndCollectionObjectConstructor()
        {
            XElement contact = new XElement(
                "contacts",
                new XElement("contact1", "jane"),
                new XElement("contact2", "john"));
            List <object> list = new List <object>();

            list.Add(contact.Element("contact1"));
            list.Add(contact.Element("contact2"));
            XStreamingElement streamElement = new XStreamingElement("contacts", list);

            GetFreshStream();
            streamElement.Save(_sourceStream);
            contact.Save(_targetStream);
            ResetStreamPos();
            Assert.True(Diff.Compare(_sourceStream, _targetStream));
        }
Пример #2
0
        public void ModifyOriginalElement()
        {
            XElement contact = new XElement(
                "contact",
                new XElement("name", "jane"),
                new XElement("phone", new XAttribute("type", "home"), "925-555-0134"));
            XStreamingElement streamElement = new XStreamingElement("contact", new object[] { contact.Elements() });

            foreach (XElement x in contact.Elements())
            {
                x.Remove();
            }
            GetFreshStream();
            streamElement.Save(_sourceStream);
            contact.Save(_targetStream);
            ResetStreamPos();
            Assert.True(Diff.Compare(_sourceStream, _targetStream));
        }
Пример #3
0
                //[Variation(Priority = 1, Desc = "Laziness of IEnumerables - Modify IEnumerable after adding")]
                public void IEnumerableLazinessTest1()
                {
                    XElement name    = new XElement("name", "jane");
                    XElement phone   = new XElement("phone", new XAttribute("type", "home"), "925-555-0134");
                    XElement contact = new XElement("contact", name, phone);
                    IEnumerable <XElement> elements = contact.Elements();

                    name.Remove(); phone.Remove();
                    XStreamingElement streamElement = new XStreamingElement("contact", new object[] { elements });

                    getFreshStream();
                    streamElement.Save(_sourceStream); contact.Save(_targetStream);
                    resetStreamPos();
                    if (!_diff.Compare(_sourceStream, _targetStream))
                    {
                        throw new TestFailedException("");
                    }
                }
Пример #4
0
                //[Variation(Priority = 1, Desc = "Modify Original Elements")]
                public void ModifyOriginalElement()
                {
                    XElement contact = new XElement("contact", new XElement("name", "jane"),
                                                    new XElement("phone", new XAttribute("type", "home"), "925-555-0134"));
                    XStreamingElement streamElement = new XStreamingElement("contact", new object[] { contact.Elements() });

                    foreach (XElement x in contact.Elements())
                    {
                        x.Remove();
                    }
                    getFreshStream();
                    streamElement.Save(_sourceStream); contact.Save(_targetStream);
                    resetStreamPos();
                    if (!_diff.Compare(_sourceStream, _targetStream))
                    {
                        throw new TestFailedException("");
                    }
                }
Пример #5
0
                //[Variation(Priority = 1, Desc = "Laziness of IEnumerables - Make Sure IEnumerable is walked after Save")]
                public void IEnumberableLazinessTest2()
                {
                    XElement name    = new XElement("name", "jane");
                    XElement phone   = new XElement("phone", new XAttribute("type", "home"), "925-555-0134");
                    XElement contact = new XElement("contact", name, phone);
                    // During debug this test will not work correctly since ToString() of
                    // streamElement gets called for displaying the value in debugger local window.
                    XStreamingElement streamElement = new XStreamingElement("contact", GetElements(contact));

                    getFreshStream();
                    contact.Save(_targetStream);
                    _invokeStatus = true;
                    streamElement.Save(_sourceStream);
                    TestLog.Compare(_invokeError == false, "IEnumberable walked before expected");
                    resetStreamPos();
                    if (!_diff.Compare(_sourceStream, _targetStream))
                    {
                        throw new TestFailedException("");
                    }
                }
Пример #6
0
        public void SaveWithNull()
        {
            XStreamingElement streamElement = new XStreamingElement("phone", "925-555-0134");

            Assert.Throws <ArgumentNullException>(() => streamElement.Save((XmlWriter)null));
        }
Пример #7
0
        //allows derived classes to override the default streaming
        //and save method
        protected virtual async Task <bool> StreamAndSaveCalculationAsync()
        {
            bool bHasCalculations = false;

            //both calculators and analyzers use observationpath for initial file
            if (!CalculatorHelpers.URIAbsoluteExists(
                    this.GCCalculatorParams.ExtensionDocToCalcURI,
                    this.GCCalculatorParams.AnalyzerParms.ObservationsPath))
            {
                this.GCCalculatorParams.AnalyzerParms.ObservationsPath
                    = this.GCCalculatorParams.ExtensionDocToCalcURI.URIDataManager.TempDocPath;
            }
            if (!CalculatorHelpers.URIAbsoluteExists(
                    this.GCCalculatorParams.ExtensionDocToCalcURI,
                    this.GCCalculatorParams.AnalyzerParms.ObservationsPath))
            {
                return(bHasCalculations);
            }
            //new temporary path to store calculator results
            //when writing to file, close it with the using
            StringWriter output = new StringWriter();

            this.GCCalculatorParams.DocToCalcReader
                = DevTreks.Data.Helpers.FileStorageIO.GetXmlReader(
                      this.GCCalculatorParams.ExtensionDocToCalcURI.URIDataManager.InitialDocToCalcURI,
                      this.GCCalculatorParams.AnalyzerParms.ObservationsPath);
            if (this.GCCalculatorParams.DocToCalcReader != null)
            {
                using (this.GCCalculatorParams.DocToCalcReader)
                {
                    XmlWriterSettings writerSettings = new XmlWriterSettings();
                    writerSettings.OmitXmlDeclaration = true;
                    writerSettings.Indent             = true;
                    writerSettings.Async = true;
                    using (XmlWriter writer = XmlWriter.Create(
                               output, writerSettings))
                    {
                        //descendant nodes are also processed using streaming techniques
                        //note that XStreamingElements were tested but had faults
                        XStreamingElement root = new XStreamingElement(Constants.ROOT_PATH,
                                                                       from el in StreamRoot()
                                                                       select el);
                        root.Save(output);
                    }
                }
            }
            using (output)
            {
                if (this.GCCalculatorParams.ErrorMessage == string.Empty &&
                    this.GCArguments.HasCalculations)
                {
                    //move the new calculations to tempDocToCalcPath
                    //this returns an error msg
                    this.GCCalculatorParams.ErrorMessage = await CalculatorHelpers.MoveURIsAsync(
                        this.GCCalculatorParams.ExtensionDocToCalcURI, output,
                        this.GCCalculatorParams.ExtensionDocToCalcURI.URIDataManager.TempDocPath);

                    bHasCalculations = true;
                }
            }
            return(bHasCalculations);
        }
Пример #8
0
        //allows derived classes to override the default streaming
        //and save method
        protected virtual async Task <bool> StreamAndSaveCalculationAsync()
        {
            bool bHasCalculations = false;

            //both calculators and analyzers use observationpath for initial file
            if (!CalculatorHelpers.URIAbsoluteExists(this.GCCalculatorParams.ExtensionDocToCalcURI,
                                                     this.GCCalculatorParams.AnalyzerParms.ObservationsPath))
            {
                this.GCCalculatorParams.AnalyzerParms.ObservationsPath
                    = this.GCCalculatorParams.ExtensionDocToCalcURI.URIDataManager.TempDocPath;
            }
            if (!CalculatorHelpers.URIAbsoluteExists(this.GCCalculatorParams.ExtensionDocToCalcURI,
                                                     this.GCCalculatorParams.AnalyzerParms.ObservationsPath))
            {
                return(bHasCalculations);
            }
            //new temporary path to store calculator results
            StringWriter output = new StringWriter();

            this.GCCalculatorParams.DocToCalcReader = await CalculatorHelpers.GetXmlReaderAsync(
                this.GCCalculatorParams.ExtensionDocToCalcURI, this.GCCalculatorParams.AnalyzerParms.ObservationsPath);

            if (this.GCCalculatorParams.DocToCalcReader != null)
            {
                using (this.GCCalculatorParams.DocToCalcReader)
                {
                    XmlWriterSettings writerSettings = new XmlWriterSettings();
                    writerSettings.OmitXmlDeclaration = true;
                    writerSettings.Indent             = true;
                    //writerSettings.Async = true;
                    using (XmlWriter writer = XmlWriter.Create(
                               output, writerSettings))
                    {
                        XStreamingElement root = new XStreamingElement(Constants.ROOT_PATH,
                                                                       from el in StreamRoot()
                                                                       select el);
                        root.Save(writer);
                        //alternative pattern
                        ////now process each task as it writes
                        //bool[] hasCalcs = await Task.WhenAll(_lvTasks);
                        //for (int i = 0; i < hasCalcs.Count(); i++)
                        //{
                        //    bool bHasCalc = hasCalcs[i];
                        //}
                    }
                }
            }
            using (output)
            {
                if (this.GCCalculatorParams.ErrorMessage == string.Empty)
                {
                    //move the new calculations to tempDocToCalcPath
                    //this returns an error msg
                    this.GCCalculatorParams.ErrorMessage = await CalculatorHelpers.MoveURIsAsync(
                        this.GCCalculatorParams.ExtensionDocToCalcURI, output,
                        this.GCCalculatorParams.ExtensionDocToCalcURI.URIDataManager.TempDocPath);

                    bHasCalculations = true;
                }
            }
            return(bHasCalculations);
        }
Пример #9
0
        //async calcs to complete
        //List<Task<bool>> _lvTasks = new List<Task<bool>>();
        //allows derived classes to override the default streaming
        //and save method
        protected virtual async Task <bool> StreamAndSaveCalculationAsync()
        {
            bool bHasCalculations = false;

            //both calculators and analyzers use observationpath for initial file
            if (!CalculatorHelpers.URIAbsoluteExists(this.GCCalculatorParams.ExtensionDocToCalcURI,
                                                     this.GCCalculatorParams.AnalyzerParms.ObservationsPath))
            {
                this.GCCalculatorParams.AnalyzerParms.ObservationsPath
                    = this.GCCalculatorParams.ExtensionDocToCalcURI.URIDataManager.TempDocPath;
            }
            if (!CalculatorHelpers.URIAbsoluteExists(this.GCCalculatorParams.ExtensionDocToCalcURI,
                                                     this.GCCalculatorParams.AnalyzerParms.ObservationsPath))
            {
                return(bHasCalculations);
            }
            //new temporary path to store calculator results
            //when writing to file, close it with the using
            StringWriter output = new StringWriter();

            this.GCCalculatorParams.DocToCalcReader = await CalculatorHelpers.GetXmlReaderAsync(
                this.GCCalculatorParams.ExtensionDocToCalcURI, this.GCCalculatorParams.AnalyzerParms.ObservationsPath);

            if (this.GCCalculatorParams.DocToCalcReader != null)
            {
                using (this.GCCalculatorParams.DocToCalcReader)
                {
                    XmlWriterSettings writerSettings = new XmlWriterSettings();
                    writerSettings.OmitXmlDeclaration = true;
                    writerSettings.Indent             = true;
                    ////can now write this this asynch
                    //writerSettings.Async = true;
                    using (XmlWriter writer = XmlWriter.Create(
                               output, writerSettings))
                    {
                        //180: switched to XStreaming, uses less memory until xml is written
                        XStreamingElement root = new XStreamingElement(Constants.ROOT_PATH,
                                                                       from el in StreamingRoot()
                                                                       select el);
                        //the write will omit xml declaration (allowing xslt transform)
                        //the write will also cause the calctasks to be completed
                        root.Save(writer);

                        //keep this code to verify that tasks are being completed once xml is serialized
                        //test whether or not this interferes with sequential completion of tasks for aggregation
                        ////now process each task as it writes
                        //bool[] hasCalcs = await Task.WhenAll(_lvTasks);
                        //for (int i = 0; i < hasCalcs.Count(); i++)
                        //{
                        //    bool bHasCalc = hasCalcs[i];
                        //}
                    }
                }
            }
            //subsequent async method calls have to use Wait()
            //or the async returns here without the completed calculations
            //see SB1GeneralCalculatorAsync.RunCalculationsAndSetUpdatesAsync
            using (output)
            {
                if (this.GCCalculatorParams.ErrorMessage == string.Empty)
                {
                    //move the new calculations to tempDocToCalcPath
                    //this returns an error msg
                    this.GCCalculatorParams.ErrorMessage = await CalculatorHelpers.MoveURIsAsync(
                        this.GCCalculatorParams.ExtensionDocToCalcURI, output,
                        this.GCCalculatorParams.ExtensionDocToCalcURI.URIDataManager.TempDocPath);

                    bHasCalculations = true;
                }
            }
            return(bHasCalculations);
        }