All atomic-powered posts filed in “Tools”:



Testing C# code that is run as the result of a event being fired

Or, How to test your Presenter classes

A couple of colleagues and I recently finished up a project for a client that involved a lot of C# code. Our unit testing tools of choice were NUnit and Rhino Mocks. The NUnit choice was a pretty easy one (although there are other platforms out there), but we spent a little more time choosing a mocking library.

Our first choice was NMock, primarily because we had experience using it on previous projects and we knew it could get the job done. Before too long, though, we switched to using Rhino Mocks. The primary reason for the change (if I am remembering correctly) was because of its superior event handling capabilities, and we grew to prefer its explicit use of the record-playback-verify mocking model.

Rhino Mocks was one of the first libraries to directly support event registration and event raising by mocks (at least, it was the first we had used). This was a big advantage over having to add a SubscribeEvent() method to our view and model interfaces and having to use syntactically obscure paradigms to capture and fire the event raisers. With Rhino Mocks we could add public events directly to our interfaces and (fairly) explicitly capture the event raisers.

Read the rest of this entry

Rolling a JRuby desktop application

Atomic has been using JRuby, several Java libraries, and Jar Jar Links to distribute five different Ruby desktop applications (read: not Rails applications) as single, executable jar files. In this article, I will show how we:
  • Work on our application as developers
  • Distribute our application as a single jar file to our users
  • Compile the Ruby source files into Java class files
  • Integrate third-party Ruby and Java libraries

This article includes an example project that demonstrates all of the above.

Read the rest of this entry
Filed in: Technologies, Tips, Tools

Ruby and Unicode Win32 MessageBoxes

Have you ever needed to display Unicode characters in a Win32 MessageBox from a Ruby script? My pair and I needed to do just that and so I thought I would share what we found.

There are a number of ways to access Win32 calls from a Ruby script. The code we were working with did the following:

1
2
3
4
5
6
7
8
9
require 'dl'
def show_message_box(message, title)
  mb_ok = 0
  mb_iconexclamation = 48
  
  user32 = DL.dlopen("user32")
  message_box = user32['MessageBoxA', 'ILSSI']
  message_box.call(0, message, title, mb_ok | mb_iconexclamation)
end

That works well until you need to display Unicode characters in the MessageBox. It turns out the MessageBoxA version of the function is for ASCII characters. There is another version of the API call, MessageBoxW, that can handle Unicode, or wide characters. So the issue becomes converting your Ruby string into a wide string so it can be passed to MessageBoxW. The MultiByteToWideChar Win32 call can do this for you. And the windows-pr gem (from win32utils) adds a nice ruby wrapper around the function.


gem install windows-pr
1
2
3
4
5
require 'windows/unicode' 
include Windows::Unicode 
$KCODE='UTF8'

str = multi_to_wide("This is a test")

This seemed to work quite well for the most part. However, from time to time we would see garbage text showing up at the end of our messages. It could easily be reproduced if the message was very short.

Having done enough C/C++ coding to recognize a string that was not being null terminated, we experimented with adding null characters to the end of the string. It turns out a wide null terminator (”\0\0”) is needed. The following code will properly display Unicode characters in a Win32 MessageBox:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
require 'dl'
require 'windows/unicode' 
include Windows::Unicode 
$KCODE='UTF8'

def win32_wide(text)
  multi_to_wide(text) + "\0\0"
end

def show_message_box(message, title)
  mb_ok = 0
  mb_iconexclamation = 48
  
  user32 = DL.dlopen("user32")
  message_box = user32['MessageBoxW', 'ILSSI']
  message_box.call(0, win32_wide(message), win32_wide(title), 
                             mb_ok | mb_iconexclamation)
end

show_message_box "MessageBox displayed from Ρουμπίνι", "¡Alert!"
Filed in: Languages, Tips, Tools

VMware server control scripts

VMware’s free VMware Server product ships with several command line utilities for controlling the server; the vmware-cmd program, in particular, offers control over starting, stopping, registering, and what not on virtual machines. vmware-cmd is a little clunky to use, though, because it requires an absolute path to the virtual machine’s vmx file. And it only works against one virtual machine at a time.

So I made some Ruby scripts to make vmware-cmd a little easier for me to use. Here’s what I’ve got so far:
  1. list: lists what virtual machines are running
  2. register: registers a virtual machine
  3. reset: resets a virtual machine
  4. start: starts a virtual machine
  5. stop: stops a virtual machine
  6. unregister: unregisters a virtual machine
  7. what_is_registered: lists the virtual machine registered on this server
  8. what_is_not_registered: lists the virtual machines in the virtual machine directory that are not registered

The register, reset, start, stop, and unregister commands all take a list of virtual machines to act against. For example: “register piggy” will register the piggy virtual machine “stop beaker piggy tortoise” will stop the beaker, piggy, and tortoise virtual machines

To use these scripts, you need:
  1. VMware server installed and running on Linux. I doubt the scripts will work on Windows.
  2. Ruby installed and on your path
  3. Edit the vmx_control.rb file and change the VmLocation constant, at the top, to contain the location your virtual machines are installed in.

These scripts are unsupported. They are made to work best with my workflow, so if my workflow does not match yours, then modify the scripts however you’d like to make them work best for you.

Download link: vmx_controltar.gz

Filed in: Tools

Presenter First Modeling Tools for Visual Studio

Brooke Hamilton has developed some of his own extensions to Presenter First and a cool-looking visual modeling tool to match. We at Atomic have not put it to use yet (none of us is using Visual Studio 2008) but it sure looks snazzy.

The tool and its source code are hosted on CodePlex: A Modeling Tool for the Presenter First Pattern

Also linked from that page is a bit of explanation regarding Brooke’s take on PF, setting it up for DSL use. I have not made use of this derivation of Presenter First (I tend to use my Presenters as the place where I describe my features—the one part of my application that cannot be auto-generated) but Brooke’s tool and approach have been working well for him, and as he says, “it’s a lot of fun, too.” Give it a read.

SkipJack gateway for ActiveMerchant

We just submitted a SkipJack payment gateway implementation for the ActiveMerchant project. ActiveMerchant is a Ruby library for dealing with credit cards, payment processing and shipping. It’s currently the most popular payment processing library for Ruby. SkipJack is a payment processing and financial services company.

Our SkipJack gateway is now available in the trunk of the ActiveMerchant repository.

Cody Fauser (ActiveMerchant maintainer) also announced Shopify’s support for the new SkipJack gateway.

The gateway supports all the standard features of all ActiveMerchant gateways: authorization, purchase, capture, void, credit and status operations. More details are available at the original submission post.

Filed in: Announcements, Tools

Roll Your Own respond_to

While refactoring some ruby code the other day I found myself wanting respond_to-like functionality. Specifically, I was calling a function that would have one of several possible outcomes, and I wanted to handle each in a clean way. Specifically, I wanted syntax like this:

1
2
3
4
5
6
7
8
zebra array do |row|
  row.even do |element|
    puts "even #{element}"
  end
  row.odd do |element|
    puts "odd #{element}"
  end
end

I put together a bit of code for doing this, called Multiblock. You could write zebra, with a Multiblock, like so:

1
2
3
4
5
6
def zebra(list)
  list.each_with_index do |element, index|
    odd_or_even = [:even, &odd][index % 2]
    yield Multiblock[odd_or_even, element]
  end
end

Here’s the source code for Multiblock:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Multiblock
  def self.[](*args)
    new *args
  end

  def initialize(*args)
    _expect *args unless args.empty?
  end

  def [](*args)
     _expect *args
     self
  end

  def method_missing(name, *rest)
    if !@matched
      @matched = [@waiting_for, :else].include? name
       if @waiting_for == name
          @result = yield *@args
       elsif :else == name
         @result = yield @waiting_for, *@args
       end
     end

      @result
  end

  private

  def _expect(waiting_for, *args)
    @waiting_for = waiting_for.to_sym
    @args = args
    @result = nil
    @matched = false
  end
end

Filed in: Languages, Tips, Tools

Some useful selenium helpers

Wouldn’t it be nice if you could evaluate arbitrary javascript in the context of your Rails application’s window when testing it with Selenium? Well, you already can. What Selenium doesn’t do for you, however, is automatically serialize the result of your computation to JSON, then deserialize that JSON into a convenient ruby object. What I want to do is stuff like this:

1
2
3
# get the center of the page's google map 
lat, lng = get_json "[c.lat(), c.lng()]",
        :c => "googleMap.getCenter()" 

or

1
2
positions = get_json "{foo: $('foo').offsetLeft, bar: $('bar').offsetLeft}"
assert positions['foo'] < positions['bar'], "bar was too far to the right"

Here’s the definition of get_json:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
   # evaluate arbitrary javascript in the context of your page. 
   def eval_js(code)
     get_eval <<-JS
       selenium.browserbot.getCurrentWindow().eval("#{
         code.gsub('"', '\\\\"').gsub("\\n"," ")
       }") 
     JS
   end

   def get_json(code, objects={})
     entries = objects.entries
     ActiveSupport::JSON.decode(
       eval_js("
         Object.toJSON(
           (function(#{entries.map (&:first).join(', ')}){
             return #{code};
           })(#{entries.map(&:last).join(', ')})
         )
       ")
     )
   end
Filed in: Languages, Tips, Tools

The Making of a Mock Object Generator for C++

About a year ago Greg Pattison and I began working together on a new C++ application for a client. It was the first C++ project of any significant size for either of us since becoming enamored with interaction-based testing techniques.

Read the rest of this entry

RubyConf 2007: Enhancing Embedded Development with Ruby

I’ve just given this presentation at Rubyconf 2007. And now you can enjoy it too:

Keynote file: Enhancing Embedded Keynote

PDF version: Enhancing Embedded PDF

Video: Full Video from Confreaks

Using deepTest to Speed Up Selenium Tests

Project XXX has 37 selenium tests with 405 assertions in 19 test files. All integration testing is done with Selenium.

Using deepTest I was able to decrease run time from 317 seconds down to 116 seconds using 4 processes.

Read the rest of this entry

Atomic at O'Reilly's Open Source Convention 2007

Bill Bereza and Matt Fletcher will be presenting 'Improving the Embedded Development Process' at OSCON this summer. The talk will focus on our experience building the Ruby tools that made embedded development effective and fun for us.

Check out the abstract here. If you're interested, consider coming to OSCON. Aside from our presentation, which will surely be the best thing ever, there will be plenty of other interesting talks. A complete OSCON schedule can be found at this link.

oscon banner

Rich Internet Application Platform Shoot-out

I’ve been tracking the Rich Internet Application (RIA) framework technology scene lately.

That’s a broad category. As the technology is put to use, its applicability grows into other domains. Mobile or otherwise-embedded devices, set-top boxes or game consoles, tables, and stand-alone or kiosk applications are all targets now. Web applications are still the largest niche for RIA platforms, so I’ve compiled a list of the web-oriented technologies for comparison. Since I most enjoy writing web applications in Ruby, I’m tracking the way each platform supports Ruby integration—specifically Ruby on Rails.

Here are the contenders, in order of fitness for web application development, according to my own opinion:

Read the rest of this entry

Migration Testing in Rails

Migrations are one of the most useful additions Rails provides. Knowing that you can rely on them working is good too. Recently we've released a plugin, migration_test_helper, which makes it easier to test the migrations you create for a Rails app individually and as a whole.

1
2
 ./script/plugin install
  svn://rubyforge.org/var/svn/migrationtest/tags/migration_test_helper
Read the rest of this entry

JSMock - a mock object library for JavaScript

There are a growing number of developers doing TDD with JavaScript which is why unit testing frameworks like JsUnit exist for it. Sadly though, while many languages have had mock object libraries for years, JavaScript has had none. Hence the birth of JSMock, a mock object library I wrote for JavaScript that fills the ‘mock object’ void in JavaScript Test Driven Development.

Read the rest of this entry