Path: redb-route/tests/redb.Route.Tests.Xml/XmlBeanTypedArgumentsTests.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.Xml/XmlBeanTypedArgumentsTests.cs ---- using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; using System.Xml.Linq; using AwesomeAssertions; using redb.Route.Core; using redb.Route.Xml; using redb.Route.Xml.Packaging; namespace redb.Route.Tests.Xml; /// Two constructors the same two strings fit — the shape X509Certificate2 has. public class ProbeCredential { public ProbeCredential(string file, string password) => Chosen = $"string:{file}/{password}"; public ProbeCredential(string file, object options) => Chosen = $"object:{file}/{options}"; public string Chosen { get; } /// Two overloads of one arity: the value alone does not say which. public static ProbeCredential From(string file) => new(file, "via-string"); public static ProbeCredential From(int slot) => new($"slot-{slot}", "via-int"); } /// One constructor taking a decimal: text without type= is a string and fits no parameter. public class ProbePrice(decimal vatRate) { public decimal VatRate { get; } = vatRate; } /// /// <constructorArg type="…"> — Spring's constructor-arg type: when several /// constructors (or factory-method overloads) accept the same values, the argument types name the /// one to call, and each value is converted to its parameter's type. Without it the choice is /// the engine's (one fitting constructor, one overload of that arity), and an ambiguous one is an /// error — never a guess. X509Certificate2 from a PFX is the case that asked for it: its /// (string, string) constructor is one of several two-argument ones. /// public sealed class XmlBeanTypedArgumentsTests : IAsyncLifetime { private readonly RouteContext _context = new(); private readonly string _dir = Path.Combine(Path.GetTempPath(), "redb-xml-typed-" + Guid.NewGuid().ToString("N")); public XmlBeanTypedArgumentsTests() => Directory.CreateDirectory(_dir); public Task InitializeAsync() => Task.CompletedTask; public async Task DisposeAsync() { await _context.DisposeAsync(); Directory.Delete(_dir, recursive: true); GC.SuppressFinalize(this); } private static string T() => $"{typeof(TType).FullName}, {typeof(TType).Assembly.GetName().Name}"; private void Load(string beans) => _context.AddXmlContextFromContent($""" {beans} """); [Fact] public void Certificate_FromPfx_ThroughTheStringStringConstructor() { using var rsa = RSA.Create(2048); var request = new CertificateRequest("CN=redb-typed-args", rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); using var issued = request.CreateSelfSigned(DateTimeOffset.UtcNow.AddMinutes(-1), DateTimeOffset.UtcNow.AddDays(1)); var pfx = Path.Combine(_dir, "node.pfx"); File.WriteAllBytes(pfx, issued.Export(X509ContentType.Pkcs12, "secret")); _context.SetProperty("node.pfx", pfx); Load($""" """); using var loaded = _context.GetFromRegistry("node-cert")!; loaded.Subject.Should().Be("CN=redb-typed-args"); loaded.HasPrivateKey.Should().BeTrue(); } [Fact] public void FactoryMethod_WithOptionalTrailingParameters_IsCalledLikeCSharpCallsIt() { // The documented example: LoadPkcs12FromFile(path, password, flags = DefaultKeySet, limits = null). // Matching by the exact parameter count never found it; the optional rest takes its defaults, // and the ReadOnlySpan overload is no candidate — no markup value can be one. using var rsa = RSA.Create(2048); var request = new CertificateRequest("CN=redb-loader", rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); using var issued = request.CreateSelfSigned(DateTimeOffset.UtcNow.AddMinutes(-1), DateTimeOffset.UtcNow.AddDays(1)); var pfx = Path.Combine(_dir, "hub.pfx"); File.WriteAllBytes(pfx, issued.Export(X509ContentType.Pkcs12, "secret")); Load($""" """); using var loaded = _context.GetFromRegistry("hub-cert")!; loaded.Subject.Should().Be("CN=redb-loader"); } [Fact] public void TypedArguments_ChooseTheConstructor_AmongOnesTheValuesAllFit() { Load($""" """); _context.GetFromRegistry("as-string")!.Chosen.Should().Be("string:a.pfx/pw"); _context.GetFromRegistry("as-object")!.Chosen.Should().Be("object:a.pfx/opts"); } [Fact] public void UntypedArguments_ThatFitSeveralConstructors_AreAnError_NotAGuess() { var act = () => Load($""" """); act.Should().Throw(); } [Fact] public void FactoryMethod_Overloads_OfOneArity_AreRefused_UntilTypesNameOne() { var ambiguous = () => Load($""" """); ambiguous.Should().Throw() .WithMessage("*has 2 public static methods 'From' taking 1 argument(s)*name the parameter types with *"); Load($""" """); _context.GetFromRegistry("from")!.Chosen.Should().Be("string:slot-7/via-int", "the value is converted to the named parameter type"); } [Fact] public void ASignatureNoConstructorHas_AndTypesOnSomeArgumentsOnly_AreErrors() { var missing = () => Load($""" """); missing.Should().Throw().WithMessage("*has no public constructor (System.Int32, System.Int32)*"); var mixed = () => Load($""" """); mixed.Should().Throw().WithMessage("*name every parameter type, or none*"); } [Fact] public void Generator_PrintsTheTypes_AndTheRuntimeHalfUsesThem() { var code = XmlCodeGenerator.Generate(XDocument.Parse($""" """), "TypedGenerated", "Tests.Generated"); code.Should().Contain("argumentTypes: [\"System.String\", \"System.Object\"]"); var built = (ProbeCredential)XmlBeans.Create(_context, T(), ["a", "b"], [], argumentTypes: ["System.String", "System.Object"]); built.Chosen.Should().Be("object:a/b"); } [Fact] public void Gate_WithTheBuiltAssemblies_HoldsTheNamedSignature() { var project = RouteProjectScaffold.Create(_dir, "Typed"); File.WriteAllText(Path.Combine(project, "routes", "main.route.xml"), $""" """); var errors = RoutePackage.Check(project, "p", "1.0.0", Type.GetType).Errors.Select(e => e.Message).ToList(); errors.Should().Contain(m => m.Contains("bean 'none'") && m.Contains("has no public constructor (System.Int32, System.Int32)")); errors.Should().Contain(m => m.Contains("bean 'from'") && m.Contains("has 2 public static methods 'From'")); errors.Should().NotContain(m => m.Contains("bean 'ok'")); } [Fact] public void Untyped_text_argument_for_a_decimal_constructor_fails_the_load() { var act = () => Load($""""""); act.Should().Throw("ActivatorUtilities passes the text as a string, which no parameter takes"); } [Fact] public void Gate_WithTheBuiltAssemblies_HoldsUntypedArgumentsAgainstTheConstructors() { var project = RouteProjectScaffold.Create(_dir, "Untyped"); File.WriteAllText(Path.Combine(project, "routes", "main.route.xml"), $""" """); var errors = RoutePackage.Check(project, "p", "1.0.0", Type.GetType).Errors.Select(e => e.Message).ToList(); errors.Should().ContainSingle(m => m.Contains("bean 'untyped'")) .Which.Should().Contain("no public constructor").And.Contain("(System.String)") .And.Contain(" m.Contains("bean 'typed'") || m.Contains("bean 'strings'")); } }