|
| 1 | +/* Copyright 2021-present MongoDB Inc. |
| 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. |
| 14 | +*/ |
| 15 | + |
| 16 | +using System; |
| 17 | +using System.IO; |
| 18 | +using FluentAssertions; |
| 19 | +using MongoDB.Driver.Core.NativeLibraryLoader; |
| 20 | +using MongoDB.Driver.TestHelpers; |
| 21 | +using MongoDB.Shared; |
| 22 | +using Xunit; |
| 23 | + |
| 24 | +namespace MongoDB.Driver.Core.Tests.Core.NativeLibraryLoader |
| 25 | +{ |
| 26 | + public class NativeLibraryLoaderTests |
| 27 | + { |
| 28 | + [Theory] |
| 29 | + [InlineData(null, "mongo-csharp-driver")] // the default assembly-based logic |
| 30 | + [InlineData("mongo-csharp-driver", "mongo-csharp-driver")] |
| 31 | + [InlineData("mongo csharp driver", "mongo csharp driver")] |
| 32 | + [InlineData("&mongo$csharp@driver%", "&mongo$csharp@driver%")] |
| 33 | + public void GetLibraryBasePath_should_get_correct_paths(string rootTestFolder, string expectedRootTestFolder) |
| 34 | + { |
| 35 | + string testAssemblyCodeBaseUri = null; // use assembly-based CodeBase for null |
| 36 | + if (rootTestFolder != null) |
| 37 | + { |
| 38 | + // mock assembly-based CodeBase path |
| 39 | + var assemblyPath = Path.Combine( |
| 40 | + RequirePlatform.GetCurrentOperatingSystem() == SupportedOperatingSystem.Windows ? "C:/" : @"\\data", |
| 41 | + rootTestFolder, |
| 42 | + GetCommonTestAssemblyFolderEnding(), |
| 43 | + "MongoDB.Driver.Core.dll"); |
| 44 | + testAssemblyCodeBaseUri = new Uri(assemblyPath).ToString(); |
| 45 | + } |
| 46 | + |
| 47 | + var subject = new TestRelativeLibraryLocator(testAssemblyCodeBaseUri); |
| 48 | + |
| 49 | + var result = subject.GetLibraryBasePath(); |
| 50 | + |
| 51 | + var expectedResult = Path.Combine(expectedRootTestFolder, GetCommonTestAssemblyFolderEnding()); |
| 52 | + result.Should().EndWith(expectedResult); |
| 53 | + } |
| 54 | + |
| 55 | + // private methods |
| 56 | + private string GetCommonTestAssemblyFolderEnding() => |
| 57 | + Path.Combine( |
| 58 | + "tests", |
| 59 | + "MongoDB.Driver.Core.Tests", |
| 60 | + "bin", |
| 61 | + GetConfigurationName(), |
| 62 | + GetTargetFrameworkMonikerName()); |
| 63 | + |
| 64 | + private string GetConfigurationName() => |
| 65 | +#if DEBUG |
| 66 | + "Debug"; |
| 67 | +#else |
| 68 | + "Release"; |
| 69 | +#endif |
| 70 | + |
| 71 | + private string GetTargetFrameworkMonikerName() => |
| 72 | +#if NETCOREAPP1_1 |
| 73 | + "netcoreapp1.1"; |
| 74 | +#elif NETCOREAPP2_1 |
| 75 | + "netcoreapp2.1"; |
| 76 | +#elif NETCOREAPP3_0 |
| 77 | + "netcoreapp3.0"; |
| 78 | +#elif NET452 |
| 79 | + "net452"; |
| 80 | +#endif |
| 81 | + |
| 82 | + // nested types |
| 83 | + private class TestRelativeLibraryLocator : RelativeLibraryLocatorBase |
| 84 | + { |
| 85 | + private readonly string _testAssemblyUri; |
| 86 | + |
| 87 | + public TestRelativeLibraryLocator(string testAssemblyUri) |
| 88 | + { |
| 89 | + _testAssemblyUri = testAssemblyUri; // can be null |
| 90 | + } |
| 91 | + |
| 92 | + public override string GetBaseAssemblyUri() => _testAssemblyUri ?? base.GetBaseAssemblyUri(); |
| 93 | + |
| 94 | + public override string GetLibraryRelativePath(OperatingSystemPlatform currentPlatform) => throw new NotImplementedException(); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments