#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2010 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
#if UNITTEST
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using NUnit.Framework;
namespace Nuclex.Support {
/// Unit Test for the strongly typed weak reference class
[TestFixture]
public class WeakReferenceTest {
#region class Dummy
/// Dummy class for testing the shared instance provider
[Serializable]
private class Dummy {
/// Initializes a new dummy
public Dummy() { }
}
#endregion // class Dummy
/// Tests whether the simple constructor works
[Test]
public void TestSimpleConstructor() {
new WeakReference(new Dummy());
}
/// Test whether the full constructor works
[Test]
public void TestFullConstructor() {
new WeakReference(new Dummy(), false);
}
///
/// Test whether the target object can be retrieved from the weak reference
///
[Test]
public void TestTargetRetrieval() {
Dummy strongReference = new Dummy();
WeakReference weakReference = new WeakReference(strongReference);
// We can not just call GC.Collect() and base our test on the assumption that
// the garbage collector will actually collect the Dummy instance. This is up
// to the garbage collector to decide. But we can keep a strong reference in
// parallel and safely assume that the WeakReference will not be invalidated!
Assert.AreSame(strongReference, weakReference.Target);
}
///
/// Test whether the target object can be reassigned in the weak reference
///
[Test]
public void TestTargetReassignment() {
Dummy strongReference1 = new Dummy();
Dummy strongReference2 = new Dummy();
WeakReference weakReference = new WeakReference(strongReference1);
Assert.AreSame(strongReference1, weakReference.Target);
weakReference.Target = strongReference2;
Assert.AreSame(strongReference2, weakReference.Target);
}
///
/// Test whether the target object can be reassigned in the weak reference
///
[Test]
public void TestSerialization() {
BinaryFormatter formatter = new BinaryFormatter();
using(MemoryStream memory = new MemoryStream()) {
WeakReference weakReference1 = new WeakReference(new Dummy());
formatter.Serialize(memory, weakReference1);
memory.Position = 0;
object weakReference2 = formatter.Deserialize(memory);
// We cannot make any more predictions but for the type of the weak reference.
// The pointee might have been garbage collected just now or the serializer
// might have decided not to serialize the pointee at all (which is a valid
// decision if the serializer found no strong reference to the pointee) in
// another of the object graph.
Assert.IsNotNull(weakReference2 as WeakReference);
}
}
}
} // namespace Nuclex.Support
#endif // UNITTEST