Thursday, 27 October 2011

Sorting out project's SVN location in Eclipse after moving the project in SVN

Just ran into an issue where I used Subversive's "SVN Repository Browser" view to move a project in SVN back one level. I had this project checked out into my Eclipse workspace, but now it was pointing to a location in SVN that no longer existed.

I could not find a way to change the URL the project was looking for through Subversive. I first tried "Team->Disconnect" on the project, which removed it from SVN. Then, Team->Share Project gives you a dialog to choose the SVN repository URL; however the input field was read-only and I could not change it.

After a little bit of googling, I came across this blog post detailing how to edit the SVN meta data to point to the right URLs.

The script in the comments seemed nice, and trying it out worked great. I'd close the project in Eclipse first just to be safe.

Execute this in the top level of your SVN project:


find . -name entries -type f -exec sed 's@svn+ssh://blah.com/your/old/location@svn+ssh://blah.com/your/new/location@g' -i {} \;

Sorted!

Restoring Ubuntu 11.10's "print screen" behaviour

After upgrading from Ubuntu 11.04 to 11.10, I noticed that the behaviour of the print screen key had changed. Previously, hitting the key would pop up the "Take Screenshot" application, which presented options to grab the whole screen / the current window or to select a region to capture.

In 11.10, hitting print screen simply captures the whole desktop without prompting you for the other options. I often use the 'select region to capture' function to capture screenshots of our websites when reporting a presentation-related bug.

However, I managed to revert the functionality back to the way I liked.

Install compizconfig-settings-manager:
sudo apt-get install compizconfig-settings-manager


You can find it installed into Applications -> Other. Launch the CompizConfig Settings Manager and then check the "Gnome Compatibility" checkbox to enable the plug-in.



Then click the plug-in itself, go to "Commands" and change "Screenshot command line" to
gnome-screenshot -i
The -i flag is for "interactive mode", which prompts you for the type of screenshot you would like to take.



The old functionality is now restored. Brilliant!

Thursday, 20 October 2011

Grails unit tests and metaclass manipulation

There's some information out on Google regarding this issue I just ran into, but I figured a quick blog post can't hurt things :)

Problem:
- Manipulating a class' metaclass will affect that class' behaviour for every test method in that test class.
- We need to be able to remove our meta class changes after every test is executed to avoid test pollution: data setup for one test should not interfere with other tests.

How do we fix this?


import grails.test.*

class StaticClass {
public static String blah() {
return "blah."
}
}

class SomethingTests extends GrailsUnitTestCase {
def testOne() {
def result = StaticClass.blah()
assertEquals("blah.", result)
}

def testTwo() {
StaticClass.metaClass.'static'.blah = { "test" }
def result = StaticClass.blah()
assertEquals("test", result) // test pollution
}

def testThree() {
registerMetaClass(StaticClass)
StaticClass.metaClass.'static'.blah = { "something" }
def result = StaticClass.blah()
assertEquals("something", result)
}

def testFour() {
def result = StaticClass.blah()
assertEquals("test", result)
}
}


By extending GrailsUnitTestCase, we can call "registerMetaClass" (which unfortunately is not available in GroovyUnitTestCase) and pass the class name that we will be performing metaclass changes on. Grails will then automatically revert any metaclass changes that happen to this class after every test.


Important note: you must call this.setUp() if you define a setUp() method in your test case.

You can see in testTwo that the static metaclass change in testOne has persisted between tests. In testThree we do the regsiterMetaClass magic, which then resets the static method's return value to what we previously overwrote it with in testOne.


class AFewOtherTests extends GrailsUnitTestCase {
def testOne() {
registerMetaClass(StaticClass)
StaticClass.metaClass.'static'.blah = { "test" }
def result = StaticClass.blah()
assertEquals("test", result)
}

def testTwo() {
def result = StaticClass.blah()
assertEquals("blah.", result)
}
}