package org.apache.lucene.facet;

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Arrays;

import org.apache.lucene.util._TestUtil;

public class TestFacetsConfig extends FacetTestCase {
  public void testPathToStringAndBack() throws Exception {
    int iters = atLeast(1000);
    for(int i=0;i<iters;i++) {
      int numParts = _TestUtil.nextInt(random(), 1, 6);
      String[] parts = new String[numParts];
      for(int j=0;j<numParts;j++) {
        String s;
        while (true) {
          s = _TestUtil.randomUnicodeString(random());
          if (s.length() > 0) {
            break;
          }
        }
        parts[j] = s;
      }

      String s = FacetsConfig.pathToString(parts);
      String[] parts2 = FacetsConfig.stringToPath(s);
      assertTrue(Arrays.equals(parts, parts2));
    }
  }
}

@@ -856,16 +856,575 @@
 rrays;%0A%0A
+import org.apache.lucene.analysis.MockAnalyzer;%0Aimport org.apache.lucene.document.Document;%0Aimport org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;%0Aimport org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;%0Aimport org.apache.lucene.index.DirectoryReader;%0Aimport org.apache.lucene.index.IndexDocument;%0Aimport org.apache.lucene.index.IndexWriter;%0Aimport org.apache.lucene.search.IndexSearcher;%0Aimport org.apache.lucene.search.MatchAllDocsQuery;%0Aimport org.apache.lucene.store.Directory;%0Aimport org.apache.lucene.util.IOUtils;%0A
 import o
@@ -1456,16 +1456,16 @@
 stUtil;%0A
-
 %0Apublic 
@@ -1511,16 +1511,19 @@
 tCase %7B%0A
+  %0A
   public
@@ -2131,14 +2131,1437 @@
 ;%0A    %7D%0A
-
   %7D%0A
+  %0A  public void testAddSameDocTwice() throws Exception %7B%0A    // LUCENE-5367: this was a problem with the previous code, making sure it%0A    // works with the new code.%0A    Directory indexDir = newDirectory(), taxoDir = newDirectory();%0A    IndexWriter indexWriter = new IndexWriter(indexDir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())));%0A    DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);%0A    FacetsConfig facetsConfig = new FacetsConfig();%0A    Document doc = new Document();%0A    doc.add(new FacetField(%22a%22, %22b%22));%0A    IndexDocument facetDoc = facetsConfig.build(taxoWriter, doc);%0A    // these two addDocument() used to fail%0A    indexWriter.addDocument(facetDoc);%0A    indexWriter.addDocument(facetDoc);%0A    IOUtils.close(indexWriter, taxoWriter);%0A    %0A    DirectoryReader indexReader = DirectoryReader.open(indexDir);%0A    DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);%0A    IndexSearcher searcher = newSearcher(indexReader);%0A    FacetsCollector fc = new FacetsCollector();%0A    searcher.search(new MatchAllDocsQuery(), fc);%0A    %0A    Facets facets = getTaxonomyFacetCounts(taxoReader, facetsConfig, fc);%0A    FacetResult res = facets.getTopChildren(10, %22a%22);%0A    assertEquals(1, res.labelValues.length);%0A    assertEquals(2, res.labelValues%5B0%5D.value);%0A    IOUtils.close(indexReader, taxoReader);%0A    %0A    IOUtils.close(indexDir, taxoDir);%0A  %7D%0A  %0A
 %7D%0A

add test to ensure we don't introduce the bug again