lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Vanlerberghe, Luc" <Luc.Vanlerber...@bvdep.com>
Subject RE: TestCase for KeywordAnalyzer split into KeywordTokenizer/KeywordAnalyzer
Date Fri, 11 Feb 2005 14:04:25 GMT
Here's the diff for the TestCase 'inline'.
It should be applied in
contrib\analyzers\src\test\org\apache\lucene\analysis

The failure in the Russian Analyzer is unrelated (I updated all sources
to HEAD i.e. 153399 to be sure) but you probably need the Russian fonts
to see the error: unicode expected:<?????????> but was:<???????????>

I wasn't aware that attachments are blocked (I should have guessed
though)
I'll use bugzilla in the future...



Index: TestKeywordAnalyzer.java
===================================================================
--- TestKeywordAnalyzer.java	(revision 153379)
+++ TestKeywordAnalyzer.java	(working copy)
@@ -16,6 +16,7 @@
  * limitations under the License.
  */
 
+import java.io.StringReader;
 import junit.framework.TestCase;
 import org.apache.lucene.index.IndexWriter;
 import org.apache.lucene.store.RAMDirectory;
@@ -60,4 +61,28 @@
               "+partnum:Q36 +space", query.toString("description"));
     assertEquals("doc found!", 1, hits.length());
   }
+  
+  public void testKeywordAnalyzerClosesStream() throws Exception {
+    // This test doesn't need the setup(), but it won't hurt either
+    ReaderClosedHelper keywordReader=new ReaderClosedHelper(new
StringReader("Illidium Space Modulator"));
+    
+    KeywordAnalyzer keywordAnalyzer=new KeywordAnalyzer();
+    
+    TokenStream tokenStream=keywordAnalyzer.tokenStream("test",
keywordReader);
+    
+    while (tokenStream.next()!=null) {
+      // discard all tokens
+    }
+    
+    tokenStream.close(); // This should be passed on to the
keywordReader
+    
+    assertTrue("Reader wasn't closed!", keywordReader.isClosed());
+    
+    try {
+      tokenStream.close();
+      fail("ReaderClosedHelper should throw IllegalStateException on
double close!");
+    } catch (IllegalStateException ise) {
+      // ok, ignore
+    }
+  }
 }
Index: ReaderClosedHelper.java
===================================================================
--- ReaderClosedHelper.java	(revision 0)
+++ ReaderClosedHelper.java	(revision 0)
@@ -0,0 +1,55 @@
+package org.apache.lucene.analysis;
+
+/**
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed 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.FilterReader;
+import java.io.IOException;
+import java.io.Reader;
+
+/**
+ * A helper class for tests that check if a reader was properly closed.
+ * Usage:
+ * <pre>
+ *   Reader reader=... // your own reader class
+ *   reader=new ReaderClosedHelper(reader); // filter all access
through a ReaderClosedHelper instance
+ *   ... normal processing that should implicitly close the reader
+ *   assertTrue("Reader wasn't closed!", reader.isClosed());
+ * </pre>
+ */
+public class ReaderClosedHelper extends FilterReader {
+  public ReaderClosedHelper(Reader in) {
+    super(in);
+    this.closed=false;
+  }
+  
+  public void close() throws IOException {
+    if (closed) {
+      throw new IllegalStateException("Reader was already closed!");
+    }
+    else {
+      // TODO: Store parts of the
Thread.currentThread().getStackTrace() here to use in the
IllegalStateException above.
+      closed=true;
+      super.close();
+    }
+  }
+  
+  public boolean isClosed() {
+    return closed;
+  }
+  
+  private boolean closed;
+}

Property changes on: ReaderClosedHelper.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision


---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-dev-help@jakarta.apache.org


Mime
View raw message