Tuesday, July 15, 2014

puzzling difference between zsh and bash

Accidentally found a little difference between zsh and bash, which seems a bit annoying.

Suppose there is a simple script.

for file in `hadoop classpath | tr ':' ' ' | sort | uniq`; do echo $file; done

and the original output of `hadoop classpath`  looks like this:

zsh %> hadoop classpath  
/usr/local/hadoop/etc/hadoop:/usr/local/hadoop/share/hadoop/common/lib/*:/usr/local/hadoop/share/hadoop/common/*:/usr/local/hadoop/share/hadoop/hdfs:/usr/local/hadoop/share/hadoop/hdfs/lib/*:/usr/local/hadoop/share/hadoop/hdfs/*:/usr/local/hadoop/share/hadoop/yarn/lib/*:/usr/local/hadoop/share/hadoop/yarn/*:/usr/local/hadoop/share/hadoop/mapreduce/lib/*:/usr/local/hadoop/share/hadoop/mapreduce/*:/usr/local/hadoop/contrib/capacity-scheduler/*.jar

If I run the above in Bourne shell, the results will be like this (which will give the unique list of all jars contained in the specified classpath):

bash-4.1$  for file in `hadoop classpath | tr ':' ' ' | sort | uniq`; do echo $file; done
/usr/local/hadoop/etc/hadoop
/usr/local/hadoop/share/hadoop/common/lib/activation-1.1.jar
/usr/local/hadoop/share/hadoop/common/lib/asm-3.2.jar
/usr/local/hadoop/share/hadoop/common/lib/avro-1.7.4.jar
/usr/local/hadoop/share/hadoop/common/lib/commons-beanutils-1.7.0.jar
/usr/local/hadoop/share/hadoop/common/lib/commons-beanutils-core-1.8.0.jar
/usr/local/hadoop/share/hadoop/common/lib/commons-cli-1.2.jar
/usr/local/hadoop/share/hadoop/common/lib/commons-codec-1.4.jar
...

In zsh, however, I still get:

zsh %> for file in `hadoop classpath | tr ':' ' ' | sort | uniq`; do echo $file; done        
/usr/local/hadoop/etc/hadoop
/usr/local/hadoop/share/hadoop/common/lib/*
/usr/local/hadoop/share/hadoop/common/*
/usr/local/hadoop/share/hadoop/hdfs
/usr/local/hadoop/share/hadoop/hdfs/lib/*
/usr/local/hadoop/share/hadoop/hdfs/*
/usr/local/hadoop/share/hadoop/yarn/lib/*
/usr/local/hadoop/share/hadoop/yarn/*
/usr/local/hadoop/share/hadoop/mapreduce/lib/*
/usr/local/hadoop/share/hadoop/mapreduce/*
/usr/local/hadoop/contrib/capacity-scheduler/*.jar


This was quite unexpected to find out. Now I am wondering how to make it work in zsh.
PS I was curious enough to ask in SO about this.



Saturday, July 12, 2014

How to mock a non-declared checked exception with Mockito

Little useful mocking hack, which costed me quite some time to find:

to throw checked exception from Scala method (which usually don't declare exceptions, which disappoints frameworks like Mockito), use answer:

mockedObject.doSomething(any[Whatever]).answers{ _ => throw new CheckedException("ouch!") } 

where CheckedException is not an instance of RuntimeException. Not because RuntimeException cannot be thrown that way (it can), but because there is a simpler method for those:

mockedObject.doSomething(any[Whatever]) throws new RuntimeException("ouch!")

So if you need to throw e.g. SQLException, method 1 will work, whereas method 2 will compile but complain during the test that such type of exception does not correspond to the signature. For something like ArithmeticException, both methods will work.