public void DifferentStringsDifferentDocuments()
        {
            ProjectStringCache cache = new ProjectStringCache();

            XmlDocument document = new XmlDocument();

            string stringToAdd = "Test1";
            string return1 = cache.Add(stringToAdd, document);
            Assert.AreEqual(1, cache.Count);

            stringToAdd = "Test2";
            XmlDocument document2 = new XmlDocument();
            string return2 = cache.Add(stringToAdd, document2);

            // The second string gets its own instance.
            Assert.AreEqual(2, cache.Count);

            // Build a new string guaranteed not to be optimized by the compiler into the same instance.
            StringBuilder builder = new StringBuilder();
            builder.Append("Test");
            builder.Append("2");
            XmlDocument document3 = new XmlDocument();
            string return3 = cache.Add(builder.ToString(), document3);

            // The new string should be the same as the other one already in the collection.
            Assert.AreSame(return2, return3);

            // No new instances for string with the same content.
            Assert.AreEqual(2, cache.Count);
        }
        public void RemoveLastInstanceDeallocatesEntry()
        {
            ProjectStringCache cache = new ProjectStringCache();

            XmlDocument document = new XmlDocument();

            string stringToAdd = "Test1";
            string return1 = cache.Add(stringToAdd, document);

            cache.Clear(document);

            // Should be no instances left.
            Assert.AreEqual(0, cache.Count);

            // Build a new string guaranteed not to be optimized by the compiler into the same instance.
            StringBuilder builder = new StringBuilder();
            builder.Append("Test");
            builder.Append("1");
            XmlDocument document2 = new XmlDocument();

            string return2 = cache.Add(builder.ToString(), document2);

            // Returned references should NOT be the same
            Assert.AreNotSame(return1, return2);
        }
        public void RemoveOneInstance()
        {
            ProjectStringCache cache = new ProjectStringCache();

            XmlDocument document = new XmlDocument();

            string stringToAdd = "Test1";
            string return1 = cache.Add(stringToAdd, document);
            Assert.AreEqual(1, cache.Count);

            XmlDocument document2 = new XmlDocument();
            string return2 = cache.Add(stringToAdd, document2);
            Assert.AreEqual(1, cache.Count);

            cache.Clear(document2);

            // Since there is still one document referencing the string, it should remain.
            Assert.AreEqual(1, cache.Count);

            // Build a new string guaranteed not to be optimized by the compiler into the same instance.
            StringBuilder builder = new StringBuilder();
            builder.Append("Test");
            builder.Append("1");
            XmlDocument document3 = new XmlDocument();

            string return3 = cache.Add(builder.ToString(), document3);

            // Returned references should be the same
            Assert.AreSame(return1, return3);

            // Still should only be one cached instance.
            Assert.AreEqual(1, cache.Count);
        }
        public void AddReturnsSameInstanceForDifferentDocument()
        {
            ProjectStringCache cache = new ProjectStringCache();

            XmlDocument document = new XmlDocument();

            string stringToAdd = "Test1";
            string return1 = cache.Add(stringToAdd, document);

            // Content of string should be the same.
            Assert.AreEqual(stringToAdd, return1);

            // Build a new string guaranteed not to be optimized by the compiler into the same instance.
            StringBuilder builder = new StringBuilder();
            builder.Append("Test");
            builder.Append("1");
            XmlDocument document2 = new XmlDocument();

            string return2 = cache.Add(builder.ToString(), document2);

            // Content of string should be the same.
            Assert.AreEqual(builder.ToString(), return2);

            // Returned references should be the same
            Assert.AreSame(return1, return2);

            // Should not have added any new string instances to the cache.
            Assert.AreEqual(1, cache.Count);
        }