Utils { static Trie dictsToTrie(List<PinyinDict> pinyinDicts) { Set<String> all = new TreeSet<String>(); Trie.TrieBuilder builder = Trie.builder(); if (pinyinDicts != null) { for (PinyinDict dict : pinyinDicts) { if (dict != null && dict.words() != null) { all.addAll(dict.words()); } } if (all.size() > 0) { for (String key : all) { builder.addKeyword(key); } return builder.build(); } } return null; } private  Utils();   }
@Test public void dictsToTrie_null_should_return_null() throws Exception { assertThat(Utils.dictsToTrie(null), nullValue()); }
@Test public void dictsToTrie_empty_list_should_return_null() throws Exception { List<PinyinDict> dicts = new ArrayList<PinyinDict>(); assertThat(Utils.dictsToTrie(dicts), nullValue()); }
@Test public void dictsToTrie_empty_dicts_should_return_null() throws Exception { List<PinyinDict> dicts = new ArrayList<PinyinDict>(); dicts.add(new PinyinMapDict() { @Override public Map<String, String[]> mapping() { return null; } }); assertThat(Utils.dictsToTrie(dicts), nullValue()); }
@Test public void dictsToTrie_null_item__should_return_null() throws Exception { List<PinyinDict> dicts = new ArrayList<PinyinDict>(); dicts.add(null); assertThat(Utils.dictsToTrie(dicts), nullValue()); }
@Test public void dictsToTrie_nonnull_should_return_trie() throws Exception { List<PinyinDict> dicts = new ArrayList<PinyinDict>(); dicts.add(new PinyinMapDict() { @Override public Map<String, String[]> mapping() { Map<String, String[]> map = new HashMap<String, String[]>(); map.put("1", null); return map; } }); assertThat(Utils.dictsToTrie(dicts), notNullValue()); }