1 // Copyright 2004-2007 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.
14 namespace Castle
.MicroKernel
.Tests
.Facilities
.FactorySupport
17 using System
.Collections
;
18 using System
.Collections
.Specialized
;
20 using Castle
.Core
.Configuration
;
21 using Castle
.Facilities
.FactorySupport
;
22 using Castle
.MicroKernel
.Tests
.ClassComponents
;
23 using NUnit
.Framework
;
26 public class FactorySupportTestCase
29 [SetUp
]public void SetUp()
31 kernel
= new DefaultKernel();
35 public void NullModelConfigurationBug()
37 kernel
.AddFacility("factories", new FactorySupportFacility());
38 kernel
.AddComponentInstance("a", new CustomerImpl());
42 public void DependancyIgnored()
44 kernel
.AddFacility("factories", new FactorySupportFacility());
45 kernel
.AddComponent("a", typeof(Factory
));
47 AddComponent("stringdictComponent", typeof(StringDictionaryDependentComponent
), "CreateWithStringDictionary");
48 AddComponent("hashtableComponent", typeof(HashTableDependentComponent
), "CreateWithHashtable");
49 AddComponent("serviceComponent", typeof(ServiceDependentComponent
), "CreateWithService");
51 kernel
.Resolve("hashtableComponent", typeof(HashTableDependentComponent
));
52 kernel
.Resolve("serviceComponent", typeof(ServiceDependentComponent
));
53 kernel
.Resolve("stringdictComponent", typeof(StringDictionaryDependentComponent
));
56 [Test
, Ignore("Bug confirmed, but cant fix it without undesired side effects")]
57 public void KernelDoesNotTryToWireComponentsPropertiesWithFactoryConfiguration()
59 kernel
.AddFacility("factories", new FactorySupportFacility());
60 kernel
.AddComponent("a", typeof(Factory
));
62 ComponentModel model
= AddComponent("cool.service", typeof(MyCoolServiceWithProperties
), "CreateCoolService");
64 model
.Parameters
.Add("someProperty", "Abc");
66 MyCoolServiceWithProperties service
= (MyCoolServiceWithProperties
) kernel
["cool.service"];
68 Assert
.IsNotNull(service
);
69 Assert
.IsNull(service
.SomeProperty
);
72 private ComponentModel
AddComponent(string key
, Type type
, string factoryMethod
)
74 MutableConfiguration config
= new MutableConfiguration(key
);
75 config
.Attributes
["factoryId"] = "a";
76 config
.Attributes
["factoryCreate"] = factoryMethod
;
77 kernel
.ConfigurationStore
.AddComponentConfiguration(key
, config
);
78 kernel
.AddComponent(key
, type
);
79 return kernel
.GetHandler(key
).ComponentModel
;
84 public static HashTableDependentComponent
CreateWithHashtable()
86 return new HashTableDependentComponent(null);
89 public static StringDictionaryDependentComponent
CreateWithStringDictionary()
91 return new StringDictionaryDependentComponent(null);
94 public static ServiceDependentComponent
CreateWithService()
96 return new ServiceDependentComponent(null);
99 public static MyCoolServiceWithProperties
CreateCoolService(string someProperty
)
101 return new MyCoolServiceWithProperties();
105 public class MyCoolServiceWithProperties
107 private string someProperty
;
109 public string SomeProperty
111 get { return someProperty; }
112 set { someProperty = value; }
116 public class StringDictionaryDependentComponent
118 public StringDictionaryDependentComponent(StringDictionary d
)
123 public class ServiceDependentComponent
125 public ServiceDependentComponent(ICommon d
)
130 public class HashTableDependentComponent
132 public HashTableDependentComponent(Hashtable d
)