Fixed unit tests after Fabians DP2 fix for generic methods.
[castle.git] / Facilities / Synchronize / Castle.Facilities.Synchronize.Tests / SynchronizeFacilityTest.cs
blobabc4c059f5864a6d1accbfea93ff7836791821c0
1 // Copyright 2004-2007 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.Facilities.Synchronize.Tests
17 using System;
18 using System.Configuration;
19 using System.Threading;
20 using System.Windows.Forms;
21 using Castle.Core;
22 using Castle.Core.Configuration;
23 using Castle.MicroKernel.Facilities;
24 using Castle.MicroKernel.Handlers;
25 using Castle.MicroKernel.Proxy;
26 using Castle.Windsor;
27 using NUnit.Framework;
29 [TestFixture]
30 public class SynchronizeFacilityTest
32 private WindsorContainer container;
33 private Exception uncaughtException;
35 [SetUp]
36 public void SetUp()
38 uncaughtException = null;
39 container = new WindsorContainer();
41 container.AddFacility("sync.facility", new SynchronizeFacility());
42 container.AddComponent("dummy.form.class", typeof(DummyForm));
43 container.AddComponent("dummy.form.service", typeof(IDummyForm), typeof(DummyForm));
44 container.AddComponent("class.in.context", typeof(ClassUsingFormInContext));
46 MutableConfiguration componentNode = new MutableConfiguration("component");
47 componentNode.Attributes[Constants.SynchronizedAttrib] = "true";
48 MutableConfiguration synchronizeNode = new MutableConfiguration("synchronize");
49 synchronizeNode.Attributes["contextType"] = typeof(WindowsFormsSynchronizationContext).AssemblyQualifiedName;
50 MutableConfiguration doWorkMethod = new MutableConfiguration("method");
51 doWorkMethod.Attributes["name"] = "DoWork";
52 doWorkMethod.Attributes["contextType"] = typeof(WindowsFormsSynchronizationContext).AssemblyQualifiedName;
53 synchronizeNode.Children.Add(doWorkMethod);
54 componentNode.Children.Add(synchronizeNode);
56 container.AddComponent("generic.class.in.context", typeof(IClassUsingContext<>),
57 typeof(ClassUsingContext<>));
59 container.Kernel.ConfigurationStore.AddComponentConfiguration("class.needing.context", componentNode);
60 container.AddComponent("class.needing.context", typeof(ClassUsingForm));
63 [Test]
64 public void SynchronizeContextReference_DifferentInstanceSameKey_AreEqual()
66 SynchronizeContextReference sync1 = new SynchronizeContextReference("key1");
67 SynchronizeContextReference sync2 = new SynchronizeContextReference("key1");
69 Assert.AreEqual(sync1, sync2);
70 Assert.AreEqual(sync1.GetHashCode(), sync2.GetHashCode());
73 [Test]
74 public void SynchronizeContextReference_DifferentInstanceDifferentKey_AreNotEqual()
76 SynchronizeContextReference sync1 = new SynchronizeContextReference("key1");
77 SynchronizeContextReference sync2 = new SynchronizeContextReference("key2");
79 Assert.AreNotEqual(sync1, sync2);
80 Assert.AreNotEqual(sync1.GetHashCode(), sync2.GetHashCode());
83 [Test]
84 public void SynchronizeContextReference_DifferentInstanceSameType_AreEqual()
86 SynchronizeContextReference sync1 = new SynchronizeContextReference(typeof(string));
87 SynchronizeContextReference sync2 = new SynchronizeContextReference(typeof(string));
89 Assert.AreEqual(sync1, sync2);
90 Assert.AreEqual(sync1.GetHashCode(), sync2.GetHashCode());
93 [Test]
94 public void SynchronizeContextReference_DifferentInstanceDifferentType_AreNotEqual()
96 SynchronizeContextReference sync1 = new SynchronizeContextReference(typeof(string));
97 SynchronizeContextReference sync2 = new SynchronizeContextReference(typeof(float));
99 Assert.AreNotEqual(sync1, sync2);
100 Assert.AreNotEqual(sync1.GetHashCode(), sync2.GetHashCode());
103 [Test]
104 public void AddControl_DifferentThread_ThrowsException()
106 DummyForm form = new DummyForm();
107 ExecuteInThread(delegate { form.AddControl(new Button()); });
108 Assert.IsNotNull(uncaughtException, "Expected an exception");
110 uncaughtException = null;
112 ClassUsingFormInContext classInCtx = new ClassUsingFormInContext();
113 ExecuteInThread(delegate { classInCtx.DoWork(form); });
114 Assert.IsNotNull(uncaughtException, "Expected an exception");
117 [Test]
118 public void AddControl_DifferentThreadUsingClass_WorksFine()
120 DummyForm form = container.Resolve<DummyForm>();
121 ExecuteInThread(delegate { form.AddControl(new Button()); });
122 Assert.IsNull(uncaughtException, "Expected no exception");
125 [Test]
126 public void AddControl_DifferentThreadUsingService_WorksFine()
128 IDummyForm form = container.Resolve<IDummyForm>();
129 ExecuteInThread(delegate { form.AddControl(new Button()); });
130 Assert.IsNull(uncaughtException, "Expected no exception");
133 [Test]
134 public void AddControl_DifferentThreadInContext_WorksFine()
136 DummyForm form = new DummyForm();
137 ClassUsingFormInContext client = container.Resolve<ClassUsingFormInContext>();
138 ExecuteInThread(delegate { client.DoWork(form); });
139 Assert.IsNull(uncaughtException, "Expected no exception");
142 [Test]
143 public void AddControl_DifferentThreadInContextUsingConfiguration_WorksFine()
145 DummyForm form = new DummyForm();
146 ClassUsingForm client = container.Resolve<ClassUsingForm>();
147 ExecuteInThread(delegate { client.DoWork(form); });
148 Assert.IsNull(uncaughtException, "Expected no exception");
151 [Test]
152 public void DoWorkGeneric_DifferentThreadInContext_WorksFine()
154 DummyForm form = new DummyForm();
155 IClassUsingContext<DummyForm> client = container.Resolve<IClassUsingContext<DummyForm>>();
156 ExecuteInThread(delegate { client.DoWork(form); });
157 Assert.IsNull(uncaughtException, "Expected no exception");
160 [Test, ExpectedException(typeof(FacilityException))]
161 public void AddContextComponent_WithoutVirtualMethod_ThrowsFacilityException()
163 container.AddComponent("class.in.context.bad", typeof(ClassInContextWithoutVirtualMethod));
166 [Test, ExpectedException(typeof(HandlerException))]
167 public void ResolveContextComponent_WithMissingDependency_ThrowsHandlerException()
169 container.AddComponent("class.in.context.bad", typeof(ClassInContextWithMissingDependency));
170 container.Resolve("class.in.context.bad");
173 [Test]
174 public void RegisterFacility_WithControlProxyHook_WorksFine()
176 WindsorContainer container2 = new WindsorContainer();
178 MutableConfiguration facNode = new MutableConfiguration("facility");
179 facNode.Attributes["id"] = "sync.facility";
180 facNode.Attributes[Constants.ControlProxyHookAttrib] = typeof(DummyProxyHook).AssemblyQualifiedName;
181 container2.Kernel.ConfigurationStore.AddFacilityConfiguration("sync.facility", facNode);
182 container2.AddFacility("sync.facility", new SynchronizeFacility());
184 container2.AddComponent("dummy.form.class", typeof(DummyForm));
185 ComponentModel model = container2.Kernel.GetHandler("dummy.form.class").ComponentModel;
186 ProxyOptions options = ProxyUtil.ObtainProxyOptions(model, false);
187 Assert.IsNotNull(options, "Proxy options should not be null");
188 Assert.IsTrue(options.Hook is DummyProxyHook, "Proxy hook should be a DummyProxyHook");
191 #if DOTNET2
192 [Test, ExpectedException(typeof(ConfigurationErrorsException))]
193 #else
194 [Test, ExpectedException(typeof(ConfigurationException))]
195 #endif
196 public void RegisterFacility_WithBadControlProxyHook_ThrowsConfigurationException()
198 WindsorContainer container2 = new WindsorContainer();
200 MutableConfiguration facNode = new MutableConfiguration("facility");
201 facNode.Attributes["id"] = "sync.facility";
202 facNode.Attributes[Constants.ControlProxyHookAttrib] = typeof(string).AssemblyQualifiedName;
203 container2.Kernel.ConfigurationStore.AddFacilityConfiguration("sync.facility", facNode);
204 container2.AddFacility("sync.facility", new SynchronizeFacility());
207 private void ExecuteInThread(ThreadStart run)
209 Thread thread = new Thread((ThreadStart) delegate
213 run();
215 catch(Exception e)
217 uncaughtException = e;
220 Application.DoEvents();
221 Application.Exit();
224 Form form = new Form();
225 IntPtr formHandle = form.Handle;
226 form.BeginInvoke((MethodInvoker) delegate { thread.Start(); });
228 Application.Run();