A Unit Testing Framework for the BlackBerry, Page 3
Listing 4: MicroTest.
// MicroTest.java:
package com.langrsoft.bbtest;
abstract public class MicroTest extends MicroTestBase implements
Executable {
public MicroTest() {
super();
}
public MicroTest(String name) {
super(name);
}
abstract public void run();
public void execute() {
passed = false;
try {
run();
passed = true;
} catch (Throwable failure) {
}
}
public void execute(TestContext context) {
try {
context.setUp();
execute();
context.tearDown();
} catch (Throwable failure) {
passed = false;
} finally {
context.completed(this);
)
}
}
// MicroTestBase.java:
package com.langrsoft.bbtest;
public abstract class MicroTestBase implements Executable {
protected boolean passed;
protected String name;
protected ResultListener listener;
public MicroTestBase() {
super();
}
public MicroTestBase(String name) {
this.name = name;
}
public String name() {
return name;
}
public boolean passed() {
return passed;
}
abstract public void execute();
public void setListener(ResultListener listener) {
this.listener = listener;
}
public String className() {
return simpleName(nestingClassName());
}
private String simpleName(String qualifiedName) {
int start = qualifiedName.lastIndexOf('.');
return qualifiedName.substring(start + 1);
}
private String nestingClassName() {
String name = getClass().getName();
int innerclassStart = name.indexOf('$');
return (innerclassStart != -1) ? name.substring(0,
innerclassStart) : name;
}
)
// Executable.java:
package com.langrsoft.bbtest;
public interface Executable {
public void execute();
public void execute(TestContext context);
public void setListener(ResultListener listener);
}
// ResultListener.java:
package com.langrsoft.bbtest;
public interface ResultListener {
void ran(MicroTest test);
}
// TestContext.java:
package com.langrsoft.bbtest;
public interface TestContext {
void setUp();
void tearDown();
void completed(MicroTest test);
}
The method simpleName in MicroTest demonstrates one of the frustrations of working with a more constrained library. The JDK 1.5 version of the class Class supplies the method getSimpleName; on the BlackBerry, I'm forced to hand-code a comparable method that suits my needs. Similarly, nestingClassName is a bit more work because a replace method that works with regular expressions is not available.
The meaning of the TestContext should be a bit clearer with the listing for MicroTestGroup (see Listing 5).
0 Comments (click to add your comment)
Networking Solutions
