Skip to content

Commit

Permalink
Treat all non-virtual methods of System.Object as not proxiable (nhib…
Browse files Browse the repository at this point in the history
  • Loading branch information
hazzik authored May 5, 2024
1 parent f25b398 commit 13dd509
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 104 deletions.
4 changes: 2 additions & 2 deletions build-common/NHibernate.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

<PropertyGroup>
<NhVersion Condition="'$(NhVersion)' == ''" >5.5</NhVersion>
<VersionPatch Condition="'$(VersionPatch)' == ''">1</VersionPatch>
<VersionPatch Condition="'$(VersionPatch)' == ''">2</VersionPatch>
<!-- Clear VersionSuffix for making release and set it to dev for making development builds -->
<VersionSuffix Condition="'$(VersionSuffix)' == ''"></VersionSuffix>
<VersionSuffix Condition="'$(VersionSuffix)' == ''">dev</VersionSuffix>
<LangVersion Condition="'$(MSBuildProjectExtension)' != '.vbproj'">9.0</LangVersion>

<VersionPrefix Condition="'$(VersionPrefix)' == ''">$(NhVersion).$(VersionPatch)</VersionPrefix>
Expand Down
82 changes: 49 additions & 33 deletions src/NHibernate.Test/NHSpecificTest/ProxyValidator/Fixture.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using NHibernate.Proxy;
using NUnit.Framework;
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.ProxyValidator
{
Expand All @@ -10,15 +9,6 @@ public class Fixture
{
private readonly IProxyValidator pv = new DynProxyTypeValidator();

private void Validate(System.Type type)
{
ICollection<string> errors = pv.ValidateType(type);
if (errors != null)
{
throw new InvalidProxyTypeException(errors);
}
}

public class ValidClass
{
private int privateField;
Expand Down Expand Up @@ -64,12 +54,35 @@ protected int NonVirtualPrivateProperty
#pragma warning restore 67
}

[Test]
public void ObjectIsValid()
{
var errors = pv.ValidateType(typeof(object));
Assert.That(errors, Is.Null);
}

[Test]
public void ValidClassTest()
{
Validate(typeof(ValidClass));
var errors = pv.ValidateType(typeof(ValidClass));
Assert.That(errors, Is.Null);
}

public class InvalidSealedToString : ValidClass
{
public sealed override string ToString()
{
return base.ToString();
}
}

[Test]
public void SealedObjectOverride()
{
var errors = pv.ValidateType(typeof(InvalidSealedToString));
Assert.That(errors, Has.Count.EqualTo(1));
}

public class InvalidPrivateConstructor : ValidClass
{
private InvalidPrivateConstructor()
Expand All @@ -80,7 +93,8 @@ private InvalidPrivateConstructor()
[Test]
public void PrivateConstructor()
{
Assert.Throws<InvalidProxyTypeException>(() => Validate(typeof(InvalidPrivateConstructor)));
var errors = pv.ValidateType(typeof(InvalidPrivateConstructor));
Assert.That(errors, Has.Count.EqualTo(1));
}

public class InvalidNonVirtualProperty : ValidClass
Expand All @@ -95,7 +109,8 @@ public int NonVirtualProperty
[Test]
public void NonVirtualProperty()
{
Assert.Throws<InvalidProxyTypeException>(() => Validate(typeof(InvalidNonVirtualProperty)));
var errors = pv.ValidateType(typeof(InvalidNonVirtualProperty));
Assert.That(errors, Has.Count.EqualTo(2));
}

public class InvalidPublicField : ValidClass
Expand All @@ -106,7 +121,8 @@ public class InvalidPublicField : ValidClass
[Test]
public void PublicField()
{
Assert.Throws<InvalidProxyTypeException>(() => Validate(typeof(InvalidPublicField)));
var errors = pv.ValidateType(typeof(InvalidPublicField));
Assert.That(errors, Has.Count.EqualTo(1));
}

public class InvalidNonVirtualEvent : ValidClass
Expand All @@ -119,7 +135,8 @@ public class InvalidNonVirtualEvent : ValidClass
[Test]
public void NonVirtualEvent()
{
Assert.Throws<InvalidProxyTypeException>(() => Validate(typeof(InvalidNonVirtualEvent)));
var errors = pv.ValidateType(typeof(InvalidNonVirtualEvent));
Assert.That(errors, Has.Count.EqualTo(2));
}

public interface ValidInterface
Expand All @@ -129,7 +146,8 @@ public interface ValidInterface
[Test]
public void Interface()
{
Validate(typeof(ValidInterface));
var errors = pv.ValidateType(typeof(ValidInterface));
Assert.That(errors, Is.Null);
}

public class MultipleErrors
Expand All @@ -153,15 +171,8 @@ public int NonVirtualProperty
[Test]
public void MultipleErrorsReported()
{
try
{
Validate(typeof(MultipleErrors));
Assert.Fail("Should have failed validation");
}
catch (InvalidProxyTypeException e)
{
Assert.IsTrue(e.Errors.Count > 1);
}
var errors = pv.ValidateType(typeof(MultipleErrors));
Assert.That(errors, Has.Count.GreaterThan(1));
}

public class InvalidNonVirtualInternalProperty : ValidClass
Expand All @@ -183,16 +194,18 @@ public class InvalidInternalField : ValidClass
[Test]
public void NonVirtualInternal()
{
Assert.Throws<InvalidProxyTypeException>(() => Validate(typeof(InvalidNonVirtualInternalProperty)));
var errors = pv.ValidateType(typeof(InvalidNonVirtualInternalProperty));
Assert.That(errors, Has.Count.EqualTo(2));
}

[Test]
public void InternalField()
{
Assert.Throws<InvalidProxyTypeException>(() => Validate(typeof(InvalidInternalField)));
var errors = pv.ValidateType(typeof(InvalidInternalField));
Assert.That(errors, Has.Count.EqualTo(1));
}

public class InvalidNonVirtualProtectedProperty : ValidClass
public class ValidNonVirtualProtectedProperty : ValidClass
{
protected int NonVirtualProperty
{
Expand All @@ -204,8 +217,8 @@ protected int NonVirtualProperty
[Test]
public void NonVirtualProtected()
{
Validate(typeof(InvalidNonVirtualProtectedProperty));
Assert.IsTrue(true, "Always should pass, protected members do not need to be virtual.");
var errors = pv.ValidateType(typeof(ValidNonVirtualProtectedProperty));
Assert.That(errors, Is.Null);
}

public class InvalidNonVirtualProtectedInternalProperty : ValidClass
Expand All @@ -220,7 +233,8 @@ protected internal int NonVirtualProperty
[Test]
public void NonVirtualProtectedInternal()
{
Assert.Throws<InvalidProxyTypeException>(() => Validate(typeof(InvalidNonVirtualProtectedInternalProperty)));
var errors = pv.ValidateType(typeof(InvalidNonVirtualProtectedInternalProperty));
Assert.That(errors, Has.Count.EqualTo(2));
}

interface INonVirtualPublicImplementsInterface
Expand All @@ -239,7 +253,8 @@ public int NonVirtualMethodImplementsInterface
[Test]
public void VirtualPublicImplementsInterface()
{
Assert.Throws<InvalidProxyTypeException>(() => Validate(typeof(NonVirtualPublicImplementsInterface)));
var errors = pv.ValidateType(typeof(NonVirtualPublicImplementsInterface));
Assert.That(errors, Has.Count.EqualTo(1));
}

public class InvalidVirtualPrivateAutoProperty : ValidClass
Expand All @@ -254,7 +269,8 @@ public virtual int NonVirtualSetterProperty
[Test]
public void PrivateSetterOnVirtualPropertyShouldThrows()
{
Assert.Throws<InvalidProxyTypeException>(() => Validate(typeof(InvalidVirtualPrivateAutoProperty)));
var errors = pv.ValidateType(typeof(InvalidVirtualPrivateAutoProperty));
Assert.That(errors, Has.Count.EqualTo(1));
}
}
}
Loading

0 comments on commit 13dd509

Please sign in to comment.