Archive

Archive for the ‘howto’ Category

Clearing Console in Cygwin

September 9th, 2009 Flo 1 comment

Just a short one but hopefully helpful!

I’m working a lot on Windows at the moment but I have Cygwin installed as Unix environment, e.g., for working with git scm.
One thing I thing I was missing is that I could not clear the console with “cls” which was pretty annoying.

After doing a little research I figured out how to fix that.

1. using CTRL – L
2. defining an alias like

alias clear='cmd /c cls'

If you just type it in a cygwin shell it will only be available for the current session.

If you want to make this alias persistent you need to edit ~/.bashrc with your favourite editor and add it.

You will also find many other predefined aliases there which are commented out.

Categories: howto Tags:

Unit Testing Delete Actions in Grails

September 4th, 2009 Flo 6 comments

I’m quite new in Groovy and Grails and wanted to share some of my lessons learned while
writing my first unit tests in Grails.

Fortunately at the time I started unit testing in Grails the Testing Plugin had already moved into Grails core.
The documentation is quite good so it’s a good idea to start there.

Today I had a little problem testing the delete method on a Controller. Let’s say I have a Domain class Song which looks like

//  Song.groovy
class Song {
    String name =""
}

I have a method in my Controller that deletes a Song like the delete action from static scaffolding. After calling the action I want to make sure that my count of Songs has decreased.
My first attempt looked like:

// SongTests.groovy
class SongTests extends GrailsUnitTestCase {

    def testSongs
    Song s1, s2

    protected void setUp() {
        super.setUp()
        s1 = new Song(name: "Song 1")
        s2 = new Song(name: "Song 2")
        testSongs = [s1, s2]
        mockDomain(Song, testSongs)
    }

    void testDelete() {
        int oldSize = testSongs.size()
        s1.delete() // Note: Normally this would happen somewhere in the controller action.
        assertEquals oldSize - 1, testSongs.size()
    }
}

But this test struggles to pass. I did some println’s and looked at the testSongs List and noticed that it did not change. After doing some research I found the cause of my problem in MockUtils.groovy

//  MockUtils.groovy
....
    static GrailsDomainClass mockDomain(Class clazz, Map errorsMap, List testInstances = []) {

        ....
        def rootInstances = testInstances.findAll { clazz.isInstance(it) } // findAll creates a new List!!!
        def childInstances = testInstances.findAll { clazz.isInstance(it) && it.class != clazz }.groupBy { it.class }

        TEST_INSTANCES[clazz] = rootInstances
        addDynamicFinders(clazz, rootInstances)
        addGetMethods(clazz, dc, rootInstances)
        addCountMethods(clazz, dc, rootInstances)
        addListMethod(clazz, rootInstances)
        addValidateMethod(clazz, dc, errorsMap, rootInstances)
        addDynamicInstanceMethods(clazz, rootInstances)
        addOtherStaticMethods(clazz, rootInstances)
...
}

private static void addDynamicInstanceMethods(Class clazz, List testInstances) {
...
        // Add delete() method.
        clazz.metaClass.delete = { Map args = [:] ->
            for (int i in 0..<testInstances.size()) {
                if (testInstances[i] == delegate) {
                    testInstances.remove(i)
                    break;
                }
            }
        }
...
}

The problem was, that findAll creates a new list. Since delete just removes the element from the internal list, the original list keeps passed in as second parameter to the mockFor method stays
untouched.

With this in mind the updated test looked like:

    void testDelete() {
        int oldSize = Song.count()
        s1.delete()
        assertEquals oldSize - 1, Song.count()
    }

Which works perfectly.
If you are just getting started too I would recommend you the following sites which give a good introduction to this topic:

The Definitive Guide to Grails Book from Graeme Rocher and Jeff Brown:
http://www.amazon.de/Definitive-Guide-Grails-Experts-Voice/dp/1590597583

Glen Smith’s Blog – MockFor(March) Series:
http://blogs.bytecode.com.au/glen/2008/03/04/mockfor-march—overcoming-grails-testing-inertia.html
http://blogs.bytecode.com.au/glen/2008/03/07/mockfor-march—unit-testing-grails-taglibs.html
http://blogs.bytecode.com.au/glen/2008/03/12/mockfor-march—unit-testing-grails-controllers.html
http://blogs.bytecode.com.au/glen/2008/03/27/mockfor-march—unit-testing-grails-services.html

Groovy Testing guide:
http://docs.codehaus.org/display/GROOVY/Testing+Guide

Make.Go.Now – Ode to MockFor(March)
http://www.make-go-now.com/2009/03/05/ode-to-mockformarch-part-1-testing-constraints/
http://www.make-go-now.com/2009/04/17/ode-to-mockformarch-part-2-mockdomain/

IBM developerWorks:
Mastering Grails: Testing your Grails application
Mastering Grails Series

Delicous in general and my stream
http://delicious.com/
http://delicious.com/fsalbrechter

If you have any suggestions or found an error I’d be happy if you drop me a line.

Categories: grails, groovy, howto Tags: , , ,

Restore Last Viewed Page in Adobe Reader and Foxit Reader

August 29th, 2009 Flo 3 comments

Have you ever had the problem that you can’t remember at which page you stopped reading a PDF?

There’s a nice option in Acrobat Reader:

Edit -> Preferences -> check Restore last view settings when reopening documents

Some other settings like zoom are also restored.

This feature can be activated in Foxit Reader too:

Edit -> Preferences -> General -> History Group -> check Restore last view settings when reopening

Categories: howto Tags: , , , ,

Disabling MySQL Foreign Key Constraints Checks

August 27th, 2009 Flo No comments

Today I had to update lots of rows in a table with a lot of foreign key constraints. The first try was to delete all records and insert the updated data resulting in:

Cannot delete or update a parent row: a foreign key constraint fails (`foo/bar`, CONSTRAINT `FKED8DCCEF810F075A` FOREIGN KEY (`widget_id`) REFERENCES `widget` (`id`))

Instead of dropping all constraints, inserting the data and adding them again afterwards, there’s another more convenient method:

You can disable the foreign key checks with:

SET FOREIGN_KEY_CHECKS = 0

Enabling them again afterwards:

SET FOREIGN_KEY_CHECKS = 1

Categories: howto Tags: ,

Brother HL 2030 on Ubuntu Intrepid

March 28th, 2009 Flo No comments

I recently had problems with printing. The printer stopped after each page for a short period and started printing the next page after a short delay. This slowed down printing a lot and was very annoying. I use Ubuntu 8.10 and Brother HL-2030.

You can use the HL-2060 printer driver, but he automatic installation choses Brother HL-2060 Foomatic/hpijs. I found a description of this driver at http://www.linuxprinting.org/show_driver.cgi?driver=hpijs, which says that it’s a HP driver.

After switching to Brother HL-2060 Foomatic/hl1250 as recommended, the printing works as expected.

This is a documented bug as well see e.g.: https://bugs.launchpad.net/ubuntu/+source/foomatic-db/+bug/137580

T61p Ubuntu Enable Trackpoint Scrolling

March 8th, 2009 Flo No comments

Nach kurzem Arbeiten mit Ubuntu habe ich festgestellt, dass mein geliebter Trackpoint nicht ganz so funktioniert.

Leider kann man standardmäßig nicht mit der dritten Maustaste + Trackpoint scrollen.

Nach kurzer Recherche hier die Lösung: http://www.thinkwiki.org/wiki/Installing_Ubuntu_6.04_on_a_ThinkPad_X41

Categories: Allgemein, howto Tags: , ,

Ubuntu wiederherstellen

March 8th, 2009 Flo No comments

Als bei meinem Versuch die IBM Recovery Suite zu installieren mein System aufeinmal nicht mehr bootete habe ich es klassisch mit fixmbr wiederhergestellt.

Leider ging dabei auch mein GRUB bootloader verloren. Es gibt aber einen einfachen und sicheren Weg das wiederherzustellen: https://help.ubuntu.com/community/RecoveringUbuntuAfterInstallingWindows

Categories: Allgemein, howto Tags: , ,