package org.apache.lucene.demo;

/**
 * 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.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;

import org.apache.lucene.util.LuceneTestCase;

public class TestDemo extends LuceneTestCase {

  private void testOneSearch(String query, int expectedHitCount) throws Exception {
    PrintStream outSave = System.out;
    try {
      ByteArrayOutputStream bytes = new ByteArrayOutputStream();
      PrintStream fakeSystemOut = new PrintStream(bytes);
      System.setOut(fakeSystemOut);
      SearchFiles.main(new String[] {"-query", query});
      fakeSystemOut.flush();
      String output = bytes.toString(); // intentionally use default encoding
      assertTrue("output=" + output, output.contains(expectedHitCount + " total matching documents"));
    } finally {
      System.setOut(outSave);
    }
  }

  public void testIndexSearch() throws Exception {
    File dir = getDataFile("test-files/docs");
    IndexFiles.main(new String[] { "-create", "-docs", dir.getPath() });
    testOneSearch("apache", 3);
    testOneSearch("patent", 8);
    testOneSearch("lucene", 0);
    testOneSearch("gnu", 6);
    testOneSearch("derivative", 8);
    testOneSearch("license", 13);
  }
}

@@ -963,16 +963,57 @@
 estCase;
+%0Aimport org.apache.lucene.util._TestUtil;
 %0A%0Apublic
@@ -1083,16 +1083,32 @@
 eSearch(
+File indexPath, 
 String q
@@ -1413,16 +1413,47 @@
 %22, query
+, %22-index%22, indexPath.getPath()
 %7D);%0A    
@@ -1813,16 +1813,77 @@
 docs%22);%0A
+    File indexDir = _TestUtil.getTempDir(%22ContribDemoTest%22);%0A
     Inde
@@ -1942,17 +1942,46 @@
 etPath()
- 
+, %22-index%22, indexDir.getPath()
 %7D);%0A    
@@ -1994,16 +1994,26 @@
 eSearch(
+indexDir, 
 %22apache%22
@@ -2036,16 +2036,26 @@
 eSearch(
+indexDir, 
 %22patent%22
@@ -2078,16 +2078,26 @@
 eSearch(
+indexDir, 
 %22lucene%22
@@ -2120,16 +2120,26 @@
 eSearch(
+indexDir, 
 %22gnu%22, 6
@@ -2159,16 +2159,26 @@
 eSearch(
+indexDir, 
 %22derivat
@@ -2205,16 +2205,26 @@
 eSearch(
+indexDir, 
 %22license

fix test: don't use hardcoded index