Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First Draft for the Protocol tool. #306

Merged
merged 6 commits into from
Jan 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion TestPlatform.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26005.1
VisualStudioVersion = 15.0.26014.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{ED0C35EB-7F31-4841-A24F-8EB708FFA959}"
EndProject
Expand Down Expand Up @@ -118,6 +118,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PerfTestProject", "test\Tes
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleDataCollector", "test\TestAssets\SimpleDataCollector\SimpleDataCollector.csproj", "{D62D754C-8F0A-406F-8BA7-E96C6FFA7C7C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.TestPlatform.Protocol", "samples\Microsoft.TestPlatform.Protocol\Microsoft.TestPlatform.Protocol.csproj", "{97DD9467-B011-4736-AAC4-2C21BF554349}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleTestProject3", "test\TestAssets\SimpleTestProject3\SimpleTestProject3.csproj", "{82E75225-FA92-4F87-909D-039D62F96BFF}"
EndProject
Global
Expand Down Expand Up @@ -646,6 +648,18 @@ Global
{D62D754C-8F0A-406F-8BA7-E96C6FFA7C7C}.Release|x64.Build.0 = Release|Any CPU
{D62D754C-8F0A-406F-8BA7-E96C6FFA7C7C}.Release|x86.ActiveCfg = Release|Any CPU
{D62D754C-8F0A-406F-8BA7-E96C6FFA7C7C}.Release|x86.Build.0 = Release|Any CPU
{97DD9467-B011-4736-AAC4-2C21BF554349}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{97DD9467-B011-4736-AAC4-2C21BF554349}.Debug|Any CPU.Build.0 = Debug|Any CPU
{97DD9467-B011-4736-AAC4-2C21BF554349}.Debug|x64.ActiveCfg = Debug|x64
{97DD9467-B011-4736-AAC4-2C21BF554349}.Debug|x64.Build.0 = Debug|x64
{97DD9467-B011-4736-AAC4-2C21BF554349}.Debug|x86.ActiveCfg = Debug|x86
{97DD9467-B011-4736-AAC4-2C21BF554349}.Debug|x86.Build.0 = Debug|x86
{97DD9467-B011-4736-AAC4-2C21BF554349}.Release|Any CPU.ActiveCfg = Release|Any CPU
{97DD9467-B011-4736-AAC4-2C21BF554349}.Release|Any CPU.Build.0 = Release|Any CPU
{97DD9467-B011-4736-AAC4-2C21BF554349}.Release|x64.ActiveCfg = Release|x64
{97DD9467-B011-4736-AAC4-2C21BF554349}.Release|x64.Build.0 = Release|x64
{97DD9467-B011-4736-AAC4-2C21BF554349}.Release|x86.ActiveCfg = Release|x86
{97DD9467-B011-4736-AAC4-2C21BF554349}.Release|x86.Build.0 = Release|x86
{82E75225-FA92-4F87-909D-039D62F96BFF}.Debug|Any CPU.ActiveCfg = Release|Any CPU
{82E75225-FA92-4F87-909D-039D62F96BFF}.Debug|Any CPU.Build.0 = Release|Any CPU
{82E75225-FA92-4F87-909D-039D62F96BFF}.Debug|x64.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -712,6 +726,7 @@ Global
{A23E3408-D569-488E-A071-E1B3625C5F09} = {8DA7CBD9-F17E-41B6-90C4-CFF55848A25A}
{57B182B8-9014-4C6D-B966-B464DE3127D5} = {8DA7CBD9-F17E-41B6-90C4-CFF55848A25A}
{D62D754C-8F0A-406F-8BA7-E96C6FFA7C7C} = {8DA7CBD9-F17E-41B6-90C4-CFF55848A25A}
{97DD9467-B011-4736-AAC4-2C21BF554349} = {B9AB7A3D-4F63-48D2-86C0-70F52F6509AB}
{82E75225-FA92-4F87-909D-039D62F96BFF} = {8DA7CBD9-F17E-41B6-90C4-CFF55848A25A}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.TestPlatform.Protocol
{
using System.IO;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;

/// <summary>
/// JsonDataSerializes serializes and deserializes data using Json format
/// </summary>
public class JsonDataSerializer
{
private static JsonDataSerializer instance;

private static JsonSerializer serializer;

/// <summary>
/// Prevents a default instance of the <see cref="JsonDataSerializer"/> class from being created.
/// </summary>
private JsonDataSerializer()
{
serializer = JsonSerializer.Create(
new JsonSerializerSettings
{
DateFormatHandling = DateFormatHandling.IsoDateFormat,
DateParseHandling = DateParseHandling.DateTimeOffset,
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
TypeNameHandling = TypeNameHandling.None
});
#if DEBUG
// MemoryTraceWriter can help diagnose serialization issues. Enable it for
// debug builds only.
serializer.TraceWriter = new MemoryTraceWriter();
#endif
}

/// <summary>
/// Gets the JSON Serializer instance.
/// </summary>
public static JsonDataSerializer Instance
{
get
{
return instance ?? (instance = new JsonDataSerializer());
}
}

/// <summary>
/// Deserialize a <see cref="Message"/> from raw JSON text.
/// </summary>
/// <param name="rawMessage">JSON string.</param>
/// <returns>A <see cref="Message"/> instance.</returns>
public Message DeserializeMessage(string rawMessage)
{
return JsonConvert.DeserializeObject<Message>(rawMessage);
}

/// <summary>
/// Deserialize the <see cref="Message.Payload"/> for a message.
/// </summary>
/// <param name="message">A <see cref="Message"/> object.</param>
/// <typeparam name="T">Payload type.</typeparam>
/// <returns>The deserialized payload.</returns>
public T DeserializePayload<T>(Message message)
{
T retValue = default(T);

// TODO: Currently we use json serializer auto only for non-testmessage types
// CHECK: Can't we just use auto for everything
if (Microsoft.TestPlatform.Protocol.MessageType.TestMessage.Equals(message.MessageType))
{
retValue = message.Payload.ToObject<T>();
}
else
{
retValue = message.Payload.ToObject<T>(serializer);
}

return retValue;
}

/// <summary>
/// Deserialize raw JSON to an object using the default serializer.
/// </summary>
/// <param name="json">JSON string.</param>
/// <typeparam name="T">Target type to deserialize.</typeparam>
/// <returns>An instance of <see cref="T"/>.</returns>
public T Deserialize<T>(string json)
{
using (var stringReader = new StringReader(json))
using (var jsonReader = new JsonTextReader(stringReader))
{
return serializer.Deserialize<T>(jsonReader);
}
}

/// <summary>
/// Serialize an empty message.
/// </summary>
/// <param name="messageType">Type of the message.</param>
/// <returns>Serialized message.</returns>
public string SerializeMessage(string messageType)
{
return JsonConvert.SerializeObject(new Message { MessageType = messageType });
}

/// <summary>
/// Serialize a message with payload.
/// </summary>
/// <param name="messageType">Type of the message.</param>
/// <param name="payload">Payload for the message.</param>
/// <returns>Serialized message.</returns>
public string SerializePayload(string messageType, object payload)
{
JToken serializedPayload = null;

// TODO: Currently we use json serializer auto only for non-testmessage types
// CHECK: Can't we just use auto for everything
if (MessageType.TestMessage.Equals(messageType))
{
serializedPayload = JToken.FromObject(payload);
}
else
{
serializedPayload = JToken.FromObject(payload, serializer);
}

return JsonConvert.SerializeObject(new Message { MessageType = messageType, Payload = serializedPayload });
}

/// <summary>
/// Serialize an object to JSON using default serialization settings.
/// </summary>
/// <typeparam name="T">Type of object to serialize.</typeparam>
/// <param name="data">Instance of the object to serialize.</param>
/// <returns>JSON string.</returns>
public string Serialize<T>(T data)
{
using (var stringWriter = new StringWriter())
using (var jsonWriter = new JsonTextWriter(stringWriter))
{
serializer.Serialize(jsonWriter, data);

return stringWriter.ToString();
}
}
}
}
30 changes: 30 additions & 0 deletions samples/Microsoft.TestPlatform.Protocol/Communication/Message.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.TestPlatform.Protocol
{
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public class Message
{
/// <summary>
/// Gets or sets the message type.
/// </summary>
public string MessageType { get; set; }

/// <summary>
/// Gets or sets the payload.
/// </summary>
public JToken Payload { get; set; }

/// <summary>
/// To string implementation.
/// </summary>
/// <returns> The <see cref="string"/>. </returns>
public override string ToString()
{
return "(" + MessageType + ") -> " + (Payload == null ? "null" : Payload.ToString(Formatting.Indented));
}
}
}
142 changes: 142 additions & 0 deletions samples/Microsoft.TestPlatform.Protocol/Communication/MessageType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.TestPlatform.Protocol
{
/// <summary>
/// The message type.
/// </summary>
public static class MessageType
{
/// <summary>
/// The session start.
/// </summary>
public const string SessionStart = "TestSession.Start";

/// <summary>
/// The session end.
/// </summary>
public const string SessionEnd = "TestSession.Terminate";

/// <summary>
/// The is aborted.
/// </summary>
public const string SessionAbort = "TestSession.Abort";

/// <summary>
/// The session connected.
/// </summary>
public const string SessionConnected = "TestSession.Connected";

/// <summary>
/// Test Message
/// </summary>
public const string TestMessage = "TestSession.Message";

/// <summary>
/// Protocol Version
/// </summary>
public const string VersionCheck = "ProtocolVersion";

/// <summary>
/// The session start.
/// </summary>
public const string DiscoveryInitialize = "TestDiscovery.Initialize";

/// <summary>
/// The discovery started.
/// </summary>
public const string StartDiscovery = "TestDiscovery.Start";

/// <summary>
/// The test cases found.
/// </summary>
public const string TestCasesFound = "TestDiscovery.TestFound";

/// <summary>
/// The discovery complete.
/// </summary>
public const string DiscoveryComplete = "TestDiscovery.Completed";

/// <summary>
/// The session start.
/// </summary>
public const string ExecutionInitialize = "TestExecution.Initialize";

/// <summary>
/// Cancel the current test run
/// </summary>
public const string CancelTestRun = "TestExecution.Cancel";

/// <summary>
/// Cancel the current test run
/// </summary>
public const string AbortTestRun = "TestExecution.Abort";

/// <summary>
/// Start test execution.
/// </summary>
public const string StartTestExecutionWithSources = "TestExecution.StartWithSources";

/// <summary>
/// Start test execution.
/// </summary>
public const string StartTestExecutionWithTests = "TestExecution.StartWithTests";

/// <summary>
/// The test run stats change.
/// </summary>
public const string TestRunStatsChange = "TestExecution.StatsChange";

/// <summary>
/// The execution complete.
/// </summary>
public const string ExecutionComplete = "TestExecution.Completed";

/// <summary>
/// The message to get runner process startInfo for run all tests in given sources
/// </summary>
public const string GetTestRunnerProcessStartInfoForRunAll = "TestExecution.GetTestRunnerProcessStartInfoForRunAll";

/// <summary>
/// The message to get runner process startInfo for run selected tests
/// </summary>
public const string GetTestRunnerProcessStartInfoForRunSelected = "TestExecution.GetTestRunnerProcessStartInfoForRunSelected";

/// <summary>
/// CustomTestHostLaunch
/// </summary>
public const string CustomTestHostLaunch = "TestExecution.CustomTestHostLaunch";

/// <summary>
/// Custom Test Host launch callback
/// </summary>
public const string CustomTestHostLaunchCallback = "TestExecution.CustomTestHostLaunchCallback";

/// <summary>
/// Extensions Initialization
/// </summary>
public const string ExtensionsInitialize = "Extensions.Initialize";

/// <summary>
/// Start Test Run All Sources
/// </summary>
public const string TestRunAllSourcesWithDefaultHost = "TestExecution.RunAllWithDefaultHost";

/// <summary>
/// Start Test Run - Testcases
/// </summary>
public const string TestRunSelectedTestCasesDefaultHost = "TestExecution.RunSelectedWithDefaultHost";

/// <summary>
/// Launch Adapter Process With DebuggerAttached
/// </summary>
public const string LaunchAdapterProcessWithDebuggerAttached = "TestExecution.LaunchAdapterProcessWithDebuggerAttached";

/// <summary>
/// Launch Adapter Process With DebuggerAttached
/// </summary>
public const string LaunchAdapterProcessWithDebuggerAttachedCallback = "TestExecution.LaunchAdapterProcessWithDebuggerAttachedCallback";

}
}
Loading