コード例 #1
0
ファイル: AvroGenTests.cs プロジェクト: opwvhk/avro
        private Assembly TestSchema(
            string schema,
            IEnumerable <string> typeNamesToCheck = null,
            IEnumerable <KeyValuePair <string, string> > namespaceMapping = null,
            IEnumerable <string> generatedFilesToCheck = null)
        {
            // Create temp folder
            string outputDir = AvroGenHelper.CreateEmptyTemporyFolder(out string uniqueId);

            try
            {
                // Save schema
                string schemaFileName = Path.Combine(outputDir, $"{uniqueId}.avsc");
                System.IO.File.WriteAllText(schemaFileName, schema);

                // Generate from schema file
                Assert.That(AvroGenTool.GenSchema(schemaFileName, outputDir, namespaceMapping ?? new Dictionary <string, string>()), Is.EqualTo(0));

                // Check if all generated files exist
                if (generatedFilesToCheck != null)
                {
                    foreach (string generatedFile in generatedFilesToCheck)
                    {
                        Assert.That(new FileInfo(Path.Combine(outputDir, generatedFile)), Does.Exist);
                    }
                }

                // Compile into netstandard library and load assembly
                Assembly assembly = AvroGenHelper.CompileCSharpFilesIntoLibrary(
                    new DirectoryInfo(outputDir)
                    .EnumerateFiles("*.cs", SearchOption.AllDirectories)
                    .Select(fi => fi.FullName),
                    uniqueId);

                if (typeNamesToCheck != null)
                {
                    // Check if the compiled code has the same number of types defined as the check list
                    Assert.That(typeNamesToCheck.Count(), Is.EqualTo(assembly.DefinedTypes.Count()));

                    // Check if types available in compiled assembly
                    foreach (string typeName in typeNamesToCheck)
                    {
                        Type type = assembly.GetType(typeName);
                        Assert.That(type, Is.Not.Null);

                        // Instantiate
                        object obj = Activator.CreateInstance(type);
                        Assert.That(obj, Is.Not.Null);
                    }
                }

                return(assembly);
            }
            finally
            {
                Directory.Delete(outputDir, true);
            }
        }
コード例 #2
0
ファイル: AvroGenTests.cs プロジェクト: opwvhk/avro
        public void NotSupportedSchema(string schema, Type expectedException)
        {
            // Create temp folder
            string outputDir = AvroGenHelper.CreateEmptyTemporyFolder(out string uniqueId);

            try
            {
                // Save schema
                string schemaFileName = Path.Combine(outputDir, $"{uniqueId}.avsc");
                System.IO.File.WriteAllText(schemaFileName, schema);

                Assert.That(AvroGenTool.GenSchema(schemaFileName, outputDir, new Dictionary <string, string>()), Is.EqualTo(1));
            }
            finally
            {
                Directory.Delete(outputDir, true);
            }
        }
コード例 #3
0
        public static AvroGenToolResult RunAvroGenTool(params string[] args)
        {
            // Save stdout and stderr
            TextWriter conOut = Console.Out;
            TextWriter conErr = Console.Error;

            try
            {
                AvroGenToolResult result        = new AvroGenToolResult();
                StringBuilder     strBuilderOut = new StringBuilder();
                StringBuilder     strBuilderErr = new StringBuilder();

                using (StringWriter writerOut = new StringWriter(strBuilderOut))
                    using (StringWriter writerErr = new StringWriter(strBuilderErr))
                    {
                        writerOut.NewLine = "\n";
                        writerErr.NewLine = "\n";

                        // Overwrite stdout and stderr to be able to capture console output
                        Console.SetOut(writerOut);
                        Console.SetError(writerErr);

                        result.ExitCode = AvroGenTool.Main(args.ToArray());

                        writerOut.Flush();
                        writerErr.Flush();

                        result.StdOut = strBuilderOut.Length == 0 ? Array.Empty <string>() : strBuilderOut.ToString().Split(writerOut.NewLine);
                        result.StdErr = strBuilderErr.Length == 0 ? Array.Empty <string>() : strBuilderErr.ToString().Split(writerErr.NewLine);
                    }

                return(result);
            }
            finally
            {
                // Restore console
                Console.SetOut(conOut);
                Console.SetError(conErr);
            }
        }