• Home
  • About
  • Lean Groovy Full Video and an Interview

    December 23rd, 2010

    This week is a bumper week for me. I’m going away for the holidays and I wanted to clear out all the old blog posts I had in the “pending” state. Then both InfoQ and DZone finally go around to publishing a video and an interview respectively.

    First up is an interview I did with fellow JetBrains Academy Member Radoslaw Holewa. In the interview we talk about open source, Groovy, static code analysis, and of course IntelliJ IDEA. Radoslaw and I will both be heading to the 33rd Degree and GeeCon conferences this Spring, so register now and meet up with us!

    Second is a full 90 minute video of my presenting at 2gx. The topic is “Slimmed Down Software – A Lean, Groovy Approach”, based on a series of articles I wrote this year. It’s not a bad performance… but geez 90 minutes is a long time. It’s like trying to perform a one man show. I made the unfortunate decision to include a poop joke about midway through that I kinda regret. Oh well. I doubt I will ever truly grow out of potty humor.

    Enjoy and see you in 2011!


    IDEA X for the Grails Developer

    December 23rd, 2010

    The new version of IntelliJ IDEA was released last week. I already blogged what IDEA X means for Groovy developers, and all of those features are obviously available to any Community Edition user (that’s the free one). This post looks at all the new Grails features available in the Ultimate Edition (the one you pay for). These features are a little harder for me to try out personally as my current project is EJB3 and not Grails (someone save me please!). I’ve tried quite a few of them personally, but feedback is always welcome in case I miss or exaggerate something. Without further ado:

    Grails Aware IDE Features

    Better MVC Rename Refactoring – Related artifacts are now renamed together, such as renaming a view when renaming a controller action, or renaming test cases when the main class changes (IDEA-45378).

    Functional-Test Plugin Support – The Functional-Test plugin got some love, and support is the same as normal unit and integration tests (IDEA-51853). Testing is good.

    Filter Support – There is a new action to easily create filters, code completion of actions and controllers works within the filter mapping code, and method like render() and getRequests() resolve from the filter body (IDEA-60331 and IDEA-60167).

    Mapping DSL Support - In domain class mappings you now get code completion that is aware of HibernateMappingBuilder, and the rename refactoring is aware of the mappings (IDEA-49089.

    Constraint DSL Support – Domain object constraints definitions now have code completion on common constraint names, completion on field names, and is correctly updated when using the Rename refacotring (IDEA-49115).

    Service and Bean Inference – The names and types of services and beans can be inferred in controllers and other artifacts (IDEA-49111 and IDEA-45610).

    GSP and View Improvements

    Easy Internationalization – I love this. There’s a new intention in GSP files to move a static string into a message bundle. Alt+Enter does the trick. IDEA-44846 and IDEA-59519

    Groovy Inspection Support – In the past, only some Groovy inspections worked in GSP files. Support for more of the inspections have been added, like “divide by zero”, “double negation”, and “unresolved expression”. It’s not clear if all inspections are supported, but at least more are IDEA-50414

    Better Variable Resolution – References to controllers and controller properties are now resolved within GSP view pages. I assume this means at least control click navigation and code completion. See IDEA-49731 and IDEA-48692. Also, release notes claim “Completion for value of action/controller parameters in built-in methods redirect(), render(), forward(), chain()”.

    g:fieldValue Support - g:fieldValue tags reference a field. Rename, completion, and find usages should all now work within the tag. IDEA-60858

    Debugging in GSP - Release notes claim “Preliminary debugging of GSP pages”. Not 100% sure what this means in practice.

    JavaScript in Tags – Release notes claim “support javascript in tags like “onSuccess”, “onFailure”, etc”. Again, I’m not sure what this means in practice.

    Find Usages

    Domain class fields - Find usages for domain class fields has been improved. The ticket indicates that usages of fields within GORM dynamic queries are now supported (IDEA-58025).

    Variables from tags – Find usage support for variables defined using a g:set tag within a GSP (IDEA-58185).

    Property Usages - i18n message properties are now included in Find Usages, and the corresponding “unused property” inspections should be working (IDEA-52117).

    Even More Code Completion – Besides what’s already been mentioned, we also have…

    Model Aware GSPs - Related to above, views are now aware of which model is returned from the controller, and autocomplete works for them. More info at IDEA-40926

    Variable Aware GSPs – When you define a variable in a GSP then IDEA now autocompletes it and stops pestering you for missing imports IDEA-50257

    Attribute Aware GSPs – Finally, code completion and navigation exists for the standard attributes: “action”, “controller”, “template”, “contextPath”, “plugin”, and well as variables defined in tags like g:each, g:set, etc.

    And more, and more, and more – There’s practically too many to list. Code completion works in type convertors, in named queries, in URL Mappings and their GSP attribute values, in scope objects like servletContext, session, request, params, and flash (they also appear in the debugger as well), in views references from ‘render’ and ‘redirect’ controller calls, and in hasOne mappings which was missed in the last release.

    Grails Framework Support

    Gant Script Support - IDEA is now aware than .groovy files in the scripts directory are Gant scripts. Targets are completed and imports are resolved better (IDEA-49738).

    Grails Target History - Running a Grails target brings up a dialog box. You can now use up and down arrow to scroll through your history (IDEA-46943).

    Ivy Integration – IDEA reads your Ivy cache and automatically loads source and javadoc jars if they’re available (IDEA-53294).

    UI Improvements – The “Browse to http://localhost:8080/xxx” message in the console now lets you click it to open a browser (IDEA-47166), the test console allows keyboard input in case your tests read from standard input (IDEA-57035), the gutter of controller actions show an icon that lets you jump to the corresponding view (IDEA-49320, and the “New” menu now has context sensitive sorting (IDEA-49086). How’s that for productivity boosting?

    I believe that covers it. If you want a free IDEA Ultimate license then there are ways to get them… speaking at your local Java/Groovy Users Group is usually the easiest, and many JUGs have giveaways if you feel lucky. If your group isn’t sponsored by JetBrains then you can always ask: email jugs@jetbrains.com!

    Happy Holidays everyone. I’m hoping Santa brings IDEA 10.1.


    Beginner Bash: Must-Know Bash Commands

    December 22nd, 2010

    I switched full time to Ubuntu Linux last year and haven’t looked back. In this year I’ve learned to love the Bash shell (which includes the Terminal in Mac and Cygwin on Windows). At this point, I can finally say I’m faster in Bash then I was in Windows Explorer, Commander, Nautilus, or the Windows command prompt; and I prided myself on being a guy with a lot of .bat files. My goal now is to write some tips I learned in the last year. Before explaining the more advanced stuff I want to introduce some of the most basic basics.

    So here it is: "Stuff I wish someone had explained clearly to me a year ago". Almost all these apply to Cygwin in Windows as well as Mac/Ubuntu Terminal.

    1. More than just ls – "ls" lists files. But you’ll be faster if you learn the options and parameters.

    ls -l
    Lists files in long format, sort of like the default on Windows. Some systems have the "ll" command mapped to this by default. If "ll" does not work for you then open up your .bash_profile file and copy this into the last line: alias ll="ls -l". That’ll map ll for you.

    ls -CF –color
    Adds color to your directory listing. Executables, directories, and files are all listed in different colors. Yes, this is way to hard to type and remember every time. So make an alias which forces ls to be colorized. Again, open .bash_profile and stick this in there: alias ls="ls -CF –color". Now all usages of ls get colored.

    ls -a
    By default, ls ignores file starting with a dot (.). The -a option lists these files.

    2. More than just find

    find . -name
    ls does not search subdirectories by default, which the "dir" Windows command did. It takes a while to get used to is, but " find . -name "*file*" " is the same as " dir /s *file* ". It searches all directories for a given file. The period (.) must be escaped to "\.". So to search for MyClass.java do "find . -name MyClass\.java ". Yeah, the quotes are a hassle.

    find -exec
    You can execute a command for each result in the find result set, just use the -exec option along with some crazy notation. For instance, to open all matching files in gedit: " find . -name "*sh" -exec gedit {} \; " Again, kinda crazy syntax.

    Pipe and XARGS
    It’s a basic recipe to know. Here’s how to search all the files in your current directory for the word "mystring": find . | XARGS grep mystring . That’s all the grep I’ve ever needed.

    where
    Don’t know where a file comes from? "where" shows you the path to matching files. For instance "where java" responds with two different locations on my machine.

    3. cp, mv, rm, and all that - I warned you this was a beginners guide.

    cp – copies a file

    mv – moves a file. Please note, there is no rename command. There is only mv.

    rm - deletes a file. rm -rf does it recursively. Scared to run it? Just do an "echo rm -rf" instead to echo everything that /would/ be deleted to the console. Then you can verify that your command is correct.

    mkdir - same as Windows, makes a directory.

    touch - creates a file or updates the timestamp if it exists. Don’t bother with " echo "" > file.txt " just touch it instead.

    cat - This can be used the same as "type" on windows. It prints out the contents of a file to the console. There are tons of other uses for cat as well, but if you’re used to typing "type" then use "cat" instead.

    > and >> - Just like Windows, > pipes output to a file. >> appends to the file.

    4. &&

    Use && to chain two commands together. The second command only executes if the first command is successful. For instance, I use "svn update && svn status" to do them both in a pair. Chaining Grails commands together is nice as well.

    5. Learn some editors - Yeah you can use gedit, jedit, or ultraedit for editing and viewing text. But there are faster options.

    less
    This is a simple, scrolling file viewer. Works great on large files. I use it all the time for looking at svn diffs: "svn diff | less". Good for log files too. It can do more than this, but having scroll up and scroll down is often all you need. q key exits.

    nano
    need to edit something super simple? Nano is not a bad little editor for this. The big advantage to nano is that the arrow keys control your cursor position. I consider that a pretty basic requirement for a text editor.

    vi and vim
    Some people love vi. I don’t. If you’re convinced you must learn it, then check out Ted’s wallpaper and the keyboard stickers.

    6. chmod +x

    If you write a script you’ll want to execute the script. Set it into executable mode with "chmod +x [filename]". You need to do this sometimes to .sh files you download from the Internet as well. Mmmm… feel the bash power.

    7. Scroll with the keyboard - This is simple, but 100 million times better than the Windows console. Shift+PageUp and Shift+PageDown scroll the current terminal window without using the mouse. If you’re using Cygwin then switch to "rxvt". It’s a better shell than the default Windows one.

    8. kill

    kill
    Kills a process. But you need to know the process ID, which you can get with "ps". Hassle.

    ps -el | grep acroread
    Looks for processes matching the name "acroread" and prints out the process ID, among other things.

    pkill
    kills a process based on name. "pkill acroread" kills any acrobat reader processes.

    kill -9
    Kills a process without waiting for it to finish or interrupt properly. I’m sure I butchered the description, so search the Interwebs yourself if you want exact documentation.

    9. know your history - yeah up and down arrow keys still work to scroll through recent commands. No you should never have to resort to up and down arrows.

    history
    Lists out all your recent commands. By default, it lists 500 commands, but you can open up your .bashrc file and edit the HISTSIZE setting to change that.

    history | grep java
    Lists out all the commands you’ve used that have the word Java in it. Nice way to see a list of your recent commands pertaining to a particular program.

    !123
    This executes a recent command. For instance, of the "history" command reveals that "ps -el" is entry number 123, then "!123! will re-execute that command.

    Ctrl+R
    This is reverse search and way faster than history. Type Ctrl+R and then starting typing. Reverse search looks up any matching commands from your history, and you can hit Ctrl+R repeatedly to scroll through matches. For example, my USB stick is mounted somewhere and I always forget the path. So I Ctrl+R, start typing "media" and up comes "cd /mount/dev8/media" or something like that. I can’t remember the exact location because I never need to know it.

    Alt+.
    The Alt+period key combination repeats the last parameter from the last command. If you create a directory with "mkdir mydirectory" then you can type "cd ", press Alt+. and the "mydirectory" part will be filled in. Play with the period, using it is a big help.

    That’s It for Now

    9 tips seems like a good place to stop. I hope this helps someone. I realize I simplified the explanations of many of these commands; I wanted to make a short, useful description for beginners rather than a long, accurate description. If you have other tips or corrections then please leave a comment.

    Next up… an intermediate Bash blog post… stay tuned.


    IntelliJ IDEA X for Groovy Developers

    December 20th, 2010

    The new version of IntelliJ IDEA was released last week. Here’s what it means for Groovy developers. I’ve tried out most of these new features, but there are one or two that I haven’t yet seen in action. Also, there’s a whole slew of Grails features that I’ll cover in a later blog post. Without further ado:

    Groovy Refactorings
    Change Method Signature – This is a big one… change method signature allows you to change parameter order, types, and names on methods. Nice.

    Introduce Constant – Move a local variable or static reference into a private final static field. Mmm… nice. (IDEA-48844)

    Refactoring Map to class – Move that local Map based interface implementation into a class of its own. That’s a good idea I never thought of before. (IDEA-57833)

    “Convert to method” intention – Closures can be converted to methods with Ctrl+Enter. Nice. (IDEA-58056)

    In-place rename for local variables – No more dialog boxes on variable renames. A small thing (but still nice).

    Groovy Language Support Improvements
    Command Expression Support – Groovy 1.8 adds a feature called command expressions. You can leave out periods, commas, and parenthesis in method calls under certain circumstances. IDEA supports the feature today (IDEA-58162)

    Rename File to Match Class – Got a class called MyClass in MYClass.groovy because you can’t work the shift key correctly? This fixes it by letting you synchronize the file name to the class name, which I am always screwing up (IDEA-47090).

    Grape Dependencies Downloading - This is big for @Grab users. If you @Grab a dependency and it isn’t in the IDE classpath then IDEA will download it and automatically configure your IDE. Happy days.

    Optimize Imports on the Fly – Cleans up duplicate imports without running Ctrl+Alt+O manually (IDEA-48793)

    Groovy Script Import Suggestion – There are some small improvements around import suggestions in scripts (IDEA-53182).

    Inspection ‘Create instance of built-in types’ - Hey, they stole this from CodeNarc! Oh well. Don’t instantiate HashMap, ArrayList, etc, just use the Groovy shortcuts like [] and [:] (IDEA-57432).

    Improved Type Inference – “def” types are now inferred correctly from factory methods, not just from constructors (IDEA-59955).

    Type Inference in Tooltips – Hover tooltips now show inferred types. Visual Studio does this for F#, a statically typed language. They use it as an example of superior tooling in a statically typed language [insert .wav file of Nelson saying "HA HA"] (IDEA-49574).

    Gant Ant Task Name Completion – Ant task names now have auto-complete within Gant scripts.

    Groovy++ Support - I haven’t tested it myself, but I’ve heard good things about the level of Groovy++ support in IDEA, including trait support.

    Groovy IDE Improvements
    “Go to | Test” (Ctrl-Shift-T) – This now works in Groovy classes. Press the shortcut in a class to jump to the test. It’s based on file names so MyClassTest is assumed to be the test for MyClass. (IDEA-46886)

    Multiline String Move Line Support – Ctrl+Shift+Up/Down moves code up and down in a batch. If you use this keyboard shortcut then multiline strings are no longer mangled. If you don’t use this keyboard shortcut then you should learn it (IDEA-51514).

    Multiline String Code Folding - Hide those unsightly heredoc style multiline strings with folding (IDEA-56345). I actually never fold anything. If you code is too difficult to understand then it doesn’t need to be folded, it needs to be changed. If you disagree then perhaps this feature is for you.

    Class Hotswapping - I believe this means in practice better debugger support. Not sure how it affects Grails, though.

    Compilation Error Linking - Compilation errors coming from generated Groovy stubs can now be clicked on, and you jump to the Groovy class instead of the stub. If you don’t know about compilation errors in Groovy stubs then consider yourself lucky and be thankful.

    Besides this it was mostly bug fixes and smaller things (except of course for the Java and Grails improvements not listed). Thanks, and see you next time!


    Mockito – Screencasts for Stubbing, Verifying, and Argument Matching

    December 15th, 2010

    We’re rolling out Mockito and trying to raise our testability at work and I’m set to give a presentation/training session tomorrow to a few new teams. In case you don’t know, Mockito is a mock object framework for Java. It’s competitors are EasyMock, JMock, and others if you’re more familiar with those. If you haven’t seen it then you may want to check out my old post “Mockito – Pros, Cons, and Best Practices“. To prepare for my presentation I decided to record myself practicing my material and post it on youtube. Enjoy!

    The first screencast is about creating mock objects and stubbing behavior. These are the absolute basics of mocking.

    The second screencast is about verifying behavior, or verifying side effects, using Mockito. This is a little more advanced but still an essential API in working with Mockito. YouTube reports this video as 13 minutes long, but don’t worry it is only 4:45. Some quirk of YouTube.

    The final screencast is about argument matchers, which add flexibility to your stub and verify phases.

    I hope you enjoyed these. They could be a little more practiced, but I’m happy enough with the quality. I recorded these on Ubuntu 10.4 using recordMyDesktop and mencode to convert from .ogv to .avi.