Make the bad header a checked exception and fix dicttool test

Change-Id: If780a2a1cb722aee83d910904c0a6315add9552d
main
Jean Chalard 2013-12-10 21:52:38 +09:00
parent 2c25c7c9e1
commit 3df617323e
2 changed files with 20 additions and 6 deletions

View File

@ -352,18 +352,19 @@ public final class FormatSpec {
public static final String DICTIONARY_ID_ATTRIBUTE = "dictionary"; public static final String DICTIONARY_ID_ATTRIBUTE = "dictionary";
private static final String DICTIONARY_DESCRIPTION_ATTRIBUTE = "description"; private static final String DICTIONARY_DESCRIPTION_ATTRIBUTE = "description";
public FileHeader(final int headerSize, final DictionaryOptions dictionaryOptions, public FileHeader(final int headerSize, final DictionaryOptions dictionaryOptions,
final FormatOptions formatOptions) { final FormatOptions formatOptions) throws UnsupportedFormatException {
mDictionaryOptions = dictionaryOptions; mDictionaryOptions = dictionaryOptions;
mFormatOptions = formatOptions; mFormatOptions = formatOptions;
mBodyOffset = formatOptions.mVersion < VERSION4 ? headerSize : 0; mBodyOffset = formatOptions.mVersion < VERSION4 ? headerSize : 0;
if (null == getLocaleString()) { if (null == getLocaleString()) {
throw new RuntimeException("Cannot create a FileHeader without a locale"); throw new UnsupportedFormatException("Cannot create a FileHeader without a locale");
} }
if (null == getVersion()) { if (null == getVersion()) {
throw new RuntimeException("Cannot create a FileHeader without a version"); throw new UnsupportedFormatException(
"Cannot create a FileHeader without a version");
} }
if (null == getId()) { if (null == getId()) {
throw new RuntimeException("Cannot create a FileHeader without an ID"); throw new UnsupportedFormatException("Cannot create a FileHeader without an ID");
} }
} }

View File

@ -42,9 +42,16 @@ public class BinaryDictOffdeviceUtilsTests extends TestCase {
private static final int TEST_FREQ = 37; // Some arbitrary value unlikely to happen by chance private static final int TEST_FREQ = 37; // Some arbitrary value unlikely to happen by chance
public void testGetRawDictWorks() throws IOException, UnsupportedFormatException { public void testGetRawDictWorks() throws IOException, UnsupportedFormatException {
final String VERSION = "1";
final String LOCALE = "test";
final String ID = "main:test";
// Create a thrice-compressed dictionary file. // Create a thrice-compressed dictionary file.
final FusionDictionary dict = new FusionDictionary(new PtNodeArray(), final DictionaryOptions testOptions = new DictionaryOptions(new HashMap<String, String>());
new DictionaryOptions(new HashMap<String, String>())); testOptions.mAttributes.put(FormatSpec.FileHeader.DICTIONARY_VERSION_ATTRIBUTE, VERSION);
testOptions.mAttributes.put(FormatSpec.FileHeader.DICTIONARY_LOCALE_ATTRIBUTE, LOCALE);
testOptions.mAttributes.put(FormatSpec.FileHeader.DICTIONARY_ID_ATTRIBUTE, ID);
final FusionDictionary dict = new FusionDictionary(new PtNodeArray(), testOptions);
dict.add("foo", TEST_FREQ, null, false /* isNotAWord */); dict.add("foo", TEST_FREQ, null, false /* isNotAWord */);
dict.add("fta", 1, null, false /* isNotAWord */); dict.add("fta", 1, null, false /* isNotAWord */);
dict.add("ftb", 1, null, false /* isNotAWord */); dict.add("ftb", 1, null, false /* isNotAWord */);
@ -72,6 +79,12 @@ public class BinaryDictOffdeviceUtilsTests extends TestCase {
final FusionDictionary resultDict = dictDecoder.readDictionaryBinary( final FusionDictionary resultDict = dictDecoder.readDictionaryBinary(
null /* dict : an optional dictionary to add words to, or null */, null /* dict : an optional dictionary to add words to, or null */,
false /* deleteDictIfBroken */); false /* deleteDictIfBroken */);
assertEquals("Wrong version attribute", VERSION, resultDict.mOptions.mAttributes.get(
FormatSpec.FileHeader.DICTIONARY_VERSION_ATTRIBUTE));
assertEquals("Wrong locale attribute", LOCALE, resultDict.mOptions.mAttributes.get(
FormatSpec.FileHeader.DICTIONARY_LOCALE_ATTRIBUTE));
assertEquals("Wrong id attribute", ID, resultDict.mOptions.mAttributes.get(
FormatSpec.FileHeader.DICTIONARY_ID_ATTRIBUTE));
assertEquals("Dictionary can't be read back correctly", assertEquals("Dictionary can't be read back correctly",
FusionDictionary.findWordInTree(resultDict.mRootNodeArray, "foo").getFrequency(), FusionDictionary.findWordInTree(resultDict.mRootNodeArray, "foo").getFrequency(),
TEST_FREQ); TEST_FREQ);