Merge "Generalize constuctors & add tests"

main
Jean Chalard 2013-07-04 08:53:18 +00:00 committed by Android (Google) Code Review
commit eec3067fd0
2 changed files with 26 additions and 9 deletions

View File

@ -72,15 +72,21 @@ public class Dicttool {
return command; 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); final Command command = getCommand(arguments);
try { try {
command.run(); command.run();
return 0;
} catch (Exception e) { } catch (Exception e) {
System.out.println("Exception while processing command " System.out.println("Exception while processing command "
+ command.getClass().getSimpleName() + " : " + e); + command.getClass().getSimpleName() + " : " + e);
e.printStackTrace(); e.printStackTrace();
return; return 1;
} }
} }
@ -89,6 +95,7 @@ public class Dicttool {
help(); help();
return; 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; 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.BinaryDictIOUtilsTests;
import com.android.inputmethod.latin.makedict.BinaryDictInputOutputTest; import com.android.inputmethod.latin.makedict.BinaryDictInputOutputTest;
import com.android.inputmethod.latin.makedict.FusionDictionaryTest; import com.android.inputmethod.latin.makedict.FusionDictionaryTest;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
@ -36,7 +38,8 @@ public class Test extends Dicttool.Command {
BinaryDictOffdeviceUtilsTests.class, BinaryDictOffdeviceUtilsTests.class,
FusionDictionaryTest.class, FusionDictionaryTest.class,
BinaryDictInputOutputTest.class, BinaryDictInputOutputTest.class,
BinaryDictIOUtilsTests.class BinaryDictIOUtilsTests.class,
BinaryDictIOTests.class
}; };
private ArrayList<Method> mAllTestMethods = new ArrayList<Method>(); private ArrayList<Method> mAllTestMethods = new ArrayList<Method>();
private ArrayList<String> mUsedTestMethods = new ArrayList<String>(); private ArrayList<String> mUsedTestMethods = new ArrayList<String>();
@ -86,12 +89,19 @@ public class Test extends Dicttool.Command {
for (final Method m : mAllTestMethods) { for (final Method m : mAllTestMethods) {
final Class<?> declaringClass = m.getDeclaringClass(); final Class<?> declaringClass = m.getDeclaringClass();
if (!mUsedTestMethods.isEmpty() && !mUsedTestMethods.contains(m.getName())) continue; if (!mUsedTestMethods.isEmpty() && !mUsedTestMethods.contains(m.getName())) continue;
final Object instance; // Some of the test classes expose a two-argument constructor, taking a long as a
if (BinaryDictIOUtilsTests.class == declaringClass) { // seed for Random, and an int for a vocabulary size to test the dictionary with. They
instance = new BinaryDictIOUtilsTests(mSeed, mMaxUnigrams); // correspond respectively to the -s and -m numerical arguments to this command, which
} else { // are stored in mSeed and mMaxUnigrams. If the two-arguments constructor is present,
instance = declaringClass.newInstance(); // 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); m.invoke(instance);
} }
} }