DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
ServiceContainerTests.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2014 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 using System;
16 using System.Linq;
17 
18 using NUnit.Framework;
19 
20 namespace Deveel.Data.Services {
21  [TestFixture]
22  public class ServiceContainerTests {
23  [Test]
25  var parent = new ServiceContainer();
26  parent.Register<TestService1>();
27 
28  var child = parent.OpenScope("child");
29 
30  var childService = child.Resolve<ITestService>();
31  var parentService = parent.Resolve<ITestService>();
32 
33  Assert.IsNotNull(childService);
34  Assert.IsInstanceOf<TestService1>(childService);
35 
36  Assert.IsNotNull(parentService);
37  Assert.IsInstanceOf<TestService1>(parentService);
38 
39  Assert.AreEqual(parentService, childService);
40  }
41 
42  [Test]
44  var instance = new TestService1();
45 
46  var parent = new ServiceContainer();
47  parent.RegisterInstance(instance);
48 
49  var child = parent.OpenScope("child");
50 
51  var childService = child.Resolve<ITestService>();
52 
53  Assert.IsNotNull(childService);
54  Assert.IsInstanceOf<TestService1>(childService);
55  Assert.AreEqual(instance, childService);
56  }
57 
58  [Test]
60  var instance = new TestService1();
61 
62  var parent = new ServiceContainer();
63  parent.RegisterInstance(instance);
64 
65  var child = parent.OpenScope("child");
66 
67  var services = child.ResolveAll<ITestService>();
68 
69  Assert.IsNotEmpty(services);
70  Assert.AreEqual(1, services.Count());
71  }
72 
73  [Test]
75  var parent = new ServiceContainer();
76  parent.Register<TestService1>();
77 
78  var child = parent.OpenScope("child");
79  child.Register<TestService2>();
80 
81  var service2 = child.Resolve<TestService2>();
82 
83  Assert.IsNotNull(service2);
84  Assert.IsNotNull(service2.Service1);
85  }
86 
87  #region ITestService
88 
89  interface ITestService {
90  }
91 
92  #endregion
93 
94  #region TestService1
95 
97 
98  }
99 
100  #endregion
101 
102  #region TestService2
103 
104  class TestService2 {
105  public TestService2(TestService1 service1) {
106  Service1 = service1;
107  }
108 
109  public TestService1 Service1 { get; private set; }
110  }
111 
112  #endregion
113  }
114 }