Thursday, June 13, 2013

compiler could see that...

I recently have met one of those not straightforward exceptions in tests. Something I first classified as 'perhaps it's config or classpath issue'. Here comes the stacktrace:
java.lang.NullPointerException
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.junit.internal.runners.ClassRoadie.runAfters(ClassRoadie.java:72)
 at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:47)
 at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:133)
 at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:112)
 at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
 at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:236)
 at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:134)
 at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:113)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
 at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
 at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
 at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:103)
 at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:74)
When I looked at the tests I was able to see all test methods are successful, but after there is an error. I was able to figure out that NPE is thrown during invocation of method within clean up method that should be run after test. Here is the code:
 @AfterClass
 public void resetConf() throws Exception{
  setTestConfiguration();
 }
Can you see that?
In debugger I have found that first argument of invoke() is null.
But it's quite ok. If it's null it's just static invocation and that's what I would expect during @AfterClass call...
Yes. Fine.
But resetConf() must be static then, you stupid reflection!

Friday, May 17, 2013

one more null check with Optional

Instead of
    if (tipDocument.isPresent()) {
      return tipDocument.get();
    }
    return null;
use
return tipDocument.orNull();

Monday, April 22, 2013

How not to check for null

There are some clever tricks how to deal with null values. Usage of guava's Optional is one of them. But this is not one of them:
    final Optional<MilestoneDocument> documentOfVersion = findDocumentOfVersion(documentType, documentVersion);
    if (documentOfVersion == null) {
      throw new IllegalStateException();
    }
    if (documentOfVersion.isPresent()) {
      return documentOfVersion.get();
    }

Tuesday, February 19, 2013

user friendly search on windows

Windows with its cmd.exe seems not to be user friendly environment for developer. But this may be changed easily, you need only to install few programs we miss from normal systems. See http://gnuwin32.sourceforge.net and download coreutils, findutils, sed, awk, grep etc. and put them to PATH. As find.exe confilicts with windows file.exe I always rename it to gfind.exe . You have to be careful with quoting - only double quote is welcomed. Also paths may be tricki, remember to replace / with \. Eg. xargs needs extra backslash to interpret one properly.

Let's find all inserts in sql files.

find . -name '*.sql' | xargs grep insert 
It is the win32 version:
gfind . -name "*.sql" | sed s!/!\\\\!g | xargs grep insert

Summary: having in mind few simple rules you may make your system much easier to talk to.

Monday, February 11, 2013

picking up python

Recently I dived into jython, which is now scripting layer of application I' maintaining. Coming from Java/Scala I have some notices:
  • indentation matters
  • it actually does not matter that much, as I indent my code :>
  • method's first argument is always self
  • this point may have some exceptions, but for me a thumb rule is :%s/()/(self)/ in my vim :>
  • there is no null, but None
  • even if it's still jvm
  • None starts with capital letter, just like False, True and some others...
  • this one really surprised me
  • you do not write new to create new object
  • just call constructor, but
  • you have to return explicitly
  • which hurts when you're used to scala's brevity