Friday, May 10, 2013

jMock custom actions, doAll expectation and onConsecutiveCalls

I had an issue with a class that I've not encountered before with projects on which I use jMock: I have a mocked class whose method modifies an object it takes as a parameter.  I suspect this is due to poor coupling in the application, but I'll refactor at a later date -- for now I welcome the challenge as an opportunity to explore three aspects of jMock with which I'm unfamiliar: custom actions, doAll and onConsecutiveCalls.

Problem

public class WillBeMocked {
    public Object processBusinessObject businessObject) {
        ReturnObject returnObject = new ReturnObject(businessObject);

        // change state of input parameter
        businessObject.incrementCounter();

        return returnObject;
    }
}

Solution


create custom jmock.org.api.Action in my test class

private static class BusinessObjectIncrementCounterAction<T> implements org.jmock.api.Action
{
    private final BusinessObject businessObjectInstance;

    public BusinessObjectIncrementCounterAction(final BusinessObject businessObjectInstance)
    {
        this. businessObjectInstance = businessObjectInstance;
    }

    @Override
    public void describeTo(final Description description)
    {
        description.appendText("calls incrementCounter() on the businessObjectInstance");
    }

    @Override
    public Object invoke(final Invocation invocation) throws Throwable
    {
        businessObjectInstance.incrementCounter();
        return null;
    }
}


create static call to construct custom Action in my test class

private static <T> org.jmock.api.Action incrementCounter(final BusinessObject businessObjectInstance)
{
    return new BusinessObjectIncrementCounterAction <T>(businessObjectInstance);
}


use doAll to execute custom Action and mocked class's method call

                
context.checking(new Expectations() {
    {
        oneOf(willBeMockedInstance).process(businessObjectInstance);

        will(doAll(incrementCounter(businessObjectInstance), 
             returnValue(new ReturnObject("A"))));
    }
});



use onConsecutiveCall to 

context.checking(new Expectations() {
    {
        exactly(2).of(willBeMockedInstance).process(businessObjectInstance);

        will(onConsecutiveCalls(doAll(incrementCounter(businessObjectInstance), 
             returnValue(new ReturnObject("A")))),
             doAll(incrementCounter(businessObjectInstance), 
             returnValue(new ReturnObject("B")))));
    }
});

Wednesday, May 8, 2013

Return code is: 401, ReasonPhrase:Unauthorized. -> [Help 1]: Nexus error via Maven via Jenkins when using a slave node: Solved


[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project project: Failed to deploy artifacts: Could not transfer artifact com.company:project:jar:build_id from/to dirname (http://server:port/nexus/content/repositories/snapshots): Failed to transfer file: http://server:port/nexus/content/repositories/snapshots/com/company/product/build_id/product-build_id.jar. Return code is: 401, ReasonPhrase:Unauthorized. -> [Help 1]

(Output modified to protect the innocent.)

I have Maven builds running on a Jenkins master with slave nodes.  The deploy goal attempts to push artifacts from the build out to our Nexus instance.

To address this issue I had to add settings.xml to the Jenkins slave owner's .m2 directory as this is used during the upload to Nexus, not the settings.xml in the .m2 directory on the Jenkins master box.

Monday, April 15, 2013

How to move a repo from GitHub to Bitbucket

Just collating two existing resources.  As with the first poster I like to store tutorial code, etc. in a private repo so that it doesn't clutter up my GitHub presence when I put something out there.

Import GitHub repo into Bitbucket

http://somatose.com/2011/10/from-github-to-bitbucket-in-60-seconds.html

Delete GitHub repo

https://help.github.com/articles/deleting-a-repository

Saturday, February 23, 2013

How to create an iso of an existing Windows system using Ubuntu: what worked and what didn't

I have a Windows XP Media Center laptop that is dying, but want to keep the image around.  I've created vms before but not from existing systems, just as fresh installs.  Searching around I stumbled upon Clonezilla which allowed me to create just such an image without purchasing software.  Unfortunately Clonezilla only almost worked (probably because of something I didn't do correctly).   disk2vhd (a Microsoft product) ended up getting the job done in the end.

I'm using Ubuntu 12.10 as my main machine.   Taking an image of a Windows XP Home Media edition with an 80G hard drive. 

The disk2vhd approach

disk2vhd didn't turn up as one of the many options that were presented as I started down this path, but it   ended up being the easiest and quickest of those I tried. I followed these instructions to create the vhd.  Then I used VirtualBox to open the image.  The only thing I had to modify in VirtualBox was this: in System -> Motherboard check the Enable IO APIC box in the Extended Features setting.


The Clonezilla approach

I had space on my system's hard drive that is sufficient but not any flash drives so I backed up using Clonezilla's ssh option to my main box.  This was convenient since I'd be creating the image of the system on the main box anyway.

Before you reference the steps below, understand that I was only able to get to the point where the vm could boot to the safe mode prompt screen, but then hung on any attempt to move forward.  I've got a hunch that trying this with cloning a Linux box would have gone better.

There are several distinct units of work: 
  • prep the source machine
  • create media
  • prepare a repository location for the backup
  • create the clone
  • create a VM from the clone

Prep the source machine

Issues with the source machine

I loaded Clonezilla on a USB drive and ran fine for two partitions (boot and D:/), but not for the main partition where the OS files were installed.  After experiencing issues with booting SpinRite6 from a USB drive that cleared up when using a DVD, I burned the Clonezilla ISO to a physical DVD.  

I then decided to try and make the source image as 'clean' as possible by doing the following:
  • Removing unused files/dirs manually, running disk clean and emptying the recycle bin on the source machine
  • Defragging the source machine
  • Updating BIOS to the latest version provided for my source machine by the manufacturer
  • Running SpinRite on the source machine

Create media

Create Clonezilla disk

Create bootable Clonezilla ISO using Tuxboot.

Create Windows boot disk

I relied upon http://support.microsoft.com/kb/305595 for instructions.  My machine didn't come with any physical media so I needed to harvest the files from C:\.  ntldr and ntdetect.com were in C:/WINDOWS/ServicePackFiles/i386.  boot.ini.backup was in C:\Windows\pss which was good since I used it to confirm the boot.ini name of my operating system.

Tried boot-repair first since screen was blank when I restarted.  After boot-repair I got Windows boot screen, but it just hung part way through the process no matter which option.

Prepare a repository location for the backup

For me the repository location is an Ubuntu 12.10 machine.  It has more space than any external drives I have and it is where I'll be creating and hosting the VM.
  • Install/confirm openssh
  • Create directory /home/partimag on the destination machine and give said directory the appropriate permissions

Create the clone

  • Boot source machine with Clonezilla media
  • When prompted, enter connect info for your destination box
  • Give yourself a few hours if you have an old 80G box you're attempting to clone :)

Create a VM from the clone

  • Create a vm with the same amount (or more) of space
  • Start the vm with the Clonezilla ISO.  Follow the prompts and select restoredisk



Tuesday, February 19, 2013

Solved: Amazon Instant Video won't play on Firefox on Ubuntu 12.10

http://helpx.adobe.com/x-productkb/multi/flash-player-11-problems-playing.html

To summarize:
  1. Install hal
  2. Remove directories from ~/.adobe/Flash_Player
  3. Restart Firefox and watch a video