示例#1
0
        public void ClientCodeGenerationDispatcher_Error_Missing_Custom()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language          = "C#",
                ClientProjectPath = "ClientProject",
                ServerProjectPath = "ServerProject"
            };

            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            // Create a new dispatcher and call an internal extensibility point to add ourselves
            // into the MEF composition container
            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                string[] compositionAssemblies = new string[] { Assembly.GetExecutingAssembly().Location };

                IDomainServiceClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, "NotAGenerator");
                Assert.IsNull(generator, "the dispatcher should not find any code generator");

                string error = string.Format(CultureInfo.CurrentCulture,
                                             Resource.Code_Generator_Not_Found,
                                             "NotAGenerator",
                                             options.Language,
                                             options.ServerProjectPath,
                                             options.ClientProjectPath,
                                             CodeDomClientCodeGenerator.GeneratorName);
                TestHelper.AssertContainsErrors(logger, error);
            }
        }
示例#2
0
        public void ClientCodeGenerationDispatcher_Custom_Warns_Full()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = "C#"
            };

            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            // Create a new dispatcher and call an internal extensibility point to add ourselves
            // into the MEF composition container
            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                string[] compositionAssemblies = new string[] { Assembly.GetExecutingAssembly().Location };

                IDomainServiceClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, MockCodeGenerator.GeneratorName);
                Assert.IsNotNull(generator, "the dispatcher did not find any code generator");
                Assert.AreEqual(generator.GetType(), typeof(MockCodeGenerator), "dispatcher found " + generator.GetType() + " but should have found MockCodeGenerator");

                // Setting this option makes our custom code generator emit the packet below to test LogWarning
                MockCodeGenerator.LogWarningsFull = true;
                string generatedCode = generator.GenerateCode(host, Enumerable.Empty <DomainServiceDescription>(), options);

                Assert.AreEqual(MockCodeGenerator.FakeGeneratedCode, generatedCode, "test code generator did not generate expected code.");
                TestHelper.AssertContainsWarningPackets(logger, MockCodeGenerator.WarningPacket);
            }
        }
示例#3
0
        public void ClientCodeGenerationDispatcher_Finds_Default_By_Name()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = "C#"
            };

            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            // Create a new dispatcher and call an internal extensibility point to add ourselves
            // into the MEF composition container
            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                string[] compositionAssemblies = new string[] { Assembly.GetExecutingAssembly().Location, typeof(T4DomainServiceClientCodeGenerator).Assembly.Location };

                IDomainServiceClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, CodeDomClientCodeGenerator.GeneratorName);
                Assert.IsNotNull(generator, "the dispatcher did not find any code generator");
                Assert.IsTrue(typeof(CodeDomClientCodeGenerator).IsAssignableFrom(generator.GetType()), "dispatcher found " + generator.GetType() + " but should have found CodeDomClientCodeGenerator");

                DomainServiceDescription dsd = DomainServiceDescription.GetDescription(typeof(DispatcherDomainService));
                string generatedCode         = generator.GenerateCode(host, new DomainServiceDescription[] { dsd }, options);

                Assert.IsFalse(string.IsNullOrEmpty(generatedCode), "expected code to have been generated");
                Assert.IsTrue(generatedCode.Contains("public sealed partial class DispatcherDomainContext : DomainContext"), "Expected generated code to contain public sealed partial class DispatcherDomainContext : DomainContext");
            }
        }
示例#4
0
        public void ClientCodeGenerationDispatcher_Finds_Derived_Custom()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = "G#"
            };

            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            // Create a new dispatcher and call an internal extensibility point to add ourselves
            // into the MEF composition container
            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                string[] compositionAssemblies = new string[] { Assembly.GetExecutingAssembly().Location };

                IDomainServiceClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, MockGSharpCodeGeneratorDerived.GeneratorName);
                Assert.IsNotNull(generator, "the dispatcher did not find any code generator");
                Assert.AreEqual(generator.GetType(), typeof(MockGSharpCodeGeneratorDerived), "dispatcher found " + generator.GetType() + " but should have found MockGSharpCodeGeneratorDerived");

                string generatedCode = generator.GenerateCode(host, Enumerable.Empty <DomainServiceDescription>(), options);

                Assert.AreEqual(MockGSharpCodeGeneratorDerived.DerivedGeneratedCode, generatedCode, "test code generator did not generate expected code.");
            }
        }
示例#5
0
        public void ClientCodeGenerationDispatcher_Custom_By_AssemblyQualifiedName()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = "C#"
            };
            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            // Create a new dispatcher and call an internal extensibility point to add ourselves
            // into the MEF composition container
            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                string[] compositionAssemblies = new string[0];

                IDomainServiceClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, typeof(T4DomainServiceClientCodeGenerator).AssemblyQualifiedName);
                Assert.IsNotNull(generator, "the dispatcher did not find the code generator");
                Assert.AreEqual(generator.GetType(), typeof(T4DomainServiceClientCodeGenerator), "dispatcher found " + generator.GetType() + " but should have found T4DomainServiceClientProxyGenerator");

                string generatedCode = generator.GenerateCode(host, Enumerable.Empty <DomainServiceDescription>(), options);

                Assert.IsFalse(string.IsNullOrEmpty(generatedCode), "expected T4 generator to generate code");
                TestHelper.AssertGeneratedCodeContains(generatedCode, T4DomainServiceClientCodeGenerator.GeneratedBoilerPlate);
                TestHelper.AssertNoErrorsOrWarnings(logger);
            }
        }
示例#6
0
        public void ClientCodeGenerationDispatcher_Generate_Using_T4_Custom()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = "C#"
            };

            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            // Create a new dispatcher and call an internal extensibility point to add ourselves
            // into the MEF composition container
            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                string[] compositionAssemblies = new string[] { typeof(T4DomainServiceClientCodeGenerator).Assembly.Location };

                IDomainServiceClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, null);
                Assert.IsNotNull(generator, "the dispatcher did not find any code generator");
                Assert.AreEqual(generator.GetType(), typeof(T4DomainServiceClientCodeGenerator), "dispatcher found " + generator.GetType() + " but should have found T4DomainServiceClientProxyGenerator");

                DomainServiceDescription dsd = DomainServiceDescription.GetDescription(typeof(DispatcherDomainService));
                string generatedCode         = generator.GenerateCode(host, new DomainServiceDescription[] { dsd }, options);

                Assert.IsFalse(string.IsNullOrEmpty(generatedCode), "expected T4 generator to generate code");
                TestHelper.AssertGeneratedCodeContains(generatedCode, T4DomainServiceClientCodeGenerator.GeneratedBoilerPlate);
                TestHelper.AssertGeneratedCodeContains(generatedCode, "public class DispatcherEntity : Entity");
                TestHelper.AssertNoErrorsOrWarnings(logger);
            }
        }
示例#7
0
        public void ClientCodeGenerationDispatcher_Throws_Exception_Logical_Name()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language          = "C#",
                ClientProjectPath = "SampleProject.csproj"
            };

            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                string[] compositionAssemblies = new string[] { Assembly.GetExecutingAssembly().Location };

                string codeGeneratorName = MockCodeGenerator.GeneratorName;

                // Ask our mock to throw
                MockCodeGenerator.ThrowException = true;

                dispatcher.GenerateCode(host, options, Enumerable.Empty <Type>(), compositionAssemblies, codeGeneratorName);
                string error = string.Format(CultureInfo.CurrentCulture, Resource.CodeGenerator_Threw_Exception, codeGeneratorName, options.ClientProjectPath, MockCodeGenerator.Exception.Message);
                TestHelper.AssertContainsErrors(logger, error);
            }
        }
示例#8
0
        public void ClientCodeGenerationDispatcher_Throws_Exception_Fully_Qualified_Name()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language          = "C#",
                ClientProjectPath = "SampleProject.csproj"
            };

            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                // Disable MEF for this test
                string[] compositionAssemblies = new string[0];

                // And use FQN instead
                string codeGeneratorName = typeof(MockCodeGenerator).AssemblyQualifiedName;

                // Ask our mock to throw
                MockCodeGenerator.ThrowException = true;

                dispatcher.GenerateCode(host, options, Enumerable.Empty <Type>(), compositionAssemblies, codeGeneratorName);
                string error = string.Format(CultureInfo.CurrentCulture, Resource.CodeGenerator_Threw_Exception, codeGeneratorName, options.ClientProjectPath, MockCodeGenerator.Exception.Message);
                TestHelper.AssertContainsErrors(logger, error);
            }
        }
示例#9
0
        public void ClientCodeGenerationDispatcher_Error_Multiple_Generators()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language          = "C#",
                ClientProjectPath = "ClientProject"
            };

            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            // Create a new dispatcher and call an internal extensibility point to add ourselves
            // into the MEF composition container
            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                string[] compositionAssemblies = new string[] { Assembly.GetExecutingAssembly().Location, typeof(T4DomainServiceClientCodeGenerator).Assembly.Location };

                IDomainServiceClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, /*generatorName*/ null);
                Assert.IsNotNull(generator, "the dispatcher did not pick a generator");
                Assert.IsTrue(generator is CodeDomClientCodeGenerator, "the dispatcher did not pick the default code generator");

                string errorParam = "    " + MockCodeGenerator.GeneratorName + Environment.NewLine +
                                    "    " + T4DomainServiceClientCodeGenerator.GeneratorName + Environment.NewLine;

                string error = (string.Format(CultureInfo.CurrentCulture,
                                              Resource.Multiple_Custom_Code_Generators_Using_Default,
                                              options.Language,
                                              errorParam,
                                              options.ClientProjectPath,
                                              MockCodeGenerator.GeneratorName,
                                              CodeDomClientCodeGenerator.GeneratorName));
                TestHelper.AssertContainsWarnings(logger, error);
            }
        }
        public void ClientCodeGenerationDispatcher_Finds_Derived_Custom()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = "G#"
            };

            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            // Create a new dispatcher and call an internal extensibility point to add ourselves
            // into the MEF composition container
            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                string[] compositionAssemblies = new string[] { Assembly.GetExecutingAssembly().Location };

                IDomainServiceClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, MockGSharpCodeGeneratorDerived.GeneratorName);
                Assert.IsNotNull(generator, "the dispatcher did not find any code generator");
                Assert.AreEqual(generator.GetType(), typeof(MockGSharpCodeGeneratorDerived), "dispatcher found " + generator.GetType() + " but should have found MockGSharpCodeGeneratorDerived");

                string generatedCode = generator.GenerateCode(host, Enumerable.Empty<DomainServiceDescription>(), options);

                Assert.AreEqual(MockGSharpCodeGeneratorDerived.DerivedGeneratedCode, generatedCode, "test code generator did not generate expected code.");
            }
        }
示例#11
0
        public void ClientCodeGenerationDispatcher_Finds_Solitary_Custom()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = "C#"
            };
            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            // Create a new dispatcher and call an internal extensibility point to add ourselves
            // into the MEF composition container
            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                string[] compositionAssemblies = new string[] { Assembly.GetExecutingAssembly().Location, typeof(TextTemplate::OpenRiaServices.DomainServices.Tools.TextTemplate.ClientCodeGenerator).Assembly.Location };

                IDomainServiceClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, /*generatorName*/ null);
                Assert.IsNotNull(generator, "the dispatcher did not find any code generator");
                Assert.AreEqual(generator.GetType(), typeof(MockCodeGenerator), "dispatcher found " + generator.GetType() + " but should have found MockCodeGenerator");

                string generatedCode = generator.GenerateCode(host, Enumerable.Empty <DomainServiceDescription>(), options);

                Assert.AreEqual(MockCodeGenerator.FakeGeneratedCode, generatedCode, "test code generator did not generate expected code.");

                // Expect informational message
                string message = string.Format(CultureInfo.CurrentCulture, Resource.Using_Custom_Code_Generator, MockCodeGenerator.GeneratorName);
                TestHelper.AssertContainsMessages(logger, message);
            }
        }
示例#12
0
 // Locates the code generator registered to work with the language for the given options
 internal static IClientCodeGenerator CreateCodeGenerator(ICodeGenerationHost host, ClientCodeGenerationOptions options)
 {
     using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
     {
         IClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, /*compositionAssemblies*/ null, /*codeGeneratorName*/ null);
         return(generator);
     }
 }
示例#13
0
        public void ClientCodeGenerationDispatcher_Error_TypeLoadException()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = "C#",
            };

            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            // Create a new dispatcher and call an internal extensibility point to add ourselves
            // into the MEF composition container
            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                // We want to include into the MEF container an assembly that will throw TypeLoadException
                // when MEF tries to analyze it.  This is to test our own recovery, which should consist
                // of logging an error making a default container containing only Tools.
                string unitTestAssemblyLocation         = Assembly.GetExecutingAssembly().Location;
                string typeLoadExceptionProjectLocation = Path.Combine(Path.GetDirectoryName(unitTestAssemblyLocation), "TypeLoadExceptionProject.dll");

                Assert.IsTrue(File.Exists(typeLoadExceptionProjectLocation), "Expected TypeLoadExceptionProject.dll to coreside with this assembly in test folder");

                // Do what MEF does to load the types so we can capture the exception
                Exception expectedException = null;
                try
                {
                    Assembly assembly = Assembly.LoadFrom(typeLoadExceptionProjectLocation);
                    assembly.GetTypes();
                }
                catch (Exception ex)
                {
                    expectedException = ex;
                }
                Assert.IsNotNull(expectedException, "We did not generate the type load exception we expected");

                string[] compositionAssemblies = new string[] { unitTestAssemblyLocation, typeLoadExceptionProjectLocation };

                IDomainServiceClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, /*generatorName*/ null);
                Assert.IsNotNull(generator, "the dispatcher did not pick default generator");
                Assert.IsTrue(generator is CodeDomClientCodeGenerator, "the dispatcher did not pick the default code generator");

                string error = (string.Format(CultureInfo.CurrentCulture,
                                              Resource.Failed_To_Create_Composition_Container,
                                              expectedException.Message));
                TestHelper.AssertContainsWarnings(logger, error);
            }
        }
示例#14
0
        public void ClientCodeGenerationDispatcher_Custom_By_AssemblyQualifiedName_Ctor_Throws()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = "C#"
            };
            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            string codeGeneratorName = typeof(ThrowingCtorCodeGenerator).AssemblyQualifiedName;

            // Create a new dispatcher and call an internal extensibility point to add ourselves
            // into the MEF composition container
            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                string[] compositionAssemblies = new string[0];

                IDomainServiceClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, codeGeneratorName);
                Assert.IsNull(generator, "the dispatcher should not find the code generator");
                string error = string.Format(CultureInfo.CurrentCulture, Resource.Code_Generator_Instantiation_Error, codeGeneratorName, ThrowingCtorCodeGenerator.ErrorMessage);
                TestHelper.AssertContainsErrors(logger, error);
            }
        }
示例#15
0
        public void ClientCodeGenerationDispatcher_Error_Multiple_Generators_Same_Name()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language          = "F#",
                ClientProjectPath = "ClientProject",
                ServerProjectPath = "ServerProject"
            };

            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            // Create a new dispatcher and call an internal extensibility point to add ourselves
            // into the MEF composition container
            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                string[] compositionAssemblies = new string[] { Assembly.GetExecutingAssembly().Location };

                string generatorName = MockFSharpCodeGenerator1.GeneratorName;

                IDomainServiceClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, generatorName);
                Assert.IsNull(generator, "the dispatcher should not pick a generator");

                string errorParam = "    " + typeof(MockFSharpCodeGenerator1).FullName + Environment.NewLine +
                                    "    " + typeof(MockFSharpCodeGenerator2).FullName + Environment.NewLine;

                string error = string.Format(CultureInfo.CurrentCulture,
                                             Resource.Multiple_Named_Code_Generators,
                                             generatorName,
                                             options.Language,
                                             errorParam,
                                             options.ServerProjectPath,
                                             options.ClientProjectPath,
                                             typeof(MockFSharpCodeGenerator1).AssemblyQualifiedName);
                TestHelper.AssertContainsErrors(logger, error);
            }
        }
        public void ClientCodeGenerationDispatcher_Custom_By_AssemblyQualifiedName()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = "C#"
            };
            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            // Create a new dispatcher and call an internal extensibility point to add ourselves
            // into the MEF composition container
            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                string[] compositionAssemblies = new string[0];

                IDomainServiceClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, typeof(T4DomainServiceClientCodeGenerator).AssemblyQualifiedName);
                Assert.IsNotNull(generator, "the dispatcher did not find the code generator");
                Assert.AreEqual(generator.GetType(), typeof(T4DomainServiceClientCodeGenerator), "dispatcher found " + generator.GetType() + " but should have found T4DomainServiceClientProxyGenerator");

                string generatedCode = generator.GenerateCode(host, Enumerable.Empty<DomainServiceDescription>(), options);

                Assert.IsFalse(string.IsNullOrEmpty(generatedCode), "expected T4 generator to generate code");
                TestHelper.AssertGeneratedCodeContains(generatedCode, T4DomainServiceClientCodeGenerator.GeneratedBoilerPlate);
                TestHelper.AssertNoErrorsOrWarnings(logger);
            }
        }
        public void ClientCodeGenerationDispatcher_Custom_By_AssemblyQualifiedName_Ctor_Throws()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = "C#"
            };
            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            string codeGeneratorName = typeof(ThrowingCtorCodeGenerator).AssemblyQualifiedName;

            // Create a new dispatcher and call an internal extensibility point to add ourselves
            // into the MEF composition container
            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                string[] compositionAssemblies = new string[0];

                IDomainServiceClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, codeGeneratorName);
                Assert.IsNull(generator, "the dispatcher should not find the code generator");
                string error = string.Format(CultureInfo.CurrentCulture, Resource.Code_Generator_Instantiation_Error, codeGeneratorName, ThrowingCtorCodeGenerator.ErrorMessage);
                TestHelper.AssertContainsErrors(logger, error);
            }
        }
        public void ClientCodeGenerationDispatcher_Throws_Exception_Logical_Name()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = "C#",
                ClientProjectPath = "SampleProject.csproj"
            };

            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                string[] compositionAssemblies = new string[] { Assembly.GetExecutingAssembly().Location };

                string codeGeneratorName = MockCodeGenerator.GeneratorName;

                // Ask our mock to throw
                MockCodeGenerator.ThrowException = true;

                dispatcher.GenerateCode(host, options, Enumerable.Empty<Type>(), compositionAssemblies, codeGeneratorName);
                string error = string.Format(CultureInfo.CurrentCulture, Resource.CodeGenerator_Threw_Exception, codeGeneratorName, options.ClientProjectPath, MockCodeGenerator.Exception.Message);
                TestHelper.AssertContainsErrors(logger, error);
            }
        }
        public void ClientCodeGenerationDispatcher_Throws_Exception_Fully_Qualified_Name()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = "C#",
                ClientProjectPath = "SampleProject.csproj"
            };

            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                // Disable MEF for this test
                string[] compositionAssemblies = new string[0];

                // And use FQN instead
                string codeGeneratorName = typeof(MockCodeGenerator).AssemblyQualifiedName;

                // Ask our mock to throw
                MockCodeGenerator.ThrowException = true;

                dispatcher.GenerateCode(host, options, Enumerable.Empty<Type>(), compositionAssemblies, codeGeneratorName);
                string error = string.Format(CultureInfo.CurrentCulture, Resource.CodeGenerator_Threw_Exception, codeGeneratorName, options.ClientProjectPath, MockCodeGenerator.Exception.Message);
                TestHelper.AssertContainsErrors(logger, error);
            }
        }
        public void ClientCodeGenerationDispatcher_Error_Multiple_Generators()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = "C#",
                ClientProjectPath = "ClientProject"
            };

            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            // Create a new dispatcher and call an internal extensibility point to add ourselves
            // into the MEF composition container
            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                string[] compositionAssemblies = new string[] { Assembly.GetExecutingAssembly().Location, typeof(T4DomainServiceClientCodeGenerator).Assembly.Location };

                IDomainServiceClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, /*generatorName*/ null);
                Assert.IsNotNull(generator, "the dispatcher did not pick a generator");
                Assert.IsTrue(generator is CodeDomClientCodeGenerator, "the dispatcher did not pick the default code generator");

                string errorParam = "    " + MockCodeGenerator.GeneratorName + Environment.NewLine +
                                    "    " + T4DomainServiceClientCodeGenerator.GeneratorName + Environment.NewLine;

                string error = (string.Format(CultureInfo.CurrentCulture, 
                                                Resource.Multiple_Custom_Code_Generators_Using_Default, 
                                                options.Language, 
                                                errorParam, 
                                                options.ClientProjectPath, 
                                                MockCodeGenerator.GeneratorName,
                                                CodeDomClientCodeGenerator.GeneratorName));
                TestHelper.AssertContainsWarnings(logger, error);
            }
        }
        public void ClientCodeGenerationDispatcher_Error_Multiple_Generators_Same_Name()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = "F#",
                ClientProjectPath = "ClientProject",
                ServerProjectPath = "ServerProject"
            };

            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            // Create a new dispatcher and call an internal extensibility point to add ourselves
            // into the MEF composition container
            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                string[] compositionAssemblies = new string[] { Assembly.GetExecutingAssembly().Location };

                string generatorName = MockFSharpCodeGenerator1.GeneratorName;

                IDomainServiceClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, generatorName);
                Assert.IsNull(generator, "the dispatcher should not pick a generator");

                string errorParam = "    " + typeof(MockFSharpCodeGenerator1).FullName + Environment.NewLine +
                                    "    " + typeof(MockFSharpCodeGenerator2).FullName + Environment.NewLine;

                string error = string.Format(CultureInfo.CurrentCulture, 
                                                        Resource.Multiple_Named_Code_Generators, 
                                                        generatorName,
                                                        options.Language, 
                                                        errorParam,
                                                        options.ServerProjectPath, 
                                                        options.ClientProjectPath,
                                                        typeof(MockFSharpCodeGenerator1).AssemblyQualifiedName);
                TestHelper.AssertContainsErrors(logger, error);
            }
        }
        public void ClientCodeGenerationDispatcher_Generate_Using_T4_Custom()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = "C#"
            };

            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            // Create a new dispatcher and call an internal extensibility point to add ourselves
            // into the MEF composition container
            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                string[] compositionAssemblies = new string[] { typeof(T4DomainServiceClientCodeGenerator).Assembly.Location };

                IDomainServiceClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, null);
                Assert.IsNotNull(generator, "the dispatcher did not find any code generator");
                Assert.AreEqual(generator.GetType(), typeof(T4DomainServiceClientCodeGenerator), "dispatcher found " + generator.GetType() + " but should have found T4DomainServiceClientProxyGenerator");

                DomainServiceDescription dsd = DomainServiceDescription.GetDescription(typeof(DispatcherDomainService));
                string generatedCode = generator.GenerateCode(host, new DomainServiceDescription[] { dsd }, options);

                Assert.IsFalse(string.IsNullOrEmpty(generatedCode), "expected T4 generator to generate code");
                TestHelper.AssertGeneratedCodeContains(generatedCode, T4DomainServiceClientCodeGenerator.GeneratedBoilerPlate);
                TestHelper.AssertGeneratedCodeContains(generatedCode, "public class DispatcherEntity : Entity");
                TestHelper.AssertNoErrorsOrWarnings(logger);
            }
        }
        public void ClientCodeGenerationDispatcher_Error_TypeLoadException()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = "C#",
            };

            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            // Create a new dispatcher and call an internal extensibility point to add ourselves
            // into the MEF composition container
            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                // We want to include into the MEF container an assembly that will throw TypeLoadException
                // when MEF tries to analyze it.  This is to test our own recovery, which should consist
                // of logging an error making a default container containing only Tools.
                string unitTestAssemblyLocation = Assembly.GetExecutingAssembly().Location;
                string typeLoadExceptionProjectLocation = Path.Combine(Path.GetDirectoryName(unitTestAssemblyLocation), "TypeLoadExceptionProject.dll");

                Assert.IsTrue(File.Exists(typeLoadExceptionProjectLocation), "Expected TypeLoadExceptionProject.dll to coreside with this assembly in test folder");

                // Do what MEF does to load the types so we can capture the exception
                Exception expectedException = null;
                try
                {
                    Assembly assembly = Assembly.LoadFrom(typeLoadExceptionProjectLocation);
                    assembly.GetTypes();
                }
                catch (Exception ex)
                {
                    expectedException = ex;
                }
                Assert.IsNotNull(expectedException, "We did not generate the type load exception we expected");

                string[] compositionAssemblies = new string[] { unitTestAssemblyLocation, typeLoadExceptionProjectLocation };

                IDomainServiceClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, /*generatorName*/ null);
                Assert.IsNotNull(generator, "the dispatcher did not pick default generator");
                Assert.IsTrue(generator is CodeDomClientCodeGenerator, "the dispatcher did not pick the default code generator");

                string error = (string.Format(CultureInfo.CurrentCulture,
                                                Resource.Failed_To_Create_Composition_Container,
                                                expectedException.Message));
                TestHelper.AssertContainsWarnings(logger, error);
            }
        }
        public void ClientCodeGenerationDispatcher_Custom_Warns_Full()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = "C#"
            };

            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            // Create a new dispatcher and call an internal extensibility point to add ourselves
            // into the MEF composition container
            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                string[] compositionAssemblies = new string[] { Assembly.GetExecutingAssembly().Location };

                IDomainServiceClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, MockCodeGenerator.GeneratorName);
                Assert.IsNotNull(generator, "the dispatcher did not find any code generator");
                Assert.AreEqual(generator.GetType(), typeof(MockCodeGenerator), "dispatcher found " + generator.GetType() + " but should have found MockCodeGenerator");

                // Setting this option makes our custom code generator emit the packet below to test LogWarning
                MockCodeGenerator.LogWarningsFull = true;
                string generatedCode = generator.GenerateCode(host, Enumerable.Empty<DomainServiceDescription>(), options);

                Assert.AreEqual(MockCodeGenerator.FakeGeneratedCode, generatedCode, "test code generator did not generate expected code.");
                TestHelper.AssertContainsWarningPackets(logger, MockCodeGenerator.WarningPacket);
            }
        }
        public void ClientCodeGenerationDispatcher_Finds_Solitary_Custom()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = "C#"
            };
            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            // Create a new dispatcher and call an internal extensibility point to add ourselves
            // into the MEF composition container
            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                string[] compositionAssemblies = new string[] { Assembly.GetExecutingAssembly().Location, typeof(TextTemplate::OpenRiaServices.DomainServices.Tools.TextTemplate.ClientCodeGenerator).Assembly.Location };

                IDomainServiceClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, /*generatorName*/ null);
                Assert.IsNotNull(generator, "the dispatcher did not find any code generator");
                Assert.AreEqual(generator.GetType(), typeof(MockCodeGenerator), "dispatcher found " + generator.GetType() + " but should have found MockCodeGenerator");

                string generatedCode = generator.GenerateCode(host, Enumerable.Empty<DomainServiceDescription>(), options);

                Assert.AreEqual(MockCodeGenerator.FakeGeneratedCode, generatedCode, "test code generator did not generate expected code.");

                // Expect informational message
                string message = string.Format(CultureInfo.CurrentCulture, Resource.Using_Custom_Code_Generator, MockCodeGenerator.GeneratorName);
                TestHelper.AssertContainsMessages(logger, message);
            }
        }
        public void ClientCodeGenerationDispatcher_Finds_Default_By_Name()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = "C#"
            };

            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            // Create a new dispatcher and call an internal extensibility point to add ourselves
            // into the MEF composition container
            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                string[] compositionAssemblies = new string[] { Assembly.GetExecutingAssembly().Location, typeof(T4DomainServiceClientCodeGenerator).Assembly.Location };

                IDomainServiceClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, CodeDomClientCodeGenerator.GeneratorName);
                Assert.IsNotNull(generator, "the dispatcher did not find any code generator");
                Assert.IsTrue(typeof(CodeDomClientCodeGenerator).IsAssignableFrom(generator.GetType()), "dispatcher found " + generator.GetType() + " but should have found CodeDomClientCodeGenerator");

                DomainServiceDescription dsd = DomainServiceDescription.GetDescription(typeof(DispatcherDomainService));
                string generatedCode = generator.GenerateCode(host, new DomainServiceDescription[] { dsd }, options);

                Assert.IsFalse(string.IsNullOrEmpty(generatedCode), "expected code to have been generated");
                Assert.IsTrue(generatedCode.Contains("public sealed partial class DispatcherDomainContext : DomainContext"), "Expected generated code to contain public sealed partial class DispatcherDomainContext : DomainContext");
            }
        }
示例#27
0
        /// <summary>
        /// Validates code gen for a specific language by comparing it against a file containing the expected output.
        /// </summary>
        /// <param name="codeGenOptions">The options specifying the type of validation to perform</param>
        /// <returns>A command that updates the comparison file</returns>
        internal static string ValidateLanguageCodeGen(CodeGenValidationOptions codeGenOptions)
        {
            Assert.IsFalse(string.IsNullOrEmpty(codeGenOptions.Language));

            var outDataDir  = GetOutputTestDataDir(codeGenOptions.RelativeDeployDir);
            var extension   = ExtensionFromLanguage(codeGenOptions.Language);
            var diffMessage = string.Empty;

            // Compose the abs path to where the test file got deployed by MSTest
            string referenceFileName = TestHelper.GetTestFileName(codeGenOptions.RelativeDeployDir, codeGenOptions.BaseReferenceFileName + extension);

            Assert.IsTrue(File.Exists(referenceFileName), "Cannot find reference file " + referenceFileName);
            string generatedCode = string.Empty;

            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language            = codeGenOptions.Language,
                ClientRootNamespace = codeGenOptions.RootNamespace,
                ClientProjectPath   = "MockProject.proj",
                UseFullTypeNames    = codeGenOptions.UseFullTypeNames
            };

            MockCodeGenerationHost host  = TestHelper.CreateMockCodeGenerationHost(codeGenOptions.Logger, codeGenOptions.SharedCodeService);
            ILogger              logger  = host as ILogger;
            EntityCatalog        catalog = new EntityCatalog(codeGenOptions.EntityTypes, logger);
            IClientCodeGenerator generator;

            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                generator = dispatcher.FindCodeGenerator(host, options, /*compositionAssemblies*/ null, /*codeGeneratorName*/ null);
            }
            Assert.IsNotNull(generator, "Failed to find a code generator");
            generatedCode = generator.GenerateCode(host, catalog.EntityDescriptions, options);

            ConsoleLogger consoleLogger = logger as ConsoleLogger;
            string        errors        = consoleLogger == null ? "" : consoleLogger.Errors;

            Assert.IsTrue(generatedCode.Length > 0, "No code was generated: " + errors);

            // Dump the generated code into a file for comparison
            bool   isCSharp          = options.Language.Equals("C#", StringComparison.InvariantCultureIgnoreCase);
            string generatedFileName = Path.Combine(outDataDir, Path.GetFileName(referenceFileName) + ".testgen");

            File.WriteAllText(generatedFileName, generatedCode);

            // TODO: (ron M3) Solve inability to get right MSBuild after checkin
            // First see if we compile
            List <string> referenceAssemblies = CompilerHelper.GetClientAssemblies(codeGenOptions.RelativeDeployDir);
            List <string> files = new List <string>();

            files.Add(generatedFileName);

            // Unconditionally force generation of Xml doc comments to catch errors
            string documentationFile = Path.GetTempFileName();

            try
            {
                if (isCSharp)
                {
                    files.AddRange(codeGenOptions.SharedFiles.Where(sharedFile => Path.GetExtension(sharedFile).Equals(".cs")));
                    CompilerHelper.CompileCSharpSource(files, referenceAssemblies, documentationFile);
                }
                else
                {
                    files.AddRange(codeGenOptions.SharedFiles.Where(sharedFile => Path.GetExtension(sharedFile).Equals(".vb")));
                    CompilerHelper.CompileVisualBasicSource(files, referenceAssemblies, options.ClientRootNamespace, documentationFile);
                }
            }
            finally
            {
                File.Delete(documentationFile);
            }

            // Do the diff
            if (codeGenOptions.FailOnDiff)
            {
                TestHelper.ValidateFilesEqual(codeGenOptions.RelativeTestDir, codeGenOptions.RelativeDeployDir, generatedFileName, referenceFileName, codeGenOptions.Language);
            }
            else
            {
                TestHelper.FilesMatch(codeGenOptions.RelativeTestDir, codeGenOptions.RelativeDeployDir, generatedFileName, referenceFileName, codeGenOptions.Language, out diffMessage);
            }

            return(diffMessage);
        }
        public void ClientCodeGenerationDispatcher_Error_Missing_Custom()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = "C#",
                ClientProjectPath = "ClientProject",
                ServerProjectPath = "ServerProject"
            };

            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            // Create a new dispatcher and call an internal extensibility point to add ourselves
            // into the MEF composition container
            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                string[] compositionAssemblies = new string[] { Assembly.GetExecutingAssembly().Location };

                IDomainServiceClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, "NotAGenerator");
                Assert.IsNull(generator, "the dispatcher should not find any code generator");

                string error = string.Format(CultureInfo.CurrentCulture, 
                                            Resource.Code_Generator_Not_Found, 
                                            "NotAGenerator", 
                                            options.Language, 
                                            options.ServerProjectPath, 
                                            options.ClientProjectPath,
                                            CodeDomClientCodeGenerator.GeneratorName);
                TestHelper.AssertContainsErrors(logger, error);
            }
        }