示例#1
0
        private void Create_WithMessages(int count)
        {
            var messages   = LoggingTestHelpers.GetTestMessages <LogMessage>(count, 1);
            var collection = new LogMessageCollection <LogMessage>(messages);

            TestCollectionPropertyDefaults(collection, count);
            Assert.Equal(messages, collection.ToArray());
        }
示例#2
0
        /// <summary>
        /// Creates a new instance of the collection class to test, pre-populated with the specified number of random log messages.
        /// </summary>
        /// <param name="count">Number of random log messages the collection should contain.</param>
        /// <param name="messages">Receives messages that have been put into the collection.</param>
        /// <returns>A new instance of the collection class to test.</returns>
        protected override ILogMessageCollection <LogMessage> CreateCollection(int count, out LogMessage[] messages)
        {
            messages = LoggingTestHelpers.GetTestMessages <LogMessage>(count);
            var collection = count == 0
                                                 ? new LogMessageCollection <LogMessage>()
                                                 : new LogMessageCollection <LogMessage>(messages);

            // the test assumes that the collection uses single-item notifications
            collection.UseMultiItemNotifications = false;

            return(collection);
        }
        // ------------------------------------------------------------------------------------------------------------------

        /// <summary>
        /// Common test code for adding a log message to the collection.
        /// The operation should throw <see cref="NotSupportedException"/>, if the collection is readonly.
        /// </summary>
        /// <param name="initialCollectionSize">Number of messages to put into the collection before running the test.</param>
        /// <param name="numberOfMessagesToAdd">Number of messages to add at once.</param>
        /// <param name="operation">Operation to invoke on the collection.</param>
        private void Common_AddRange(
            int initialCollectionSize,
            int numberOfMessagesToAdd,
            Action <TLogMessageCollection, IEnumerable <LogMessage> > operation)
        {
            // create collection to test
            using (var collection = CreateCollection(initialCollectionSize, out var messages))
            {
                var eventWatcher = collection.AttachEventWatcher();

                // create new messages that is not in the collection, yet
                // (use seed 1 to generate a message that differs from the messages generated by default)
                var newMessages = LoggingTestHelpers.GetTestMessages <LogMessage>(numberOfMessagesToAdd, 1);

                if (CollectionIsReadOnly)
                {
                    // collection is read-only and should throw exception
                    Assert.Throws <NotSupportedException>(() => operation(collection, newMessages));
                    Assert.Equal(initialCollectionSize, collection.Count);
                }
                else
                {
                    // add message to the collection
                    operation(collection, newMessages);

                    // check whether the collection contains the new message now
                    Assert.Equal(initialCollectionSize + numberOfMessagesToAdd, collection.Count);
                    var expectedMessages = new List <LogMessage>(messages);
                    expectedMessages.AddRange(newMessages);
                    Assert.Equal(expectedMessages.ToArray(), collection.ToArray());                     // does not take IsReadOnly into account
                    Assert.All(collection, message => Assert.Equal(CollectionProvidesProtectedMessages, message.IsReadOnly));

                    // add events that should have been raised
                    for (int i = 0; i < numberOfMessagesToAdd; i++)
                    {
                        eventWatcher.ExpectCollectionChanged(
                            new NotifyCollectionChangedEventArgs(
                                NotifyCollectionChangedAction.Add,
                                new[] { newMessages[i] },
                                initialCollectionSize + i));
                    }

                    eventWatcher.ExpectPropertyChanged(new PropertyChangedEventArgs("Count"));
                    eventWatcher.ExpectPropertyChanged(new PropertyChangedEventArgs("Item[]"));
                }

                // check events that should have been raised
                eventWatcher.CheckInvocations();
            }
        }
示例#4
0
        /// <summary>
        /// Creates a new instance of the collection class to test, pre-populated with the specified number of random log messages.
        /// The collection should be disposed at the end to avoid generating orphaned log files.
        /// </summary>
        /// <param name="count">Number of random log messages the collection should contain.</param>
        /// <param name="purpose">Purpose of the log file backing the collection.</param>
        /// <param name="writeMode">Write mode of the log file backing the collection.</param>
        /// <param name="isReadOnly"><c>true</c> to create a read-only log file; otherwise <c>false</c>.</param>
        /// <param name="messages">Receives messages that have been put into the collection.</param>
        /// <returns>A new instance of the collection class to test.</returns>
        private static FileBackedLogMessageCollection CreateCollection(
            int count,
            LogFilePurpose purpose,
            LogFileWriteMode writeMode,
            bool isReadOnly,
            out LogMessage[] messages)
        {
            string path = Guid.NewGuid().ToString("D") + ".gplog";

            // create a collection backed by a new file
            using (var file1 = LogFile.OpenOrCreate(path, purpose, writeMode))
            {
                // generate the required number of log message and add them to the collection
                var fileLogMessages = LoggingTestHelpers.GetTestMessages <LogFileMessage>(count);
                for (long i = 0; i < fileLogMessages.Length; i++)
                {
                    fileLogMessages[i].Id = i;
                }
                messages = fileLogMessages.Cast <LogMessage>().ToArray();
                if (count > 0)
                {
                    file1.Write(messages);
                }
            }

            // open the created log file again as expected for the test
            var file2 = isReadOnly
                                            ? LogFile.OpenReadOnly(path)
                                            : LogFile.Open(path, writeMode);

            // let the collection delete the log file on its disposal
            file2.Messages.AutoDelete = true;

            // the collection should now contain the messages written into it
            // (the file-backed collection assigns message ids on its own, but they should be the same as the ids assigned to the test set)
            Assert.Equal(messages, file2.Messages.ToArray());

            // the test assumes that the collection uses single-item notifications
            file2.Messages.UseMultiItemNotifications = false;

            return(file2.Messages);
        }
        /// <summary>
        /// Creates a new instance of the collection class to test, pre-populated with the specified number of random log messages.
        /// The collection should be disposed at the end to avoid generating orphaned log files.
        /// </summary>
        /// <param name="count">Number of random log messages the collection should contain.</param>
        /// <param name="messages">Receives messages that have been put into the collection.</param>
        /// <returns>A new instance of the collection class to test.</returns>
        protected override FileBackedLogMessageCollection CreateCollection(int count, out LogMessage[] messages)
        {
            string path = Guid.NewGuid().ToString("D") + ".gplog";

            // create a collection backed by a new file
            using (var file1 = LogFile.OpenOrCreate(path, LogFilePurpose.Analysis, LogFileWriteMode.Fast))
            {
                // generate the required number of log message and add them to the collection
                var fileLogMessages = LoggingTestHelpers.GetTestMessages <LogFileMessage>(count);
                for (long i = 0; i < fileLogMessages.Length; i++)
                {
                    fileLogMessages[i].Id = i;
                }
                messages = fileLogMessages.Cast <LogMessage>().ToArray();
                if (count > 0)
                {
                    file1.Write(messages);
                }
            }

            // open the created log file again with support for reading and writing only
            var file2 = LogFile.Open(path, LogFileWriteMode.Fast);

            // let the collection delete the log file on its disposal
            file2.Messages.AutoDelete = true;

            // the collection should now have the default state
            TestCollectionPropertyDefaults(file2.Messages, count, false);

            // the collection should now contain the messages written into it
            // (the file-backed collection assigns message ids on its own, but they should be the same as the ids assigned to the test set)
            Assert.Equal(messages, file2.Messages.ToArray());

            // the test assumes that the collection uses single-item notifications
            file2.Messages.UseMultiItemNotifications = false;

            return(file2.Messages);
        }