public void Container_WriteInvalidSyncMarker()
        {
            Assert.Throws <SerializationException>(() =>
            {
                var expected = new List <ClassOfInt>
                {
                    ClassOfInt.Create(true),
                    ClassOfInt.Create(true),
                };

                var writer = AvroContainer.CreateWriter <ClassOfInt>(this.resultStream, Codec.Null);
                foreach (var obj in expected)
                {
                    var block = writer.CreateBlockAsync().Result;
                    block.Write(obj);
                    writer.WriteBlockAsync(block).Wait();
                    this.resultStream.Seek(this.resultStream.Position - 1, SeekOrigin.Begin);
                    int markerLastByte = this.resultStream.ReadByte();
                    this.resultStream.Seek(this.resultStream.Position - 1, SeekOrigin.Begin);
                    this.resultStream.WriteByte((byte)((markerLastByte + 10) % 255));
                }
                writer.Dispose();

                this.resultStream.Seek(0, SeekOrigin.Begin);

                var reader = AvroContainer.CreateReader <ClassOfInt>(this.resultStream);

                var actual = new List <ClassOfInt>();
                while (reader.MoveNext())
                {
                    actual.AddRange(reader.Current.Objects);
                }
            }
                                                   );
        }
コード例 #2
0
        public void SequentialWriter_MicrosoftWriterApacheReader()
        {
            var expected = new List <ClassOfInt>();

            for (var i = 0; i < 7; i++)
            {
                expected.Add(ClassOfInt.Create(true));
            }

            var w = AvroContainer.CreateWriter <ClassOfInt>(this.resultStream, Codec.Deflate);

            using (var writer = new SequentialWriter <ClassOfInt>(w, 2))
            {
                expected.ForEach(writer.Write);
            }

            this.resultStream.Seek(0, SeekOrigin.Begin);
            var reader = DataFileReader <GenericRecord> .OpenReader(this.resultStream);

            var actual = new List <GenericRecord>(reader);

            for (var i = 0; i < expected.Count; ++i)
            {
                Assert.AreEqual(expected[i].PrimitiveInt, actual[i]["PrimitiveInt"]);
            }
        }
        public void Container_SerializeThreeObjects()
        {
            var expected = new List <ClassOfInt>
            {
                ClassOfInt.Create(true),
                ClassOfInt.Create(true),
                ClassOfInt.Create(true)
            };

            using (var writer = AvroContainer.CreateWriter <ClassOfInt>(this.resultStream, Codec.Null))
            {
                var metadata = new Dictionary <string, byte[]> {
                    { Utilities.GetRandom <string>(false), Utilities.GetRandom <byte[]>(false) }
                };
                writer.SetMetadata(metadata);

                var block = writer.CreateBlockAsync().Result;
                expected.ForEach(block.Write);
                writer.WriteBlockAsync(block).Wait();
            }

            this.resultStream.Seek(0, SeekOrigin.Begin);
            using (var reader = AvroContainer.CreateReader <ClassOfInt>(this.resultStream))
            {
                reader.MoveNext();
                Assert.True(expected.SequenceEqual(reader.Current.Objects));
            }
        }
        public void Container_SyncAfterEachObject()
        {
            var expected = new List <ClassOfInt>
            {
                ClassOfInt.Create(true),
                ClassOfInt.Create(true),
                ClassOfInt.Create(true),
            };

            var writer = AvroContainer.CreateWriter <ClassOfInt>(this.resultStream, Codec.Null);

            expected.ForEach(
                e =>
            {
                var block = writer.CreateBlockAsync().Result;
                block.Write(e);
                writer.WriteBlockAsync(block).Wait();
            });
            writer.Dispose();

            this.resultStream.Seek(0, SeekOrigin.Begin);

            var reader = AvroContainer.CreateReader <ClassOfInt>(this.resultStream);

            var actual = new List <ClassOfInt>();

            while (reader.MoveNext())
            {
                actual.AddRange(reader.Current.Objects);
            }
            Assert.True(expected.SequenceEqual(actual));
        }
コード例 #5
0
ファイル: TestClasses.cs プロジェクト: mahmuthaktan/AvroTest
 public static ClassWithSchemaNullableField Create()
 {
     return(new ClassWithSchemaNullableField
     {
         NullableValueNullableSchema = Utilities.GetRandom <int>(false),
         NullableValueNotNullableSchema = Utilities.GetRandom <int>(false),
         NotNullableValueNullableSchema = Utilities.GetRandom <int>(false),
         NotNullableValueNotNullableSchema = Utilities.GetRandom <int>(false),
         ReferenceFieldNullableSchema = ClassOfInt.Create(true),
         ReferenceFieldNotNullableSchema = ClassOfInt.Create(true)
     });
 }
        public void Container_ApacheWriterMicrosoftReader()
        {
            var serializer = AvroSerializer.Create <ClassOfInt>(new AvroSerializerSettings {
                Resolver = new AvroDataContractResolver(true)
            });
            var schema = ApacheAvro.Schema.Parse(serializer.WriterSchema.ToString()) as ApacheAvro.UnionSchema;

            Assert.NotNull(schema);

            var recordSchema = schema.Schemas[1] as ApacheAvro.RecordSchema;

            Assert.NotNull(recordSchema);

            var expected = new List <GenericRecord>();

            for (var i = 0; i < 7; i++)
            {
                var record = new GenericRecord(recordSchema);
                record.Add("PrimitiveInt", ClassOfInt.Create(true).PrimitiveInt);
                expected.Add(record);
            }

            using (var memoryStream = new MemoryStream())
            {
                var datumWriter = new GenericWriter <GenericRecord>(schema);
                var writer      = DataFileWriter <GenericRecord> .OpenWriter(datumWriter, memoryStream);

                writer.WriteHeader();
                foreach (var obj in expected)
                {
                    writer.Append(obj);
                }
                writer.Flush();

                memoryStream.Seek(0, SeekOrigin.Begin);

                var reader = AvroContainer.CreateReader <ClassOfInt>(memoryStream, true, new AvroSerializerSettings {
                    Resolver = new AvroDataContractResolver(true)
                }, new CodecFactory());
                var actual = new List <ClassOfInt>();
                while (reader.MoveNext())
                {
                    actual.AddRange(reader.Current.Objects);
                }

                Assert.Equal(expected.Count, actual.Count);
                for (var i = 0; i < expected.Count; ++i)
                {
                    Assert.Equal(expected[i]["PrimitiveInt"], actual[i].PrimitiveInt);
                }
            }
        }
        public void Container_SyncAfterDeflateCodec()
        {
            var expected = new List <ClassOfInt>();

            for (var j = 0; j < 7; j++)
            {
                expected.Add(ClassOfInt.Create(true));
            }

            var writer = AvroContainer.CreateWriter <ClassOfInt>(this.resultStream, Codec.Deflate);

            var i = 0;

            while (i < expected.Count)
            {
                var block = writer.CreateBlockAsync().Result;
                for (var j = 0; j < 2; j++)
                {
                    if (i >= expected.Count)
                    {
                        break;
                    }
                    block.Write(expected[i]);
                    i++;
                }
                writer.WriteBlockAsync(block).Wait();
            }
            writer.Dispose();

            this.resultStream.Seek(0, SeekOrigin.Begin);

            var reader = AvroContainer.CreateReader <ClassOfInt>(this.resultStream);

            var actual = new List <ClassOfInt>();

            while (reader.MoveNext())
            {
                actual.AddRange(reader.Current.Objects);
            }
            Assert.True(expected.SequenceEqual(actual));
        }
        public void Container_MicrosoftWriterApacheReader()
        {
            var expected = new List <ClassOfInt>();

            for (var i = 0; i < 7; i++)
            {
                expected.Add(ClassOfInt.Create(true));
            }

            using (var memoryStream = new MemoryStream())
            {
                var writer = AvroContainer.CreateWriter <ClassOfInt>(memoryStream, Codec.Deflate);

                var i = 0;
                while (i < expected.Count)
                {
                    var block = writer.CreateBlockAsync().Result;
                    for (var j = 0; j < 2; j++)
                    {
                        if (i >= expected.Count)
                        {
                            break;
                        }
                        block.Write(expected[i]);
                        i++;
                    }
                    writer.WriteBlockAsync(block).Wait();
                }
                writer.Dispose();

                memoryStream.Seek(0, SeekOrigin.Begin);
                var reader = DataFileReader <GenericRecord> .OpenReader(memoryStream);

                var actual = new List <GenericRecord>(reader);

                for (var k = 0; k < expected.Count; ++k)
                {
                    Assert.Equal(expected[k].PrimitiveInt, actual[k]["PrimitiveInt"]);
                }
            }
        }
コード例 #9
0
ファイル: TestClasses.cs プロジェクト: mahmuthaktan/AvroTest
        public static ClassOfObjectDictionary Create()
        {
            var list = new List <ClassOfInt>();

            list.Add(ClassOfInt.Create(false));
            var dictionary = new Dictionary <string, ClassOfInt>();

            dictionary.Add("TestKey", ClassOfInt.Create(false));

            return(new ClassOfObjectDictionary
            {
                PatchSet = new Dictionary <string, object>()
                {
                    { "Name", "OData" },
                    { "Age", 15 },
                    { "Key", new Guid() },
                    { "ClassOfIntReference", ClassOfInt.Create(false) },
                    { "ClassOfIntReferenceArray", list },
                    { "ClassOfIntReferenceMap", dictionary }
                },
            });
        }
コード例 #10
0
        public void SequentialReaderWriter_SerializeThreeObjects()
        {
            var expected = new List <ClassOfInt>
            {
                ClassOfInt.Create(true),
                ClassOfInt.Create(true),
                ClassOfInt.Create(true),
            };

            var w = AvroContainer.CreateWriter <ClassOfInt>(this.resultStream, Codec.Null);

            using (var writer = new SequentialWriter <ClassOfInt>(w, 24))
            {
                expected.ForEach(writer.Write);
            }

            this.resultStream.Seek(0, SeekOrigin.Begin);
            var r = AvroContainer.CreateReader <ClassOfInt>(this.resultStream);

            using (var reader = new SequentialReader <ClassOfInt>(r))
            {
                Assert.IsTrue(expected.SequenceEqual(reader.Objects));
            }
        }
コード例 #11
0
        public void SequentialReaderWriter_SyncAfterDeflateCodec()
        {
            var expected = new List <ClassOfInt>();

            for (var i = 0; i < 7; i++)
            {
                expected.Add(ClassOfInt.Create(true));
            }

            var w = AvroContainer.CreateWriter <ClassOfInt>(this.resultStream, Codec.Deflate);

            using (var writer = new SequentialWriter <ClassOfInt>(w, 2))
            {
                expected.ForEach(writer.Write);
            }

            this.resultStream.Seek(0, SeekOrigin.Begin);
            var r = AvroContainer.CreateReader <ClassOfInt>(this.resultStream);

            using (var reader = new SequentialReader <ClassOfInt>(r))
            {
                Assert.IsTrue(expected.SequenceEqual(reader.Objects));
            }
        }
コード例 #12
0
        public void AvroRecord_SerializeInvalidObject()
        {
            const string Schema = @"{
                                ""type"":""record"",
                                ""name"":""Microsoft.Hadoop.Avro.Tests.SimpleIntClass"",
                                ""fields"":
                                    [
                                        { ""name"":""IntField"", ""type"":""int"" },
                                    ]
                            }";

            var serializer = AvroSerializer.CreateGeneric(Schema);

            using (var memoryStream = new MemoryStream())
            {
                Utilities.ShouldThrow <ArgumentNullException>(() => serializer.Serialize(memoryStream, null));
                Utilities.ShouldThrow <SerializationException>(() => serializer.Serialize(memoryStream, ClassOfInt.Create(false)));
            }
        }
コード例 #13
0
        public void AvroEnum_SerializeInvalidObject()
        {
            const string Schema = @"{
                        ""type"":""enum"",
                        ""name"":""Microsoft.Hadoop.Avro.Tests.TestEnum"",
                        ""symbols"":
                        [
                            ""EnumValue3"",
                            ""EnumValue2"",
                            ""EnumValue1""
                        ]
                    }";

            const string InvalidEnumSchema = @"{
                ""type"":""enum"",
                ""name"":""Microsoft.Hadoop.Avro.Tests.InvalidEnum"",
                ""symbols"":
                [
                    ""InvalidValue3"",
                    ""InvalidValue2"",
                    ""InvalidValue1""
                ]
            }";

            var invalidSchemaSerializer = AvroSerializer.CreateGeneric(InvalidEnumSchema);
            var invalidEnum             = new AvroEnum(invalidSchemaSerializer.WriterSchema)
            {
                Value = "InvalidValue3"
            };
            var serializer = AvroSerializer.CreateGeneric(Schema);

            using (var memoryStream = new MemoryStream())
            {
                Utilities.ShouldThrow <ArgumentNullException>(() => serializer.Serialize(memoryStream, null));
                Utilities.ShouldThrow <SerializationException>(() => serializer.Serialize(memoryStream, ClassOfInt.Create(false)));
                Utilities.ShouldThrow <SerializationException>(() => serializer.Serialize(memoryStream, invalidEnum));
            }
        }
 public static void VerifyEquality <T>(T obj1, T obj2, T obj3)
 {
     TestEquals(obj1, obj2, obj3);
     TestEquals(obj1, ClassOfInt.Create(false), ClassOfInt.Create(false));
 }
コード例 #15
0
ファイル: TestClasses.cs プロジェクト: mahmuthaktan/AvroTest
 public static TwoFieldsOfTheSameTypeClass Create()
 {
     return(new TwoFieldsOfTheSameTypeClass {
         Field1 = ClassOfInt.Create(true), Field2 = ClassOfInt.Create(true)
     });
 }
コード例 #16
0
ファイル: TestClasses.cs プロジェクト: mahmuthaktan/AvroTest
 public static ClassWithFields Create()
 {
     return(new ClassWithFields {
         Field1 = ClassOfInt.Create(true), Field2 = ClassOfInt.Create(true)
     });
 }
コード例 #17
0
ファイル: TestClasses.cs プロジェクト: mahmuthaktan/AvroTest
 public static NonDataContractClassWithFields Create()
 {
     return(new NonDataContractClassWithFields {
         Field1 = ClassOfInt.Create(true)
     });
 }