Archive for March, 2009

JUnit addons

JUnit-addons is a useful library that extends JUnit asserting features. To name a few:

junitx.framework.ArrayAssert: If you’ve written test cases long enough you’ll know that assertEquals won’t work on arrays, and you’ll repeatedly write a loop to assert so. This class takes care of it all.

junitx.util.PrivateAccessor: I’ve always written utility classes to help me set private fields when mocking test objects. Looks like I’m not the only one with the problem.

junitx.framework.Assert.assertNotEquals(): Might be useful in certain cases. Not really recommended because you really want to match the value you’re asserting. It’s too easy to pass a NotEquals() test.

Comments

javax.crypto.Mac not threadsafe

Although not explicit, the usage of the class quite obviously shows that multiple threads shouldn’t use the same instance to calculate a hash.

In particular, even if the update() methods were not used and multiple threads call the doFinal() method together, you are likely to get a bad result (which is worse than an Exception).

Something unit test cases cannot catch.

Comments

PHP XML XPath

The problem with the simple XML-RPC wordpress solution was that it did not have feedback whether the post was successful. I knew I could get the response from curl_exec, but I have to figure out the XPath part. Similarly I was hoping for some ready-made function to do that, but it seems things were not so straightforward. Furthermore, being on PHP4 means I didn’t have the luxury of the newer XML functions such as SimpleXML.

After some fiddling, this was the simplest I could go:


$dom = domxml_open_mem($response);
$faultString = xpath_eval(xpath_new_context($dom), 
	  "/methodResponse/fault/value/struct/member[name='faultString']/value/string/text()");
return $faultString->nodeset[0]->content;

The XPath expression was also a level-up: I had to select a node based on the child data of its sibling.

Comments

WordPress XML-RPC

I wanted a stripped-down “mobile” version of the WP admin interface for posting, but I couldn’t find a suitable one. So while I thought about writing my own, I looked for ready made XML RPC solutions.

Indeed, as I expected, one function is all it takes.


function wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$categories=array(1)){
	$categories = implode(",", $categories);
	$XML = "<title>$title</title>".
	"<category>$categories</category>".
	$body;
	$params = array('','',$username,$password,$XML,1);
	$request = xmlrpc_encode_request('blogger.newPost',$params);
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
	curl_setopt($ch, CURLOPT_URL, $rpcurl);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 1);
	curl_exec($ch);
	curl_close($ch);
}

I made a small HTML form and submitted it to this PHP function, and that’s it! 15 minutes worth of work.

Comments