Пример #1
0
        private static void TestEnumForByNameCore <T>(Stream stream, T value, T deserialized, string property)
        {
            if (property == null)
            {
                Assert.That(deserialized, Is.EqualTo(value));
                stream.Position = 0;
                Assert.That(Unpacking.UnpackString(stream), Is.EqualTo(value.ToString()));
            }
            else
            {
                var propertyInfo = typeof(T).GetProperty(property);
#if !UNITY
                Assert.That(propertyInfo.GetValue(deserialized, null), Is.EqualTo(propertyInfo.GetValue(value, null)));
#else
                Assert.That(propertyInfo.GetGetMethod().Invoke(deserialized, null), Is.EqualTo(propertyInfo.GetGetMethod().Invoke(value, null)));
#endif // !UNITY
                stream.Position = 0;
                // Properties are sorted by lexical order
                var index  = Array.IndexOf(typeof(T).GetProperties().OrderBy(p => p.Name).ToArray(), propertyInfo);
                var result = Unpacking.UnpackArray(stream);
                Assert.That(
#if !UNITY
                    result[index].Equals(propertyInfo.GetValue(value, null).ToString()),
                    result[index] + " == " + propertyInfo.GetValue(value, null)
#else
                    result[index].Equals(propertyInfo.GetGetMethod().Invoke(value, null).ToString()),
                    result[index] + " == " + propertyInfo.GetGetMethod().Invoke(value, null)
#endif // !UNITY
                    );
            }
        }
        private void TestEnumForByName <T>(SerializationContext context, T value, string property)
        {
            var serializer = context.GetSerializer <T>();

            using (var stream = new MemoryStream())
            {
                serializer.Pack(stream, value);
                stream.Position = 0;
                var deserialized = serializer.Unpack(stream);

                if (property == null)
                {
                    Assert.That(deserialized, Is.EqualTo(value));
                    stream.Position = 0;
                    Assert.That(Unpacking.UnpackString(stream), Is.EqualTo(value.ToString()));
                }
                else
                {
                    var propertyInfo = typeof(T).GetProperty(property);
                    Assert.That(propertyInfo.GetValue(deserialized, null), Is.EqualTo(propertyInfo.GetValue(value, null)));
                    stream.Position = 0;
                    var result = Unpacking.UnpackDictionary(stream);
                    Assert.That(
                        result[property].Equals(propertyInfo.GetValue(value, null).ToString()),
                        result[property] + " == " + propertyInfo.GetValue(value, null)
                        );
                }
            }
        }
        private void TestEnumForByName <T>(SerializationContext context, T value, string property)
        {
            var serializer = context.GetSerializer <T>();

            using (var stream = new MemoryStream())
            {
                serializer.Pack(stream, value);
                stream.Position = 0;
                var deserialized = serializer.Unpack(stream);

                if (property == null)
                {
                    Assert.That(deserialized, Is.EqualTo(value));
                    stream.Position = 0;
                    Assert.That(Unpacking.UnpackString(stream), Is.EqualTo(value.ToString()));
                }
                else
                {
                    var propertyInfo = typeof(T).GetProperty(property);
                    Assert.That(propertyInfo.GetValue(deserialized, null), Is.EqualTo(propertyInfo.GetValue(value, null)));
                    stream.Position = 0;
                    // Properties are sorted by lexical order
                    var index  = Array.IndexOf(typeof(T).GetProperties().OrderBy(p => p.Name).ToArray(), propertyInfo);
                    var result = Unpacking.UnpackArray(stream);
                    Assert.That(
                        result[index].Equals(propertyInfo.GetValue(value, null).ToString()),
                        result[index] + " == " + propertyInfo.GetValue(value, null)
                        );
                }
            }
        }
        private static void TestEnumForByNameCore <T>(Stream stream, T value, T deserialized, string property)
        {
            if (property == null)
            {
                Assert.That(deserialized, Is.EqualTo(value));
                stream.Position = 0;
                Assert.That(Unpacking.UnpackString(stream), Is.EqualTo(value.ToString()));
            }
            else
            {
                var propertyInfo = typeof(T).GetProperty(property);
#if !UNITY
                Assert.That(propertyInfo.GetValue(deserialized, null), Is.EqualTo(propertyInfo.GetValue(value, null)));
#else
                Assert.That(propertyInfo.GetGetMethod().Invoke(deserialized, null), Is.EqualTo(propertyInfo.GetGetMethod().Invoke(value, null)));
#endif // !UNITY
                stream.Position = 0;
                var result = Unpacking.UnpackDictionary(stream);
                Assert.That(
#if !UNITY
                    result[property].Equals(propertyInfo.GetValue(value, null).ToString()),
                    result[property] + " == " + propertyInfo.GetValue(value, null)
#else
                    result[property].Equals(propertyInfo.GetGetMethod().Invoke(value, null).ToString()),
                    result[property] + " == " + propertyInfo.GetGetMethod().Invoke(value, null)
#endif // !UNITY
                    );
            }
        }
        public void TestCharArrayContent()
        {
            var serializer = this.CreateTarget <char[]>(GetSerializationContext());

            using (var stream = new MemoryStream())
            {
                serializer.Pack(stream, new char[] { 'a', 'b', 'c', 'd' });
                stream.Position = 0;
                Assert.That(Unpacking.UnpackString(stream), Is.EqualTo("abcd"));
            }
        }
        public void TestDispatch_MethodExists_Success()
        {
            var svcFile = ".\\Services.svc";

            File.WriteAllText(
                svcFile,
                String.Format(CultureInfo.InvariantCulture, "<% @ ServiceHost Service=\"{0}\" %>", typeof(TestService).FullName)
                );
            try
            {
                var configuration = new RpcServerConfiguration();
                configuration.ServiceTypeLocatorProvider = conf => new FileBasedServiceTypeLocator();

                using (var server = new RpcServer(configuration))
                    using (var transportManager = new NullServerTransportManager(server))
                        using (var transport = new NullServerTransport(transportManager))
                            using (var requestContext = DispatchTestHelper.CreateRequestContext())
                                using (var argumentsBuffer = new MemoryStream())
                                    using (var waitHandle = new ManualResetEventSlim())
                                    {
                                        var message = Guid.NewGuid().ToString();
                                        using (var argumentsPacker = Packer.Create(argumentsBuffer, false))
                                        {
                                            argumentsPacker.PackArrayHeader(1);
                                            argumentsPacker.Pack(message);
                                        }

                                        argumentsBuffer.Position = 0;

                                        var target = new LocatorBasedDispatcher(server);
                                        MessagePackObject response = MessagePackObject.Nil;
                                        requestContext.MethodName = "Echo:TestService:1";
                                        requestContext.MessageId  = 1;
                                        requestContext.SetTransport(transport);
                                        requestContext.ArgumentsUnpacker = Unpacker.Create(argumentsBuffer);
                                        transport.Sent +=
                                            (sender, e) =>
                                        {
                                            response = Unpacking.UnpackString(e.Context.GetReturnValueData()).Value;
                                            waitHandle.Set();
                                        };
                                        target.Dispatch(transport, requestContext);

                                        Assert.That(waitHandle.Wait(TimeSpan.FromSeconds(1)));

                                        Assert.That(message == response, "{0} != {1}", message, response);
                                    }
            }
            finally
            {
                File.Delete(svcFile);
            }
        }
Пример #7
0
        public void TestSetException_ExceptionIsNotNull_Serialized()
        {
            using (var server = new RpcServer())
                using (var transportManager = new NullServerTransportManager(server))
                    using (var transport = new NullServerTransport(transportManager))
                        using (var responseContext = DispatchTestHelper.CreateResponseContext(transport))
                        {
                            var target = new Target(server);
                            target.InvokeSetException(responseContext, "Method", new RpcMissingMethodException("Method"));

                            // Details should be tested in ServerResponseContextTest.TestSerialize...
                            Assert.That(Unpacking.UnpackString(responseContext.GetErrorData()).Value == RpcError.NoMethodError.Identifier);
                        }
        }
        private void TestEnumForByName(SerializationContext context, Type builtType, params string[] builtMembers)
        {
            var serializer = context.GetSerializer(builtType);
            var value      = Enum.Parse(builtType, String.Join(",", builtMembers));

            using (var stream = new MemoryStream())
            {
                serializer.PackTo(Packer.Create(stream, false), value);
                stream.Position = 0;
                var deserialized = serializer.Unpack(stream);

                Assert.That(deserialized, Is.EqualTo(value));
                stream.Position = 0;
                Assert.That(Unpacking.UnpackString(stream), Is.EqualTo(value.ToString()));
            }
        }
        public void TestDispatch_MethodNotExists_NoMethodError()
        {
            using (var server = new RpcServer())
                using (var transportManager = new NullServerTransportManager(server))
                    using (var transport = new NullServerTransport(transportManager))
                        using (var requestContext = DispatchTestHelper.CreateRequestContext())
                            using (var argumentsBuffer = new MemoryStream())
                                using (var waitHandle = new ManualResetEventSlim())
                                {
                                    var message = Guid.NewGuid().ToString();
                                    using (var argumentsPacker = Packer.Create(argumentsBuffer, false))
                                    {
                                        argumentsPacker.PackArrayHeader(1);
                                        argumentsPacker.Pack(message);
                                    }

                                    argumentsBuffer.Position = 0;

                                    var target = new LocatorBasedDispatcher(server);
                                    MessagePackObject response = MessagePackObject.Nil;
                                    requestContext.MethodName = "Echo:TestServices:1";
                                    requestContext.MessageId  = 1;
                                    requestContext.SetTransport(transport);
                                    requestContext.ArgumentsUnpacker = Unpacker.Create(argumentsBuffer);
                                    transport.Sent +=
                                        (sender, e) =>
                                    {
                                        response = Unpacking.UnpackString(e.Context.GetErrorData()).Value;
                                        waitHandle.Set();
                                    };
                                    target.Dispatch(transport, requestContext);

                                    Assert.That(waitHandle.Wait(TimeSpan.FromSeconds(1)));

                                    Assert.That(RpcError.NoMethodError.Identifier == response, "{0} != {1}", message, response);
                                }
        }
 public void TestEnum()
 {
     TestCore(DayOfWeek.Sunday, stream => ( DayOfWeek )Enum.Parse(typeof(DayOfWeek), Unpacking.UnpackString(stream)), (x, y) => x == y);
 }
 public void TestUri()
 {
     TestCore(new Uri("http://www.example.com"), stream => new Uri(Unpacking.UnpackString(stream)), null);
 }
 public void TestString()
 {
     TestCore("abc", stream => Unpacking.UnpackString(stream), null);
 }