示例#1
0
        public void testForLocking()
        {
            parameters.SetInteger("name", 123);

            bool IsLocked = parameters.IsLocked("name");
            Assert.IsFalse(IsLocked);

            long key = 1234567890;
            parameters.Lock("name", key);

            IsLocked = parameters.IsLocked("name");
            Assert.IsTrue(IsLocked);

            try
            {
                parameters.Lock("name", key);
                Assert.Fail("should never reach here");
            }
            catch(BadStateException e)
            {
                // already locked
                Assert.AreEqual(
                    "The object 'name' is not in the appropriate state.",
                    e.Message);
            }

            try
            {
                // cannot modify
                parameters.SetInteger("name", 456);
                Assert.Fail("should never reach here");
            }
            catch(BadStateException e)
            {
                // locked, cannot modify
                Assert.AreEqual(
                    "The object 'name' is not in the appropriate state.",
                    e.Message);
            }

            try
            {
                parameters.Unlock("name", key + 1);
                Assert.Fail("should never reach here");
            }
            catch(UnexpectedValueException e)
            {
                // bad key
                Assert.AreEqual("Unexpected value: bad key", e.Message);
            }

            parameters.Unlock("name", key);

            IsLocked = parameters.IsLocked("name");
            Assert.IsFalse(IsLocked);

            try
            {
                parameters.Unlock("name", key);
                Assert.Fail("should never reach here");
            }
            catch(BadStateException e)
            {
                // already unlocked
                Assert.AreEqual(
                    "The object 'name' is not in the appropriate state.",
                    e.Message);
            }

            // unlocked, can modify
            parameters.SetInteger("name", 456);

            int value = parameters.GetInteger("name");
            Assert.IsTrue(value == 456);

            // nested test

            Parameters nested = new Parameters();

            parameters.SetNestedParameters("nested", nested);

            nested.SetInteger("internal", 789);

            Parameters nested2 = new Parameters();

            nested.SetNestedParameters("more nested", nested2);

            nested2.SetInteger("more internal", 987);

            // lock something within the hierarchy

            long otherKey = 987654321;
            nested.Lock("internal", otherKey);

            try
            {
                // cannot modify
                nested.SetInteger("internal", 123);
                Assert.Fail("should never reach here");
            }
            catch(BadStateException e)
            {
                // locked, cannot modify
                Assert.AreEqual(
                    "The object 'internal' is not in the appropriate state.",
                    e.Message);
            }

            // lock the whole hierarchy except the element
            // that is already locked
            parameters.Lock("nested", key);

            try
            {
                nested2.SetInteger("more internal", 123);
                Assert.Fail("should never reach here");
            }
            catch(BadStateException e)
            {
                // locked deeply, cannot modify
                Assert.AreEqual("The object 'more internal'"
                    + " is not in the appropriate state.",
                    e.Message);
            }

            try
            {
                parameters.SetInteger("nested", 123);
                Assert.Fail("should never reach here");
            }
            catch(BadStateException e)
            {
                // locked as a whole, cannot modify
                Assert.AreEqual(
                    "The object 'nested' is not in the appropriate state.",
                    e.Message);
            }

            // Unlock everything except the element
            // that was already locked before

            parameters.Unlock("nested", key);

            // unlocked, can modify
            nested2.SetInteger("more internal", 123);

            try
            {
                nested.SetInteger("internal", 123);
                Assert.Fail("should never reach here");
            }
            catch(BadStateException e)
            {
                // still locked, cannot modify
                Assert.AreEqual(
                    "The object 'internal' is not in the appropriate state.",
                    e.Message);
            }

            try
            {
                nested.Unlock("internal", key);
                Assert.Fail("should never reach here");
            }
            catch(UnexpectedValueException e)
            {
                // bad key
                Assert.AreEqual("Unexpected value: bad key", e.Message);
            }

            try
            {
                nested.SetInteger("internal", 123);
                Assert.Fail("should never reach here");
            }
            catch(BadStateException e)
            {
                // still locked
                Assert.AreEqual(
                    "The object 'internal' is not in the appropriate state.",
                    e.Message);
            }

            nested.Unlock("internal", otherKey);

            // finally unlocked
            nested.SetInteger("internal", 123);

            // unlocked, can replace the whole
            parameters.SetInteger("nested", 123);
        }