From b6cc4333a88becaed03ead29a8a2d41babca6a41 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Wed, 3 Jul 2013 19:18:22 +0900 Subject: [PATCH] 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 --- .../inputmethod/latin/dicttool/Dicttool.java | 13 ++++++++--- .../inputmethod/latin/dicttool/Test.java | 22 ++++++++++++++----- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Dicttool.java b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Dicttool.java index 7b311c3ec..cacee5268 100644 --- a/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Dicttool.java +++ b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Dicttool.java @@ -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)); } } diff --git a/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Test.java b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Test.java index df5ea35bb..972b6e7e6 100644 --- a/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Test.java +++ b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Test.java @@ -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 mAllTestMethods = new ArrayList(); private ArrayList mUsedTestMethods = new ArrayList(); @@ -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); } }