1
// Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
.Services
17 using System
.Collections
;
18 using System
.Collections
.Generic
;
20 using Castle
.Components
.Common
.EmailSender
;
21 using NUnit
.Framework
;
23 using Rhino
.Mocks
.Constraints
;
27 public class EmailTemplateServiceTestCase
29 private MockRepository mockRepository
= new MockRepository();
30 private EmailTemplateService service
;
31 private IViewEngineManager viewEngineManagerMock
;
32 private MockEngineContext engineContext
;
33 private DummyController controller
;
34 private ControllerContext controllerContext
;
39 viewEngineManagerMock
= mockRepository
.CreateMock
<IViewEngineManager
>();
41 service
= new EmailTemplateService(viewEngineManagerMock
);
43 engineContext
= new MockEngineContext(null, null, null, null);
44 controller
= new DummyController();
45 controllerContext
= new ControllerContext();
49 public void RenderMailMessage_BackwardCompatibility_PassOnControllerContext()
51 string templateName
= "welcome";
53 using(mockRepository
.Record())
55 viewEngineManagerMock
.Process(templateName
, null, engineContext
, controller
, controllerContext
);
57 Is
.Equal("mail\\" + templateName
),
59 Is
.Same(engineContext
),
61 Is
.Same(controllerContext
));
64 using(mockRepository
.Playback())
66 service
.RenderMailMessage(templateName
, engineContext
, controller
, controllerContext
, false);
71 public void RenderMailMessage_BackwardCompatibility_UsesTemplateNameAsItIsIfStartsWithSlash()
73 string templateName
= "/emailtemplates/welcome";
75 using(mockRepository
.Record())
77 viewEngineManagerMock
.Process(templateName
, null, engineContext
, controller
, controllerContext
);
79 Is
.Equal(templateName
),
81 Is
.Same(engineContext
),
83 Is
.Same(controllerContext
));
86 using(mockRepository
.Playback())
88 service
.RenderMailMessage(templateName
, engineContext
, controller
, controllerContext
, false);
93 public void RenderMailMessage_InvokesViewEngineManager()
95 string templateName
= "welcome";
96 Hashtable parameters
= new Hashtable();
98 using(mockRepository
.Record())
100 viewEngineManagerMock
.Process(templateName
, "layout", null, null);
101 LastCall
.Constraints(
102 Is
.Equal("mail\\" + templateName
),
108 using(mockRepository
.Playback())
110 service
.RenderMailMessage(templateName
, "layout", parameters
);
115 public void RenderMailMessage_MessageIsConstructedCorrectly()
117 string templateName
= "welcome";
118 Hashtable parameters
= new Hashtable();
120 using(mockRepository
.Record())
122 Expect
.Call(delegate() { viewEngineManagerMock.Process(templateName, "layout", null, null); }
)
124 Is
.Equal("mail\\" + templateName
),
128 .Do(new Render(RendersEmail
));
131 using(mockRepository
.Playback())
133 Message message
= service
.RenderMailMessage(templateName
, "layout", parameters
);
135 Assert
.AreEqual("hammett@noemail.com", message
.To
);
136 Assert
.AreEqual("copied@noemail.com", message
.Cc
);
137 Assert
.AreEqual("bcopied@noemail.com", message
.Bcc
);
138 Assert
.AreEqual("contact@noemail.com", message
.From
);
139 Assert
.AreEqual("Hello!", message
.Subject
);
140 Assert
.AreEqual("This is the\r\nbody\r\n", message
.Body
);
141 Assert
.AreEqual(1, message
.Headers
.Count
);
145 public delegate void Render(
146 string templateName
, string layoutName
, TextWriter output
, IDictionary
<string, object> parameters
);
148 public static void RendersEmail(string templateName
, string layoutName
, TextWriter output
,
149 IDictionary
<string, object> parameters
)
151 output
.WriteLine("to: hammett@noemail.com");
152 output
.WriteLine("cc: copied@noemail.com");
153 output
.WriteLine("bcc: bcopied@noemail.com");
154 output
.WriteLine("from: contact@noemail.com");
155 output
.WriteLine("subject: Hello!");
156 output
.WriteLine("X-something: Mime-super-content");
157 output
.WriteLine("");
158 output
.WriteLine("This is the");
159 output
.WriteLine("body");
162 private class DummyController
: Controller