Skip to content

Commit eae74c3

Browse files
author
Artem Abashev
committed
Basic unit tests.
1 parent 0397b0d commit eae74c3

18 files changed

+11498
-1
lines changed

DotAmf.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{3B
99
EndProject
1010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExampleService", "Examples\ExampleService\ExampleService.csproj", "{BF7F4EF2-8104-410E-8091-E977CFBB7465}"
1111
EndProject
12+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{BB6A307C-223B-406E-B75B-D478187B0DBB}"
13+
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotAmf", "Tests\DotAmf\DotAmf.csproj", "{CE809E33-59C8-4C66-81AE-2F99207CEC9A}"
15+
EndProject
1216
Global
1317
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1418
Debug|Any CPU = Debug|Any CPU
@@ -49,11 +53,22 @@ Global
4953
{BF7F4EF2-8104-410E-8091-E977CFBB7465}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
5054
{BF7F4EF2-8104-410E-8091-E977CFBB7465}.Release|Mixed Platforms.Build.0 = Release|Any CPU
5155
{BF7F4EF2-8104-410E-8091-E977CFBB7465}.Release|x86.ActiveCfg = Release|Any CPU
56+
{CE809E33-59C8-4C66-81AE-2F99207CEC9A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
57+
{CE809E33-59C8-4C66-81AE-2F99207CEC9A}.Debug|Any CPU.Build.0 = Debug|Any CPU
58+
{CE809E33-59C8-4C66-81AE-2F99207CEC9A}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
59+
{CE809E33-59C8-4C66-81AE-2F99207CEC9A}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
60+
{CE809E33-59C8-4C66-81AE-2F99207CEC9A}.Debug|x86.ActiveCfg = Debug|Any CPU
61+
{CE809E33-59C8-4C66-81AE-2F99207CEC9A}.Release|Any CPU.ActiveCfg = Release|Any CPU
62+
{CE809E33-59C8-4C66-81AE-2F99207CEC9A}.Release|Any CPU.Build.0 = Release|Any CPU
63+
{CE809E33-59C8-4C66-81AE-2F99207CEC9A}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
64+
{CE809E33-59C8-4C66-81AE-2F99207CEC9A}.Release|Mixed Platforms.Build.0 = Release|Any CPU
65+
{CE809E33-59C8-4C66-81AE-2F99207CEC9A}.Release|x86.ActiveCfg = Release|Any CPU
5266
EndGlobalSection
5367
GlobalSection(SolutionProperties) = preSolution
5468
HideSolutionNode = FALSE
5569
EndGlobalSection
5670
GlobalSection(NestedProjects) = preSolution
5771
{BF7F4EF2-8104-410E-8091-E977CFBB7465} = {3B6BCA40-E0E2-41FF-AB07-BDA371D65A2E}
72+
{CE809E33-59C8-4C66-81AE-2F99207CEC9A} = {BB6A307C-223B-406E-B75B-D478187B0DBB}
5873
EndGlobalSection
5974
EndGlobal

Examples/ExampleService/ExampleService.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@
6363
<Content Include="Default.htm" />
6464
<Content Include="MyService.svc" />
6565
<Content Include="swfobject.js" />
66-
<Content Include="Web.config" />
66+
<Content Include="Web.config">
67+
<SubType>Designer</SubType>
68+
</Content>
6769
</ItemGroup>
6870
<ItemGroup>
6971
<Compile Include="MyService.svc.cs">

Tests/DotAmf/AbstractTest.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using System.Reflection;
5+
using System.Xml;
6+
using DotAmf.Serialization;
7+
8+
namespace DotAmf
9+
{
10+
public abstract class AbstractTest
11+
{
12+
protected abstract DataContractAmfSerializer CreateSerializer<T>();
13+
14+
protected Stream GetInput(string sampleName)
15+
{
16+
var assembly = Assembly.GetExecutingAssembly();
17+
var resourceName = "DotAmf.Samples." + sampleName;
18+
19+
return assembly.GetManifestResourceStream(resourceName);
20+
}
21+
22+
protected Stream GetOutput()
23+
{
24+
return new MemoryStream();
25+
}
26+
27+
protected XmlWriter GetAmfxWriter(Stream output)
28+
{
29+
var settings = new XmlWriterSettings
30+
{
31+
CloseOutput = false,
32+
Indent = false,
33+
NamespaceHandling = NamespaceHandling.OmitDuplicates,
34+
NewLineHandling = NewLineHandling.Replace,
35+
NewLineChars = string.Empty,
36+
OmitXmlDeclaration = true
37+
};
38+
39+
return XmlWriter.Create(output, settings);
40+
}
41+
42+
protected XmlReader GetAmfxReader(Stream input)
43+
{
44+
var settings = new XmlReaderSettings
45+
{
46+
DtdProcessing = DtdProcessing.Ignore,
47+
IgnoreComments = true,
48+
IgnoreWhitespace = true,
49+
IgnoreProcessingInstructions = true
50+
};
51+
52+
return XmlReader.Create(input, settings);
53+
}
54+
55+
protected void ValidateAmfx(Stream output, string sampleName)
56+
{
57+
using (var outputReader = new StreamReader(output))
58+
using (var sampleReader = new StreamReader(GetInput(sampleName)))
59+
{
60+
var result = outputReader.ReadToEnd();
61+
var sample = sampleReader.ReadToEnd();
62+
63+
if (result != sample)
64+
{
65+
var message = string.Format(Errors.AmfxMismatch, result, sample);
66+
throw new InvalidDataException(message);
67+
}
68+
}
69+
}
70+
71+
protected void ValidateAmf(Stream output, string sampleName)
72+
{
73+
using (var outputReader = new StreamReader(output))
74+
using (var sampleReader = new StreamReader(GetInput(sampleName)))
75+
{
76+
var converter = new Func<string, string>(value => string.Join(" ", value.ToCharArray().Select(x => Convert.ToInt32(x).ToString("X2"))));
77+
78+
var result = converter(outputReader.ReadToEnd());
79+
var sample = converter(sampleReader.ReadToEnd());
80+
81+
if (result != sample)
82+
{
83+
var message = string.Format(Errors.AmfMismatch, result, sample);
84+
throw new InvalidDataException(message);
85+
}
86+
}
87+
}
88+
}
89+
}

Tests/DotAmf/Amf3DecoderTest.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using DotAmf.Data;
2+
using DotAmf.Serialization;
3+
using NUnit.Framework;
4+
5+
namespace DotAmf
6+
{
7+
[TestFixture(Description = "AMF to AMFX decoding tests.")]
8+
public class Amf3DecoderTest : AbstractTest
9+
{
10+
#region .ctor
11+
public Amf3DecoderTest()
12+
{
13+
_options = new AmfEncodingOptions
14+
{
15+
AmfVersion = AmfVersion.Amf3,
16+
UseContextSwitch = false
17+
};
18+
}
19+
#endregion
20+
21+
#region Data
22+
private readonly AmfEncodingOptions _options;
23+
#endregion
24+
25+
#region Test methods
26+
[Test(Description = "Simple string decoding.")]
27+
public void TestString1()
28+
{
29+
PerformTest<string>("String1.amf", "String1.amfx");
30+
}
31+
#endregion
32+
33+
#region Helper methods
34+
private void PerformTest<T>(string inputName, string sampleName)
35+
{
36+
var serializer = CreateSerializer<T>();
37+
38+
using (var input = GetInput(inputName))
39+
using (var output = GetOutput())
40+
{
41+
var writer = GetAmfxWriter(output);
42+
serializer.ReadObject(input, writer);
43+
44+
output.Flush();
45+
output.Position = 0;
46+
47+
ValidateAmfx(output, sampleName);
48+
}
49+
}
50+
51+
protected override DataContractAmfSerializer CreateSerializer<T>()
52+
{
53+
return new DataContractAmfSerializer(typeof(T), _options);
54+
}
55+
#endregion
56+
}
57+
}

Tests/DotAmf/Amf3EncoderTest.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using DotAmf.Data;
2+
using DotAmf.Serialization;
3+
using NUnit.Framework;
4+
5+
namespace DotAmf
6+
{
7+
[TestFixture(Description = "AMFX to AMF encoding tests.")]
8+
public class Amf3EncoderTest : AbstractTest
9+
{
10+
#region .ctor
11+
public Amf3EncoderTest()
12+
{
13+
_options = new AmfEncodingOptions
14+
{
15+
AmfVersion = AmfVersion.Amf3,
16+
UseContextSwitch = false
17+
};
18+
}
19+
#endregion
20+
21+
#region Data
22+
private readonly AmfEncodingOptions _options;
23+
#endregion
24+
25+
#region Test methods
26+
[Test(Description = "Simple string encoding.")]
27+
public void TestString1()
28+
{
29+
PerformTest<string>("String1.amfx", "String1.amf");
30+
}
31+
#endregion
32+
33+
#region Helper methods
34+
private void PerformTest<T>(string inputName, string sampleName)
35+
{
36+
var serializer = CreateSerializer<T>();
37+
38+
using (var input = GetInput(inputName))
39+
using (var output = GetOutput())
40+
{
41+
var reader = GetAmfxReader(input);
42+
reader.Read();
43+
serializer.WriteObject(output, reader);
44+
45+
output.Flush();
46+
output.Position = 0;
47+
48+
ValidateAmf(output, sampleName);
49+
}
50+
}
51+
52+
protected override DataContractAmfSerializer CreateSerializer<T>()
53+
{
54+
return new DataContractAmfSerializer(typeof(T), _options);
55+
}
56+
#endregion
57+
}
58+
}

Tests/DotAmf/DotAmf.csproj

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{CE809E33-59C8-4C66-81AE-2F99207CEC9A}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>DotAmf</RootNamespace>
11+
<AssemblyName>DotAmf.Tests</AssemblyName>
12+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>bin\Debug\</OutputPath>
20+
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25+
<DebugType>pdbonly</DebugType>
26+
<Optimize>true</Optimize>
27+
<OutputPath>bin\Release\</OutputPath>
28+
<DefineConstants>TRACE</DefineConstants>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
</PropertyGroup>
32+
<ItemGroup>
33+
<Reference Include="nunit.framework, Version=2.6.2.12296, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
34+
<HintPath>..\..\packages\NUnit.2.6.2\lib\nunit.framework.dll</HintPath>
35+
</Reference>
36+
<Reference Include="System" />
37+
<Reference Include="System.Core" />
38+
<Reference Include="System.Runtime.Serialization" />
39+
<Reference Include="System.Xml.Linq" />
40+
<Reference Include="System.Data.DataSetExtensions" />
41+
<Reference Include="Microsoft.CSharp" />
42+
<Reference Include="System.Data" />
43+
<Reference Include="System.Xml" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="AbstractTest.cs" />
47+
<Compile Include="Amf3DecoderTest.cs" />
48+
<Compile Include="Amf3EncoderTest.cs" />
49+
<Compile Include="Errors.Designer.cs">
50+
<AutoGen>True</AutoGen>
51+
<DesignTime>True</DesignTime>
52+
<DependentUpon>Errors.resx</DependentUpon>
53+
</Compile>
54+
<Compile Include="Properties\AssemblyInfo.cs" />
55+
</ItemGroup>
56+
<ItemGroup>
57+
<EmbeddedResource Include="Errors.resx">
58+
<Generator>ResXFileCodeGenerator</Generator>
59+
<LastGenOutput>Errors.Designer.cs</LastGenOutput>
60+
</EmbeddedResource>
61+
<EmbeddedResource Include="Samples\String1.amf" />
62+
<EmbeddedResource Include="Samples\String1.amfx" />
63+
</ItemGroup>
64+
<ItemGroup>
65+
<ProjectReference Include="..\..\Src\DotAmf\DotAmf.csproj">
66+
<Project>{dbf8762e-17d7-4aae-9f06-133de9870fbc}</Project>
67+
<Name>DotAmf</Name>
68+
</ProjectReference>
69+
</ItemGroup>
70+
<ItemGroup>
71+
<None Include="packages.config" />
72+
</ItemGroup>
73+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
74+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
75+
Other similar extension points exist, see Microsoft.Common.targets.
76+
<Target Name="BeforeBuild">
77+
</Target>
78+
<Target Name="AfterBuild">
79+
</Target>
80+
-->
81+
</Project>

0 commit comments

Comments
 (0)