Beispiel #1
0
        public void TestModifyOnUnmodifiable()
        {
            CharArrayMap <int?> map = new CharArrayMap <int?>(TEST_VERSION_CURRENT, 2, false);

            map.Put("foo", 1);
            map.Put("bar", 2);
            int size = map.Count;

            assertEquals(2, size);
            assertTrue(map.ContainsKey("foo"));
            assertEquals(1, map.Get("foo"));
            assertTrue(map.ContainsKey("bar"));
            assertEquals(2, map.Get("bar"));

            map = CharArrayMap.UnmodifiableMap(map);
            assertEquals("Map size changed due to unmodifiableMap call", size, map.Count);
            var NOT_IN_MAP = "SirGallahad";

            assertFalse("Test String already exists in map", map.ContainsKey(NOT_IN_MAP));
            assertNull("Test String already exists in map", map.Get(NOT_IN_MAP));

            try
            {
                map.Put(NOT_IN_MAP.ToCharArray(), 3);
                fail("Modified unmodifiable map");
            }
            catch (System.NotSupportedException)
            {
                // expected
                assertFalse("Test String has been added to unmodifiable map", map.ContainsKey(NOT_IN_MAP));
                assertNull("Test String has been added to unmodifiable map", map.Get(NOT_IN_MAP));
                assertEquals("Size of unmodifiable map has changed", size, map.Count);
            }

            try
            {
                map.Put(NOT_IN_MAP, 3);
                fail("Modified unmodifiable map");
            }
            catch (System.NotSupportedException)
            {
                // expected
                assertFalse("Test String has been added to unmodifiable map", map.ContainsKey(NOT_IN_MAP));
                assertNull("Test String has been added to unmodifiable map", map.Get(NOT_IN_MAP));
                assertEquals("Size of unmodifiable map has changed", size, map.Count);
            }

            try
            {
                map.Put(new StringBuilder(NOT_IN_MAP), 3);
                fail("Modified unmodifiable map");
            }
            catch (System.NotSupportedException)
            {
                // expected
                assertFalse("Test String has been added to unmodifiable map", map.ContainsKey(NOT_IN_MAP));
                assertNull("Test String has been added to unmodifiable map", map.Get(NOT_IN_MAP));
                assertEquals("Size of unmodifiable map has changed", size, map.Count);
            }

            #region Added for better .NET support
            try
            {
                map.Add(NOT_IN_MAP, 3);
                fail("Modified unmodifiable map");
            }
            catch (System.NotSupportedException)
            {
                // expected
                assertFalse("Test String has been added to unmodifiable map", map.ContainsKey(NOT_IN_MAP));
                assertNull("Test String has been added to unmodifiable map", map.Get(NOT_IN_MAP));
                assertEquals("Size of unmodifiable map has changed", size, map.Count);
            }

            try
            {
                map.Add(new KeyValuePair <string, int?>(NOT_IN_MAP, 3));
                fail("Modified unmodifiable map");
            }
            catch (System.NotSupportedException)
            {
                // expected
                assertFalse("Test String has been added to unmodifiable map", map.ContainsKey(NOT_IN_MAP));
                assertNull("Test String has been added to unmodifiable map", map.Get(NOT_IN_MAP));
                assertEquals("Size of unmodifiable map has changed", size, map.Count);
            }

            try
            {
                map[new StringBuilder(NOT_IN_MAP)] = 3;
                fail("Modified unmodifiable map");
            }
            catch (System.NotSupportedException)
            {
                // expected
                assertFalse("Test String has been added to unmodifiable map", map.ContainsKey(NOT_IN_MAP));
                assertNull("Test String has been added to unmodifiable map", map.Get(NOT_IN_MAP));
                assertEquals("Size of unmodifiable map has changed", size, map.Count);
            }

            try
            {
#pragma warning disable 612, 618
                map.Remove(new KeyValuePair <string, int?>("foo", 1));
#pragma warning restore 612, 618
                fail("Modified unmodifiable map");
            }
            catch (System.NotSupportedException)
            {
                // expected
                assertEquals("Size of unmodifiable map has changed", size, map.Count);
            }
            #endregion

            try
            {
                map.Clear();
                fail("Modified unmodifiable map");
            }
            catch (System.NotSupportedException)
            {
                // expected
                assertEquals("Size of unmodifiable map has changed", size, map.size());
            }

            try
            {
                map.EntrySet().Clear();
                fail("Modified unmodifiable map");
            }
            catch (System.NotSupportedException)
            {
                // expected
                assertEquals("Size of unmodifiable map has changed", size, map.size());
            }

            try
            {
                map.Keys.Clear();
                fail("Modified unmodifiable map");
            }
            catch (System.NotSupportedException)
            {
                // expected
                assertEquals("Size of unmodifiable map has changed", size, map.size());
            }

            try
            {
                map.Put((object)NOT_IN_MAP, 3);
                fail("Modified unmodifiable map");
            }
            catch (System.NotSupportedException)
            {
                // expected
                assertFalse("Test String has been added to unmodifiable map", map.ContainsKey(NOT_IN_MAP));
                assertNull("Test String has been added to unmodifiable map", map.Get(NOT_IN_MAP));
                assertEquals("Size of unmodifiable map has changed", size, map.size());
            }

            try
            {
                map.PutAll(Collections.SingletonMap <string, int?>(NOT_IN_MAP, 3));
                fail("Modified unmodifiable map");
            }
            catch (System.NotSupportedException)
            {
                // expected
                assertFalse("Test String has been added to unmodifiable map", map.ContainsKey(NOT_IN_MAP));
                assertNull("Test String has been added to unmodifiable map", map.Get(NOT_IN_MAP));
                assertEquals("Size of unmodifiable map has changed", size, map.size());
            }

            assertTrue(map.ContainsKey("foo"));
            assertEquals(1, map.Get("foo"));
            assertTrue(map.ContainsKey("bar"));
            assertEquals(2, map.Get("bar"));
        }