Skip to content

Commit

Permalink
Add the possibility to create mapped classes from a protected constru…
Browse files Browse the repository at this point in the history
…ctor but not from a private one.
  • Loading branch information
lanwin committed May 7, 2010
1 parent 7b25618 commit 51b1b70
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 165 deletions.
@@ -1,11 +1,12 @@
using System;
using MongoDB.Configuration;
using MongoDB.Serialization;
using NUnit.Framework;

namespace MongoDB.UnitTests.Serialization
{
[TestFixture]
public class SerializationFactoryTests
public class SerializationFactoryTests : SerializationTestBase
{
[Test]
public void GetBsonReaderSettingsDefaults()
Expand All @@ -23,5 +24,32 @@ public void ReadLocalTimeCanBeChangedByConfig()
var readerSettings = factory.GetBsonReaderSettings(typeof(int));
Assert.AreEqual(readerSettings.ReadLocalTime, false);
}

public class ProtectedConstructor
{
protected ProtectedConstructor(){}
}

[Test]
public void CanCreateObjectFromProtectedConstructor()
{
var obj = Deserialize<ProtectedConstructor>(EmptyDocumentBson);

Assert.IsNotNull(obj);
}

public class PrivateConstructor
{
private PrivateConstructor() { }
}

[Test]
[ExpectedException(typeof(MissingMethodException))]
public void CanNotCreateObjectFromPrivateConstructor()
{
var obj = Deserialize<PrivateConstructor>(EmptyDocumentBson);

Assert.IsNotNull(obj);
}
}
}
Expand Up @@ -8,6 +8,8 @@ namespace MongoDB.UnitTests.Serialization
{
public abstract class SerializationTestBase
{
public const string EmptyDocumentBson = "BQAAAAA=";

protected virtual IMappingStore MappingStore
{
get { return new AutoMappingStore(); }
Expand Down
64 changes: 32 additions & 32 deletions source/MongoDB/Binary.cs
@@ -1,70 +1,70 @@
using System;

namespace MongoDB

namespace MongoDB
{
/// <summary>
///
/// </summary>
public class Binary{
public class Binary
{
/// <summary>
///
/// </summary>
public enum TypeCode:byte{
public enum TypeCode : byte
{
/// <summary>
///
/// </summary>
Unknown = 0,
/// <summary>
///
/// </summary>
General = 2,
// Uuid = 3 is now replaced by Guid
/// <summary>
///
/// </summary>
Md5 = 5,
/// <summary>
///
/// </summary>
UserDefined = 80
UserDefined = 80
}

/// <summary>
/// Gets or sets the bytes.
/// Initializes a new instance of the <see cref = "Binary" /> class.
/// </summary>
/// <value>The bytes.</value>
public byte[] Bytes{get;set;}
public Binary()
{
}

/// <summary>
/// Gets or sets the subtype.
/// Initializes a new instance of the <see cref = "Binary" /> class.
/// </summary>
/// <value>The subtype.</value>
public TypeCode Subtype{get;set;}
/// <param name = "bytes">The value.</param>
public Binary(byte[] bytes)
{
Bytes = bytes;
Subtype = TypeCode.General;
}

/// <summary>
/// Initializes a new instance of the <see cref="Binary"/> class.
/// Gets or sets the bytes.
/// </summary>
public Binary() { }
/// <value>The bytes.</value>
public byte[] Bytes { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="Binary"/> class.
/// Gets or sets the subtype.
/// </summary>
/// <param name="value">The value.</param>
public Binary(byte[] value){
Bytes = value;
Subtype = TypeCode.General;
}
/// <value>The subtype.</value>
public TypeCode Subtype { get; set; }

/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// Returns a <see cref = "System.String" /> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// A <see cref = "System.String" /> that represents this instance.
/// </returns>
public override string ToString (){
public override string ToString()
{
return String.Format(@"{{ ""$binary"": ""{0}"", ""$type"" : {1} }}",
Convert.ToBase64String(Bytes),
(int)Subtype);
}
}
}
(int)Subtype);
}
}
}

0 comments on commit 51b1b70

Please sign in to comment.