|
| 1 | +namespace NHibernate.Test |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Threading.Tasks; |
| 5 | + using DotNet.Testcontainers.Containers; |
| 6 | + using NUnit.Framework; |
| 7 | + using Testcontainers.Db2; |
| 8 | + using Testcontainers.FirebirdSql; |
| 9 | + using Testcontainers.MariaDb; |
| 10 | + using Testcontainers.MsSql; |
| 11 | + using Testcontainers.MySql; |
| 12 | + using Testcontainers.Oracle; |
| 13 | + using Testcontainers.PostgreSql; |
| 14 | + |
| 15 | + [SetUpFixture] |
| 16 | + public class TestContainerSetup |
| 17 | + { |
| 18 | + private static IDatabaseContainer _container; |
| 19 | + private static readonly object _lock = new object(); |
| 20 | + |
| 21 | + internal static string GetConnectionString(string connectionString) |
| 22 | + { |
| 23 | + var parts = connectionString.Split(['='], System.StringSplitOptions.RemoveEmptyEntries); |
| 24 | + if (parts.Length != 2 || parts[0] != "testcontainers") |
| 25 | + { |
| 26 | + throw new System.ArgumentException("Invalid testcontainers connection string format. Expected format: testcontainers=DbType"); |
| 27 | + } |
| 28 | + // For now, only one container is supported. In the future, we can extend this to support multiple containers. |
| 29 | + if (_container == null) |
| 30 | + { |
| 31 | + lock (_lock) |
| 32 | + { |
| 33 | + if (_container == null) |
| 34 | + { |
| 35 | + var container = GetContainer(parts[1]); |
| 36 | + Task.Run(() => container.StartAsync()).GetAwaiter().GetResult(); |
| 37 | + _container = container; |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + return _container.GetConnectionString(); |
| 42 | + } |
| 43 | + |
| 44 | + private static IDatabaseContainer GetContainer(string dbType) |
| 45 | + { |
| 46 | + switch (dbType.ToLower()) |
| 47 | + { |
| 48 | + case "db2": |
| 49 | + return new Db2Builder().Build(); |
| 50 | + case "firebirdsql": |
| 51 | + return new FirebirdSqlBuilder().Build(); |
| 52 | + case "mariadb": |
| 53 | + return new MariaDbBuilder().Build(); |
| 54 | + case "mssql": |
| 55 | + return new MsSqlBuilder().Build(); |
| 56 | + case "mysql": |
| 57 | + return new MySqlBuilder().Build(); |
| 58 | + case "oracle": |
| 59 | + return new OracleBuilder().Build(); |
| 60 | + case "postgresql": |
| 61 | + return new PostgreSqlBuilder().Build(); |
| 62 | + default: |
| 63 | + throw new NotSupportedException("Database type not supported: " + dbType); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + [OneTimeTearDown] |
| 68 | + public async Task TearDown() |
| 69 | + { |
| 70 | + if (_container != null) |
| 71 | + { |
| 72 | + await _container.DisposeAsync(); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments