DateRangeMapper extends MultipleColumnMapper { public DateRangeMapper(String field, Boolean validated, String from, String to, String pattern) { super(field, validated, Arrays.asList(from, to), DATE_TYPES); if (StringUtils.isBlank(from)) { throw new IndexException("from column name is required"); } if (StringUtils.isBlank(to)) { throw new IndexException("to column name is required"); } this.from = from; this.to = to; this.parser = new DateParser(pattern); tree = DateRangePrefixTree.INSTANCE; strategy = new NumberRangePrefixTreeStrategy(tree, field); } DateRangeMapper(String field, Boolean validated, String from, String to, String pattern); @Override List<IndexableField> indexableFields(Columns columns); @Override SortField sortField(String name, boolean reverse); NRShape makeShape(Date from, Date to); Date base(Object value); @Override String toString(); final String from; final String to; final DateParser parser; final NumberRangePrefixTreeStrategy strategy; }
@Test public void testConstructorWithDefaultArgs() { DateRangeMapper mapper = dateRangeMapper("from", "to").build("field"); assertEquals("Name is not properly set", "field", mapper.field); assertEquals("From is not properly set", "from", mapper.from); assertEquals("To is not properly set", "to", mapper.to); assertEquals("Mapped columns are not properly set", 2, mapper.mappedColumns.size()); assertTrue("Mapped columns are not properly set", mapper.mappedColumns.contains("to")); assertTrue("Mapped columns are not properly set", mapper.mappedColumns.contains("from")); assertEquals("Date pattern is not set to default value", DEFAULT_PATTERN, mapper.parser.pattern); assertNotNull("Strategy is not set to default value", mapper.strategy); }
@Test public void testConstructorWithAllArgs() { DateRangeMapper mapper = dateRangeMapper("from", "to").validated(true) .pattern("yyyyMMdd") .build("field"); assertEquals("Name is not properly set", "field", mapper.field); assertEquals("From is not properly set", "from", mapper.from); assertEquals("To is not properly set", "to", mapper.to); assertEquals("Date pattern is not set to default value", "yyyyMMdd", mapper.parser.pattern); assertNotNull("Strategy is not properly set", mapper.strategy); }
@Test public void testJsonSerialization() { DateRangeMapperBuilder builder = dateRangeMapper("from", "to").pattern("yyyy-MM-dd"); testJson(builder, "{type:\"date_range\",from:\"from\",to:\"to\",pattern:\"yyyy-MM-dd\"}"); }
@Test public void testJsonSerializationDefaults() { DateRangeMapperBuilder builder = dateRangeMapper("from", "to"); testJson(builder, "{type:\"date_range\",from:\"from\",to:\"to\"}"); }
@Test(expected = IndexException.class) public void testConstructorWithNullFrom() { dateRangeMapper(null, "to").build("name"); }
@Test(expected = IndexException.class) public void testConstructorWithEmptyFrom() { dateRangeMapper("", "to").build("name"); }
@Test(expected = IndexException.class) public void testConstructorWithBlankFrom() { dateRangeMapper(" ", "to").build("name"); }
@Test(expected = IndexException.class) public void testConstructorWithNullTo() { dateRangeMapper("from", null).build("name"); }
@Test(expected = IndexException.class) public void testConstructorWithEmptyTo() { dateRangeMapper("from", "").build("name"); }
@Test(expected = IndexException.class) public void testConstructorWithBlankTo() { dateRangeMapper("from", " ").build("name"); }
@Test public void testExtractAnalyzers() { DateRangeMapper mapper = dateRangeMapper("from", "to").build("name"); assertNull("Analyzer must be null", mapper.analyzer); }