Пример #1
0
        public void TestCustomSerialization()
        {
            BuildProperty p1 = new BuildProperty("name", "value", PropertyType.GlobalProperty);
            BuildProperty p2 = new BuildProperty("name2", "value2", PropertyType.OutputProperty);
            BuildProperty p3 = new BuildProperty("name3", "value3", PropertyType.EnvironmentProperty);

            MemoryStream stream = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(stream);
            BinaryReader reader = new BinaryReader(stream);

            try
            {
                stream.Position = 0;
                p1.WriteToStream(writer);
                p2.WriteToStream(writer);
                p3.WriteToStream(writer);
                long streamWriteEndPosition = stream.Position;

                stream.Position = 0;
                BuildProperty p4 = BuildProperty.CreateFromStream(reader);
                BuildProperty p5 = BuildProperty.CreateFromStream(reader);
                BuildProperty p6 = BuildProperty.CreateFromStream(reader);
                long          streamReadEndPosition = stream.Position;
                Assert.IsTrue(streamWriteEndPosition == streamReadEndPosition, "Stream end positions should be equal");
                CompareBuildProperty(p1, p4);
                CompareBuildProperty(p2, p5);
                CompareBuildProperty(p3, p6);
            }
            finally
            {
                reader.Close();
                writer = null;
                stream = null;
            }
        }
Пример #2
0
        public void TestCustomSerializationCompressesPropertyValueAndExpandedValue()
        {
            // Create a non-literal string, so the CLR won't intern it (in real builds,
            // the strings are not literals, and the CLR won't intern them)
            string v1         = "non_expandable_property" + new Random().Next();
            int    i          = new Random().Next();
            string v2         = "expandable_$(property)" + i;
            string v2Expanded = "expandable_" + i;

            // Verify it is not interned
            Assertion.Assert(null == String.IsInterned(v1));
            Assertion.Assert(null == String.IsInterned(v2));

            // Property with finalValue == Value
            BuildProperty p = new BuildProperty("name", v1);

            Assertion.Assert(Object.ReferenceEquals(p.FinalValueEscaped, p.Value));

            // Property with finalValue != Value
            BuildProperty q = new BuildProperty("name", v2);

            q.Evaluate(new Expander(new BuildPropertyGroup()));
            Assertion.Assert(!Object.ReferenceEquals(q.FinalValueEscaped, q.Value));
            Assertion.AssertEquals(v2, q.Value);
            Assertion.AssertEquals(v2Expanded, q.FinalValueEscaped);

            // "Transmit across the wire"
            MemoryStream stream = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(stream);
            BinaryReader reader = new BinaryReader(stream);

            try
            {
                stream.Position = 0;
                p.WriteToStream(writer);
                q.WriteToStream(writer);

                // The property that had identical value and finalvalueescaped should
                // be deserialized into a property that has identical references for its
                // value and finalvalueescaped.
                stream.Position = 0;
                BuildProperty p2 = BuildProperty.CreateFromStream(reader);
                Assertion.Assert(Object.ReferenceEquals(p2.FinalValueEscaped, p2.Value));
                Assertion.AssertEquals(v1, p2.Value);

                // The property that had different value and finalvalueescaped should be deserialized
                // normally
                BuildProperty q2 = BuildProperty.CreateFromStream(reader);
                Assertion.Assert(!Object.ReferenceEquals(q2.FinalValueEscaped, q2.Value));
                Assertion.AssertEquals(v2, q2.Value);
                Assertion.AssertEquals(v2Expanded, q2.FinalValueEscaped);
            }
            finally
            {
                reader.Close();
            }
        }