Thursday, June 12, 2008

Can't send love over ssh


Today I tried to send some love via ssh (from windows) and it failed. I'm guessing ssh isn't a creature of the heart.

Monday, June 9, 2008

Protocopter!

It's like flying... only you're coding.


So... recently Coda (I'll refer to him using his internet identity), and I have decided to fork off of LOLCode (we were running into stumbling blocks currently) and implement a language with all the features we've been designing for lolcode, except with a friendlier, less-ambiguous syntax for parsers (so we could write one easily). This of course would then let us source-to-source LOLCode files into our emerging interesting language. Hence, the birth of Protocopter.


<Disclaimer> Coda and I are performing the fork, however all LOLers are welcome. Coda and I cannot steal credit for what the community has helped put together. We feel we can accomplish a LOLCode implementation easier if we have a language that has less dissonance with the LOLCode spec.

Protocopter is still in it's fetal stage, as Coda and I debate over various syntax issues. Please bear that in mind. So far, protocopter is a dynamic language with Prototype object orientation, and an interesting lisp-like syntax. </Disclaimer>


Here's some sample syntax from our recent discussions/draft:


[Object : either] <- function(lhs, rhs)(!if lhs true rhs)

[test] <- true
[test2] <- false

(!if (!either test test2) {
! console:print "One of these is true!"
})


A few notes here:
  • ! is the "execute" operator, and calls a function

  • : is the scope operator, and allows you to reference another scope

  • Object is the base-class prototype of ALL objects in protocopter. Scopes are also objects, so therefore the either function shows up in all scopes

  • We're debating the "function" syntax, but you can see the idea is, function takes an argument list and a lambda-expression (or s-expresion or whatever you want to call it).
  • { statement-block } defines a closure.

  • [ identifier ] is the allocation operator. It will allocate a "slot" on the current scope with the given name.



On a side note, recently I've been having a lot less free time (and a lot more random things to take care of at home). Instead of making dumber posts (like some might consider a lolcode post), I'm going to try to make sure this blog always has the best content I can provide, it just might be less frequently. Hope all [2,10) of you enjoyed this!

Tuesday, June 3, 2008

EasyMock for unit testing

So at work I wanted to test my EJBs and ended up using EasyMock to accomplish this. I used EjbMock to create a fake JNDI Context, and then bind mock remote interfaces to the jndi context. It's pretty standard mock stuff, but it was fun to set up and use. Anyway, I just wanted to cover slightly more advanced EasyMock usage.

First off, The best part of EasyMock is that:
  • you can stub out interfaces really easily (using a DSL in fact)
  • You can test arguments to those stubs to ensure they're correct.


So, I'll just go over the process I used to make sure a Map matched correctly what *should* have been sent to an interface.

My first take at a mock call was this:

//Set up arguments to mock call
HashMap expected = ...

//Set up return type
SomeType myResult = ...


MockClass mock = createMock(MockClass.class);
expect(mock.someCall(expected)).andReturn(myResult).once();

replay(mock);

//Perform actions
...

//Verify Mocks were used correctly
verify(mock);

//Verify Results
...


This would have worked great, except the logic I was testing would create the Map passed into "someCall", therefore EasyMock kept complaining that the map wasn't the one expected. My first thought was to replace "expected" with "(Map)anyObject()", however this would allow any map to be passed in. Well, how can I make sure my logic is passing the right map then?

The solution is to use something called a IArgumentMatcher. Here's how it's done.

First we upgrade our source class:

package not.your.package;

import static not.your.package.CollectionTestingUtil.*;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;

import java.util.*;
import not.your.package.*;
import org.junit.Test;

class TestClassUnderTest {
@Test
public void testSomethingUseful() {
//Set up arguments to mock call
HashMap<String, String> expected = ...;

//Set up return type
SomeType myResult = ...

MockClass mock = createMock(MockClass.class);
expect(mock.someCall(eqMap(expected))).andReturn(myResult).once();

//Perform actions
...

//Verify Mocks were used correctly
verify(mock);

//Verify Results
...
}
}



That shouldn't compile yet because the CollectionTestingUtil class doesn't exist. Well, let's make this guy, as he contains all the real magic.

First of, we make a CollectionTestingUtil class. We throw a static "eqMap" method on it (to look like all the other EasyMock calls). The idea being that eqMap will register some class that can verify the arguments to the mock call are correct.

As I'm lazy, we'll pass the arguments into the static method into an Anonymous Inner Class that implements the IArgumentMatcher interface.

The IArgumentMatcher interface has two methods you need to subclass. One for pretty-printing (appendTo) and the other to actually perform the test (matches). The matches method will give you the parameter passed to the mock class and let you determine if it's "up-to-snuff". Below is my CollectionTestingUtil.


package not.your.package;
import static org.easymock.EasyMock.reportMatcher;
import java.util.Map;
import org.easymock.IArgumentMatcher;

public class CollectionTestingUtil
{
/**
* Helper method to verify that a map contains all the same keys/values
*
* @param in
* The input map.
* @return
* The value for the call.
*/
public static <S,T> Map<S,T> eqMap(final Map<S,T> in) {
reportMatcher(new IArgumentMatcher() {

@Override
public void appendTo(StringBuffer out)
{
out.append("eqMap(");
for(S key : in.keySet()) {
out.append("[").append(key).append("=>").append(in.get(key)).append("]");
}
out.append(")");
}

@SuppressWarnings("unchecked")
@Override
public boolean matches(Object actual)
{
if(!(actual instanceof Map)) {
return false;
}
Map dummy = (Map)actual;
for(S key : in.keySet()) {
if(!dummy.containsKey(key)) {
return false;
}
if(!dummy.get(key).equals(in.get(key))) {
return false;
}
}
//TODO - Check for values in new map NOT in existing map.
return true;
}
});
return null;
}
}



Obviously not quite my complete implementation (hence the TODO), but enough to get you started. You can do all sorts of tests on inputs here as well. If you really want to test interactions with your system, make strict mock (createStrickMock) and ensure that the order in which arguments are called is correct.

I'd also recommend setting up some base classes (at least if you're doing EJB mocks) that will handle registering mocks on JNDI, or some JMS mocking helpers if you have to interact with either of those.

In my 10 minute attempt to mock EntityManager to test my EJBs, I realized that although JBoss might know how to inject into a protected/private member, I have no clue how to do this.

Anyway, hope this helps someone else. For more info on EasyMock, follow this readme is great.