package fr.inra.lipm.jezlucene.cfg;
import static org.junit.Assert.*;
import org.apache.lucene.document.CompressionTools;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StoredField;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
/**
* @author llegrand
*/
@RunWith(MockitoJUnitRunner.class)
public class DocFieldTest {
@Rule
public ExpectedException exception = ExpectedException.none();
private final DocField minInstance = new DocField("My field", "/xml/path@attr", StoredField.TYPE);
private final DocField fullInstance = new DocField("My field", "/xml/path@attr", StoredField.TYPE, DocField.ValueType.BYTES, "md5");
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
}
@Test
public final void testContructorMin() throws Exception {
assertTrue("My field".equals(minInstance.getName()));
assertTrue("/xml/path@attr".equals(minInstance.getPattern()));
assertTrue(minInstance.getType().equals(minInstance.getType()));
assertNull(minInstance.getOption());
assertFalse(minInstance.hasValue());
}
@Test
public final void testContructorMinNullName() throws Exception {
exception.expect(NullPointerException.class);
exception.expectMessage("Field's name is required");
new DocField(null, "/xml/path@attr", StoredField.TYPE);
}
@Test
public final void testContructorMinNullPattern() throws Exception {
exception.expect(NullPointerException.class);
exception.expectMessage("Field's pattern is required");
new DocField("My field", null, StoredField.TYPE);
}
@Test
public final void testContructorMinNullType() throws Exception {
exception.expect(NullPointerException.class);
exception.expectMessage("Field's type is required");
new DocField("My field", "/xml/path@attr", null);
}
@Test
public final void testContructorFull() throws Exception {
assertTrue("My field".equals(fullInstance.getName()));
assertTrue("/xml/path@attr".equals(fullInstance.getPattern()));
assertTrue(fullInstance.getType().equals(fullInstance.getType()));
assertTrue("md5".equals(fullInstance.getOption()));
assertTrue(DocField.ValueType.BYTES.equals(fullInstance.getType()));
assertFalse(fullInstance.hasValue());
}
@Test
public final void testContructorFullNullValueType() throws Exception {
exception.expect(NullPointerException.class);
exception.expectMessage("Field's value type is required");
new DocField("My field", "/xml/path@attr", StoredField.TYPE, null, "md5");
}
@Test
public final void testSetValue() throws Exception {
exception.expect(NullPointerException.class);
exception.expectMessage("Value can't be null");
minInstance.setValue(null);
}
@Test
public final void testSetValueAndGetField() throws Exception {
final String expectedValue = "my value";
minInstance.setValue(expectedValue);
final Field field = minInstance.getLuceneField();
assertNotNull(field);
assertTrue(expectedValue.equals(field.stringValue()));
}
@Test
public final void testBinarySetValueAndGetField() throws Exception {
final String expectedValue = "my value";
fullInstance.setValue(expectedValue);
final Field field = fullInstance.getLuceneField();
assertNotNull(field);
assertFalse(expectedValue.equals(field.stringValue()));
assertArrayEquals(CompressionTools.compress(expectedValue.getBytes()), field.binaryValue().bytes);
}
@Test
public final void testBinaryGetField() throws Exception {
final String expectedValue = "my value";
final Field field = fullInstance.getLuceneField(expectedValue.getBytes());
assertNotNull(field);
assertArrayEquals(CompressionTools.compress(expectedValue.getBytes()), field.binaryValue().bytes);
}
}