lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mikemcc...@apache.org
Subject svn commit: r657320 - /lucene/java/trunk/src/test/org/apache/lucene/index/TestPayloads.java
Date Sat, 17 May 2008 08:12:09 GMT
Author: mikemccand
Date: Sat May 17 01:12:09 2008
New Revision: 657320

URL: http://svn.apache.org/viewvc?rev=657320&view=rev
Log:
fix intermittant failure when random byte sequence creates invalid UTF16 string

Modified:
    lucene/java/trunk/src/test/org/apache/lucene/index/TestPayloads.java

Modified: lucene/java/trunk/src/test/org/apache/lucene/index/TestPayloads.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/index/TestPayloads.java?rev=657320&r1=657319&r2=657320&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/index/TestPayloads.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/index/TestPayloads.java Sat May 17 01:12:09
2008
@@ -20,6 +20,7 @@
 import java.io.File;
 import java.io.IOException;
 import java.io.Reader;
+import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -27,6 +28,7 @@
 import java.util.Random;
 
 import org.apache.lucene.util.LuceneTestCase;
+import org.apache.lucene.util.UnicodeUtil;
 
 import org.apache.lucene.analysis.Analyzer;
 import org.apache.lucene.analysis.Token;
@@ -510,8 +512,7 @@
                 int freq = tp.freq();
                 for (int i = 0; i < freq; i++) {
                     tp.nextPosition();
-                    String s = new String(tp.getPayload(new byte[5], 0));
-                    assertEquals(s, terms.term().text);
+                    assertEquals(pool.bytesToString(tp.getPayload(new byte[5], 0)), terms.term().text);
                 }
             }
             tp.close();
@@ -526,17 +527,18 @@
         private byte[] payload;
         private boolean first;
         private ByteArrayPool pool;
-        
+        private String term;
         PoolingPayloadTokenStream(ByteArrayPool pool) {
             this.pool = pool;
             payload = pool.get();
             generateRandomData(payload);
+            term = pool.bytesToString(payload);
             first = true;
         }
         
         public Token next() throws IOException {
             if (!first) return null;            
-            Token t = new Token(new String(payload), 0, 0);
+            Token t = new Token(term, 0, 0);
             t.setPayload(new Payload(payload));
             return t;        
         }
@@ -557,6 +559,18 @@
             }
         }
         
+        private UnicodeUtil.UTF8Result utf8Result = new UnicodeUtil.UTF8Result();
+
+        synchronized String bytesToString(byte[] bytes) {
+            String s = new String(bytes);
+            UnicodeUtil.UTF16toUTF8(s, 0, s.length(), utf8Result);
+            try {
+                return new String(utf8Result.result, 0, utf8Result.length, "UTF-8");
+            } catch (UnsupportedEncodingException uee) {
+                return null;
+            }
+        }
+    
         synchronized byte[] get() {
             return (byte[]) pool.remove(0);
         }



Mime
View raw message