public void GetControllerType_WithMultipleDirectRouteControllers_ThrowsInvalidOperationException()
        {
            // Arrange
            var requestContext = new RequestContext();

            requestContext.RouteData = new RouteData();
            SubRouteCollection subRoutes = new SubRouteCollection();

            DirectRouteTestHelpers.AddDirectRouteFromController <AttributeRouteAtControllerLevelController>(
                subRoutes
                );
            DirectRouteTestHelpers.AddDirectRouteFromMethod <WithRoutingAttributeController>(
                subRoutes,
                c => c.Action()
                );
            requestContext.RouteData.Route = new RouteCollectionRoute(subRoutes);
            requestContext.RouteData.AddDirectRouteMatches();

            var controllerActivator = new Mock <IControllerActivator>(MockBehavior.Strict).Object;
            var activatorResolver   = new Resolver <IControllerActivator>();

            var factory = new DefaultControllerFactory(
                controllerActivator,
                activatorResolver,
                null
                );

            // Act & Assert
            Assert.Throws <InvalidOperationException>(
                () => factory.GetControllerType(requestContext, controllerName: null)
                );
        }
        public void GetControllerType_WithMatchedDirectRoute_UseControllerDescriptorType()
        {
            // Arrange
            var requestContext = new RequestContext();

            requestContext.RouteData       = new RouteData();
            requestContext.RouteData.Route =
                DirectRouteTestHelpers.BuildDirectRouteFromController <AttributeRouteAtControllerLevelController>();
            requestContext.RouteData.AddDirectRouteMatches();

            var expectedControllerType = typeof(AttributeRouteAtControllerLevelController);

            var controllerActivator = new Mock <IControllerActivator>(MockBehavior.Strict).Object;
            var activatorResolver   = new Resolver <IControllerActivator>();

            var factory = new DefaultControllerFactory(
                controllerActivator,
                activatorResolver,
                null
                );

            // Act
            // if it would not get the controller type from the DirectRoute, then it would not
            // be able to find any controller
            var type = factory.GetControllerType(requestContext, controllerName: null);

            // Assert
            Assert.Equal(expectedControllerType, type);
        }
Ejemplo n.º 3
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.IsNotNull(verifiedC1, "Couldn't find real C1 type");
            Assert.AreEqual(verifiedC1, c1Type, "Should have found C1Controller type.");
        }
Ejemplo n.º 4
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.");
        }
        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);
        }
Ejemplo n.º 6
0
        public void GetControllerTypeWithEmptyControllerNameThrows()
        {
            // Arrange
            RequestContext           requestContext = new RequestContext(new Mock <HttpContextBase>().Object, new RouteData());
            DefaultControllerFactory factory        = new DefaultControllerFactory();

            // Act
            Assert.Throws <ArgumentException>(
                delegate { factory.GetControllerType(requestContext, String.Empty); },
                "Value cannot be null or empty.\r\nParameter name: controllerName");
        }
        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 GetControllerTypeWithEmptyControllerNameThrows()
        {
            // Arrange
            DefaultControllerFactory factory = new DefaultControllerFactory();

            // Act
            ExceptionHelper.ExpectArgumentException(
                delegate {
                factory.GetControllerType(
                    String.Empty);
            },
                "Value cannot be null or empty.\r\nParameter name: controllerName");
        }
        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 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.");
        }
Ejemplo n.º 11
0
        public void GetControllerTypeForOneAssembly()
        {
            // 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") });
            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.Equal(verifiedC1, c1Type);
            Assert.Equal(verifiedC2, c2Type);
            Assert.Equal(2, controllerTypeCache.Count);
        }
        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.");
        }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 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.");
        }
Ejemplo n.º 15
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);
        }
Ejemplo n.º 16
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 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.");
        }
        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);
        }
Ejemplo n.º 19
0
        public void GetControllerType_WithMatchedDirectRoute_UseTargetMethodType()
        {
            // Arrange
            var requestContext = new RequestContext();

            requestContext.RouteData       = new RouteData();
            requestContext.RouteData.Route = DirectRouteTestHelpers.BuildDirectRouteStubsFrom <WithRoutingAttributeController>(c => c.Action())[0];

            var expectedControllerType = typeof(WithRoutingAttributeController);

            var controllerActivator = new Mock <IControllerActivator>(MockBehavior.Strict).Object;
            var activatorResolver   = new Resolver <IControllerActivator>();

            var factory = new DefaultControllerFactory(controllerActivator, activatorResolver, null);

            // Act
            // if it would not get the controller type from the DirectRoute, then it would not
            // be able to find any controller
            var type = factory.GetControllerType(requestContext, "no_such_controller");

            // Assert
            Assert.Equal(expectedControllerType, type);
        }
        public void GetControllerType_WithMatchedDirectRoute_UseTargetMethodType()
        {
            // Arrange
            var requestContext = new RequestContext();
            requestContext.RouteData = new RouteData();
            requestContext.RouteData.Route = DirectRouteTestHelpers.BuildDirectRouteStubsFrom<WithRoutingAttributeController>(c => c.Action())[0]; 
            
            var expectedControllerType = typeof(WithRoutingAttributeController);

            var controllerActivator = new Mock<IControllerActivator>(MockBehavior.Strict).Object;
            var activatorResolver = new Resolver<IControllerActivator>();

            var factory = new DefaultControllerFactory(controllerActivator, activatorResolver, null);

            // Act
            // if it would not get the controller type from the DirectRoute, then it would not 
            // be able to find any controller
            var type = factory.GetControllerType(requestContext, "no_such_controller");

            // Assert
            Assert.Equal(expectedControllerType, type);
        }
        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);
        }
        public void GetControllerTypeWithEmptyControllerNameThrows()
        {
            // Arrange
            RequestContext requestContext = new RequestContext(new Mock<HttpContextBase>().Object, new RouteData());
            DefaultControllerFactory factory = new DefaultControllerFactory();

            // Act
            Assert.Throws<ArgumentException>(
                delegate { factory.GetControllerType(requestContext, String.Empty); },
                "Value cannot be null or empty.\r\nParameter name: controllerName");
        }
        public void GetControllerType_WithMultipleDirectRouteControllers_ThrowsInvalidOperationException()
        {
            // Arrange
            var requestContext = new RequestContext();
            requestContext.RouteData = new RouteData();
            SubRouteCollection subRoutes = new SubRouteCollection();
            DirectRouteTestHelpers.AddDirectRouteFromController<AttributeRouteAtControllerLevelController>(subRoutes);
            DirectRouteTestHelpers.AddDirectRouteFromMethod<WithRoutingAttributeController>(subRoutes, c => c.Action());
            requestContext.RouteData.Route = new RouteCollectionRoute(subRoutes);
            requestContext.RouteData.AddDirectRouteMatches();

            var controllerActivator = new Mock<IControllerActivator>(MockBehavior.Strict).Object;
            var activatorResolver = new Resolver<IControllerActivator>();

            var factory = new DefaultControllerFactory(controllerActivator, activatorResolver, null);

            // Act & Assert
            Assert.Throws<InvalidOperationException>(() => factory.GetControllerType(requestContext, controllerName: null));
        }
        public void GetControllerType_WithMatchedDirectRoute_UseControllerDescriptorType()
        {
            // Arrange
            var requestContext = new RequestContext();
            requestContext.RouteData = new RouteData();
            requestContext.RouteData.Route = DirectRouteTestHelpers.BuildDirectRouteFromController<AttributeRouteAtControllerLevelController>();
            requestContext.RouteData.AddDirectRouteMatches();

            var expectedControllerType = typeof(AttributeRouteAtControllerLevelController);

            var controllerActivator = new Mock<IControllerActivator>(MockBehavior.Strict).Object;
            var activatorResolver = new Resolver<IControllerActivator>();

            var factory = new DefaultControllerFactory(controllerActivator, activatorResolver, null);

            // Act
            // if it would not get the controller type from the DirectRoute, then it would not 
            // be able to find any controller
            var type = factory.GetControllerType(requestContext, controllerName: null);

            // Assert
            Assert.Equal(expectedControllerType, type);
        }