LUCENE-1564: don't let Field.setValue switch between binary & non-binary fields

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@754789 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2009-03-15 23:24:39 +00:00
parent 36329624f9
commit 904b7967c1
2 changed files with 40 additions and 0 deletions

View File

@ -207,16 +207,28 @@ public final class Field extends AbstractField implements Fieldable, Serializabl
* 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 @@ public final class Field extends AbstractField implements Fieldable, Serializabl
/** 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 @@ public final class Field extends AbstractField implements Fieldable, Serializabl
/** 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;
}

View File

@ -260,4 +260,23 @@ public class TestDocument extends LuceneTestCase
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
}
}
}