Thursday, 25 October 2012

Convert Your Physical Machine to VMWare or VirtualBox

This is very handy when you want to install a new OS or machine but want to keep your old machine running in case you need to boot / or get some settings you forgot about.

Disk2Vhd

First get Disk2Vhd. This is a super small utility written by the guys from Sysinternals (so it must be good). Just run it, choose drive C: (OS) and save the file. It runs while your OS is running. Once done you will have a VHD of your hard drive.

Virtual Box

Now you can simply create the Machine in Virtual Box and add the drive as the primary and boot from it. If you have any issues with BSOD (Blue screen of death) such as 0x0000007B it could be because the SATA drivers are not compatible. What I found works for this is to add the drive as an IDE instead. In virtual box this is easy:
Other issues may be: Make sure you create the machine as the correct platform (64bit) or (32Bit) else it wont boot either.

VMWare

I found the same to apply for VMWare, however VMWare doesn't allow you to choose IDE when adding the existing VHD. For this just add a new hard disk and choose the IDE option:



Now simply remove these disks, close VMWare and edit the .vmx file 

ide0:0.present = "TRUE"
ide0:0.fileName = "MY_DISK.vhd"
ide0:0.mode = "independent-persistent"
scsi0:0.present = "FALSE"
ide0:0.redo = ""
usb:0.present = "TRUE"
usb:0.deviceType = "hid"
usb:0.port = "0"
usb:0.parent = "-1"
view raw vmx hosted with ❤ by GitHub
You should now be able to boot from either and not worry about exporting all your settings.

Simple BDD - Take 2

A while back I wrote the post Simple BDD where the idea was to show how you can write a self documenting, human readable test in BDD style without the need of any fancy frameworks using fluent syntax. I was literally inundated by feedback from one person. The complaint was that it is weird to write statements such as:

Given
Given
When
When
When
Then
Then

The argument is that this doesn't really read like a story and just seems like I am stuttering.

So I would like to make a small modification to this to show 2 points.

The test name

The test name will really be something descriptive that is most logical:

GivenARectangleWithWidthOf10AndHeightOf10TheAreaShouldCalculate100

I am sure I don't have to add the virtual spaces to make it more readable at this point as you would get the idea. The wording here is completely logical.

Implementation

To actually write this implementation that satisfies the test I am going write a Shape and Calculator and throw in a Visitor just to make it more interesting.

public class Shape
{
}
public class Rectangle : Shape
{
public int Width { get; set; }
public int Height { get; set; }
public T Accept<T>(ShapeVisitor<T> visitor)
{
return visitor.Visit(this);
}
}
public abstract class ShapeVisitor<T>
{
public abstract T Visit(Rectangle shape);
}
public class ShapeAreaCalculator : ShapeVisitor<int>
{
public override int Visit(Rectangle shape)
{
return shape.Width * shape.Height;
}
}
view raw Classes.cs hosted with ❤ by GitHub

Now to write the grammar in a little bit more like a human, I thought to simply add an And method.

public TestClass And(TestClass setup)
{
return this;
}
view raw And.cs hosted with ❤ by GitHub
Here is the full test and test class implementation:
[TestClass]
public class TestClass
{
private Rectangle _rectangle;
private ShapeAreaCalculator _calculator;
[TestMethod]
public void GivenARectangleWithWidthOf10AndHeightOf10TheAreaShouldCalculate100()
{
this
.Given(GivenNewRectangle()
.And(GivenShapeAreaCalculator(new ShapeAreaCalculator())))
.When(WhenRectangleWidth(10)
.And(WhenRectangleHeight(10)))
.Then(() => _rectangle.Accept(_calculator) == 100)
.ThenAreaIs(100);
}
public TestClass Given(TestClass when)
{
return this;
}
public TestClass GivenNewRectangle()
{
_rectangle = new Rectangle();
return this;
}
public TestClass GivenShapeAreaCalculator(ShapeAreaCalculator calculator)
{
_calculator = calculator;
return this;
}
public TestClass When(TestClass when)
{
return this;
}
public TestClass WhenRectangleWidth(int n)
{
_rectangle.Width = n;
return this;
}
public TestClass WhenRectangleHeight(int n)
{
_rectangle.Height = n;
return this;
}
public TestClass And(TestClass setup)
{
return this;
}
public TestClass ThenAreaIs(int area)
{
int result = _calculator.Visit(_rectangle);
Assert.AreEqual(result, area);
return this;
}
public TestClass Then(Func<bool> a)
{
Assert.IsTrue(a());
return this;
}
}
view raw TestClass.cs hosted with ❤ by GitHub

Note

I DO recommend that you do not use too many evaluations (Asserts/Then) in one test and to rather break them up. The fact that I have shown it is just an example of what the syntax can do. Also the grammar is not set in stone and the idea is to adapt it to what you prefer.