/*
 * 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.
 */
package org.apache.lucene.analysis.miscellaneous;

import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.BaseTokenStreamTestCase;
import org.apache.lucene.analysis.MockTokenizer;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.Tokenizer;
import org.apache.lucene.analysis.core.KeywordTokenizer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.FlagsAttribute;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.apache.lucene.analysis.tokenattributes.PayloadAttribute;
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
import org.apache.lucene.analysis.tokenattributes.TypeAttribute;

/**
 */
public class TestTrimFilter extends BaseTokenStreamTestCase {

  public void testTrim() throws Exception {
    char[] a = " a ".toCharArray();
    char[] b = "b   ".toCharArray();
    char[] ccc = "cCc".toCharArray();
    char[] whitespace = "   ".toCharArray();
    char[] empty = "".toCharArray();

    TokenStream ts = new IterTokenStream(new Token(new String(a, 0, a.length), 1, 5),
                    new Token(new String(b, 0, b.length), 6, 10),
                    new Token(new String(ccc, 0, ccc.length), 11, 15),
                    new Token(new String(whitespace, 0, whitespace.length), 16, 20),
                    new Token(new String(empty, 0, empty.length), 21, 21));
    ts = new TrimFilter(ts);

    assertTokenStreamContents(ts, new String[] { "a", "b", "cCc", "", ""});
  }
  
  /**
   * @deprecated (3.0) does not support custom attributes
   */
  @Deprecated
  private static class IterTokenStream extends TokenStream {
    final Token tokens[];
    int index = 0;
    CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
    OffsetAttribute offsetAtt = addAttribute(OffsetAttribute.class);
    PositionIncrementAttribute posIncAtt = addAttribute(PositionIncrementAttribute.class);
    FlagsAttribute flagsAtt = addAttribute(FlagsAttribute.class);
    TypeAttribute typeAtt = addAttribute(TypeAttribute.class);
    PayloadAttribute payloadAtt = addAttribute(PayloadAttribute.class);
    
    public IterTokenStream(Token... tokens) {
      super();
      this.tokens = tokens;
    }
    
    @Override
    public boolean incrementToken() throws IOException {
      if (index >= tokens.length)
        return false;
      else {
        clearAttributes();
        Token token = tokens[index++];
        termAtt.setEmpty().append(token);
        offsetAtt.setOffset(token.startOffset(), token.endOffset());
        posIncAtt.setPositionIncrement(token.getPositionIncrement());
        flagsAtt.setFlags(token.getFlags());
        typeAtt.setType(token.type());
        payloadAtt.setPayload(token.getPayload());
        return true;
      }
    }
  }
  
  /** blast some random strings through the analyzer */
  public void testRandomStrings() throws Exception {
    Analyzer a = new Analyzer() {

      @Override
      protected TokenStreamComponents createComponents(String fieldName) {
        Tokenizer tokenizer = new MockTokenizer(MockTokenizer.KEYWORD, false);
        return new TokenStreamComponents(tokenizer, new TrimFilter(tokenizer));
      } 
    };
    checkRandomData(random(), a, 1000*RANDOM_MULTIPLIER);
    a.close();
  }
  
  public void testEmptyTerm() throws IOException {
    Analyzer a = new Analyzer() {
      @Override
      protected TokenStreamComponents createComponents(String fieldName) {
        Tokenizer tokenizer = new KeywordTokenizer();
        return new TokenStreamComponents(tokenizer, new TrimFilter(tokenizer));
      }
    };
    checkOneTerm(a, "", "");
    a.close();
  }
}

@@ -978,16 +978,69 @@
 stCase;%0A
+import org.apache.lucene.analysis.CannedTokenStream;%0A
 import o
@@ -1072,32 +1072,32 @@
 .MockTokenizer;%0A
-
 import org.apach
@@ -1273,421 +1273,8 @@
 zer;
-%0Aimport org.apache.lucene.analysis.tokenattributes.CharTermAttribute;%0Aimport org.apache.lucene.analysis.tokenattributes.FlagsAttribute;%0Aimport org.apache.lucene.analysis.tokenattributes.OffsetAttribute;%0Aimport org.apache.lucene.analysis.tokenattributes.PayloadAttribute;%0Aimport org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;%0Aimport org.apache.lucene.analysis.tokenattributes.TypeAttribute;
 %0A%0A/*
@@ -1605,20 +1605,22 @@
 s = new 
-Iter
+Canned
 TokenStr
@@ -1992,24 +1992,24 @@
 ilter(ts);%0A%0A
+
     assertTo
@@ -2083,1299 +2083,8 @@
 %0A  %0A
-  /**%0A   * @deprecated (3.0) does not support custom attributes%0A   */%0A  @Deprecated%0A  private static class IterTokenStream extends TokenStream %7B%0A    final Token tokens%5B%5D;%0A    int index = 0;%0A    CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);%0A    OffsetAttribute offsetAtt = addAttribute(OffsetAttribute.class);%0A    PositionIncrementAttribute posIncAtt = addAttribute(PositionIncrementAttribute.class);%0A    FlagsAttribute flagsAtt = addAttribute(FlagsAttribute.class);%0A    TypeAttribute typeAtt = addAttribute(TypeAttribute.class);%0A    PayloadAttribute payloadAtt = addAttribute(PayloadAttribute.class);%0A    %0A    public IterTokenStream(Token... tokens) %7B%0A      super();%0A      this.tokens = tokens;%0A    %7D%0A    %0A    @Override%0A    public boolean incrementToken() throws IOException %7B%0A      if (index %3E= tokens.length)%0A        return false;%0A      else %7B%0A        clearAttributes();%0A        Token token = tokens%5Bindex++%5D;%0A        termAtt.setEmpty().append(token);%0A        offsetAtt.setOffset(token.startOffset(), token.endOffset());%0A        posIncAtt.setPositionIncrement(token.getPositionIncrement());%0A        flagsAtt.setFlags(token.getFlags());%0A        typeAtt.setType(token.type());%0A        payloadAtt.setPayload(token.getPayload());%0A        return true;%0A      %7D%0A    %7D%0A  %7D%0A  %0A
   /*

simplify test to use CannedTokenStream