Archive for ‘HowTo’

January 22nd, 2010

ResultSet Mocking with JMock

by Tim Cull

I found myself recently wanting to mock out a whole mess of database interaction on a legacy system. This system didn’t have a strict data access layer, so direct calls to the database were strewn throughout the business logic.

Because JDBC is such a verbose library, mocking it out can be a challenge. For this task, I found myself with horrific-looking mock methods like this:

       private void mockSpecificPeopleQuery() throws SQLException {
		final PreparedStatement stmt = context.mock(PreparedStatement.class, "specificpeoplePreparedStatement");
		final ResultSet rs = context.mock(ResultSet.class, "specficpeopleResultSet");
		final Sequence rsSequence = context.sequence("specificpeople");
		context.checking(new Expectations() {{
			one(this.dbConnection).prepareStatement("SELECT mbid, people_id, name FROM specific_people"); will(returnValue(stmt));
			one(stmt).executeQuery(); will(returnValue(rs));
			one(rs).next(); inSequence(rsSequence); will(returnValue(true));
			one(rs).getString(1); will(returnValue("mbidCher"));
			one(rs).getInt(2); will(returnValue(2)); //"people_id" column
			one(rs).getString(3); will(returnValue("Cher")); //"name" column
			one(rs).next(); inSequence(rsSequence); will(returnValue(false));
			one(rs).close(); inSequence(rsSequence);
			one(stmt).close(); inSequence(rsSequence);
		}});

	}

I thought there had to be a better way. I remembered and was inspired by a colleague of mine (Denis) who had once nicely encapsulated all this in a helper class. So I wrote myself a simple extension to the JMock Expectations class that makes mocked-out ResultSets a whole lot easier to read, more like this:

November 3rd, 2008

HowTo: Sort Outlook Email With Keyboard Shortcuts

by Tim Cull

I finally broke down and set up a macro to let me sort through my morning email without actually touching a mouse. It’s not exactly rocket science, but it does require a quick Google search, which I hope to save you here:

Step 1: Copy and paste a macro off the Internet like this one for moving an Outlook email to a different folder:

Step 2: Set up a custom Outlook tool bar and name it so it has a keyboard shortcut

Step 3 (possibly optional): Digitally sign your Outlook macro. I didn’t actually have to do this step, though it seems like I should have had to.

I got a good start on these steps from this post about creating Outlook email shortcuts, but its link to a macro is broken.

September 14th, 2008

How to Add Two Wireless Access Points to Your Home

by Tim Cull

I have a long, skinny house. It’s so long that one wireless access point doesn’t quite reach from one end to the other. It doesn’t help that all my neighbors have their own in one great storm of EM radiation.

So, I have to have two wireless routers. The problem is that most consumer routers are set up by default to assume they’re the only one on your network and that they are the ones connecting to your ISP. After much struggling, I found some directions on how to add a second wireless access point to your network.

The one thing that I have to add is if you have a switch or hub between your two wireless routers (as I do) then you need to make sure your “Internet side” router is connected to your switch with a cross-over cable to the “uplink” port and your other router is connected to your switch with a normal cable to a normal port.

July 1st, 2008

TimeMachine and Linkstation NAS: How do I get thee to reconcile?

by Tim Cull

I recently bought my very first Mac ever. After years of struggling with crappy movie editing software, draining system resources and general bloat on Windows I finally bought myself a beautiful 24″ iMac and so far the experience has been every bit the bundle of awesomeness I’d hoped it would be. Except for TimeMachine.

I’ve got a 500GB Buffalo Linkstation I used for backup and for storing music files for a year. It’s a great machine that just works and serves my two laptops and a desktop very well. The problem is that the backup software what comes with it only works on Windows.

Crap.

So, rather than let my Linkstation become an expensive bookend, I hit the Internet to find an answer and wasn’t entirely pleased with the options:

1) Turn on this undocumented and apparently not very stable feature on OS X.

2) Rebaseline my Linkstation with an “opened” firmware and use rsync.

3) Purchase some kind of third party backup software for Mac like SuperDuper! that does backup instead of TimeMachine

I went with option 2. Yeah, I know option 1 sounds easier, but the stuff I read really made it sound unstable and like a waste of time, and anyway the geek in me was really warming to the idea of a Linkstation that was basically a really cheap Linux computer with a giant hard drive.

Many hours later I’m ready to declare success. So what if I could have just bought a couple of TimeMachine devices (at the same hourly-billable equivalent rate). Instead I’d learned something new that reinforced my technical chops.

So dear readers, here’s how you do it. Specifically, here’s how you backup from an iMac running OS X 10.5.2 to a Buffalo Linkstation Pro LS-GL running on an ARM processor. With any other combination your results may vary so make sure you read up before you start.

Backup all your data
The stuff you’re about to do has the potential to brick your Linkstation if you screw it up. Yes, brick it, as in won’t turn on, unrecoverable, gone, expensive book-end. There are some instructions on un-bricking a Linkstation but thankfully I didn’t have to try them out.

Download the jtymod firmware for Linkstation
Some kind soul created a hacked firmware called jtymod that is basically the standard firmware with telnet, ssh, and a few other necessary services enabled. Make sure you get the “fixed” jtymod version not the regular jtymod version. I don’t know what the fix was for, but the forum posts I read made it sound pretty necessary. Once you’ve downloaded it, you just unzip it and you have both the installer and the binary images you need to install. This part worked like a dream for me so I don’t have any advice for troubleshooting. If you run into trouble, the Linkstation hacker wiki is an excellent resource.

Reset the Root Password

You should see an option to reset the root password on your linkstation now as shown in this screen shot. After you clear the password, you should be able to set it again by logging in as “root” with a blank password and then changing it:

ssh root@192.168.1.1

passwd root

Install it using the included installer exe
Took me a while to figure out the installer was included in the zip file. I looked all over Google for general instructions on “how to install firmware images on a Linkstation” before I finally figured out I didn’t need them. Just run the exe included in the firmware zip file. Maybe you’re smarter than I am and figured that out yourself.

Write a simple shell script using rsync
The BackupUsers.sh shell script is stone simple:

#!/bin/sh
rm /Users/trcull/Documents/BackupUsers.stdout
rm /Users/trcull/Documents/BackupUsers.stderr
echo "Starting BackupUsers.sh" > /Users/trcull/Documents/BackupUsers.stdout
date >> /Users/trcull/Documents/BackupUsers.stdout
sleep -60
rsync -va /Users admin@192.168.1.6:/mnt/disk1/macbackup &> /Users/trcull/Documents/BackupUsers.stdout

You can see some much more involved examples on the rsync site and on the Linkstation wiki rsync page.

Setup some shared keys so you don’t have to type a password
If you want this job to run unattended, you have to set up some shared keys between your Linkstation and your Mac.
Configure it to run using launchd
Apple has decided to replace crond with something called launchd. There’s a decent writeup for using launchd to get you started. I ended up creating a plist file that looks like this:

< ?xml version="1.0" encoding="UTF-8"?>
< !DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Debug</key>
	<true />
	<key>ExitTimeOut</key>
	<integer>7100</integer>
	<key>Label</key>
	<string>net.kroppel.BackupUsers</string>
	<key>OnDemand</key>
	<false />
	<key>Program</key>
	<string>/Library/Scripts/CustomScripts/BackupUsers.sh</string>
	<key>StartInterval</key>
	<integer>7200</integer>
	<key>ThrottleInterval</key>
	<integer>7000</integer>
	<key>UserName</key>
	<string>trcull</string>
</dict>
</plist>

You may wonder why I have a ThrottleInterval in there. That’s because my rsync exits with a code 23 because it doesn’t have permissions on some of the directories it’s trying to copy. This causes launchd to think the script failed and needs to be spawned again. It took me days of my Linkstation constantly thrashing on its drive before I realized what was going on.

Done!
Now you’re in backup heaven. Still glad you didn’t just give in and buy a Time Capsule instead?

Tags: ,