Пример #1
0
        public void HeterogeneousTest()
        {
            var heterogeneousSchema = @"
            [
                ""string"",
                ""null"",
                { ""type"" : ""record"", ""name"" : ""Dervied2"", ""fields"" :
                    [
                        { ""name"" : ""A"", ""type"" : ""string""},
                        { ""name"" : ""C"", ""type"" : ""double""}
                    ]
                },

            ]
            ";
            var schema        = Schema.Parse(heterogeneousSchema);
            var derived2write = new Derived2()
            {
                A = "derived2", C = 3.14
            };

            // union types (except for [null, type]) need to be manually registered
            var unionSchema = schema as UnionSchema;
            var cache       = new ClassCache();

            cache.LoadClassCache(typeof(Derived2), unionSchema[2]);

            var writer = new ReflectWriter <object>(schema, cache);
            var reader = new ReflectReader <object>(schema, schema, cache);

            using (var stream = new MemoryStream(256))
            {
                var encoder = new BinaryEncoder(stream);
                writer.Write(derived2write, encoder);
                writer.Write("string value", encoder);
                writer.Write(null, encoder);
                stream.Seek(0, SeekOrigin.Begin);

                var decoder      = new BinaryDecoder(stream);
                var derived2read = (Derived2)reader.Read(decoder);
                var stringRead   = (string)reader.Read(decoder);
                var nullRead     = reader.Read(decoder);
                Assert.AreEqual(derived2read.A, derived2write.A);
                Assert.AreEqual(derived2read.C, derived2write.C);
                Assert.AreEqual(stringRead, "string value");
                Assert.IsNull(nullRead);
            }
        }
Пример #2
0
        public void ByteArray()
        {
            var schema        = Schema.Parse(_fixedSchema);
            var fixedRecWrite = new ByteArrayFixedRec()
            {
                myFixed = new byte[16] {
                    1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6
                }
            };
            var fixedRecBad = new ByteArrayFixedRec()
            {
                myFixed = new byte[10]
            };
            ByteArrayFixedRec fixedRecRead = null;

            var writer = new ReflectWriter <ByteArrayFixedRec>(schema);
            var reader = new ReflectReader <ByteArrayFixedRec>(schema, schema);

            Assert.Throws(typeof(AvroException), () => {
                using (var stream = new MemoryStream(256))
                {
                    writer.Write(fixedRecBad, new BinaryEncoder(stream));
                }
            });
            using (var stream = new MemoryStream(256))
            {
                writer.Write(fixedRecWrite, new BinaryEncoder(stream));
                stream.Seek(0, SeekOrigin.Begin);
                fixedRecRead = reader.Read(null, new BinaryDecoder(stream));
                Assert.IsTrue(fixedRecRead.myFixed.Length == 16);
                Assert.IsTrue(fixedRecWrite.myFixed.SequenceEqual(fixedRecRead.myFixed));
            }
        }
Пример #3
0
        private static void checkAlternateDeserializers <S>(S expected, Stream input, long startPos, Schema ws, Schema rs) where S : class
        {
            input.Position = startPos;
            var     reader = new ReflectReader <S>(ws, rs);
            Decoder d      = new BinaryDecoder(input);
            S       output = reader.Read(null, d);

            Assert.AreEqual(input.Length, input.Position); // Ensure we have read everything.
            AssertReflectRecordEqual(rs, expected, ws, output, reader.Reader.ClassCache);
        }
Пример #4
0
        private static S deserialize <S>(Stream ms, Schema ws, Schema rs) where S : class
        {
            long    initialPos = ms.Position;
            var     r          = new ReflectReader <S>(ws, rs);
            Decoder d          = new BinaryDecoder(ms);
            S       output     = r.Read(null, d);

            Assert.AreEqual(ms.Length, ms.Position); // Ensure we have read everything.
            checkAlternateDeserializers(output, ms, initialPos, ws, rs);
            return(output);
        }
Пример #5
0
        public void BaseClassTest()
        {
            var schema        = Schema.Parse(BaseClassSchema);
            var derived1write = new Derived1()
            {
                A = "derived1", B = 7
            };
            var derived2write = new Derived2()
            {
                A = "derived2", C = 3.14
            };

            // union types (except for [null, type]) need to be manually registered
            var unionSchema = schema as UnionSchema;
            var cache       = new ClassCache();

            cache.LoadClassCache(typeof(Derived1), unionSchema[0]);
            cache.LoadClassCache(typeof(Derived2), unionSchema[1]);
            var x = schema as RecordSchema;

            var writer = new ReflectWriter <BaseClass>(schema, cache);
            var reader = new ReflectReader <BaseClass>(schema, schema, cache);

            using (var stream = new MemoryStream(256))
            {
                var encoder = new BinaryEncoder(stream);
                writer.Write(derived1write, encoder);
                writer.Write(derived2write, encoder);
                stream.Seek(0, SeekOrigin.Begin);

                var decoder      = new BinaryDecoder(stream);
                var derived1read = (Derived1)reader.Read(decoder);
                var derived2read = (Derived2)reader.Read(decoder);
                Assert.AreEqual(derived1read.A, derived1write.A);
                Assert.AreEqual(derived1read.B, derived1write.B);
                Assert.AreEqual(derived2read.A, derived2write.A);
                Assert.AreEqual(derived2read.C, derived2write.C);
            }
        }
Пример #6
0
        public void NullableTest()
        {
            var nullableSchema = @"
            [
                ""null"",
                { ""type"" : ""record"", ""name"" : ""Dervied2"", ""fields"" :
                    [
                        { ""name"" : ""A"", ""type"" : ""string""},
                        { ""name"" : ""C"", ""type"" : ""double""}
                    ]
                },

            ]
            ";
            var schema         = Schema.Parse(nullableSchema);
            var derived2write  = new Derived2()
            {
                A = "derived2", C = 3.14
            };

            var writer = new ReflectWriter <Derived2>(schema);
            var reader = new ReflectReader <Derived2>(schema, schema);

            using (var stream = new MemoryStream(256))
            {
                var encoder = new BinaryEncoder(stream);
                writer.Write(derived2write, encoder);
                writer.Write(null, encoder);
                stream.Seek(0, SeekOrigin.Begin);

                var decoder      = new BinaryDecoder(stream);
                var derived2read = reader.Read(decoder);
                var derived2null = reader.Read(decoder);
                Assert.AreEqual(derived2read.A, derived2write.A);
                Assert.AreEqual(derived2read.C, derived2write.C);
                Assert.IsNull(derived2null);
            }
        }
Пример #7
0
        public void RecurseTest()
        {
            var schema    = Schema.Parse(_recurse);
            var recursive = new Recursive
            {
                Fo = new SimpleFoo
                {
                    Age       = 67,
                    Attending = true,
                    FactTime  = 90909099L,
                    Id        = new byte[0] {
                    },
                    Name      = "Ebere",
                    Point     = 888D,
                    Precision = 787F
                },
                Recurse = new Recursive
                {
                    Fo = new SimpleFoo
                    {
                        Age       = 6,
                        Attending = false,
                        FactTime  = 90L,
                        Id        = new byte[0] {
                        },
                        Name      = "Ebere Abanonu",
                        Point     = 88D,
                        Precision = 78F
                    },
                }
            };

            var writer = new ReflectWriter <Recursive>(schema);
            var reader = new ReflectReader <Recursive>(schema, schema);

            using (var stream = new MemoryStream(256))
            {
                writer.Write(recursive, new BinaryEncoder(stream));
                stream.Seek(0, SeekOrigin.Begin);
                var recursiveRead = reader.Read(new BinaryDecoder(stream));
                Assert.IsTrue(recursiveRead.Fo.Attending);
                Assert.AreEqual(recursiveRead.Recurse.Fo.Name, "Ebere Abanonu");
            }
        }
Пример #8
0
        public void ListTest()
        {
            var schema        = Schema.Parse(_simpleList);
            var fixedRecWrite = new List <string>()
            {
                "value"
            };

            var writer = new ReflectWriter <List <string> >(schema);
            var reader = new ReflectReader <List <string> >(schema, schema);

            using (var stream = new MemoryStream(256))
            {
                writer.Write(fixedRecWrite, new BinaryEncoder(stream));
                stream.Seek(0, SeekOrigin.Begin);
                var fixedRecRead = reader.Read(new BinaryDecoder(stream));
                Assert.IsTrue(fixedRecRead.Count == 1);
                Assert.AreEqual(fixedRecWrite[0], fixedRecRead[0]);
            }
        }
Пример #9
0
        public Z SerializeDeserialize(Z z)
        {
            try
            {
                Protocol protocol = Protocol.Parse(_avroTestSchemaV1);
                Schema   schema   = null;
                foreach (var s in protocol.Types)
                {
                    if (s.Name == "Z")
                    {
                        schema = s;
                    }
                }

                var avroWriter = new ReflectWriter <Z>(schema);
                var avroReader = new ReflectReader <Z>(schema, schema);

                byte[] serialized;

                using (var stream = new MemoryStream(256))
                {
                    avroWriter.Write(z, new BinaryEncoder(stream));
                    serialized = stream.ToArray();
                }

                Z deserialized = null;
                using (var stream = new MemoryStream(serialized))
                {
                    deserialized = avroReader.Read(default(Z), new BinaryDecoder(stream));
                }

                return(deserialized);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                throw ex;
            }
        }
Пример #10
0
        public void ListRecTest()
        {
            var schema        = Schema.Parse(_recordList);
            var fixedRecWrite = new List <ListRec>()
            {
                new ListRec()
                {
                    S = "hello"
                }
            };

            var writer = new ReflectWriter <List <ListRec> >(schema);
            var reader = new ReflectReader <List <ListRec> >(schema, schema);

            using (var stream = new MemoryStream(256))
            {
                writer.Write(fixedRecWrite, new BinaryEncoder(stream));
                stream.Seek(0, SeekOrigin.Begin);
                var fixedRecRead = reader.Read(new BinaryDecoder(stream));
                Assert.IsTrue(fixedRecRead.Count == 1);
                Assert.AreEqual(fixedRecWrite[0].S, fixedRecRead[0].S);
            }
        }
Пример #11
0
        public void GenericFixedDefaultConverter()
        {
            var         schema = Schema.Parse(_fixedSchema);
            var         rs     = schema as RecordSchema;
            FixedSchema fs     = null;

            foreach (var f in rs.Fields)
            {
                if (f.Name == "myFixed")
                {
                    fs = f.Schema as FixedSchema;
                }
            }
            var fixedRecWrite = new GenericFixedRec()
            {
                myFixed = new GenericFixed(fs)
                {
                    Value = new byte[16] {
                        1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6
                    }
                }
            };
            GenericFixedRec fixedRecRead = null;

            ClassCache.AddDefaultConverter <byte[], GenericFixed>((a, s) => new GenericFixed(s as FixedSchema, a), (p, s) => p.Value);
            var writer = new ReflectWriter <GenericFixedRec>(schema);
            var reader = new ReflectReader <GenericFixedRec>(schema, schema);

            using (var stream = new MemoryStream(256))
            {
                writer.Write(fixedRecWrite, new BinaryEncoder(stream));
                stream.Seek(0, SeekOrigin.Begin);
                fixedRecRead = reader.Read(null, new BinaryDecoder(stream));
                Assert.IsTrue(fixedRecRead.myFixed.Value.Length == 16);
                Assert.IsTrue(fixedRecWrite.myFixed.Value.SequenceEqual(fixedRecRead.myFixed.Value));
            }
        }