forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestRuleSetupAndRestoreClassEnv.cs
More file actions
411 lines (374 loc) · 20.4 KB
/
Copy pathTestRuleSetupAndRestoreClassEnv.cs
File metadata and controls
411 lines (374 loc) · 20.4 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
using Lucene.Net.Codecs;
using Lucene.Net.Codecs.Asserting;
using Lucene.Net.Codecs.CheapBastard;
using Lucene.Net.Codecs.Compressing;
using Lucene.Net.Codecs.Lucene3x;
using Lucene.Net.Codecs.Lucene40;
using Lucene.Net.Codecs.Lucene41;
using Lucene.Net.Codecs.Lucene42;
using Lucene.Net.Codecs.Lucene45;
using Lucene.Net.Codecs.Lucene46;
using Lucene.Net.Codecs.MockRandom;
using Lucene.Net.Codecs.SimpleText;
using Lucene.Net.Diagnostics;
using Lucene.Net.Index;
using Lucene.Net.Search;
using Lucene.Net.Search.Similarities;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Threading;
using JCG = J2N.Collections.Generic;
using RandomizedTesting.Generators;
using AssumptionViolatedException = NUnit.Framework.InconclusiveException;
namespace Lucene.Net.Util
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
/// <summary>
/// Setup and restore suite-level environment (fine grained junk that
/// doesn't fit anywhere else).
/// </summary>
// LUCENENET specific: This class was refactored to be called directly from LuceneTestCase, since
// we didn't port over the entire test suite from Java.
internal sealed class TestRuleSetupAndRestoreClassEnv : AbstractBeforeAfterRule
{
///// <summary>
///// Restore these system property values.
///// </summary>
//private JCG.Dictionary<string, string> restoreProperties = new JCG.Dictionary<string, string>(); // LUCENENET: Never read
private Codec savedCodec;
private CultureInfo savedLocale;
private InfoStream savedInfoStream;
private TimeZoneInfo savedTimeZone;
internal CultureInfo locale;
internal TimeZoneInfo timeZone;
internal Similarity similarity;
internal Codec codec;
/// <seealso cref="LuceneTestCase.SuppressCodecsAttribute"/>
internal ISet<string> avoidCodecs;
internal class ThreadNameFixingPrintStreamInfoStream : TextWriterInfoStream
{
public ThreadNameFixingPrintStreamInfoStream(TextWriter @out)
: base(@out)
{
}
public override void Message(string component, string message)
{
if ("TP".Equals(component, StringComparison.Ordinal))
{
return; // ignore test points!
}
string name;
if (Thread.CurrentThread.Name != null && Thread.CurrentThread.Name.StartsWith("TEST-", StringComparison.Ordinal))
{
// The name of the main thread is way too
// long when looking at IW verbose output...
name = "main";
}
else
{
name = Thread.CurrentThread.Name;
}
m_stream.WriteLine(component + " " + m_messageID + " [" + DateTime.Now + "; " + name + "]: " + message);
}
}
public override void Before()
{
// LUCENENET specific - SOLR setup code removed
// if verbose: print some debugging stuff about which codecs are loaded.
if (LuceneTestCase.Verbose)
{
// LUCENENET: Only list the services if the underlying ICodecFactory
// implements IServiceListable
if (Codec.GetCodecFactory() is IServiceListable)
{
ICollection<string> codecs = Codec.AvailableCodecs;
foreach (string codec in codecs)
{
Console.WriteLine("Loaded codec: '" + codec + "': " + Codec.ForName(codec).GetType().Name);
}
}
// LUCENENET: Only list the services if the underlying IPostingsFormatFactory
// implements IServiceListable
if (PostingsFormat.GetPostingsFormatFactory() is IServiceListable)
{
ICollection<string> postingsFormats = PostingsFormat.AvailablePostingsFormats;
foreach (string postingsFormat in postingsFormats)
{
Console.WriteLine("Loaded postingsFormat: '" + postingsFormat + "': " + PostingsFormat.ForName(postingsFormat).GetType().Name);
}
}
}
savedInfoStream = InfoStream.Default;
Random random = LuceneTestCase.Random;
bool v = random.NextBoolean();
if (LuceneTestCase.UseInfoStream)
{
InfoStream.Default = new ThreadNameFixingPrintStreamInfoStream(Console.Out);
}
else if (v)
{
InfoStream.Default = new NullInfoStream();
}
Type targetClass = LuceneTestCase.TestType;
avoidCodecs = new JCG.HashSet<string>();
var suppressCodecsAttribute = targetClass.GetCustomAttribute<LuceneTestCase.SuppressCodecsAttribute>();
if (suppressCodecsAttribute != null)
{
avoidCodecs.UnionWith(suppressCodecsAttribute.Value);
}
// set back to default
LuceneTestCase.OldFormatImpersonationIsActive = false;
savedCodec = Codec.Default;
int randomVal = random.Next(10);
if ("Lucene3x".Equals(LuceneTestCase.TestCodec, StringComparison.Ordinal) || ("random".Equals(LuceneTestCase.TestCodec, StringComparison.Ordinal) &&
"random".Equals(LuceneTestCase.TestPostingsFormat, StringComparison.Ordinal) &&
"random".Equals(LuceneTestCase.TestDocValuesFormat, StringComparison.Ordinal) &&
randomVal == 3 &&
!ShouldAvoidCodec("Lucene3x"))) // preflex-only setup
{
codec = Codec.ForName("Lucene3x");
if (Debugging.AssertsEnabled) Debugging.Assert((codec is PreFlexRWCodec), "fix your ICodecFactory to scan Lucene.Net.Tests before Lucene.Net.TestFramework");
LuceneTestCase.OldFormatImpersonationIsActive = true;
}
else if ("Lucene40".Equals(LuceneTestCase.TestCodec, StringComparison.Ordinal) || ("random".Equals(LuceneTestCase.TestCodec, StringComparison.Ordinal) &&
"random".Equals(LuceneTestCase.TestPostingsFormat, StringComparison.Ordinal) &&
randomVal == 0 &&
!ShouldAvoidCodec("Lucene40"))) // 4.0 setup
{
codec = Codec.ForName("Lucene40");
LuceneTestCase.OldFormatImpersonationIsActive = true;
if (Debugging.AssertsEnabled) Debugging.Assert((codec is Lucene40RWCodec), "fix your ICodecFactory to scan Lucene.Net.Tests before Lucene.Net.TestFramework");
if (Debugging.AssertsEnabled) Debugging.Assert((PostingsFormat.ForName("Lucene40") is Lucene40RWPostingsFormat), "fix your IPostingsFormatFactory to scan Lucene.Net.Tests before Lucene.Net.TestFramework");
}
else if ("Lucene41".Equals(LuceneTestCase.TestCodec, StringComparison.Ordinal) || ("random".Equals(LuceneTestCase.TestCodec, StringComparison.Ordinal) &&
"random".Equals(LuceneTestCase.TestPostingsFormat, StringComparison.Ordinal) &&
"random".Equals(LuceneTestCase.TestDocValuesFormat, StringComparison.Ordinal) &&
randomVal == 1 &&
!ShouldAvoidCodec("Lucene41")))
{
codec = Codec.ForName("Lucene41");
LuceneTestCase.OldFormatImpersonationIsActive = true;
if (Debugging.AssertsEnabled) Debugging.Assert((codec is Lucene41RWCodec), "fix your ICodecFactory to scan Lucene.Net.Tests before Lucene.Net.TestFramework");
}
else if ("Lucene42".Equals(LuceneTestCase.TestCodec, StringComparison.Ordinal) || ("random".Equals(LuceneTestCase.TestCodec, StringComparison.Ordinal) &&
"random".Equals(LuceneTestCase.TestPostingsFormat, StringComparison.Ordinal) &&
"random".Equals(LuceneTestCase.TestDocValuesFormat, StringComparison.Ordinal) &&
randomVal == 2 &&
!ShouldAvoidCodec("Lucene42")))
{
codec = Codec.ForName("Lucene42");
LuceneTestCase.OldFormatImpersonationIsActive = true;
if (Debugging.AssertsEnabled) Debugging.Assert((codec is Lucene42RWCodec), "fix your ICodecFactory to scan Lucene.Net.Tests before Lucene.Net.TestFramework");
}
else if ("Lucene45".Equals(LuceneTestCase.TestCodec, StringComparison.Ordinal) || ("random".Equals(LuceneTestCase.TestCodec, StringComparison.Ordinal) &&
"random".Equals(LuceneTestCase.TestPostingsFormat, StringComparison.Ordinal) &&
"random".Equals(LuceneTestCase.TestDocValuesFormat, StringComparison.Ordinal) &&
randomVal == 5 &&
!ShouldAvoidCodec("Lucene45")))
{
codec = Codec.ForName("Lucene45");
LuceneTestCase.OldFormatImpersonationIsActive = true;
if (Debugging.AssertsEnabled) Debugging.Assert((codec is Lucene45RWCodec), "fix your ICodecFactory to scan Lucene.Net.Tests before Lucene.Net.TestFramework");
}
else if (("random".Equals(LuceneTestCase.TestPostingsFormat, StringComparison.Ordinal) == false)
|| ("random".Equals(LuceneTestCase.TestDocValuesFormat, StringComparison.Ordinal) == false))
{
// the user wired postings or DV: this is messy
// refactor into RandomCodec....
PostingsFormat format;
if ("random".Equals(LuceneTestCase.TestPostingsFormat, StringComparison.Ordinal))
{
format = PostingsFormat.ForName("Lucene41");
}
else if ("MockRandom".Equals(LuceneTestCase.TestPostingsFormat, StringComparison.Ordinal))
{
format = new MockRandomPostingsFormat(new J2N.Randomizer(random.NextInt64()));
}
else
{
format = PostingsFormat.ForName(LuceneTestCase.TestPostingsFormat);
}
DocValuesFormat dvFormat;
if ("random".Equals(LuceneTestCase.TestDocValuesFormat, StringComparison.Ordinal))
{
dvFormat = DocValuesFormat.ForName("Lucene45");
}
else
{
dvFormat = DocValuesFormat.ForName(LuceneTestCase.TestDocValuesFormat);
}
codec = new Lucene46CodecAnonymousClass(format, dvFormat);
}
else if ("SimpleText".Equals(LuceneTestCase.TestCodec, StringComparison.Ordinal)
|| ("random".Equals(LuceneTestCase.TestCodec, StringComparison.Ordinal) && randomVal == 9 && LuceneTestCase.Rarely(random) && !ShouldAvoidCodec("SimpleText")))
{
codec = new SimpleTextCodec();
}
else if ("CheapBastard".Equals(LuceneTestCase.TestCodec, StringComparison.Ordinal)
|| ("random".Equals(LuceneTestCase.TestCodec, StringComparison.Ordinal) && randomVal == 8 && !ShouldAvoidCodec("CheapBastard") && !ShouldAvoidCodec("Lucene41")))
{
// we also avoid this codec if Lucene41 is avoided, since that's the postings format it uses.
codec = new CheapBastardCodec();
}
else if ("Asserting".Equals(LuceneTestCase.TestCodec, StringComparison.Ordinal)
|| ("random".Equals(LuceneTestCase.TestCodec, StringComparison.Ordinal) && randomVal == 6 && !ShouldAvoidCodec("Asserting")))
{
codec = new AssertingCodec();
}
else if ("Compressing".Equals(LuceneTestCase.TestCodec, StringComparison.Ordinal)
|| ("random".Equals(LuceneTestCase.TestCodec, StringComparison.Ordinal) && randomVal == 5 && !ShouldAvoidCodec("Compressing")))
{
codec = CompressingCodec.RandomInstance(random);
}
else if (!"random".Equals(LuceneTestCase.TestCodec, StringComparison.Ordinal))
{
codec = Codec.ForName(LuceneTestCase.TestCodec);
}
else if ("random".Equals(LuceneTestCase.TestPostingsFormat, StringComparison.Ordinal))
{
codec = new RandomCodec(random, avoidCodecs);
}
else
{
if (Debugging.AssertsEnabled) Debugging.Assert(false);
}
Codec.Default = codec;
// Initialize locale/ timezone.
string testTimeZone = SystemProperties.GetProperty("tests:timezone", "random"); // LUCENENET specific - reformatted with :
// LUCENENET: We need to ensure our random generator stays consistent here so we can repeat the session exactly,
// so always call this whether the return value is useful or not.
CultureInfo randomLocale = LuceneTestCase.RandomCulture(random);
// LUCENENET: Allow NUnit properties to set the culture and respect that culture over our random one.
// Note that SetCultureAttribute may also be on the test method, but we don't need to deal with that here.
if (targetClass.Assembly.HasAttribute<NUnit.Framework.SetCultureAttribute>(inherit: true)
|| targetClass.HasAttribute<NUnit.Framework.SetCultureAttribute>(inherit: true))
{
locale = CultureInfo.CurrentCulture;
}
else
{
// LUCENENET: Accept either tests:culture or tests:locale (culture preferred).
string testLocale = SystemProperties.GetProperty("tests:culture", SystemProperties.GetProperty("tests:locale", "random")); // LUCENENET specific - reformatted with :
// Always pick a random one for consistency (whether tests.locale was specified or not).
savedLocale = CultureInfo.CurrentCulture;
locale = testLocale.Equals("random", StringComparison.Ordinal) ? randomLocale : LuceneTestCase.CultureForName(testLocale);
#if FEATURE_CULTUREINFO_CURRENTCULTURE_SETTER
CultureInfo.CurrentCulture = locale;
#else
Thread.CurrentThread.CurrentCulture = locale;
#endif
}
// TimeZone.getDefault will set user.timezone to the default timezone of the user's locale.
// So store the original property value and restore it at end.
// LUCENENET specific - commented
//restoreProperties["user:timezone"] = SystemProperties.GetProperty("user:timezone");
savedTimeZone = TimeZoneInfo.Local;
TimeZoneInfo randomTimeZone = LuceneTestCase.RandomTimeZone(random);
timeZone = testTimeZone.Equals("random", StringComparison.Ordinal) ? randomTimeZone : TimeZoneInfo.FindSystemTimeZoneById(testTimeZone);
//TimeZone.Default = TimeZone; // LUCENENET NOTE: There doesn't seem to be an equivalent to this, but I don't think we need it.
similarity = random.NextBoolean() ? (Similarity)new DefaultSimilarity() : new RandomSimilarityProvider(random);
// Check codec restrictions once at class level.
try
{
CheckCodecRestrictions(codec);
}
catch (Exception e)
{
Console.Error.WriteLine("NOTE: " + e.Message + " Suppressed codecs: " + avoidCodecs);
throw; // LUCENENET: CA2200: Rethrow to preserve stack details (https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2200-rethrow-to-preserve-stack-details)
}
}
private sealed class Lucene46CodecAnonymousClass : Lucene46Codec
{
private readonly PostingsFormat format;
private readonly DocValuesFormat dvFormat;
public Lucene46CodecAnonymousClass(PostingsFormat format, DocValuesFormat dvFormat)
{
this.format = format;
this.dvFormat = dvFormat;
}
public override PostingsFormat GetPostingsFormatForField(string field)
{
return format;
}
public override DocValuesFormat GetDocValuesFormatForField(string field)
{
return dvFormat;
}
public override string ToString()
{
return base.ToString() + ": " + format.ToString() + ", " + dvFormat.ToString();
}
}
/// <summary>
/// Check codec restrictions.
/// </summary>
/// <exception cref="AssumptionViolatedException"> if the class does not work with a given codec. </exception>
private void CheckCodecRestrictions(Codec codec)
{
LuceneTestCase.AssumeFalse("Class not allowed to use codec: " + codec.Name + ".", ShouldAvoidCodec(codec.Name));
if (codec is RandomCodec randomCodec && avoidCodecs.Count > 0)
{
foreach (string name in randomCodec.FormatNames)
{
LuceneTestCase.AssumeFalse("Class not allowed to use postings format: " + name + ".", ShouldAvoidCodec(name));
}
}
PostingsFormat pf = codec.PostingsFormat;
LuceneTestCase.AssumeFalse("Class not allowed to use postings format: " + pf.Name + ".", ShouldAvoidCodec(pf.Name));
LuceneTestCase.AssumeFalse("Class not allowed to use postings format: " + LuceneTestCase.TestPostingsFormat + ".", ShouldAvoidCodec(LuceneTestCase.TestPostingsFormat));
}
/// <summary>
/// After suite cleanup (always invoked).
/// </summary>
public override void After()
{
// LUCENENT specific - Not used in .NET
//foreach (KeyValuePair<string, string> e in restoreProperties)
//{
// SystemProperties.SetProperty(e.Key, e.Value);
//}
//restoreProperties.Clear();
Codec.Default = savedCodec;
InfoStream.Default = savedInfoStream;
if (savedLocale != null)
{
locale = savedLocale;
#if FEATURE_CULTUREINFO_CURRENTCULTURE_SETTER
CultureInfo.CurrentCulture = savedLocale;
#else
Thread.CurrentThread.CurrentCulture = savedLocale;
#endif
}
if (savedTimeZone != null)
{
timeZone = savedTimeZone;
}
}
/// <summary>
/// Should a given codec be avoided for the currently executing suite?
/// </summary>
private bool ShouldAvoidCodec(string codec)
{
return avoidCodecs.Count > 0 && avoidCodecs.Contains(codec);
}
}
}