Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don’t worry, you can still create the pull request.
  • 19 commits
  • 9 files changed
  • 0 commit comments
  • 1 contributor
Commits on May 21, 2012
@donbing donbing update to .net 4 and add rhino mocks 7558cd9
@donbing donbing initial, rahter weak passing test ca2d116
@donbing donbing make TelemeteryClient ctor dependancy for TelemeteryDiagnosticControls befaa29
@donbing donbing extract interface from TelemeteryClient
make and change TelemeteryDiagnosticControls depend on abstracted
telemetery client
3cc4aea
@donbing donbing using dynamic mock for TelemeteryClient in Diagnosti controls test 6095359
@donbing donbing refactor test for TelemeteryDiagnosticControls and Assert send was ca…
…lled on ITelemeteryClient
3ceca89
@donbing donbing TelemeteryDiagnosticControls test now cover most of the behavior
but the tests are horrid :)
50492ab
Commits on May 22, 2012
@donbing donbing make connection method static
so i can move it to another class in a mo
2be2706
@donbing donbing move static method
to make it instance method next
de5aac9
@donbing donbing wrapped telemetry client in new connection class
tests still pass :)
81914ff
@donbing donbing make tryconnect into instance method 22a54d5
@donbing donbing make connection a field, init in ctor 018ec90
@donbing donbing push connection out of diagnostic controls ctor
refactored tests of connection to focus on connection itself
c1e1ab8
@donbing donbing extract IConnection interface form ITelemetryClient and use in connec…
…tion
49e2989
@donbing donbing make tests check disconnect and number of time connect is called 8e9119d
@donbing donbing seperated connetion and channel interfaces 5db1f1e
@donbing donbing extract connection behaviour from client to superclass be203eb
@donbing donbing remove superclass from client :) 163b297
@donbing donbing inline pointless methods from connector class d6fb46e
View
9 TDDMicroExercises/CSharp/TDDMicroExercises.csproj
@@ -10,11 +10,12 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TDDMicroExercises</RootNamespace>
<AssemblyName>TDDMicroExercises</AssemblyName>
- <TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
+ <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileUpgradeFlags>
</FileUpgradeFlags>
<OldToolsVersion>2.0</OldToolsVersion>
<UpgradeBackupLocation />
+ <TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@@ -38,6 +39,9 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>lib\nunit.framework.dll</HintPath>
</Reference>
+ <Reference Include="Rhino.Mocks">
+ <HintPath>packages\RhinoMocks.3.6.1\lib\net\Rhino.Mocks.dll</HintPath>
+ </Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Web" />
@@ -60,6 +64,9 @@
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
+ <ItemGroup>
+ <None Include="packages.config" />
+ </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- 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.
View
82 TDDMicroExercises/CSharp/TelemetrySystem.Tests/TelemetryDiagnosticControlsTest.cs
@@ -1,14 +1,96 @@
using NUnit.Framework;
+using NUnit.Framework.SyntaxHelpers;
+using Rhino.Mocks;
namespace TDDMicroExercises.TelemetrySystem.Tests
{
[TestFixture]
public class TelemetryDiagnosticControlsTest
{
+ private const string ExpectedTelemetryClientResponse = "foo";
+
[Test]
public void CheckTransmission_should_send_a_diagnostic_message_and_receive_a_status_message_response()
{
+ var channel = MockRepository.GenerateMock<ITelemetryChannel>();
+ var connetion = MockRepository.GenerateMock<IConnection>();
+ var telemetryConnection = new TelemetryConnector(connetion);
+
+ using (channel.GetMockRepository().Ordered())
+ {
+ connetion
+ .Stub(client => client.OnlineStatus)
+ .Return(false).Repeat.Once();
+
+ connetion
+ .Stub(client => client.OnlineStatus)
+ .Return(true);
+ }
+
+ channel
+ .Stub(client => client.Receive())
+ .Return(ExpectedTelemetryClientResponse);
+
+ var telemetryDiagnosticControls = new TelemetryDiagnosticControls(channel, telemetryConnection);
+
+ telemetryDiagnosticControls.CheckTransmission();
+
+ connetion.AssertWasCalled(conn => conn.Connect(Arg<string>.Is.Anything));
+ channel.AssertWasCalled(client => client.Send(TelemetryClient.DiagnosticMessage));
+
+ Assert.That(telemetryDiagnosticControls.DiagnosticInfo, Is.EqualTo(ExpectedTelemetryClientResponse));
+ }
+ }
+
+ [TestFixture]
+ public class ConnectionTests
+ {
+ [Test, ExpectedException(typeof(System.Exception), ExpectedMessage = "Unable to connect.")]
+ public void CheckConnection_ThrowsAfterThreeFailedConnections()
+ {
+ var telemetryClient = MockRepository.GenerateMock<IConnection>();
+ var telemetryConnection = new TelemetryConnector(telemetryClient);
+
+ using (telemetryClient.GetMockRepository().Ordered())
+ {
+ telemetryClient.Expect(client => client.Disconnect());
+
+ telemetryClient
+ .Stub(client => client.OnlineStatus)
+ .Return(false)
+ .Repeat.Times(3);
+ }
+
+ telemetryConnection.TryConnect(3, "any connection string");
+
+ telemetryClient.AssertWasCalled(client => client.Connect(Arg<string>.Is.Anything), opt => opt.Repeat.Times(2));
+ telemetryClient.VerifyAllExpectations();
}
+ [Test]
+ public void CheckConnection_ConnectsOnThirdConnectionRetry()
+ {
+ var telemetryClient = MockRepository.GenerateMock<IConnection>();
+ var telemetryConnection = new TelemetryConnector(telemetryClient);
+
+ using (telemetryClient.GetMockRepository().Ordered())
+ {
+ telemetryClient.Expect(client => client.Disconnect());
+
+ telemetryClient
+ .Stub(client => client.OnlineStatus)
+ .Return(false)
+ .Repeat.Times(2);
+
+ telemetryClient
+ .Stub(client => client.OnlineStatus)
+ .Return(true);
+ }
+
+ telemetryConnection.TryConnect(3, "any connection string");
+
+ telemetryClient.AssertWasCalled(client => client.Connect(Arg<string>.Is.Anything),opt => opt.Repeat.Times(2));
+ telemetryClient.VerifyAllExpectations();
+ }
}
}
View
74 TDDMicroExercises/CSharp/TelemetrySystem/TelemetryClient.cs
@@ -2,40 +2,52 @@
namespace TDDMicroExercises.TelemetrySystem
{
- public class TelemetryClient
- {
- public const string DiagnosticMessage = "AT#UD";
+ public interface IConnection
+ {
+ bool OnlineStatus { get; }
+ void Connect(string telemetryServerConnectionString);
+ void Disconnect();
+ }
- private bool _onlineStatus;
- private string _diagnosticMessageResult = string.Empty;
+ public interface ITelemetryChannel
+ {
+ void Send(string message);
+ string Receive();
+ }
- private readonly Random _connectionEventsSimulator = new Random(42);
+ public class TelemetryConnection : IConnection
+ {
+ protected readonly Random ConnectionEventsSimulator = new Random(42);
- public bool OnlineStatus
- {
- get { return _onlineStatus; }
- }
+ public bool OnlineStatus { get; private set; }
- public void Connect(string telemetryServerConnectionString)
- {
- if (string.IsNullOrEmpty(telemetryServerConnectionString))
- {
- throw new ArgumentNullException();
- }
+ public void Connect(string telemetryServerConnectionString)
+ {
+ if (string.IsNullOrEmpty(telemetryServerConnectionString))
+ {
+ throw new ArgumentNullException();
+ }
- // simulate the operation on a real modem
- bool success = _connectionEventsSimulator.Next(1, 10) <= 8;
+ // simulate the operation on a real modem
+ var success = ConnectionEventsSimulator.Next(1, 10) <= 8;
- _onlineStatus = success;
+ OnlineStatus = success;
+ }
- }
+ public void Disconnect()
+ {
+ OnlineStatus = false;
+ }
+ }
- public void Disconnect()
- {
- _onlineStatus = false;
- }
+ public class TelemetryClient : ITelemetryChannel
+ {
+ public const string DiagnosticMessage = "AT#UD";
+
+ private readonly Random randomGenerator = new Random(42);
+ private string diagnosticMessageResult = string.Empty;
- public void Send(string message)
+ public void Send(string message)
{
if (string.IsNullOrEmpty(message))
{
@@ -45,7 +57,7 @@ public void Send(string message)
if (message == DiagnosticMessage)
{
// simulate a status report
- _diagnosticMessageResult =
+ diagnosticMessageResult =
"LAST TX rate................ 100 MBPS\r\n"
+ "HIGHEST TX rate............. 100 MBPS\r\n"
+ "LAST RX rate................ 100 MBPS\r\n"
@@ -71,19 +83,19 @@ public string Receive()
{
string message;
- if (string.IsNullOrEmpty(_diagnosticMessageResult) == false)
+ if (string.IsNullOrEmpty(diagnosticMessageResult) == false)
{
- message = _diagnosticMessageResult;
- _diagnosticMessageResult = string.Empty;
+ message = diagnosticMessageResult;
+ diagnosticMessageResult = string.Empty;
}
else
{
// simulate a received message
message = string.Empty;
- int messageLenght = _connectionEventsSimulator.Next(50, 110);
+ int messageLenght = randomGenerator.Next(50, 110);
for(int i = messageLenght; i >=0; --i)
{
- message += (char)_connectionEventsSimulator.Next(40, 126);
+ message += (char)randomGenerator.Next(40, 126);
}
}
View
51 TDDMicroExercises/CSharp/TelemetrySystem/TelemetryDiagnosticControls.cs
@@ -7,40 +7,57 @@ public class TelemetryDiagnosticControls
{
private const string DiagnosticChannelConnectionString = "*111#";
- private readonly TelemetryClient _telemetryClient;
- private string _diagnosticInfo = string.Empty;
+ private readonly ITelemetryChannel channel;
+ private readonly TelemetryConnector connection;
+ private string diagnosticInfo = string.Empty;
- public TelemetryDiagnosticControls()
+ public TelemetryDiagnosticControls(ITelemetryChannel client, TelemetryConnector connection)
{
- _telemetryClient = new TelemetryClient();
+ channel = client;
+ this.connection = connection;
}
public string DiagnosticInfo
{
- get { return _diagnosticInfo; }
- set { _diagnosticInfo = value; }
+ get { return diagnosticInfo; }
+ set { diagnosticInfo = value; }
}
public void CheckTransmission()
{
- _diagnosticInfo = string.Empty;
+ diagnosticInfo = string.Empty;
- _telemetryClient.Disconnect();
+ connection.TryConnect(3, DiagnosticChannelConnectionString);
- int retryLeft = 3;
- while (_telemetryClient.OnlineStatus == false && retryLeft > 0)
+ channel.Send(TelemetryClient.DiagnosticMessage);
+
+ diagnosticInfo = channel.Receive();
+ }
+ }
+
+ public class TelemetryConnector
+ {
+ private readonly IConnection connection;
+
+ public TelemetryConnector(IConnection connection)
+ {
+ this.connection = connection;
+ }
+
+ public void TryConnect(int retryCount, string diagnosticChannelConnectionString)
+ {
+ connection.Disconnect();
+
+ while (connection.OnlineStatus == false && retryCount > 0)
{
- _telemetryClient.Connect(DiagnosticChannelConnectionString);
- retryLeft -= 1;
+ connection.Connect(diagnosticChannelConnectionString);
+ retryCount -= 1;
}
-
- if(_telemetryClient.OnlineStatus == false)
+
+ if (connection.OnlineStatus == false)
{
throw new Exception("Unable to connect.");
}
-
- _telemetryClient.Send(TelemetryClient.DiagnosticMessage);
- _diagnosticInfo = _telemetryClient.Receive();
}
}
}
View
4 TDDMicroExercises/CSharp/packages.config
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+ <package id="RhinoMocks" version="3.6.1" />
+</packages>
View
BIN TDDMicroExercises/CSharp/packages/RhinoMocks.3.6.1/RhinoMocks.3.6.1.nupkg
Binary file not shown.
View
BIN TDDMicroExercises/CSharp/packages/RhinoMocks.3.6.1/lib/net/Rhino.Mocks.dll
Binary file not shown.
View
5,624 TDDMicroExercises/CSharp/packages/RhinoMocks.3.6.1/lib/net/Rhino.Mocks.xml
5,624 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
4 TDDMicroExercises/CSharp/packages/repositories.config
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<repositories>
+ <repository path="..\packages.config" />
+</repositories>

No commit comments for this range

Something went wrong with that request. Please try again.