/// <summary>
        /// This method is part of the <see cref="IDomainServiceClientCodeGenerator" /> interface. The RIA Services Code Generation process uses this method as the entry point into the code generator.
        /// </summary>
        /// <param name="codeGenerationHost">The code generation host for this instance.</param>
        /// <param name="domainServiceDescriptions">The list of all the DomainServiceDescription objects.</param>
        /// <param name="options">The code generation objects.</param>
        /// <returns>The generated code.</returns>
        public string GenerateCode(ICodeGenerationHost codeGenerationHost, IEnumerable <DomainServiceDescription> domainServiceDescriptions, ClientCodeGenerationOptions options)
        {
            this._codeGenerationHost        = codeGenerationHost;
            this._domainServiceDescriptions = domainServiceDescriptions;
            this._options = options;

            this._enumTypesToGenerate = new HashSet <Type>();

            if (this.EntityGenerator == null)
            {
                this.CodeGenerationHost.LogError(string.Format(CultureInfo.CurrentCulture, TextTemplateResource.EntityGeneratorNotFound));
            }
            if (this.ComplexObjectGenerator == null)
            {
                this.CodeGenerationHost.LogError(string.Format(CultureInfo.CurrentCulture, TextTemplateResource.ComplexObjectGeneratorNotFound));
            }
            if (this.DomainContextGenerator == null)
            {
                this.CodeGenerationHost.LogError(string.Format(CultureInfo.CurrentCulture, TextTemplateResource.DomainContextGeneratorNotFound));
            }
            if (this.WebContextGenerator == null)
            {
                this.CodeGenerationHost.LogError(string.Format(CultureInfo.CurrentCulture, TextTemplateResource.WebContextGeneratorNotFound));
            }
            if (this.EnumGenerator == null)
            {
                this.CodeGenerationHost.LogError(string.Format(CultureInfo.CurrentCulture, TextTemplateResource.EnumGeneratorNotFound));
            }

            if (!this.CodeGenerationHost.HasLoggedErrors)
            {
                return(this.GenerateCode());
            }
            return(null);
        }
예제 #2
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");
            }
        }
예제 #3
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);
            }
        }
예제 #4
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);
            }
        }
예제 #5
0
        // Invokes the code generator discovered via the host and options
        internal static string GenerateCode(ICodeGenerationHost host, ClientCodeGenerationOptions options, IEnumerable <Type> entityTypes)
        {
            IClientCodeGenerator generator = CreateCodeGenerator(host, options);
            EntityCatalog        catalog   = new EntityCatalog(entityTypes, host as ILogger);

            return(generator.GenerateCode(host, catalog.EntityDescriptions, options));
        }
예제 #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_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.");
            }
        }
예제 #8
0
        // Invokes the code generator discovered via the host and options
        internal static string GenerateCode(ICodeGenerationHost host, ClientCodeGenerationOptions options, IEnumerable <Type> domainServiceTypes)
        {
            IDomainServiceClientCodeGenerator generator = CreateCodeGenerator(host, options);
            DomainServiceCatalog catalog = new DomainServiceCatalog(domainServiceTypes, host as ILogger);

            return(generator.GenerateCode(host, catalog.DomainServiceDescriptions, options));
        }
예제 #9
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);
            }
        }
        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_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);
            }
        }
예제 #12
0
        internal static string GenerateCode(string language, Type entityType, ILoggingService logger)
        {
            ClientCodeGenerationOptions options = CreateMockCodeGenContext(language, false);
            ICodeGenerationHost         host    = CreateMockCodeGenerationHost(logger, null);

            return(GenerateCode(host, options, new Type[] { entityType }));
        }
예제 #13
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);
            }
        }
        public void ClientCodeGenerationOptionTests_Properties()
        {
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions();

            // Verify defaults
            Assert.IsNull(options.Language);
            Assert.IsNull(options.ClientRootNamespace);
            Assert.IsNull(options.ServerRootNamespace);
            Assert.IsNull(options.ClientProjectPath);
            Assert.IsNull(options.ServerProjectPath);
            Assert.IsFalse(options.IsApplicationContextGenerationEnabled);
            Assert.IsFalse(options.UseFullTypeNames);

            // Null languge throws
            ExceptionHelper.ExpectArgumentNullException(() => options.Language = null, Resource.Null_Language_Property, "value");
            ExceptionHelper.ExpectArgumentNullException(() => options.Language = string.Empty, Resource.Null_Language_Property, "value");

            // Now test a range of values for each property
            foreach (string language in new string[] { "C#", "VB", "notALanguage" })
            {
                options.Language = language;
                Assert.AreEqual(language, options.Language);
            }

            foreach (string rootNamespace in new string[] { null, string.Empty, "testRoot" })
            {
                options.ClientRootNamespace = rootNamespace;
                Assert.AreEqual(rootNamespace, options.ClientRootNamespace);
            }

            foreach (string rootNamespace in new string[] { null, string.Empty, "testRoot" })
            {
                options.ServerRootNamespace = rootNamespace;
                Assert.AreEqual(rootNamespace, options.ServerRootNamespace);
            }

            foreach (string projectName in new string[] { null, string.Empty, "testProj" })
            {
                options.ClientProjectPath = projectName;
                Assert.AreEqual(projectName, options.ClientProjectPath);
            }

            foreach (string projectName in new string[] { null, string.Empty, "testProj" })
            {
                options.ServerProjectPath = projectName;
                Assert.AreEqual(projectName, options.ServerProjectPath);
            }

            foreach (bool theBool in new bool[] { true, false })
            {
                options.IsApplicationContextGenerationEnabled = theBool;
                Assert.AreEqual(theBool, options.IsApplicationContextGenerationEnabled);
            }

            foreach (bool theBool in new bool[] { true, false })
            {
                options.UseFullTypeNames = theBool;
                Assert.AreEqual(theBool, options.UseFullTypeNames);
            }
        }
예제 #15
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);
            }
        }
예제 #16
0
        internal static string GenerateCode(string language, IEnumerable <Type> entityTypes, ILoggingService logger, ISharedCodeService typeService)
        {
            ClientCodeGenerationOptions options = CreateMockCodeGenContext(language, false);
            ICodeGenerationHost         host    = CreateMockCodeGenerationHost(logger, typeService);

            return(GenerateCode(host, options, entityTypes));
        }
예제 #17
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);
            }
        }
        public void ClientCodeGenerationOptionTests_Properties()
        {
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions();

            // Verify defaults
            Assert.IsNull(options.Language);
            Assert.IsNull(options.ClientRootNamespace);
            Assert.IsNull(options.ServerRootNamespace);
            Assert.IsNull(options.ClientProjectPath);
            Assert.IsNull(options.ServerProjectPath);
            Assert.IsFalse(options.IsApplicationContextGenerationEnabled);
            Assert.IsFalse(options.UseFullTypeNames);

            // Null languge throws
            ExceptionHelper.ExpectArgumentNullException(() => options.Language = null, Resource.Null_Language_Property, "value");
            ExceptionHelper.ExpectArgumentNullException(() => options.Language = string.Empty, Resource.Null_Language_Property, "value");

            // Now test a range of values for each property
            foreach (string language in new string[] { "C#", "VB", "notALanguage" })
            {
                options.Language = language;
                Assert.AreEqual(language, options.Language);
            }

            foreach (string rootNamespace in new string[] { null, string.Empty, "testRoot" })
            {
                options.ClientRootNamespace = rootNamespace;
                Assert.AreEqual(rootNamespace, options.ClientRootNamespace);
            }

            foreach (string rootNamespace in new string[] { null, string.Empty, "testRoot" })
            {
                options.ServerRootNamespace = rootNamespace;
                Assert.AreEqual(rootNamespace, options.ServerRootNamespace);
            }

            foreach (string projectName in new string[] { null, string.Empty, "testProj" })
            {
                options.ClientProjectPath = projectName;
                Assert.AreEqual(projectName, options.ClientProjectPath);
            }

            foreach (string projectName in new string[] { null, string.Empty, "testProj" })
            {
                options.ServerProjectPath = projectName;
                Assert.AreEqual(projectName, options.ServerProjectPath);
            }

            foreach (bool theBool in new bool[] { true, false })
            {
                options.IsApplicationContextGenerationEnabled = theBool;
                Assert.AreEqual(theBool, options.IsApplicationContextGenerationEnabled);
            }

            foreach (bool theBool in new bool[] { true, false })
            {
                options.UseFullTypeNames = theBool;
                Assert.AreEqual(theBool, options.UseFullTypeNames);
            }
        }
        public string GenerateCode(ICodeGenerationHost host, IEnumerable <EntityDescription> domainServiceDescriptions, ClientCodeGenerationOptions options)
        {
            this._codeGenerationHost        = host;
            this._options                   = options;
            this._domainServiceDescriptions = domainServiceDescriptions.ToList();

            return(this.TransformText());
        }
        public string GenerateCode(ICodeGenerationHost host, IEnumerable<OpenRiaServices.DomainServices.Server.DomainServiceDescription> domainServiceDescriptions, ClientCodeGenerationOptions options)
        {
            this._codeGenerationHost = host;
            this._options = options;
            this._domainServiceDescriptions = domainServiceDescriptions.ToList();

            return this.TransformText();
        }
예제 #21
0
        internal static string GenerateCodeAssertSuccess(string language, IEnumerable <Type> entityTypes, ConsoleLogger logger, ISharedCodeService typeService, bool useFullNames)
        {
            ClientCodeGenerationOptions options = CreateMockCodeGenContext(language, useFullNames);
            ICodeGenerationHost         host    = CreateMockCodeGenerationHost(logger, typeService);
            string generatedCode = GenerateCode(host, options, entityTypes);

            TestHelper.AssertCodeGenSuccess(generatedCode, ((MockCodeGenerationHost)host).LoggingService as ConsoleLogger);
            return(generatedCode);
        }
예제 #22
0
        internal static void GenerateCodeAssertFailure(string language, IEnumerable <Type> domainServiceTypes, params string[] errors)
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = CreateMockCodeGenContext(language, false);
            ICodeGenerationHost         host    = CreateMockCodeGenerationHost(logger, null);
            string generatedCode = GenerateCode(host, options, domainServiceTypes);

            TestHelper.AssertCodeGenFailure(generatedCode, logger, errors);
        }
예제 #23
0
        /// <summary>
        /// Default constructor accepting the current <see cref="ClientCodeGenerationOptions"/> context.
        /// </summary>
        /// <param name="options">The current <see cref="ClientCodeGenerationOptions"/> options.</param>
        public ClientProxyFixupCodeDomVisitor(ClientCodeGenerationOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            this._options  = options;
            this._isCSharp = (this._options.Language == "C#");
        }
        public void ClientCodeGenerationDispatcher_Null_Language_Throws()
        {
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions();
            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(null, null);

            ExceptionHelper.ExpectArgumentException(() =>
            {
                new ClientCodeGenerationDispatcher().FindCodeGenerator(host, options, /*compositionAssemblies*/ null, /*codeGeneratorName*/ null);
            }, Resource.Null_Language_Property, "options");
        }
예제 #25
0
        internal static string GenerateCodeAssertSuccess(string language, Type entityType)
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = CreateMockCodeGenContext(language, false);
            ICodeGenerationHost         host    = CreateMockCodeGenerationHost(logger, null);
            string generatedCode = GenerateCode(host, options, new Type[] { entityType });

            TestHelper.AssertCodeGenSuccess(generatedCode, logger);
            return(generatedCode);
        }
예제 #26
0
        public void ClientCodeGenerationDispatcher_Null_Language_Throws()
        {
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions();
            ICodeGenerationHost         host    = TestHelper.CreateMockCodeGenerationHost(null, null);

            ExceptionHelper.ExpectArgumentException(() =>
            {
                new ClientCodeGenerationDispatcher().FindCodeGenerator(host, options, /*compositionAssemblies*/ null, /*codeGeneratorName*/ null);
            }, Resource.Null_Language_Property, "options");
        }
예제 #27
0
        internal static ClientCodeGenerationOptions CreateMockCodeGenContext(string language, bool useFullTypeNames)
        {
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions
            {
                Language          = language,
                ClientProjectPath = "MockProject.csproj",
                UseFullTypeNames  = useFullTypeNames
            };

            return(options);
        }
예제 #28
0
        internal static string GenerateCodeAssertWarnings(string language, Type domainServiceType, params string[] warnings)
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = CreateMockCodeGenContext(language, false);
            ICodeGenerationHost         host    = CreateMockCodeGenerationHost(logger, null);
            string generatedCode = GenerateCode(host, options, new Type[] { domainServiceType });

            TestHelper.AssertContainsWarnings(logger, warnings);
            Assert.IsFalse(string.IsNullOrEmpty(generatedCode), "Expected code to generate with warnings");
            return(generatedCode);
        }
        private static CodeDomClientCodeGenerator CreateProxyGenerator(bool isCSharp)
        {
            MockCodeGenerationHost     host      = new MockCodeGenerationHost();
            CodeDomClientCodeGenerator generator = isCSharp
                                                        ? (CodeDomClientCodeGenerator) new CSharpCodeDomClientCodeGenerator()
                                                        : (CodeDomClientCodeGenerator) new VisualBasicCodeDomClientCodeGenerator();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = isCSharp ? "C#" : "VB",
            };

            generator.Initialize(host, new DomainServiceDescription[] { DomainServiceDescription.GetDescription(typeof(MockOrder_DomainService)) }, options);
            return(generator);
        }
예제 #30
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);
            }
        }
        private static CodeDomClientCodeGenerator CreateProxyGenerator(bool isCSharp)
        {
            MockCodeGenerationHost     host      = new MockCodeGenerationHost();
            CodeDomClientCodeGenerator generator = isCSharp
                                                        ? (CodeDomClientCodeGenerator) new CSharpCodeDomClientCodeGenerator()
                                                        : (CodeDomClientCodeGenerator) new VisualBasicCodeDomClientCodeGenerator();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = isCSharp ? "C#" : "VB",
            };

            var entityDescription = new EntityDescription();

            generator.Initialize(host, new EntityDescription[] { entityDescription }, options);

            return(generator);
        }
예제 #32
0
        private string GenerateCode()
        {
            var options = new ClientCodeGenerationOptions
            {
                Language            = _isCSharp ? "C#" : "VisualBasic",
                ClientProjectPath   = "MockProject.proj",
                ClientRootNamespace = "TestRootNS",
                UseFullTypeNames    = _useFullTypeNames
            };

            var host      = TestHelper.CreateMockCodeGenerationHost(ConsoleLogger, MockSharedCodeService);
            var generator = (_isCSharp) ? (CodeDomClientCodeGenerator) new CSharpCodeDomClientCodeGenerator() : (CodeDomClientCodeGenerator) new VisualBasicCodeDomClientCodeGenerator();

            _entityCatalog = new EntityCatalog(_entityTypes, ConsoleLogger);

            string generatedCode = generator.GenerateCode(host, _entityCatalog.EntityDescriptions, options);

            return(generatedCode);
        }
예제 #33
0
        private string GenerateCode()
        {
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language            = this._isCSharp ? "C#" : "VisualBasic",
                ClientProjectPath   = "MockProject.proj",
                ClientRootNamespace = "TestRootNS",
                UseFullTypeNames    = this._useFullTypeNames
            };

            MockCodeGenerationHost host      = TestHelper.CreateMockCodeGenerationHost(this.ConsoleLogger, this.MockSharedCodeService);
            ClientCodeGenerator    generator = (ClientCodeGenerator) new CSharpClientCodeGenerator();

            this._domainServiceCatalog = new DomainServiceCatalog(this._domainServiceTypes, this.ConsoleLogger);

            string generatedCode = generator.GenerateCode(host, this._domainServiceCatalog.DomainServiceDescriptions, options);

            return(generatedCode);
        }
예제 #34
0
        public void T4CodeGenTest_VBCodeGenTest()
        {
            Type[] domainServiceTypes           = new Type[] {};
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language            = "VB",
                ClientProjectPath   = "MockProject.proj",
                ClientRootNamespace = "TestRootNS",
                UseFullTypeNames    = false
            };
            ConsoleLogger          consoleLogger         = new ConsoleLogger();
            MockSharedCodeService  mockSharedCodeService = new MockSharedCodeService(Array.Empty <Type>(), Array.Empty <MethodBase>(), Array.Empty <string>());
            MockCodeGenerationHost host                 = TestHelper.CreateMockCodeGenerationHost(consoleLogger, mockSharedCodeService);
            ClientCodeGenerator    generator            = (ClientCodeGenerator) new VBTestClientCodeGenerator();
            DomainServiceCatalog   domainServiceCatalog = new DomainServiceCatalog(domainServiceTypes, consoleLogger);
            string generatedCode = generator.GenerateCode(host, domainServiceCatalog.DomainServiceDescriptions, options);

            Assert.IsTrue(string.IsNullOrEmpty(generatedCode));
            Assert.IsTrue(string.IsNullOrEmpty(consoleLogger.Errors));
        }
        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);
            }
        }
        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");
            }
        }
        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_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 virtual string GenerateCode(ICodeGenerationHost codeGenerationHost, IEnumerable<DomainServiceDescription> domainServiceDescriptions, ClientCodeGenerationOptions options)
 {
     return MockGSharpCodeGeneratorBase.GeneratedCode;
 }
 private static CodeDomClientCodeGenerator CreateProxyGenerator(bool isCSharp)
 {
     MockCodeGenerationHost host = new MockCodeGenerationHost();
     CodeDomClientCodeGenerator generator = isCSharp
                                                 ? (CodeDomClientCodeGenerator) new CSharpCodeDomClientCodeGenerator() 
                                                 : (CodeDomClientCodeGenerator) new VisualBasicCodeDomClientCodeGenerator();
     ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
     {
         Language = isCSharp ? "C#" : "VB",
     };
     generator.Initialize(host, new DomainServiceDescription[] { DomainServiceDescription.GetDescription(typeof(MockOrder_DomainService))}, options );
     return generator;
 }
        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_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 string GenerateCode(ICodeGenerationHost host, IEnumerable<DomainServiceDescription> descriptions, ClientCodeGenerationOptions options)
        {
            Assert.IsNotNull(host, "host cannot be null when code generator is called");
            Assert.IsNotNull(options, "options cannot be null when code generator is called");
            Assert.IsNotNull(descriptions, "descriptions cannot be null when code generator is called");

            // These 2 test helpers reset each time they are read
            bool logWarningsFull = MockCodeGenerator.LogWarningsFull;
            MockCodeGenerator.LogWarningsFull = false;

            bool logErrorsFull = MockCodeGenerator.LogErrorsFull;
            MockCodeGenerator.LogErrorsFull = false;

            bool throwException = MockCodeGenerator.ThrowException;
            MockCodeGenerator.ThrowException = false;

            if (throwException)
            {
                throw MockCodeGenerator.Exception;
            }

            if (logWarningsFull)
            {
                ConsoleLogger.LogPacket p = MockCodeGenerator.WarningPacket;
                host.LogWarning(p.Message, p.Subcategory, p.ErrorCode, p.HelpString, p.File, p.LineNumber, p.ColumnNumber, p.EndLineNumber, p.EndColumnNumber);
            }
            if (logErrorsFull)
            {
                ConsoleLogger.LogPacket p = MockCodeGenerator.ErrorPacket;
                host.LogError(p.Message, p.Subcategory, p.ErrorCode, p.HelpString, p.File, p.LineNumber, p.ColumnNumber, p.EndLineNumber, p.EndColumnNumber);
            }

            return MockCodeGenerator.FakeGeneratedCode;
        }
 public string GenerateCode(ICodeGenerationHost codeGenerationHost, IEnumerable<DomainServiceDescription> domainServiceDescriptions, ClientCodeGenerationOptions options)
 {
     throw new NotImplementedException();
 }
        private string GenerateCode()
        {
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = this._isCSharp ? "C#" : "VisualBasic",
                ClientProjectPath = "MockProject.proj",
                ClientRootNamespace = "TestRootNS",
                UseFullTypeNames = this._useFullTypeNames
            };

            MockCodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(this.ConsoleLogger, this.MockSharedCodeService);
            ClientCodeGenerator generator = (ClientCodeGenerator)new CSharpClientCodeGenerator();
            this._domainServiceCatalog = new DomainServiceCatalog(this._domainServiceTypes, this.ConsoleLogger);

            string generatedCode = generator.GenerateCode(host, this._domainServiceCatalog.DomainServiceDescriptions, options);
            return generatedCode;
        }
예제 #50
0
        public void T4CodeGenTest_VBCodeGenTest()
        {
            Type[] domainServiceTypes = new Type[] {};
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = "VB",
                ClientProjectPath = "MockProject.proj",
                ClientRootNamespace = "TestRootNS",
                UseFullTypeNames = false
            };
            ConsoleLogger consoleLogger = new ConsoleLogger();
            MockSharedCodeService mockSharedCodeService = new MockSharedCodeService(new Type[0], new MethodBase[0], new string[0]);
            MockCodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(consoleLogger, mockSharedCodeService);
            ClientCodeGenerator generator = (ClientCodeGenerator)new VBTestClientCodeGenerator();
            DomainServiceCatalog domainServiceCatalog = new DomainServiceCatalog(domainServiceTypes, consoleLogger);
            string generatedCode = generator.GenerateCode(host, domainServiceCatalog.DomainServiceDescriptions, options);

            Assert.IsTrue(string.IsNullOrEmpty(generatedCode));
            Assert.IsTrue(string.IsNullOrEmpty(consoleLogger.Errors));
        }
        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_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 override string GenerateCode(ICodeGenerationHost codeGenerationHost, IEnumerable<DomainServiceDescription> domainServiceDescriptions, ClientCodeGenerationOptions options)
 {
     return MockGSharpCodeGeneratorDerived.DerivedGeneratedCode;
 }