Example #1
0
        public void GetControllerTypeForAssembliesWithSameTypeNamesInSameNamespaceThrows()
        {
            // Arrange
            RequestContext           requestContext      = new RequestContext(new Mock <HttpContextBase>().Object, new RouteData());
            DefaultControllerFactory factory             = GetDefaultControllerFactory("ns1a.ns1b");
            MockBuildManager         buildManagerMock    = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly4") });
            ControllerTypeCache      controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager        = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;

            // Act
            ExceptionHelper.ExpectException <InvalidOperationException>(
                delegate {
                factory.GetControllerType(requestContext, "C1");
            },
                @"Multiple types were found that match the controller named 'C1'. This can happen if the route that services this request does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.

The request for 'C1' has found the following matching controllers:
NS1a.NS1b.C1Controller
NS1a.NS1b.C1Controller");

            // Assert
            Assert.AreEqual <int>(4, controllerTypeCache.Count, "Cache should have 4 controller types.");
        }
Example #2
0
        public void GetControllerTypeForManyAssemblies()
        {
            // Arrange
            RequestContext           requestContext      = new RequestContext(new Mock <HttpContextBase>().Object, new RouteData());
            DefaultControllerFactory factory             = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b", "ns3a.ns3b", "ns4a.ns4b");
            MockBuildManager         buildManagerMock    = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly2") });
            ControllerTypeCache      controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager        = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;

            // Act
            Type c1Type = factory.GetControllerType(requestContext, "C1");
            Type c2Type = factory.GetControllerType(requestContext, "C2");
            Type c3Type = factory.GetControllerType(requestContext, "c3"); // lower case
            Type c4Type = factory.GetControllerType(requestContext, "c4"); // lower case

            // Assert
            Assembly asm1       = Assembly.Load("MvcAssembly1");
            Type     verifiedC1 = asm1.GetType("NS1a.NS1b.C1Controller");
            Type     verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller");
            Assembly asm2       = Assembly.Load("MvcAssembly2");
            Type     verifiedC3 = asm2.GetType("NS3a.NS3b.C3Controller");
            Type     verifiedC4 = asm2.GetType("NS4a.NS4b.C4Controller");

            Assert.NotNull(verifiedC1);
            Assert.NotNull(verifiedC2);
            Assert.NotNull(verifiedC3);
            Assert.NotNull(verifiedC4);
            Assert.Equal(verifiedC1, c1Type);
            Assert.Equal(verifiedC2, c2Type);
            Assert.Equal(verifiedC3, c3Type);
            Assert.Equal(verifiedC4, c4Type);
            Assert.Equal(4, controllerTypeCache.Count);
        }
Example #3
0
        public void GetControllerTypeForAssembliesWithSameTypeNamesInDifferentNamespaces()
        {
            // Arrange
            RequestContext           requestContext      = new RequestContext(new Mock <HttpContextBase>().Object, new RouteData());
            DefaultControllerFactory factory             = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b");
            MockBuildManager         buildManagerMock    = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly3") });
            ControllerTypeCache      controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager        = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;

            // Act
            Type c1Type = factory.GetControllerType(requestContext, "C1");
            Type c2Type = factory.GetControllerType(requestContext, "C2");

            // Assert
            Assembly asm1       = Assembly.Load("MvcAssembly1");
            Type     verifiedC1 = asm1.GetType("NS1a.NS1b.C1Controller");
            Type     verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller");

            Assert.NotNull(verifiedC1);
            Assert.NotNull(verifiedC2);
            Assert.Equal(verifiedC1, c1Type);
            Assert.Equal(verifiedC2, c2Type);
            Assert.Equal(4, controllerTypeCache.Count);
        }
 public ControllerTypeCacheTests()
 {
     _controllerTypeCache = new ControllerTypeCache(new NoCache())
                               {
                                   ReferencedAssemblies = (() => new List<Assembly>{ GetType().Assembly })
                               };
 }
Example #5
0
        public void GetControllerTypeDoesNotThrowIfSameControllerMatchedMultipleNamespaces()
        {
            // both namespaces "ns3a" and "ns3a.ns3b" will match a controller type, but it's actually
            // the same type. in this case, we shouldn't throw.

            // Arrange
            RequestContext requestContext = GetRequestContextWithNamespaces("ns3a", "ns3a.ns3b");

            requestContext.RouteData.DataTokens["UseNamespaceFallback"] = false;
            DefaultControllerFactory factory             = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b");
            MockBuildManager         buildManagerMock    = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly3") });
            ControllerTypeCache      controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager        = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;

            // Act
            Type c1Type = factory.GetControllerType(requestContext, "C1");

            // Assert
            Assembly asm3       = Assembly.Load("MvcAssembly3");
            Type     verifiedC1 = asm3.GetType("NS3a.NS3b.C1Controller");

            Assert.NotNull(verifiedC1);
            Assert.Equal(verifiedC1, c1Type);
        }
        public void GetControllerTypeSearchesRouteDefinedNamespacesBeforeApplicationDefinedNamespaces()
        {
            // Arrange
            RequestContext           requestContext      = GetRequestContextWithNamespaces("ns3a.ns3b");
            DefaultControllerFactory factory             = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b");
            MockBuildManager         buildManagerMock    = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly3") });
            ControllerTypeCache      controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager        = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;
            factory.RequestContext      = requestContext;

            // Act
            Type c1Type = factory.GetControllerType("C1");
            Type c2Type = factory.GetControllerType("C2");

            // Assert
            Assembly asm1       = Assembly.Load("MvcAssembly1");
            Type     verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller");
            Assembly asm3       = Assembly.Load("MvcAssembly3");
            Type     verifiedC1 = asm3.GetType("NS3a.NS3b.C1Controller");

            Assert.IsNotNull(verifiedC1, "Couldn't find real C1 type");
            Assert.IsNotNull(verifiedC2, "Couldn't find real C2 type");
            Assert.AreEqual <Type>(verifiedC1, c1Type, "Should have found C1Controller type.");
            Assert.AreEqual <Type>(verifiedC2, c2Type, "Should have found C2Controller type.");
            Assert.AreEqual <int>(4, controllerTypeCache.Count, "Cache should have 4 controller types.");
        }
Example #7
0
        public void GetControllerTypeSearchesOnlyRouteDefinedNamespacesIfRequested()
        {
            // Arrange
            RequestContext requestContext = GetRequestContextWithNamespaces("ns3a.ns3b");

            requestContext.RouteData.DataTokens["UseNamespaceFallback"] = false;
            DefaultControllerFactory factory             = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b");
            MockBuildManager         buildManagerMock    = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly3") });
            ControllerTypeCache      controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager        = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;

            // Act
            Type c1Type = factory.GetControllerType(requestContext, "C1");
            Type c2Type = factory.GetControllerType(requestContext, "C2");

            // Assert
            Assembly asm3       = Assembly.Load("MvcAssembly3");
            Type     verifiedC1 = asm3.GetType("NS3a.NS3b.C1Controller");

            Assert.NotNull(verifiedC1);
            Assert.Equal(verifiedC1, c1Type);
            Assert.Null(c2Type);
        }
Example #8
0
        public void GetControllerTypeSearchesRouteDefinedNamespacesBeforeApplicationDefinedNamespaces()
        {
            // Arrange
            RequestContext           requestContext      = GetRequestContextWithNamespaces("ns3a.ns3b");
            DefaultControllerFactory factory             = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b");
            MockBuildManager         buildManagerMock    = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly3") });
            ControllerTypeCache      controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager        = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;

            // Act
            Type c1Type = factory.GetControllerType(requestContext, "C1");
            Type c2Type = factory.GetControllerType(requestContext, "C2");

            // Assert
            Assembly asm1       = Assembly.Load("MvcAssembly1");
            Type     verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller");
            Assembly asm3       = Assembly.Load("MvcAssembly3");
            Type     verifiedC1 = asm3.GetType("NS3a.NS3b.C1Controller");

            Assert.NotNull(verifiedC1);
            Assert.NotNull(verifiedC2);
            Assert.Equal(verifiedC1, c1Type);
            Assert.Equal(verifiedC2, c2Type);
            Assert.Equal(4, controllerTypeCache.Count);
        }
        public void GetControllerTypeForAssembliesWithSameTypeNamesInDifferentNamespaces()
        {
            // Arrange
            DefaultControllerFactory factory             = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b");
            MockBuildManager         buildManagerMock    = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly3") });
            ControllerTypeCache      controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager        = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;

            // Act
            Type c1Type = factory.GetControllerType("C1");
            Type c2Type = factory.GetControllerType("C2");

            // Assert
            Assembly asm1       = Assembly.Load("MvcAssembly1");
            Type     verifiedC1 = asm1.GetType("NS1a.NS1b.C1Controller");
            Type     verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller");

            Assert.IsNotNull(verifiedC1, "Couldn't find real C1 type");
            Assert.IsNotNull(verifiedC2, "Couldn't find real C2 type");
            Assert.AreEqual <Type>(verifiedC1, c1Type, "Should have found C1Controller type.");
            Assert.AreEqual <Type>(verifiedC2, c2Type, "Should have found C2Controller type.");
            Assert.AreEqual <int>(4, controllerTypeCache.Count, "Cache should have 4 controller types.");
        }
        public void GetControllerTypeForManyAssemblies()
        {
            // Arrange
            DefaultControllerFactory factory             = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b", "ns3a.ns3b", "ns4a.ns4b");
            MockBuildManager         buildManagerMock    = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly2") });
            ControllerTypeCache      controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager        = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;

            // Act
            Type c1Type = factory.GetControllerType("C1");
            Type c2Type = factory.GetControllerType("C2");
            Type c3Type = factory.GetControllerType("c3"); // lower case
            Type c4Type = factory.GetControllerType("c4"); // lower case

            // Assert
            Assembly asm1       = Assembly.Load("MvcAssembly1");
            Type     verifiedC1 = asm1.GetType("NS1a.NS1b.C1Controller");
            Type     verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller");
            Assembly asm2       = Assembly.Load("MvcAssembly2");
            Type     verifiedC3 = asm2.GetType("NS3a.NS3b.C3Controller");
            Type     verifiedC4 = asm2.GetType("NS4a.NS4b.C4Controller");

            Assert.IsNotNull(verifiedC1, "Couldn't find real C1 type");
            Assert.IsNotNull(verifiedC2, "Couldn't find real C2 type");
            Assert.IsNotNull(verifiedC3, "Couldn't find real C3 type");
            Assert.IsNotNull(verifiedC4, "Couldn't find real C4 type");
            Assert.AreEqual <Type>(verifiedC1, c1Type, "Should have found C1Controller type.");
            Assert.AreEqual <Type>(verifiedC2, c2Type, "Should have found C2Controller type.");
            Assert.AreEqual <Type>(verifiedC3, c3Type, "Should have found C3Controller type.");
            Assert.AreEqual <Type>(verifiedC4, c4Type, "Should have found C4Controller type.");
            Assert.AreEqual <int>(4, controllerTypeCache.Count, "Cache should have 4 controller types.");
        }
 public void IsNamespaceMatch(string testNamespace, bool expectedResult)
 {
     // Act & Assert
     Assert.Equal(
         expectedResult,
         ControllerTypeCache.IsNamespaceMatch(testNamespace, "Dummy.Controllers")
         );
 }
 public ControllerTypeCacheTests()
 {
     _controllerTypeCache = new ControllerTypeCache(new NoCache())
     {
         ReferencedAssemblies = (() => new List <Assembly> {
             GetType().Assembly
         })
     };
 }
        public void GetControllerTypeForNoAssemblies()
        {
            // Arrange
            DefaultControllerFactory factory             = new DefaultControllerFactory();
            MockBuildManager         buildManagerMock    = new MockBuildManager(new Assembly[] { });
            ControllerTypeCache      controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager        = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;

            // Act
            Type controllerType = factory.GetControllerType("sometype");

            // Assert
            Assert.IsNull(controllerType, "Shouldn't have found a controller type.");
            Assert.AreEqual <int>(0, controllerTypeCache.Count, "Cache should be empty.");
        }
Example #14
0
        public void GetControllerTypeForNoAssemblies()
        {
            // Arrange
            RequestContext           requestContext      = new RequestContext(new Mock <HttpContextBase>().Object, new RouteData());
            DefaultControllerFactory factory             = new DefaultControllerFactory();
            MockBuildManager         buildManagerMock    = new MockBuildManager(new Assembly[] { });
            ControllerTypeCache      controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager        = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;

            // Act
            Type controllerType = factory.GetControllerType(requestContext, "sometype");

            // Assert
            Assert.IsNull(controllerType, "Shouldn't have found a controller type.");
            Assert.AreEqual <int>(0, controllerTypeCache.Count, "Cache should be empty.");
        }
Example #15
0
        public void IsControllerType()
        {
            // Act
            bool isController1 = ControllerTypeCache.IsControllerType(null);
            bool isController2 = ControllerTypeCache.IsControllerType(typeof(NonPublicController));
            bool isController3 = ControllerTypeCache.IsControllerType(typeof(MisspelledKontroller));
            bool isController4 = ControllerTypeCache.IsControllerType(typeof(AbstractController));
            bool isController5 = ControllerTypeCache.IsControllerType(typeof(NonIControllerController));
            bool isController6 = ControllerTypeCache.IsControllerType(typeof(Goodcontroller));

            // Assert
            Assert.IsFalse(isController1, "IsControllerType(null) should return false.");
            Assert.IsFalse(isController2, "Non-public types should not be considered controller types.");
            Assert.IsFalse(isController3, "Types not ending in 'Controller' should not be considered controller types.");
            Assert.IsFalse(isController4, "Abstract types should not be considered controller types.");
            Assert.IsFalse(isController5, "Types not implementing IController should not be considered controller types.");
            Assert.IsTrue(isController6, "The 'Controller' suffix on controller types is not required to be case-sensitive.");
        }
Example #16
0
        public void GetControllerTypeForNoAssemblies()
        {
            // Arrange
            RequestContext           requestContext      = new RequestContext(new Mock <HttpContextBase>().Object, new RouteData());
            DefaultControllerFactory factory             = new DefaultControllerFactory();
            MockBuildManager         buildManagerMock    = new MockBuildManager(new Assembly[] { });
            ControllerTypeCache      controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager        = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;

            // Act
            Type controllerType = factory.GetControllerType(requestContext, "sometype");

            // Assert
            Assert.Null(controllerType);
            Assert.Equal(0, controllerTypeCache.Count);
        }
Example #17
0
        public void IsControllerType()
        {
            // Act
            bool isController1 = ControllerTypeCache.IsControllerType(null);
            bool isController2 = ControllerTypeCache.IsControllerType(typeof(NonPublicController));
            bool isController3 = ControllerTypeCache.IsControllerType(typeof(MisspelledKontroller));
            bool isController4 = ControllerTypeCache.IsControllerType(typeof(AbstractController));
            bool isController5 = ControllerTypeCache.IsControllerType(typeof(NonIControllerController));
            bool isController6 = ControllerTypeCache.IsControllerType(typeof(Goodcontroller));

            // Assert
            Assert.False(isController1);
            Assert.False(isController2);
            Assert.False(isController3);
            Assert.False(isController4);
            Assert.False(isController5);
            Assert.True(isController6);
        }
Example #18
0
        public void IsNamespaceMatch()
        {
            // Arrange
            var tests = new[] {
                new { TestNamespace = (string)null, ExpectedResult = false, Reason = "Null requested namespace should not match any target namespace." },
                new { TestNamespace = "", ExpectedResult = true, Reason = "Empty requested namespace should match any target namespace." },
                new { TestNamespace = "Dummy.Controllers", ExpectedResult = true, Reason = "Exact matches between requested namespace and target namespace are valid." },
                new { TestNamespace = "Dummy", ExpectedResult = false, Reason = "Parent namespace matches between exact requested namespace and target namespace are invalid." },
                new { TestNamespace = "Dummy.Controller.*", ExpectedResult = false, Reason = "Wildcard namespace prefix did not match." },
                new { TestNamespace = "Dummy.Controllers.*", ExpectedResult = true, Reason = "Wildcard namespace prefix matched." },
                new { TestNamespace = "Dummy.*", ExpectedResult = true, Reason = "Wildcard namespace prefix matched." },
                new { TestNamespace = "Dummy.Controllers*", ExpectedResult = false, Reason = "Wildcard end token is incorrect ('*' vs. '.*')." }
            };

            // Act & assert
            foreach (var test in tests)
            {
                bool retVal = ControllerTypeCache.IsNamespaceMatch(test.TestNamespace, "Dummy.Controllers");
                Assert.AreEqual(test.ExpectedResult, retVal, test.Reason);
            }
        }
        public void GetControllerTypeThatDoesntExist()
        {
            // Arrange
            RequestContext requestContext = new RequestContext(
                new Mock <HttpContextBase>().Object,
                new RouteData()
                );
            DefaultControllerFactory factory = GetDefaultControllerFactory(
                "ns1a.ns1b",
                "ns2a.ns2b",
                "ns3a.ns3b",
                "ns4a.ns4b"
                );
            MockBuildManager buildManagerMock = new MockBuildManager(
                new Assembly[]
            {
                Assembly.Load("MvcAssembly1"),
                Assembly.Load("MvcAssembly2"),
                Assembly.Load("MvcAssembly3"),
                Assembly.Load("MvcAssembly4")
            }
                );
            ControllerTypeCache controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager        = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;

            // Act
            Type randomType1 = factory.GetControllerType(requestContext, "Cx");
            Type randomType2 = factory.GetControllerType(requestContext, "Cy");
            Type randomType3 = factory.GetControllerType(requestContext, "Foo.Bar");
            Type randomType4 = factory.GetControllerType(requestContext, "C1Controller");

            // Assert
            Assert.Null(randomType1);
            Assert.Null(randomType2);
            Assert.Null(randomType3);
            Assert.Null(randomType4);
            Assert.Equal(8, controllerTypeCache.Count);
        }
        public void GetControllerTypeForAssembliesWithSameTypeNamesInSameNamespaceThrows()
        {
            // Arrange
            DefaultControllerFactory factory             = GetDefaultControllerFactory("ns1a.ns1b");
            MockBuildManager         buildManagerMock    = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly4") });
            ControllerTypeCache      controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager        = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;

            // Act
            ExceptionHelper.ExpectException <InvalidOperationException>(
                delegate {
                factory.GetControllerType("C1");
            },
                @"The controller name 'C1' is ambiguous between the following types:
NS1a.NS1b.C1Controller
NS1a.NS1b.C1Controller");

            // Assert
            Assert.AreEqual <int>(4, controllerTypeCache.Count, "Cache should have 4 controller types.");
        }
Example #21
0
        public void GetControllerTypeSearchesAllNamespacesAsLastResort()
        {
            // Arrange
            RequestContext           requestContext      = GetRequestContextWithNamespaces("ns3a.ns3b");
            DefaultControllerFactory factory             = GetDefaultControllerFactory("ns1a.ns1b");
            MockBuildManager         buildManagerMock    = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1") });
            ControllerTypeCache      controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager        = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;

            // Act
            Type c2Type = factory.GetControllerType(requestContext, "C2");

            // Assert
            Assembly asm1       = Assembly.Load("MvcAssembly1");
            Type     verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller");

            Assert.NotNull(verifiedC2);
            Assert.Equal(verifiedC2, c2Type);
            Assert.Equal(2, controllerTypeCache.Count);
        }
Example #22
0
        public void GetControllerTypeSearchesAllNamespacesAsLastResort()
        {
            // Arrange
            RequestContext           requestContext      = GetRequestContextWithNamespaces("ns3a.ns3b");
            DefaultControllerFactory factory             = GetDefaultControllerFactory("ns1a.ns1b");
            MockBuildManager         buildManagerMock    = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1") });
            ControllerTypeCache      controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager        = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;

            // Act
            Type c2Type = factory.GetControllerType(requestContext, "C2");

            // Assert
            Assembly asm1       = Assembly.Load("MvcAssembly1");
            Type     verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller");

            Assert.IsNotNull(verifiedC2, "Couldn't find real C2 type");
            Assert.AreEqual <Type>(verifiedC2, c2Type, "Should have found C2Controller type.");
            Assert.AreEqual <int>(2, controllerTypeCache.Count, "Cache should have 2 controller types.");
        }
        public void GetControllerTypeForAssembliesWithSameTypeNamesInDifferentNamespacesThrowsIfAmbiguous()
        {
            // Arrange
            RequestContext requestContext = new RequestContext(
                new Mock <HttpContextBase>().Object,
                new RouteData()
                );
            DefaultControllerFactory factory = GetDefaultControllerFactory(
                "ns1a.ns1b",
                "ns3a.ns3b"
                );
            MockBuildManager buildManagerMock = new MockBuildManager(
                new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly3") }
                );
            ControllerTypeCache controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager        = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;

            // Act
            Assert.Throws <InvalidOperationException>(
                delegate
            {
                factory.GetControllerType(requestContext, "C1");
            },
                "Multiple types were found that match the controller named 'C1'. This can happen if the route that services this request does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter."
                + Environment.NewLine
                + Environment.NewLine
                + "The request for 'C1' has found the following matching controllers:"
                + Environment.NewLine
                + "NS1a.NS1b.C1Controller"
                + Environment.NewLine
                + "NS3a.NS3b.C1Controller"
                );

            // Assert
            Assert.Equal(4, controllerTypeCache.Count);
        }
        public void GetControllerTypeForOneAssembly()
        {
            // Arrange
            DefaultControllerFactory factory             = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b");
            MockBuildManager         buildManagerMock    = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1") });
            ControllerTypeCache      controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager        = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;

            // Act
            Type c1Type = factory.GetControllerType("C1");
            Type c2Type = factory.GetControllerType("c2");

            // Assert
            Assembly asm1       = Assembly.Load("MvcAssembly1");
            Type     verifiedC1 = asm1.GetType("NS1a.NS1b.C1Controller");
            Type     verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller");

            Assert.AreEqual <Type>(verifiedC1, c1Type, "Should have found C1Controller type.");
            Assert.AreEqual <Type>(verifiedC2, c2Type, "Should have found C2Controller type.");
            Assert.AreEqual <int>(2, controllerTypeCache.Count, "Cache should have 2 controller types.");
        }
        public void GetControllerTypeThatDoesntExist()
        {
            // Arrange
            DefaultControllerFactory factory             = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b", "ns3a.ns3b", "ns4a.ns4b");
            MockBuildManager         buildManagerMock    = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly2"), Assembly.Load("MvcAssembly3"), Assembly.Load("MvcAssembly4") });
            ControllerTypeCache      controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager        = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;

            // Act
            Type randomType1 = factory.GetControllerType("Cx");
            Type randomType2 = factory.GetControllerType("Cy");
            Type randomType3 = factory.GetControllerType("Foo.Bar");
            Type randomType4 = factory.GetControllerType("C1Controller");

            // Assert
            Assert.IsNull(randomType1, "Controller type should not have been found.");
            Assert.IsNull(randomType2, "Controller type should not have been found.");
            Assert.IsNull(randomType3, "Controller type should not have been found.");
            Assert.IsNull(randomType4, "Controller type should not have been found.");
            Assert.AreEqual <int>(8, controllerTypeCache.Count, "Cache should have 8 controller types.");
        }
        public void GetControllerTypeSearchesOnlyRouteDefinedNamespacesIfRequested()
        {
            // Arrange
            RequestContext requestContext = GetRequestContextWithNamespaces("ns3a.ns3b");
            requestContext.RouteData.DataTokens["UseNamespaceFallback"] = false;
            DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b");
            MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly3") });
            ControllerTypeCache controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;

            // Act
            Type c1Type = factory.GetControllerType(requestContext, "C1");
            Type c2Type = factory.GetControllerType(requestContext, "C2");

            // Assert
            Assembly asm3 = Assembly.Load("MvcAssembly3");
            Type verifiedC1 = asm3.GetType("NS3a.NS3b.C1Controller");
            Assert.NotNull(verifiedC1);
            Assert.Equal(verifiedC1, c1Type);
            Assert.Null(c2Type);
        }
        public void GetControllerTypeSearchesAllNamespacesAsLastResort()
        {
            // Arrange
            RequestContext requestContext = GetRequestContextWithNamespaces("ns3a.ns3b");
            DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b");
            MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1") });
            ControllerTypeCache controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;

            // Act
            Type c2Type = factory.GetControllerType(requestContext, "C2");

            // Assert
            Assembly asm1 = Assembly.Load("MvcAssembly1");
            Type verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller");
            Assert.NotNull(verifiedC2);
            Assert.Equal(verifiedC2, c2Type);
            Assert.Equal(2, controllerTypeCache.Count);
        }
        public void GetControllerTypeForAssembliesWithSameTypeNamesInSameNamespaceThrows()
        {
            // Arrange
            RequestContext requestContext = new RequestContext(new Mock<HttpContextBase>().Object, new RouteData());
            DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b");
            MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly4") });
            ControllerTypeCache controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;

            // Act
            Assert.Throws<InvalidOperationException>(
                delegate { factory.GetControllerType(requestContext, "C1"); },
                "Multiple types were found that match the controller named 'C1'. This can happen if the route that services this request does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter." + Environment.NewLine
              + Environment.NewLine
              + "The request for 'C1' has found the following matching controllers:" + Environment.NewLine
              + "NS1a.NS1b.C1Controller" + Environment.NewLine
              + "NS1a.NS1b.C1Controller");

            // Assert
            Assert.Equal(4, controllerTypeCache.Count);
        }
        public void GetControllerTypeForAssembliesWithSameTypeNamesInDifferentNamespaces()
        {
            // Arrange
            RequestContext requestContext = new RequestContext(new Mock<HttpContextBase>().Object, new RouteData());
            DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b");
            MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly3") });
            ControllerTypeCache controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;

            // Act
            Type c1Type = factory.GetControllerType(requestContext, "C1");
            Type c2Type = factory.GetControllerType(requestContext, "C2");

            // Assert
            Assembly asm1 = Assembly.Load("MvcAssembly1");
            Type verifiedC1 = asm1.GetType("NS1a.NS1b.C1Controller");
            Type verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller");
            Assert.NotNull(verifiedC1);
            Assert.NotNull(verifiedC2);
            Assert.Equal(verifiedC1, c1Type);
            Assert.Equal(verifiedC2, c2Type);
            Assert.Equal(4, controllerTypeCache.Count);
        }
        public void GetControllerTypeDoesNotThrowIfSameControllerMatchedMultipleNamespaces()
        {
            // both namespaces "ns3a" and "ns3a.ns3b" will match a controller type, but it's actually
            // the same type. in this case, we shouldn't throw.

            // Arrange
            RequestContext requestContext = GetRequestContextWithNamespaces("ns3a", "ns3a.ns3b");
            requestContext.RouteData.DataTokens["UseNamespaceFallback"] = false;
            DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b");
            MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly3") });
            ControllerTypeCache controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;

            // Act
            Type c1Type = factory.GetControllerType(requestContext, "C1");

            // Assert
            Assembly asm3 = Assembly.Load("MvcAssembly3");
            Type verifiedC1 = asm3.GetType("NS3a.NS3b.C1Controller");
            Assert.NotNull(verifiedC1);
            Assert.Equal(verifiedC1, c1Type);
        }
        public void GetControllerTypeForManyAssemblies()
        {
            // Arrange
            RequestContext requestContext = new RequestContext(new Mock<HttpContextBase>().Object, new RouteData());
            DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b", "ns3a.ns3b", "ns4a.ns4b");
            MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly2") });
            ControllerTypeCache controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;

            // Act
            Type c1Type = factory.GetControllerType(requestContext, "C1");
            Type c2Type = factory.GetControllerType(requestContext, "C2");
            Type c3Type = factory.GetControllerType(requestContext, "c3"); // lower case
            Type c4Type = factory.GetControllerType(requestContext, "c4"); // lower case

            // Assert
            Assembly asm1 = Assembly.Load("MvcAssembly1");
            Type verifiedC1 = asm1.GetType("NS1a.NS1b.C1Controller");
            Type verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller");
            Assembly asm2 = Assembly.Load("MvcAssembly2");
            Type verifiedC3 = asm2.GetType("NS3a.NS3b.C3Controller");
            Type verifiedC4 = asm2.GetType("NS4a.NS4b.C4Controller");
            Assert.NotNull(verifiedC1);
            Assert.NotNull(verifiedC2);
            Assert.NotNull(verifiedC3);
            Assert.NotNull(verifiedC4);
            Assert.Equal(verifiedC1, c1Type);
            Assert.Equal(verifiedC2, c2Type);
            Assert.Equal(verifiedC3, c3Type);
            Assert.Equal(verifiedC4, c4Type);
            Assert.Equal(4, controllerTypeCache.Count);
        }
        public void GetControllerTypeSearchesRouteDefinedNamespacesBeforeApplicationDefinedNamespaces()
        {
            // Arrange
            RequestContext requestContext = GetRequestContextWithNamespaces("ns3a.ns3b");
            DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b");
            MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly3") });
            ControllerTypeCache controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;

            // Act
            Type c1Type = factory.GetControllerType(requestContext, "C1");
            Type c2Type = factory.GetControllerType(requestContext, "C2");

            // Assert
            Assembly asm1 = Assembly.Load("MvcAssembly1");
            Type verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller");
            Assembly asm3 = Assembly.Load("MvcAssembly3");
            Type verifiedC1 = asm3.GetType("NS3a.NS3b.C1Controller");
            Assert.NotNull(verifiedC1);
            Assert.NotNull(verifiedC2);
            Assert.Equal(verifiedC1, c1Type);
            Assert.Equal(verifiedC2, c2Type);
            Assert.Equal(4, controllerTypeCache.Count);
        }
        public void GetControllerTypeThatDoesntExist()
        {
            // Arrange
            RequestContext requestContext = new RequestContext(new Mock<HttpContextBase>().Object, new RouteData());
            DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b", "ns3a.ns3b", "ns4a.ns4b");
            MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly2"), Assembly.Load("MvcAssembly3"), Assembly.Load("MvcAssembly4") });
            ControllerTypeCache controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;

            // Act
            Type randomType1 = factory.GetControllerType(requestContext, "Cx");
            Type randomType2 = factory.GetControllerType(requestContext, "Cy");
            Type randomType3 = factory.GetControllerType(requestContext, "Foo.Bar");
            Type randomType4 = factory.GetControllerType(requestContext, "C1Controller");

            // Assert
            Assert.Null(randomType1);
            Assert.Null(randomType2);
            Assert.Null(randomType3);
            Assert.Null(randomType4);
            Assert.Equal(8, controllerTypeCache.Count);
        }
        public void GetControllerTypeForNoAssemblies()
        {
            // Arrange
            RequestContext requestContext = new RequestContext(new Mock<HttpContextBase>().Object, new RouteData());
            DefaultControllerFactory factory = new DefaultControllerFactory();
            MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { });
            ControllerTypeCache controllerTypeCache = new ControllerTypeCache();

            factory.BuildManager = buildManagerMock;
            factory.ControllerTypeCache = controllerTypeCache;

            // Act
            Type controllerType = factory.GetControllerType(requestContext, "sometype");

            // Assert
            Assert.Null(controllerType);
            Assert.Equal(0, controllerTypeCache.Count);
        }