lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mikemcc...@apache.org
Subject svn commit: r754789 - in /lucene/java/trunk/src: java/org/apache/lucene/document/Field.java test/org/apache/lucene/document/TestDocument.java
Date Sun, 15 Mar 2009 23:24:39 GMT
Author: mikemccand
Date: Sun Mar 15 23:24:39 2009
New Revision: 754789

URL: http://svn.apache.org/viewvc?rev=754789&view=rev
Log:
LUCENE-1564: don't let Field.setValue switch between binary & non-binary fields

Modified:
    lucene/java/trunk/src/java/org/apache/lucene/document/Field.java
    lucene/java/trunk/src/test/org/apache/lucene/document/TestDocument.java

Modified: lucene/java/trunk/src/java/org/apache/lucene/document/Field.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/document/Field.java?rev=754789&r1=754788&r2=754789&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/document/Field.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/document/Field.java Sun Mar 15 23:24:39 2009
@@ -207,16 +207,28 @@
    *  href="http://wiki.apache.org/lucene-java/ImproveIndexingSpeed">ImproveIndexingSpeed</a>
    *  for details.</p> */
   public void setValue(String value) {
+    if (isBinary) {
+      throw new IllegalArgumentException("cannot set a String value on a binary field");
+    }
     fieldsData = value;
   }
 
   /** Expert: change the value of this field.  See <a href="#setValue(java.lang.String)">setValue(String)</a>.
*/
   public void setValue(Reader value) {
+    if (isBinary) {
+      throw new IllegalArgumentException("cannot set a Reader value on a binary field");
+    }
+    if (isStored) {
+      throw new IllegalArgumentException("cannot set a Reader value on a stored field");
+    }
     fieldsData = value;
   }
 
   /** Expert: change the value of this field.  See <a href="#setValue(java.lang.String)">setValue(String)</a>.
*/
   public void setValue(byte[] value) {
+    if (!isBinary) {
+      throw new IllegalArgumentException("cannot set a byte[] value on a non-binary field");
+    }
     fieldsData = value;
     binaryLength = value.length;
     binaryOffset = 0;
@@ -224,6 +236,9 @@
 
   /** Expert: change the value of this field.  See <a href="#setValue(java.lang.String)">setValue(String)</a>.
*/
   public void setValue(byte[] value, int offset, int length) {
+    if (!isBinary) {
+      throw new IllegalArgumentException("cannot set a byte[] value on a non-binary field");
+    }
     fieldsData = value;
     binaryLength = length;
     binaryOffset = offset;
@@ -232,6 +247,12 @@
   
   /** Expert: change the value of this field.  See <a href="#setValue(java.lang.String)">setValue(String)</a>.
*/
   public void setValue(TokenStream value) {
+    if (isBinary) {
+      throw new IllegalArgumentException("cannot set a TokenStream value on a binary field");
+    }
+    if (isStored) {
+      throw new IllegalArgumentException("cannot set a TokenStream value on a stored field");
+    }
     fieldsData = value;
   }
 

Modified: lucene/java/trunk/src/test/org/apache/lucene/document/TestDocument.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/document/TestDocument.java?rev=754789&r1=754788&r2=754789&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/document/TestDocument.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/document/TestDocument.java Sun Mar 15 23:24:39
2009
@@ -260,4 +260,23 @@
       dir.close();
       assertEquals("did not see all IDs", 7, result);
     }
+
+  public void testFieldSetValueChangeBinary() {
+    Field field1 = new Field("field1", new byte[0],
+                             Field.Store.YES);
+    Field field2 = new Field("field2", "",
+                             Field.Store.YES, Field.Index.ANALYZED);
+    try {
+      field1.setValue("abc");
+      fail("did not hit expected exception");
+    } catch (IllegalArgumentException iae) {
+      // expected
+    }
+    try {
+      field2.setValue(new byte[0]);
+      fail("did not hit expected exception");
+    } catch (IllegalArgumentException iae) {
+      // expected
+    }
+  }
 }



Mime
View raw message