List Columns in a ResultSet

Posted by in Software, Tutorial

In order to list all columns in a JDBC ResultSet (e.g. for debug purposes), you need to iterate through its metadata:

Note: Column numbers start from 1, not 0 (hence the md.getColumnName(i + 1) above). I usually wrap this in a if(logger.isTraceEnabled()) so it doesn’t get triggered all the time. HTH, A little experiment: If you find this post and ad below useful, please check the ad out :-)

Pass Through a Corporate Proxy in Java

Posted by in Software, Tutorial

TL;DR: There’s no easy way to create a proxy :) I’ve tried for a while to build a way to request something through a proxy, for testing an emulated android app. The solution came from Octavian who kindly provided me a snippet to run once per app lifetime. It tackles the problem from two fronts: Sets proxy system properties (http.proxy* and https.proxy*) Sets the default Authenticator The code is:

One thing that can be improved would be a configuration-based (e.g. defined environment variable) bypass code, rather than a hostname…read more

Mock an Observable Interface

Posted by in Software, Tutorial

TL;DR: Use a doAnswer() with a new Answer<Observable<Type>>() if you have to mock observables. Today I wrote my first test where I had to mock an observable interface:

where the User model is:

Solution In my test class (named UserDatabaseTest), I first mocked the interface:

` Then, my setup() method is:

where USER_NAME is a String. To use it, you need to attach an observer and assert:

This is quite simple. Alternative Note: This I haven’t tried yet, but here it goes… If you want…read more

Load Glyphs with LibGDX

Posted by in Software

TL;DR: Remember to give the font loader the list of characters to render from the font! I’m trying to get a more resolution-independent mechanism to load simple shapes in place. After investigating SVG loading for several days I got nowhere. Then, I found out that libGDX actually supports vector fonts (TTF). Woo-hoo! Internally, it’ll convert the loaded font to a BitmapFont, for a specific size (if you want to use the same font with different sizes, you’ll have to load and store it accordingly). Preliminary stuff It is important (to…read more

About Java Exceptions in SE 7

Posted by in Software

TL;DR: JDK 7 improves on exception handling (less code, base exception class…). Base exception class Reflective operations exceptions now have a base class ReflectiveOperationException. This allows you to to a global catch rather than one for each exception. Now you can write:

Note: Reflective operations are ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchFieldException, NoSuchMethodException. Multi-catch Previously, you’d had to write code like this:

Now, you can write:

Woo-hoo! Less lines of code! Final catch Normally, when you re-throw form a catch, you only re-throw the caught exception (or a…read more

TIL: Lambda Expressions

Posted by in Software

TL;DR: A lambda expression is a shortcut notation to anonymous inner classes containing a single method. Usually, they define implementations for functional interfaces (interfaces declaring a single method). Today I finally found out what lambda expressions are (formally) by watching this tutorial. Lambda expressions are a concise way of passing pieces of code around. You can look at lambda expressions as shorthand for anonymous inner classes. There is a catch though: They are valid for functional interfaces. Functional interfaces Functional interfaces are interfaces defining a single method, like this:

read more

Manually Create a Retrofit/OkHttp Response for Testing

Posted by in Software

I’ve got a method in an use case which requires a Retofit Response object as parameter and I need to test it. Now, I can use the Retrofit itself and build one with the network, but it would require the full android shebang. I’ve opted instead for building a Retrofit Response manually. My method is testing headers, so I’ll need to build a Response with headers. The Response method to do this is:

A note: Where you see Response , it’s actually retrofit.Response. The OkHttp counterpart is prefixed: com.squareup.okhttp.Response….read more

Android: Provide Local Asset in Testing

Posted by in Software

In the past few days I had a little bit of a problem: InstrumentationTestCase gives access to local assets while AndroidTestCase gives access to getAssets() for assets specific to testing. I had a scenario to test where I get a file from my local src/androidTest/assets directory while the code would need to write some files in /data/data/ directory. Quite unfortunate I’d say. My solution was to create an InputStream method based on this answer, to cover both cases. The code is:

This way, if the method is called from…read more

Use a Java Enum with Strings

Posted by in Software

In my project I wanted to log messages using predefined categories. Traditionally, I do it via classes (if the messages are specific to the functions provided by that class):

or an interface (pretty much the same thing):

These solutions are prone to errors (e.g. when performing copy-paste and forgetting to change the string), so I wondered if there is an enum-based approach. Of course there is:

Quite nice, but more verbose than the interface variant (and also prone to the copy-paste duplication). That’s why I ended up…read more