achieved 100% test coverage for the read only collection wrapper; optimized the indexer of the license key class; increased test coverage for the license key class to 100%

git-svn-id: file:///srv/devel/repo-conversion/nusu@95 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2008-11-27 19:02:48 +00:00
parent c43bfd47c8
commit de7c28fa84
5 changed files with 218 additions and 8 deletions

View file

@ -31,6 +31,13 @@ namespace Nuclex.Support.Licensing {
[TestFixture]
public class LicenseKeyTest {
/// <summary>Tests the default constructor of the license key class</summary>
[Test]
public void TestDefaultConstructor() {
new LicenseKey();
}
/// <summary>Validates the correct translation of keys to GUIDs and back</summary>
[Test]
public void TestGuidKeyConversion() {
@ -88,6 +95,43 @@ namespace Nuclex.Support.Licensing {
}
/// <summary>Tests whether license keys can be modified without destroying them</summary>
[Test, ExpectedException(typeof(ArgumentException))]
public void TestParseInvalidLicenseKey() {
LicenseKey.Parse("hello world");
}
/// <summary>
/// Tests whether an exception is thrown if the indexer of a license key is used
/// with an invalid index to retrieve a component of the key
/// </summary>
[Test, ExpectedException(typeof(IndexOutOfRangeException))]
public void TestGetByIndexerWithInvalidIndex() {
LicenseKey key = new LicenseKey();
int indexMinusOne = key[-1];
}
/// <summary>
/// Tests whether an exception is thrown if the indexer of a license key is used
/// with an invalid index to set a component of the key
/// </summary>
[Test, ExpectedException(typeof(IndexOutOfRangeException))]
public void TestSetByIndexerWithInvalidIndex() {
LicenseKey key = new LicenseKey();
key[-1] = 0;
}
/// <summary>
/// Verifies that a license key can be converted into a byte array
/// </summary>
[Test]
public void TestToByteArray() {
Guid someGuid = Guid.NewGuid();
LicenseKey someKey = new LicenseKey(someGuid);
CollectionAssert.AreEqual(someGuid.ToByteArray(), someKey.ToByteArray());
}
}
} // namespace Nuclex.Support.Licensing

View file

@ -113,12 +113,18 @@ namespace Nuclex.Support.Licensing {
if((index < 0) || (index > 3))
throw new IndexOutOfRangeException("Index out of range");
using(MemoryStream guidBytes = new MemoryStream(this.guid.ToByteArray())) {
guidBytes.Position = index * 4;
new BinaryWriter(guidBytes).Write(value);
// Convert the GUID into binary data so we can replace one of its values
byte[] guidBytes = this.guid.ToByteArray();
this.guid = new Guid(guidBytes.ToArray());
}
// Overwrite the section at the index specified by the user with the new value
Array.Copy(
BitConverter.GetBytes(value), 0, // source and start index
guidBytes, index * 4, // destination and start index
4 // length
);
// Replacement finished, now we can reconstruct our guid
this.guid = new Guid(guidBytes);
}
}