@Test
  public void extractFieldNames() {
    assertEquals(Collections.emptySet(), plugin.extractFieldNames(""));
    assertEquals(Collections.emptySet(), plugin.extractFieldNames("text"));
    assertEquals(Collections.emptySet(), plugin.extractFieldNames("+(^:text"));
    assertEquals(Collections.emptySet(), plugin.extractFieldNames("foo :bar"));

    assertEquals(Collections.singleton("foo"), plugin.extractFieldNames("foo:bar"));
    assertEquals(Collections.singleton("both"), plugin.extractFieldNames("both:(one two)"));
    assertEquals(Collections.singleton("title__"), plugin.extractFieldNames("title__:text"));
    assertEquals(
        Collections.singleton("title_zh_TW"), plugin.extractFieldNames("title_zh_TW:text"));
    assertEquals(
        Collections.singleton("property.Blog.BlogPostClass.title"),
        plugin.extractFieldNames("property.Blog.BlogPostClass.title:value"));
    assertEquals(
        Collections.singleton("property.Blog.Blog..Post$5EClass.title"),
        plugin.extractFieldNames("property.Blog.Blog..Post$5EClass.title:value"));

    assertEquals(
        new HashSet<String>(
            Arrays.asList(
                "abc",
                "g_h.i",
                "m$n-o",
                "_\u0103\u00EE\u00E2\u0219\u021B\u00E8\u00E9\u00EA\u00EB")),
        plugin.extractFieldNames(
            "+abc:def AND -g_h.i:jkl AND (m$n-o:pqr OR "
                + "_\u0103\u00EE\u00E2\u0219\u021B\u00E8\u00E9\u00EA\u00EB:stu^3)"));
  }
  @Test
  public void withFieldAliases() {
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put("qf", "title^0.4 comment^0.40 date^1.0");
    parameters.put("xwiki.multilingualFields", "title, property.*, foo, comment");
    parameters.put("xwiki.supportedLocales", "en, fr, zh_TW");
    parameters.put("xwiki.typedDynamicFields", "property.*");
    parameters.put("xwiki.dynamicFieldTypes", "boolean, int");

    String query =
        "title:text AND x:y AND property.Blog.BlogPostClass.summary:wiki AND title_ro:value";
    SolrParams paramsWithAliases = plugin.withFieldAliases(query, new MapSolrParams(parameters));

    assertEquals("title__ title_en title_fr title_zh_TW", paramsWithAliases.get("f.title.qf"));
    assertEquals(
        "property.Blog.BlogPostClass.summary__ property.Blog.BlogPostClass.summary_en "
            + "property.Blog.BlogPostClass.summary_fr property.Blog.BlogPostClass.summary_zh_TW "
            + "property.Blog.BlogPostClass.summary_boolean property.Blog.BlogPostClass.summary_int",
        paramsWithAliases.get("f.property.Blog.BlogPostClass.summary.qf"));

    // Event if this field doesn't appear in the query, it's a default field so it has to have the
    // alias.
    assertEquals(
        "comment__ comment_en comment_fr comment_zh_TW", paramsWithAliases.get("f.comment.qf"));

    // These fields are not declared as multilingual.
    assertNull(paramsWithAliases.get("f.x.qf"));
    assertNull(paramsWithAliases.get("f.title_ro.qf"));

    // This is a default field but it's not declared as multilingual.
    assertNull(paramsWithAliases.get("f.date.qf"));

    // This multilingual field doesn't appear in the query and it's not a default field either.
    assertNull(paramsWithAliases.get("f.foo.qf"));
  }
  @Test
  public void withFieldAliasesWhenNoSupportedLocales() {
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put("qf", "comment^0.40");
    parameters.put("xwiki.multilingualFields", "title, comment");

    SolrParams paramsWithAliases =
        plugin.withFieldAliases("title:text", new MapSolrParams(parameters));

    // Aliases for the ROOT locale.
    assertEquals("title__", paramsWithAliases.get("f.title.qf"));
    assertEquals("comment__", paramsWithAliases.get("f.comment.qf"));
  }
  @Test
  public void withFieldAliasesWhenNoMultilingualFields() {
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put("qf", "title^0.4 comment^0.40 date^1.0");
    parameters.put("xwiki.supportedLocales", "en, ro");

    SolrParams paramsWithAliases =
        plugin.withFieldAliases("title:text", new MapSolrParams(parameters));

    // The existing parameters should have been preserved.
    assertEquals(2, paramsWithAliases.toNamedList().size());
    assertEquals("title^0.4 comment^0.40 date^1.0", paramsWithAliases.get("qf"));
    assertEquals("en, ro", paramsWithAliases.get("xwiki.supportedLocales"));
  }
  @Test
  public void withFieldAliasesWhenNoFieldsInQuery() {
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put("qf", "title^0.4 comment^0.40");
    parameters.put("xwiki.multilingualFields", "title, foo");
    parameters.put("xwiki.supportedLocales", "en, fr");
    parameters.put("xwiki.typedDynamicFields", "property.*");
    parameters.put("xwiki.dynamicFieldTypes", "long, date");

    SolrParams paramsWithAliases = plugin.withFieldAliases("text", new MapSolrParams(parameters));

    // 5 existing parameters plus one alias.
    assertEquals(6, paramsWithAliases.toNamedList().size());

    // A default multilingual field.
    assertEquals("title__ title_en title_fr", paramsWithAliases.get("f.title.qf"));

    // Not a multilingual field.
    assertNull(paramsWithAliases.get("f.comment.qf"));

    // Not a default field.
    assertNull(paramsWithAliases.get("f.foo.qf"));
  }