forked from SolrNet/SolrNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutofacMulticoreFixture.cs
217 lines (186 loc) · 8.94 KB
/
AutofacMulticoreFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#region license
// Copyright (c) 2007-2010 Mauricio Scheffer
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion
using System.Configuration;
using Autofac;
using AutofacContrib.SolrNet.Config;
using MbUnit.Framework;
using SolrNet;
using SolrNet.Impl;
using System.Collections.Generic;
namespace AutofacContrib.SolrNet.Tests
{
[TestFixture]
public class AutofacMulticoreFixture
{
[Test]
public void ResolveSolrOperations()
{
// Arrange
var builder = new ContainerBuilder();
var cores = new SolrServers {
new SolrServerElement {
Id = "entity1",
DocumentType = typeof (Entity1).AssemblyQualifiedName,
Url = "http://localhost:8983/solr/coreEntity1",
},
};
builder.RegisterModule(new SolrNetModule(cores));
var container = builder.Build();
// Act
var m = container.Resolve<ISolrOperations<Entity1>>();
// Assert
Assert.IsTrue(m is SolrServer<Entity1>);
}
[Test]
public void ResolveSolrReadOnlyOperations()
{
// Arrange
var builder = new ContainerBuilder();
var cores = new SolrServers {
new SolrServerElement {
Id = "entity1",
DocumentType = typeof (Entity1).AssemblyQualifiedName,
Url = "http://localhost:8983/solr/coreEntity1",
},
};
builder.RegisterModule(new SolrNetModule(cores));
var container = builder.Build();
// Act
var solrReadOnlyOperations = container.Resolve<ISolrReadOnlyOperations<Entity1>>();
// Assert
Assert.IsTrue(solrReadOnlyOperations is SolrServer<Entity1>);
}
[Test]
public void ResolveSolrOperations_withMultiCore()
{
// Arrange
var builder = new ContainerBuilder();
var cores = new SolrServers {
new SolrServerElement {
Id = "entity1",
DocumentType = typeof (Entity1).AssemblyQualifiedName,
Url = "http://localhost:8983/solr/coreEntity1",
},
new SolrServerElement {
Id = "entity2",
DocumentType = typeof (Entity2).AssemblyQualifiedName,
Url = "http://localhost:8983/solr/coreEntity2",
},
};
builder.RegisterModule(new SolrNetModule(cores));
var container = builder.Build();
// Act
var solrOperations1 = container.Resolve<ISolrOperations<Entity1>>();
var solrOperations2 = container.Resolve<ISolrOperations<Entity2>>();
// Assert
Assert.IsTrue(solrOperations1 is SolrServer<Entity1>);
Assert.IsTrue(solrOperations2 is SolrServer<Entity2>);
}
[Test]
public void ResolveSolrOperations_viaNamedWithMultiCore()
{
// Arrange
var builder = new ContainerBuilder();
var cores = new SolrServers {
new SolrServerElement {
Id = "entity1",
DocumentType = typeof (Entity1).AssemblyQualifiedName,
Url = "http://localhost:8983/solr/coreEntity1",
},
new SolrServerElement {
Id = "entity2",
DocumentType = typeof (Entity2).AssemblyQualifiedName,
Url = "http://localhost:8983/solr/coreEntity2",
},
};
builder.RegisterModule(new SolrNetModule(cores));
var container = builder.Build();
// Act
var solrOperations1 = container.ResolveNamed<ISolrOperations<Entity1>>("entity1");
var solrOperations2 = container.ResolveNamed<ISolrOperations<Entity2>>("entity2");
// Assert
Assert.IsTrue(solrOperations1 is SolrServer<Entity1>);
Assert.IsTrue(solrOperations2 is SolrServer<Entity2>);
}
[Test]
public void ResolveSolrOperations_viaNamedWithMultiCoreForDictionary()
{
// Arrange
var builder = new ContainerBuilder();
var cores = new SolrServers {
new SolrServerElement {
Id = "dictionary1",
DocumentType = typeof (Dictionary<string, object>).AssemblyQualifiedName,
Url = "http://localhost:8983/solr/coreDictionaryEntity1",
},
new SolrServerElement {
Id = "dictionary2",
DocumentType = typeof (Dictionary<string, object>).AssemblyQualifiedName,
Url = "http://localhost:8983/solr/coreDictionaryEntity2",
},
};
builder.RegisterModule(new SolrNetModule(cores));
var container = builder.Build();
// Act
var solrOperations1 = container.ResolveNamed<ISolrOperations<Dictionary<string, object>>>("dictionary1");
var solrOperations2 = container.ResolveNamed<ISolrOperations<Dictionary<string, object>>>("dictionary2");
// Assert
Assert.IsTrue(solrOperations1 is SolrServer<Dictionary<string, object>>);
Assert.IsTrue(solrOperations2 is SolrServer<Dictionary<string, object>>);
}
[Test]
public void ResolveSolrReadOnlyOperations_viaNamedWithMultiCoreForDictionary()
{
// Arrange
var builder = new ContainerBuilder();
var cores = new SolrServers {
new SolrServerElement {
Id = "dictionary1",
DocumentType = typeof (Dictionary<string, object>).AssemblyQualifiedName,
Url = "http://localhost:8983/solr/coreDictionaryEntity1",
},
new SolrServerElement {
Id = "dictionary2",
DocumentType = typeof (Dictionary<string, object>).AssemblyQualifiedName,
Url = "http://localhost:8983/solr/coreDictionaryEntity2",
},
};
builder.RegisterModule(new SolrNetModule(cores));
var container = builder.Build();
// Act
var solrReadOnlyOperations1 = container.ResolveNamed<ISolrReadOnlyOperations<Dictionary<string, object>>>("dictionary1");
var solrReadOnlyOperations2 = container.ResolveNamed<ISolrReadOnlyOperations<Dictionary<string, object>>>("dictionary2");
// Assert
Assert.IsTrue(solrReadOnlyOperations1 is SolrServer<Dictionary<string, object>>);
Assert.IsTrue(solrReadOnlyOperations2 is SolrServer<Dictionary<string, object>>);
}
[Test]
public void ResolveSolrOperations_fromConfigSection()
{
// Arrange
var builder = new ContainerBuilder();
var solrConfig = (SolrConfigurationSection)ConfigurationManager.GetSection("solr");
builder.RegisterModule(new SolrNetModule(solrConfig.SolrServers));
var container = builder.Build();
// Act
var m = container.Resolve<ISolrOperations<Entity1>>();
// Assert
Assert.IsTrue(m is SolrServer<Entity1>);
}
}
public class Entity1 { }
public class Entity2 { }
}