1
0
mirror of synced 2024-11-05 02:59:16 +00:00

Finished the Connectionstring Parser with extensive unit tests

This commit is contained in:
Menno van Lavieren 2017-06-26 11:43:55 +02:00
parent 451bedd4e0
commit 5f9014125d
6 changed files with 245 additions and 14 deletions

View File

@ -0,0 +1,200 @@
using System;
using Tapeti;
using Tapeti.Helpers;
using Xunit;
namespace Tapet.Tests
{
// ReSharper disable InconsistentNaming
public class ConnectionStringParserTest
{
[Fact]
public void EmptyConnectionString()
{
AssertConnectionString("", new TapetiConnectionParams());
}
[Fact]
public void SimpleHostname()
{
AssertConnectionString("HostName=rabbit.com", new TapetiConnectionParams
{
HostName = "rabbit.com"
});
}
[Fact]
public void LongHostname()
{
AssertConnectionString("HostName=rabbit.1234.internal.prod.Uname-IT.com", new TapetiConnectionParams
{
HostName = "rabbit.1234.internal.prod.Uname-IT.com"
});
}
[Fact]
public void HostnameAndPort()
{
AssertConnectionString("HostName=rabbit.com;Port=60001", new TapetiConnectionParams
{
HostName = "rabbit.com",
Port = 60001
});
}
[Fact]
public void HostnameAndVirtualHost()
{
AssertConnectionString("HostName=rabbit.com;VirtualHost=/staging", new TapetiConnectionParams
{
HostName = "rabbit.com",
VirtualHost = "/staging"
});
}
[Fact]
public void HostnameAndPrefetchCount()
{
AssertConnectionString("HostName=rabbit.com;PrefetchCount=5", new TapetiConnectionParams
{
HostName = "rabbit.com",
PrefetchCount = 5
});
}
[Fact]
public void HostnameAndUsername()
{
AssertConnectionString("HostName=rabbit.com;Username=myApplication", new TapetiConnectionParams
{
HostName = "rabbit.com",
Username = "myApplication"
});
}
[Fact]
public void HostnameAndUsernameAndPassword()
{
AssertConnectionString("HostName=rabbit.com;Username=myApplication;Password=abcd1234", new TapetiConnectionParams
{
HostName = "rabbit.com",
Username = "myApplication",
Password = "abcd1234"
});
}
[Fact]
public void HostnameAndUsernameAndFunnyPassword()
{
AssertConnectionString("HostName=rabbit.com;Username=myApplication;Password=a#$%h^5Gs!`~EE357(-+)=_\\/&*|6$<>{}[]MyFaFoRiTePeTnAmE", new TapetiConnectionParams
{
HostName = "rabbit.com",
Username = "myApplication",
Password = "a#$%h^5Gs!`~EE357(-+)=_\\/&*|6$<>{}[]MyFaFoRiTePeTnAmE"
});
}
[Fact]
public void ExtraSemicolon()
{
AssertConnectionString("HostName=rabbit.com;Username=myApplication;Password=abcd1234;", new TapetiConnectionParams
{
HostName = "rabbit.com",
Username = "myApplication",
Password = "abcd1234"
});
}
[Fact]
public void ExtraMultipleSemicolon()
{
AssertConnectionString("HostName=rabbit.com;;;;;;;;;;;;;;Username=myApplication;Password=abcd1234;;;", new TapetiConnectionParams
{
HostName = "rabbit.com",
Username = "myApplication",
Password = "abcd1234"
});
}
[Fact]
public void OnlySemicolons()
{
AssertConnectionString(";;;", new TapetiConnectionParams
{
});
}
[Fact]
public void DoubleQuotes()
{
AssertConnectionString("HostName=rabbit.com;Username=\"myApplication\"", new TapetiConnectionParams
{
HostName = "rabbit.com",
Username = "myApplication",
});
}
[Fact]
public void DoubleQuotesInDoubleQuotes()
{
AssertConnectionString("HostName=rabbit.com;Username=\"\"\"myApplication\"\"\";Password=abcd1234", new TapetiConnectionParams
{
HostName = "rabbit.com",
Username = "\"myApplication\"",
Password = "abcd1234"
});
}
[Fact]
public void CharactersInDoubleQuotes()
{
AssertConnectionString("HostName=rabbit.com;Username=\"Application?culture=\"\"nl-NL\"\";nr=5;\";Password=abcd1234", new TapetiConnectionParams
{
HostName = "rabbit.com",
Username = "Application?culture=\"nl-NL\";nr=5;",
Password = "abcd1234"
});
}
[Fact]
public void DifferentOrder()
{
AssertConnectionString("Username=myApplication;PrefetchCount=10;Password=1234;Port=80;VirtualHost=/test;HostName=messagebus.universe.local", new TapetiConnectionParams
{
HostName = "messagebus.universe.local",
Port = 80,
VirtualHost= "/test",
Username = "myApplication",
Password = "1234",
PrefetchCount= 10
});
}
[Fact]
public void DifferentOrderAndQuotes()
{
AssertConnectionString("Port=80;Username=myApplication;PrefetchCount=\"10\";Password=\"ab\"\"cd\";VirtualHost=\"/test\";HostName=\"messagebus.universe.local\"", new TapetiConnectionParams
{
HostName = "messagebus.universe.local",
Port = 80,
VirtualHost = "/test",
Username = "myApplication",
Password = "ab\"cd",
PrefetchCount = 10
});
}
private void AssertConnectionString(string connectionstring, TapetiConnectionParams expected)
{
var parsed = ConnectionStringParser.Parse(connectionstring);
Assert.Equal(expected.HostName, parsed.HostName);
Assert.Equal(expected.Port, parsed.Port);
Assert.Equal(expected.VirtualHost, parsed.VirtualHost);
Assert.Equal(expected.Username, parsed.Username);
Assert.Equal(expected.Password, parsed.Password);
Assert.Equal(expected.PrefetchCount, parsed.PrefetchCount);
}
}
}

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\xunit.runner.visualstudio.2.2.0\build\net20\xunit.runner.visualstudio.props" Condition="Exists('..\packages\xunit.runner.visualstudio.2.2.0\build\net20\xunit.runner.visualstudio.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@ -12,6 +13,8 @@
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@ -57,6 +60,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ConnectionStringParser.cs" />
<Compile Include="TypeNameRoutingKeyStrategyTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
@ -69,7 +73,16 @@
<Name>Tapeti</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\xunit.runner.visualstudio.2.2.0\build\net20\xunit.runner.visualstudio.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\xunit.runner.visualstudio.2.2.0\build\net20\xunit.runner.visualstudio.props'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">

View File

@ -6,4 +6,5 @@
<package id="xunit.core" version="2.1.0" targetFramework="net452" />
<package id="xunit.extensibility.core" version="2.1.0" targetFramework="net452" />
<package id="xunit.extensibility.execution" version="2.1.0" targetFramework="net452" />
<package id="xunit.runner.visualstudio" version="2.2.0" targetFramework="net461" developmentDependency="true" />
</packages>

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Tapeti.Helpers
{
public class ConnectionstringParser
public class ConnectionStringParser
{
readonly TapetiConnectionParams result = new TapetiConnectionParams();
@ -16,10 +16,10 @@ namespace Tapeti.Helpers
public static TapetiConnectionParams Parse(string connectionstring)
{
return new ConnectionstringParser(connectionstring).result;
return new ConnectionStringParser(connectionstring).result;
}
private ConnectionstringParser(string connectionstring)
private ConnectionStringParser(string connectionstring)
{
this.connectionstring = connectionstring;
@ -35,7 +35,6 @@ namespace Tapeti.Helpers
if (current == '=')
{
MoveNext();
var value = ParseValue();
SetValue(key, value);
}
@ -70,21 +69,29 @@ namespace Tapeti.Helpers
private string ParseValue()
{
var valueBuilder = new StringBuilder();
MoveNext();
if (current == '"')
{
while (current == '"') // support two double quotes to be an escaped quote
while (MoveNext())
{
while (MoveNext() && current != '"')
if (current == '"')
{
valueBuilder.Append(current);
// support two double quotes to be an escaped quote
if (!MoveNext() || current != '"')
break;
}
MoveNext(); // consume the trailing double quote
valueBuilder.Append(current);
}
}
else
{
while (current != ';')
{
valueBuilder.Append(current);
if (!MoveNext())
break;
}
}
// Always leave the ';' to be consumed by the caller
@ -113,12 +120,12 @@ namespace Tapeti.Helpers
private void SetValue(string key, string value)
{
switch (key.ToLowerInvariant()) {
case "HostName": result.HostName = value; break;
case "Port": result.Port = int.Parse(value); break;
case "VirtualHost": result.VirtualHost = value; break;
case "Username": result.Username = value; break;
case "Password": result.Password = value; break;
case "PrefetchCount": result.PrefetchCount = ushort.Parse(value); break;
case "hostname": result.HostName = value; break;
case "port": result.Port = int.Parse(value); break;
case "virtualhost": result.VirtualHost = value; break;
case "username": result.Username = value; break;
case "password": result.Password = value; break;
case "prefetchcount": result.PrefetchCount = ushort.Parse(value); break;
}
}
}

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\xunit.runner.visualstudio.2.2.0\build\net20\xunit.runner.visualstudio.props" Condition="Exists('..\packages\xunit.runner.visualstudio.2.2.0\build\net20\xunit.runner.visualstudio.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@ -13,6 +14,8 @@
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFrameworkProfile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
@ -86,6 +89,12 @@
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\xunit.runner.visualstudio.2.2.0\build\net20\xunit.runner.visualstudio.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\xunit.runner.visualstudio.2.2.0\build\net20\xunit.runner.visualstudio.props'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">

View File

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="SimpleInjector" version="3.3.2" targetFramework="net461" />
<package id="xunit.runner.visualstudio" version="2.2.0" targetFramework="net461" developmentDependency="true" />
</packages>