File: DefaultControllerFactoryTest.cs

package info (click to toggle)
mono 5.18.0.240%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,253,216 kB
  • sloc: cs: 10,925,936; xml: 2,804,987; ansic: 643,970; cpp: 120,384; perl: 59,272; asm: 21,383; sh: 20,162; makefile: 18,157; python: 4,715; pascal: 924; sql: 859; sed: 16; php: 1
file content (768 lines) | stat: -rw-r--r-- 33,371 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System.Reflection;
using System.Web.Routing;
using System.Web.SessionState;
using Moq;
using Xunit;
using Xunit.Extensions;
using Assert = Microsoft.TestCommon.AssertEx;

namespace System.Web.Mvc.Test
{
    [CLSCompliant(false)]
    public class DefaultControllerFactoryTest
    {
        static DefaultControllerFactoryTest()
        {
            MvcTestHelper.CreateMvcAssemblies();
        }

        [Fact]
        public void CreateAmbiguousControllerException_RouteWithoutUrl()
        {
            // Arrange
            RouteBase route = new Mock<RouteBase>().Object;

            Type[] matchingTypes = new Type[]
            {
                typeof(object),
                typeof(string)
            };

            // Act
            InvalidOperationException exception = DefaultControllerFactory.CreateAmbiguousControllerException(route, "Foo", matchingTypes);

            // Assert
            Assert.Equal(@"Multiple types were found that match the controller named 'Foo'. 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 'Foo' has found the following matching controllers:
System.Object
System.String", exception.Message);
        }

        [Fact]
        public void CreateAmbiguousControllerException_RouteWithUrl()
        {
            // Arrange
            RouteBase route = new Route("{controller}/blah", new Mock<IRouteHandler>().Object);

            Type[] matchingTypes = new Type[]
            {
                typeof(object),
                typeof(string)
            };

            // Act
            InvalidOperationException exception = DefaultControllerFactory.CreateAmbiguousControllerException(route, "Foo", matchingTypes);

            // Assert
            Assert.Equal(@"Multiple types were found that match the controller named 'Foo'. This can happen if the route that services this request ('{controller}/blah') 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 'Foo' has found the following matching controllers:
System.Object
System.String", exception.Message);
        }

        [Fact]
        public void CreateControllerWithNullContextThrows()
        {
            // Arrange
            DefaultControllerFactory factory = new DefaultControllerFactory();

            // Act
            Assert.ThrowsArgumentNull(
                delegate
                {
                    ((IControllerFactory)factory).CreateController(
                        null,
                        "foo");
                },
                "requestContext");
        }

        [Fact]
        public void CreateControllerWithEmptyControllerNameThrows()
        {
            // Arrange
            DefaultControllerFactory factory = new DefaultControllerFactory();

            // Act
            Assert.Throws<ArgumentException>(
                delegate
                {
                    ((IControllerFactory)factory).CreateController(
                        new RequestContext(new Mock<HttpContextBase>().Object, new RouteData()),
                        String.Empty);
                },
                "Value cannot be null or empty.\r\nParameter name: controllerName");
        }

        [Fact]
        public void CreateControllerReturnsControllerInstance()
        {
            // Arrange
            RequestContext requestContext = new RequestContext(new Mock<HttpContextBase>().Object, new RouteData());
            Mock<DefaultControllerFactory> factoryMock = new Mock<DefaultControllerFactory>();
            factoryMock.CallBase = true;
            factoryMock.Setup(o => o.GetControllerType(requestContext, "moo")).Returns(typeof(DummyController));

            // Act
            IController controller = ((IControllerFactory)factoryMock.Object).CreateController(requestContext, "moo");

            // Assert
            Assert.IsType<DummyController>(controller);
        }

        [Fact]
        public void CreateControllerCanReturnNull()
        {
            // Arrange
            RequestContext requestContext = new RequestContext(new Mock<HttpContextBase>().Object, new RouteData());
            Mock<DefaultControllerFactory> factoryMock = new Mock<DefaultControllerFactory>();
            factoryMock.Setup(o => o.GetControllerType(requestContext, "moo")).Returns(typeof(DummyController));
            factoryMock.Setup(o => o.GetControllerInstance(requestContext, typeof(DummyController))).Returns((ControllerBase)null);

            // Act
            IController controller = ((IControllerFactory)factoryMock.Object).CreateController(requestContext, "moo");

            // Assert
            Assert.Null(controller);
        }

        [Fact]
        public void DisposeControllerFactoryWithDisposableController()
        {
            // Arrange
            IControllerFactory factory = new DefaultControllerFactory();
            Mock<ControllerBase> mockController = new Mock<ControllerBase>();
            Mock<IDisposable> mockDisposable = mockController.As<IDisposable>();
            mockDisposable.Setup(d => d.Dispose()).Verifiable();

            // Act
            factory.ReleaseController(mockController.Object);

            // Assert
            mockDisposable.Verify();
        }

        [Fact]
        public void GetControllerInstanceThrowsIfControllerTypeIsNull()
        {
            // Arrange
            Mock<HttpContextBase> contextMock = new Mock<HttpContextBase>();
            Mock<HttpRequestBase> requestMock = new Mock<HttpRequestBase>();
            contextMock.Setup(o => o.Request).Returns(requestMock.Object);
            requestMock.Setup(o => o.Path).Returns("somepath");
            RequestContext requestContext = new RequestContext(contextMock.Object, new RouteData());
            Mock<DefaultControllerFactory> factoryMock = new Mock<DefaultControllerFactory> { CallBase = true };
            factoryMock.Setup(o => o.GetControllerType(requestContext, "moo")).Returns((Type)null);

            // Act
            Assert.ThrowsHttpException(
                delegate { ((IControllerFactory)factoryMock.Object).CreateController(requestContext, "moo"); },
                "The controller for path 'somepath' was not found or does not implement IController.",
                404);
        }

        [Fact]
        public void GetControllerInstanceThrowsIfControllerTypeIsNotControllerBase()
        {
            // Arrange
            RequestContext requestContext = new RequestContext(new Mock<HttpContextBase>().Object, new RouteData());
            DefaultControllerFactory factory = new DefaultControllerFactory();

            // Act
            Assert.Throws<ArgumentException>(
                delegate { factory.GetControllerInstance(requestContext, typeof(int)); },
                "The controller type 'System.Int32' must implement IController.\r\nParameter name: controllerType");
        }

        [Fact]
        public void GetControllerInstanceWithBadConstructorThrows()
        {
            // Arrange
            Mock<HttpContextBase> contextMock = new Mock<HttpContextBase>();
            RequestContext requestContext = new RequestContext(contextMock.Object, new RouteData());
            Mock<DefaultControllerFactory> factoryMock = new Mock<DefaultControllerFactory>();
            factoryMock.CallBase = true;
            factoryMock.Setup(o => o.GetControllerType(requestContext, "moo")).Returns(typeof(DummyControllerThrows));

            // Act
            Exception ex = Assert.Throws<InvalidOperationException>(
                delegate { ((IControllerFactory)factoryMock.Object).CreateController(requestContext, "moo"); },
                "An error occurred when trying to create a controller of type 'System.Web.Mvc.Test.DefaultControllerFactoryTest+DummyControllerThrows'. Make sure that the controller has a parameterless public constructor.");

            Assert.Equal("constructor", ex.InnerException.InnerException.Message);
        }

        [Fact]
        public void GetControllerSessionBehaviorGuardClauses()
        {
            // Arrange
            RequestContext requestContext = new RequestContext(new Mock<HttpContextBase>().Object, new RouteData());
            IControllerFactory factory = new DefaultControllerFactory();

            // Act & Assert
            Assert.ThrowsArgumentNull(
                () => factory.GetControllerSessionBehavior(null, "controllerName"),
                "requestContext"
                );
            Assert.ThrowsArgumentNullOrEmpty(
                () => factory.GetControllerSessionBehavior(requestContext, null),
                "controllerName"
                );
            Assert.ThrowsArgumentNullOrEmpty(
                () => factory.GetControllerSessionBehavior(requestContext, ""),
                "controllerName"
                );
        }

        [Fact]
        public void GetControllerSessionBehaviorReturnsDefaultForNullControllerType()
        {
            // Arrange
            var factory = new DefaultControllerFactory();

            // Act
            SessionStateBehavior result = factory.GetControllerSessionBehavior(null, null);

            // Assert
            Assert.Equal(SessionStateBehavior.Default, result);
        }

        [Fact]
        public void GetControllerSessionBehaviorReturnsDefaultForControllerWithoutAttribute()
        {
            // Arrange
            var factory = new DefaultControllerFactory();

            // Act
            SessionStateBehavior result = factory.GetControllerSessionBehavior(null, typeof(object));

            // Assert
            Assert.Equal(SessionStateBehavior.Default, result);
        }

        [Fact]
        public void GetControllerSessionBehaviorReturnsAttributeValueFromController()
        {
            // Arrange
            var factory = new DefaultControllerFactory();

            // Act
            SessionStateBehavior result = factory.GetControllerSessionBehavior(null, typeof(MyReadOnlyController));

            // Assert
            Assert.Equal(SessionStateBehavior.ReadOnly, result);
        }

        [SessionState(SessionStateBehavior.ReadOnly)]
        class MyReadOnlyController
        {
        }

        [Fact]
        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");
        }

        [Fact]
        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);
        }

        [Fact]
        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);
        }

        [Fact]
        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);
        }

        [Fact]
        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);
        }

        [Fact]
        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);
        }

        [Fact]
        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.

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

            // Assert
            Assert.Equal(4, controllerTypeCache.Count);
        }

        [Fact]
        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.

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

            // Assert
            Assert.Equal(4, controllerTypeCache.Count);
        }

        [Fact]
        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);
        }

        [Fact]
        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);
        }

        [Fact]
        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);
        }

        [Fact]
        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);
        }

        [Fact]
        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);
        }

        [Theory]
        [InlineData(null, false)]
        [InlineData("", true)]
        [InlineData("Dummy", false)]
        [InlineData("Dummy.*", true)]
        [InlineData("Dummy.Controller.*", false)]
        [InlineData("Dummy.Controllers", true)]
        [InlineData("Dummy.Controllers.*", true)]
        [InlineData("Dummy.Controllers*", false)]
        public void IsNamespaceMatch(string testNamespace, bool expectedResult)
        {
            // Act & Assert
            Assert.Equal(expectedResult, ControllerTypeCache.IsNamespaceMatch(testNamespace, "Dummy.Controllers"));
        }

        [Fact]
        public void GetControllerInstanceConsultsSetControllerActivator()
        {
            //Arrange
            Mock<IControllerActivator> activator = new Mock<IControllerActivator>();
            DefaultControllerFactory factory = new DefaultControllerFactory(activator.Object);
            RequestContext context = new RequestContext();

            //Act
            factory.GetControllerInstance(context, typeof(Goodcontroller));

            //Assert
            activator.Verify(l => l.Create(context, typeof(Goodcontroller)));
        }

        [Fact]
        public void GetControllerDelegatesToActivatorResolver()
        {
            //Arrange
            var context = new RequestContext();
            var expectedController = new Goodcontroller();
            var resolverActivator = new Mock<IControllerActivator>();
            resolverActivator.Setup(a => a.Create(context, typeof(Goodcontroller))).Returns(expectedController);
            var activatorResolver = new Resolver<IControllerActivator> { Current = resolverActivator.Object };
            var factory = new DefaultControllerFactory(null, activatorResolver, null);

            //Act
            IController returnedController = factory.GetControllerInstance(context, typeof(Goodcontroller));

            //Assert
            Assert.Same(returnedController, expectedController);
        }

        [Fact]
        public void GetControllerDelegatesToDependencyResolveWhenActivatorResolverIsNull()
        {
            // Arrange
            var context = new RequestContext();
            var expectedController = new Goodcontroller();
            var dependencyResolver = new Mock<IDependencyResolver>(MockBehavior.Strict);
            dependencyResolver.Setup(dr => dr.GetService(typeof(Goodcontroller))).Returns(expectedController);
            var factory = new DefaultControllerFactory(null, null, dependencyResolver.Object);

            // Act
            IController returnedController = factory.GetControllerInstance(context, typeof(Goodcontroller));

            // Assert
            Assert.Same(returnedController, expectedController);
        }

        [Fact]
        public void GetControllerDelegatesToActivatorCreateInstanceWhenDependencyResolverReturnsNull()
        {
            // Arrange
            var context = new RequestContext();
            var dependencyResolver = new Mock<IDependencyResolver>();
            var factory = new DefaultControllerFactory(null, null, dependencyResolver.Object);

            // Act & Assert
            Assert.Throws<InvalidOperationException>(
                () => factory.GetControllerInstance(context, typeof(NoParameterlessCtor)),
                "An error occurred when trying to create a controller of type 'System.Web.Mvc.Test.DefaultControllerFactoryTest+NoParameterlessCtor'. Make sure that the controller has a parameterless public constructor."
                );
        }

        [Fact]
        public void ActivatorResolverAndDependencyResolverAreNeverCalledWhenControllerActivatorIsPassedInConstructor()
        {
            // Arrange
            var context = new RequestContext();
            var expectedController = new Goodcontroller();

            Mock<IControllerActivator> activator = new Mock<IControllerActivator>();
            activator.Setup(a => a.Create(context, typeof(Goodcontroller))).Returns(expectedController);

            var resolverActivator = new Mock<IControllerActivator>(MockBehavior.Strict);
            var activatorResolver = new Resolver<IControllerActivator> { Current = resolverActivator.Object };

            var dependencyResolver = new Mock<IDependencyResolver>(MockBehavior.Strict);

            //Act
            var factory = new DefaultControllerFactory(activator.Object, activatorResolver, dependencyResolver.Object);
            IController returnedController = factory.GetControllerInstance(context, typeof(Goodcontroller));

            //Assert
            Assert.Same(returnedController, expectedController);
        }

        class NoParameterlessCtor : IController
        {
            public NoParameterlessCtor(int x)
            {
            }

            public void Execute(RequestContext requestContext)
            {
            }
        }

        private static DefaultControllerFactory GetDefaultControllerFactory(params string[] namespaces)
        {
            ControllerBuilder builder = new ControllerBuilder();
            builder.DefaultNamespaces.UnionWith(namespaces);
            return new DefaultControllerFactory() { ControllerBuilder = builder };
        }

        private static RequestContext GetRequestContextWithNamespaces(params string[] namespaces)
        {
            RouteData routeData = new RouteData();
            routeData.DataTokens["namespaces"] = namespaces;
            Mock<HttpContextBase> mockHttpContext = new Mock<HttpContextBase>();
            RequestContext requestContext = new RequestContext(mockHttpContext.Object, routeData);
            return requestContext;
        }

        private sealed class DummyController : ControllerBase
        {
            protected override void ExecuteCore()
            {
                throw new NotImplementedException();
            }
        }

        private sealed class DummyControllerThrows : IController
        {
            public DummyControllerThrows()
            {
                throw new Exception("constructor");
            }

            #region IController Members

            void IController.Execute(RequestContext requestContext)
            {
                throw new NotImplementedException();
            }

            #endregion
        }

        public interface IDisposableController : IController, IDisposable
        {
        }
    }

    // BAD: type isn't public
    internal class NonPublicController : Controller
    {
    }

    // BAD: type doesn't end with 'Controller'
    public class MisspelledKontroller : Controller
    {
    }

    // BAD: type is abstract
    public abstract class AbstractController : Controller
    {
    }

    // BAD: type doesn't implement IController
    public class NonIControllerController
    {
    }

    // GOOD: 'Controller' suffix should be case-insensitive
    public class Goodcontroller : Controller
    {
    }
}