Archive for the ‘How to’s’ Category

Read a File as String with Java

Thursday, June 16th, 2011

Introduction

I’m always Googling for a way to do this. This seems to be the best “idiomatic” solution I’ve found. So without further ado…

Example

public String readFile(String path) throws IOException {
    FileInputStream stream = new FileInputStream(new File(path));
    try{
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        return Charset.defaultCharset().decode(bb).toString();
    }
    finally {
        stream.close();
    }
}

Tags:
Posted in Development, How to's, quick tips | No Comments »

Unit Test Private Java Methods using Reflection

Thursday, May 5th, 2011

Introduction

I realise that the thought of Unit Testing private methods in Java classes can be like a red rag to a bull for some people and I understand the arguments.

Therefore, I present the following as a “how to”, not a moral argument for or against!

Example

The class under test

public class Product() {
    private String privateMethod(String id) {
    //Do something private
    return "product_" + id;
  }
}

The (Reflection Based) Unit Test

import java.lang.reflect.Method;

import static org.junit.Assert.*;
import org.junit.Test;

public class ProductTest {
    private Product product; // the class under test
    private Method m;
    private static String METHOD_NAME = "privateMethod";
    private Class[] parameterTypes;
    private Object[] parameters;

    @Before
    public void setUp() throws Exception {
        product = new Product();
        parameterTypes = new Class[1];
        parameterTypes[0] = java.lang.String.class;
        m = product.getClass().getDeclaredMethod(METHOD_NAME, parameterTypes);
        m.setAccessible(true);
        parameters = new Object[1];
    }

    @Test
    public void testPrivateMethod() throws Exception {
        parameters[0] = "someIdentifier";
        String result = (String) m.invoke(product, parameters); 

        //Do your assertions
        assertNotNull(result);
    }
}

Update

I’ve since been told that if dp4j.jar is in the classpath at compile-time, it will inject the necessary reflection to make this work. I haven’t had time to try this yet so YMMV.

Tags: , ,
Posted in Development, How to's | No Comments »

How to stop XSLT from escaping XML in output

Tuesday, September 28th, 2010

I was asked by a colleague today how to prevent XSLT from escaping XML “special characters” in its output. i.e. < was being escaped to &lt ;.

I didn't know but somebody else did...

<xsl:value-of select=”somethingWhichContainsXml” />

should be 

<xsl:value-of select=”somethingWhichContainsXml” disable-output-escaping=”yes” />

Tags: ,
Posted in Development, How to's | No Comments »

Display Version in Android Application

Thursday, August 26th, 2010

I was looking for a way to display the Version on the splash screen of my Android app, more specifically, the “versionName” as defined in the application’s AndroidManifest.xml

After A LOT of looking I discovered PackageInfo, which is a Java API to all of the info held in the manifest.

Armed with this information is was easy to produce the following:

private void displayVersionName() {
    String versionName = "";
    PackageInfo packageInfo;
    try {
        packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
    	versionName = "v " + packageInfo.versionName;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    TextView tv = (TextView) findViewById(R.id.versionNameTextView);
    tv.setText(versionName);
}

Tags: ,
Posted in Development, How to's | No Comments »

Log4j Nested Diagnostic Contexts (NDC)

Wednesday, June 2nd, 2010

Introduction and Problem

Log4j is a brilliant set of libraries which provides good “out of the box” logging capabilities.

Sometimes however, in multi-threaded environments (for example web apps), it can be hard to decipher the interleaved messages from the various clients
(more...)

Tags: , ,
Posted in Development, How to's | No Comments »