More working tests.
[castle.git] / MonoRail / Castle.MonoRail.Framework.Tests / Resources / ControllerWithResourcesTestCase.cs
blob4475d345bb033296d73e9642eadde56979993aad
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Framework.Tests.Resources
17 using System.Collections;
18 using Castle.MonoRail.Framework.Resources;
19 using Descriptors;
20 using NUnit.Framework;
21 using Rhino.Mocks;
22 using Test;
24 [TestFixture]
25 public class ControllerWithResourcesTestCase
27 private MockRepository mockRepository = new MockRepository();
28 private MockEngineContext engineContext;
29 private ViewEngineManagerStub viewEngStub;
30 private MockServices services;
31 private IResourceFactory resourceFactoryMock;
33 [SetUp]
34 public void Init()
36 resourceFactoryMock = mockRepository.CreateMock<IResourceFactory>();
38 MockRequest request = new MockRequest();
39 MockResponse response = new MockResponse();
40 services = new MockServices();
41 viewEngStub = new ViewEngineManagerStub();
42 services.ViewEngineManager = viewEngStub;
43 services.ResourceFactory = resourceFactoryMock;
44 engineContext = new MockEngineContext(request, response, services, null);
47 [Test]
48 public void CreatesResourcesSpecifiedThroughAttributes()
50 ControllerWithResource controller = new ControllerWithResource();
52 IControllerContext context = services.ControllerContextFactory.
53 Create("", "home", "index", services.ControllerDescriptorProvider.BuildDescriptor(controller));
55 using(mockRepository.Record())
57 Expect.Call(resourceFactoryMock.Create(
58 new ResourceDescriptor(null, "key", "Castle.MonoRail.Framework.Tests.Resources.Language", "neutral",
59 "Castle.MonoRail.Framework.Tests"),
60 typeof(ControllerWithResourcesTestCase).Assembly)).Return(new DummyResource());
63 using(mockRepository.Playback())
65 controller.Process(engineContext, context);
67 Assert.AreEqual(1, context.Resources.Count);
68 Assert.IsNotNull(context.Resources["key"]);
72 [Test, ExpectedException(typeof(MonoRailException), "There is a duplicated entry on the resource dictionary. Resource entry name: key")]
73 public void DuplicatedResourceEntriesHaveDecentErrorMessage()
75 ControllerWithResourceDuplicated controller = new ControllerWithResourceDuplicated();
77 IControllerContext context = services.ControllerContextFactory.
78 Create("", "home", "index", services.ControllerDescriptorProvider.BuildDescriptor(controller));
80 using(mockRepository.Record())
82 Expect.Call(resourceFactoryMock.Create(
83 new ResourceDescriptor(null, "key", "Castle.MonoRail.Framework.Tests.Resources.Language", "neutral",
84 "Castle.MonoRail.Framework.Tests"),
85 typeof(ControllerWithResourcesTestCase).Assembly)).Return(new DummyResource());
88 using(mockRepository.Playback())
90 controller.Process(engineContext, context);
94 #region Controllers
96 [Resource("key", "Castle.MonoRail.Framework.Tests.Resources.Language", CultureName = "neutral", AssemblyName = "Castle.MonoRail.Framework.Tests")]
97 private class ControllerWithResource : Controller
99 public void Index()
104 [Resource("key", "Castle.MonoRail.Framework.Tests.Resources.Language", CultureName = "neutral", AssemblyName = "Castle.MonoRail.Framework.Tests")]
105 [Resource("key", "Castle.MonoRail.Framework.Tests.Resources.Language", CultureName = "neutral", AssemblyName = "Castle.MonoRail.Framework.Tests")]
106 private class ControllerWithResourceDuplicated : Controller
108 public void Index()
113 #endregion
115 private class DummyResource : IResource
117 public object this[string key]
119 get { throw new System.NotImplementedException(); }
122 public string GetString(string key)
124 throw new System.NotImplementedException();
127 public object GetObject(string key)
129 throw new System.NotImplementedException();
132 public IEnumerator GetEnumerator()
134 throw new System.NotImplementedException();