Generalize constuctors & add tests

If there is any two-args constructor with the right arg types
in a test class, use it. Also, add a test suite to dicttool test.

Also, have the return value of dicttool reflect success or
failure.

Change-Id: I51ab2a4abb93a0a483e8d6ef3c39d1ff1bce1dbd
main
Jean Chalard 2013-07-03 19:18:22 +09:00
parent d365d82d51
commit b6cc4333a8
2 changed files with 26 additions and 9 deletions

View File

@ -72,15 +72,21 @@ public class Dicttool {
return command;
}
private void execute(final String[] arguments) {
/**
* Executes the specified command with the specified arguments.
* @param arguments the arguments passed to dicttool.
* @return 0 for success, an error code otherwise (always 1 at the moment)
*/
private int execute(final String[] arguments) {
final Command command = getCommand(arguments);
try {
command.run();
return 0;
} catch (Exception e) {
System.out.println("Exception while processing command "
+ command.getClass().getSimpleName() + " : " + e);
e.printStackTrace();
return;
return 1;
}
}
@ -89,6 +95,7 @@ public class Dicttool {
help();
return;
}
new Dicttool().execute(arguments);
// Exit with the success/error code from #execute() as status.
System.exit(new Dicttool().execute(arguments));
}
}

View File

@ -16,10 +16,12 @@
package com.android.inputmethod.latin.dicttool;
import com.android.inputmethod.latin.makedict.BinaryDictIOTests;
import com.android.inputmethod.latin.makedict.BinaryDictIOUtilsTests;
import com.android.inputmethod.latin.makedict.BinaryDictInputOutputTest;
import com.android.inputmethod.latin.makedict.FusionDictionaryTest;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
@ -36,7 +38,8 @@ public class Test extends Dicttool.Command {
BinaryDictOffdeviceUtilsTests.class,
FusionDictionaryTest.class,
BinaryDictInputOutputTest.class,
BinaryDictIOUtilsTests.class
BinaryDictIOUtilsTests.class,
BinaryDictIOTests.class
};
private ArrayList<Method> mAllTestMethods = new ArrayList<Method>();
private ArrayList<String> mUsedTestMethods = new ArrayList<String>();
@ -86,12 +89,19 @@ public class Test extends Dicttool.Command {
for (final Method m : mAllTestMethods) {
final Class<?> declaringClass = m.getDeclaringClass();
if (!mUsedTestMethods.isEmpty() && !mUsedTestMethods.contains(m.getName())) continue;
final Object instance;
if (BinaryDictIOUtilsTests.class == declaringClass) {
instance = new BinaryDictIOUtilsTests(mSeed, mMaxUnigrams);
} else {
instance = declaringClass.newInstance();
// Some of the test classes expose a two-argument constructor, taking a long as a
// seed for Random, and an int for a vocabulary size to test the dictionary with. They
// correspond respectively to the -s and -m numerical arguments to this command, which
// are stored in mSeed and mMaxUnigrams. If the two-arguments constructor is present,
// then invoke it; otherwise, invoke the default constructor.
Constructor<?> twoArgsConstructor = null;
try {
twoArgsConstructor = declaringClass.getDeclaredConstructor(Long.TYPE, Integer.TYPE);
} catch (NoSuchMethodException e) {
// No constructor with two args
}
final Object instance = null == twoArgsConstructor ? declaringClass.newInstance()
: twoArgsConstructor.newInstance(mSeed, mMaxUnigrams);
m.invoke(instance);
}
}