@Test public void dateFields() { final long nowTime = new Date().getTime(); List<IndexableField> fields = pf.makeFields("date", new Date(nowTime), FieldSpec.INDEXED_STORED); assertEquals(2, fields.size()); boolean haveStored = false; boolean haveIndexed = false; for (IndexableField f : fields) { if (f.fieldType().stored()) { long fieldValue = f.numericValue().longValue(); assertEquals(nowTime, fieldValue); haveStored = true; } if (!f.fieldType().stored()) { long fieldValue = f.numericValue().longValue(); assertEquals(DateTools.round(nowTime, DateTools.Resolution.SECOND), fieldValue); haveIndexed = true; } } if (!(haveIndexed && haveStored)) { fail("Expected one indexed and one stored field"); } }
@Test public void storedFields() { IndexableField stringField = pf.makeFields("string", "bâr", FieldSpec.STORED).get(0); IndexableField intField = pf.makeFields("int", 1024, FieldSpec.STORED).get(0); IndexableField longField = pf.makeFields("long", 1024L, FieldSpec.STORED).get(0); assertEquals("string", stringField.name()); assertEquals("int", intField.name()); assertEquals("long", longField.name()); assertEquals("bâr", stringField.stringValue()); assertNull(stringField.binaryValue()); assertNull(stringField.numericValue()); assertEquals(1024, intField.numericValue().intValue()); assertEquals("1024", intField.stringValue()); assertNull(intField.binaryValue()); assertEquals(1024L, longField.numericValue().longValue()); assertEquals("1024", longField.stringValue()); assertNull(longField.binaryValue()); Value stringValue = pf.valueFromField(Type.STRING, stringField); assertEquals("bâr", stringValue.getStringValue()); Value intValue = pf.valueFromField(Type.INT, intField); assertEquals(1024, intValue.getIntValue()); Value longValue = pf.valueFromField(Type.LONG, longField); assertEquals(1024, longValue.getLongValue()); }
@Test public void isLowercaseField() { assertFalse(PropertyFields.isLowercaseField("p_foo")); assertFalse(PropertyFields.isLowercaseField("p_bar:foo")); assertTrue(PropertyFields.isLowercaseField("p_l_bar:foo")); assertFalse(PropertyFields.isLowercaseField(ResourceFields.NAME_FIELD_NAME)); assertTrue(PropertyFields.isLowercaseField(ResourceFields.NAME_LC_FIELD_NAME)); assertFalse(PropertyFields.isLowercaseField("what:ever")); }
@Test public void dateValueIndexFieldEncoding() { String[] dateFormats = new String[] { "Long-format", "yyyy-MM-dd HH:mm:ss Z", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd HH", "yyyy-MM-dd" }; Date now = new Date(); String[] dateStrings = new String[] { Long.toString(now.getTime()), "2005-10-10 14:22:00 +0100", "2005-10-10 14:22:00", "2005-10-10 14:22", "2005-10-10 14", "2005-10-10" }; for (int i = 0; i < dateStrings.length; i++) { try { pf.queryTerm("someDate", dateStrings[i], PropertyType.Type.TIMESTAMP, false); } catch (Exception e) { fail( "Failed to encode index field value for date format '" + dateFormats[i] + "', date string '" + dateStrings[i] + "':" + e.getMessage()); } } }