DateMapper extends SingleColumnMapper.SingleFieldMapper<Long> { public DateMapper(String field, String column, Boolean validated, String pattern) { super(field, column, true, validated, null, Long.class, DATE_TYPES); this.parser = new DateParser(pattern); } DateMapper(String field, String column, Boolean validated, String pattern); @Override Optional<Field> indexedField(String name, Long value); @Override Optional<Field> sortedField(String name, Long value); @Override SortField sortField(String name, boolean reverse); @Override String toString(); final DateParser parser; }
@Test public void testConstructorWithAllArgs() { DateMapper mapper = dateMapper().validated(true).column("column").pattern("yyyy-MM-dd").build("field"); assertEquals("Name is not properly set", "field", mapper.field); assertTrue("Validated is not properly set", mapper.validated); assertEquals("Column is not properly set", "column", mapper.column); assertEquals("Mapped columns are not properly set", 1, mapper.mappedColumns.size()); assertTrue("Mapped columns are not properly set", mapper.mappedColumns.contains("column")); assertEquals("Date pattern is not set to default value", "yyyy-MM-dd", mapper.parser.pattern); }
@Test public void testJsonSerialization() { DateMapperBuilder builder = dateMapper().validated(true).column("column").pattern("yyyy-MM-dd"); testJson(builder, "{type:\"date\",validated:true,column:\"column\",pattern:\"yyyy-MM-dd\"}"); }
@Test public void testJsonSerializationDefaults() { DateMapperBuilder builder = dateMapper(); testJson(builder, "{type:\"date\"}"); }
@Test(expected = IllegalArgumentException.class) public void testConstructorWithWrongPattern() { dateMapper().pattern("hello").build("name"); }
@Test public void testBaseClass() { DateMapper mapper = dateMapper().pattern("yyyyMMdd").build("name"); assertEquals("Base class is wrong", Long.class, mapper.base); }
@Test public void testBaseNull() { DateMapper mapper = dateMapper().pattern("yyyyMMdd").build("name"); assertNull("Base value is not properly parsed", mapper.base("test", null)); }
@Test public void testBaseDate() throws ParseException { DateMapper mapper = dateMapper().pattern("yyyyMMdd").build("name"); Date date = new SimpleDateFormat("yyyyMMdd").parse("20161127"); Long parsed = mapper.base("test", date); Long expected = new SimpleDateFormat("yyyyMMdd").parse("20161127").getTime(); assertEquals("Base value is not properly parsed", expected, parsed); }
@Test public void testBaseDateTruncating() throws ParseException { DateMapper mapper = dateMapper().pattern("yyyyMMdd").build("name"); Date date = new SimpleDateFormat("yyyyMMdd HHmmss").parse("20161127 010203"); Long parsed = mapper.base("test", date); Long expected = new SimpleDateFormat("yyyyMMdd").parse("20161127").getTime(); assertEquals("Base value is not properly parsed", expected, parsed); }
@Test public void testBaseInteger() throws ParseException { DateMapper mapper = dateMapper().pattern("yyyyMMdd").build("name"); Long parsed = mapper.base("test", 20161127); Long expected = new SimpleDateFormat("yyyyMMdd").parse("20161127").getTime(); assertEquals("Base value is not properly parsed", expected, parsed); }
@Test public void testBaseLong() throws ParseException { DateMapper mapper = dateMapper().pattern("yyyyMMdd").build("name"); Long parsed = mapper.base("test", 20161127L); Long expected = new SimpleDateFormat("yyyyMMdd").parse("20161127").getTime(); assertEquals("Base value is not properly parsed", expected, parsed); }
@Test public void testBaseFloat() throws ParseException { DateMapper mapper = dateMapper().pattern("yyyy").build("name"); Long parsed = mapper.base("test", 2016F); Long expected = new SimpleDateFormat("yyyy").parse("2016").getTime(); assertEquals("Base value is not properly parsed", expected, parsed); }
@Test public void testBaseFloatWithDecimal() throws ParseException { DateMapper mapper = dateMapper().pattern("yyyy").build("name"); Long parsed = mapper.base("test", 2016.3F); Long expected = new SimpleDateFormat("yyyy").parse("2016").getTime(); assertEquals("Base value is not properly parsed", expected, parsed); }
@Test public void testBaseDouble() throws ParseException { DateMapper mapper = dateMapper().pattern("yyyyMMdd").build("name"); Long parsed = mapper.base("test", 20161127D); Long expected = new SimpleDateFormat("yyyyMMdd").parse("20161127").getTime(); assertEquals("Base value is not properly parsed", expected, parsed); }
@Test public void testBaseDoubleWithDecimal() throws ParseException { DateMapper mapper = dateMapper().pattern("yyyyMMdd").build("name"); Long parsed = mapper.base("test", 20161127.3D); Long expected = new SimpleDateFormat("yyyyMMdd").parse("20161127").getTime(); assertEquals("Base value is not properly parsed", expected, parsed); }
@Test public void testBaseString() throws ParseException { DateMapper mapper = dateMapper().pattern("yyyyMMdd").build("name"); Long parsed = mapper.base("test", "20161127"); Long expected = new SimpleDateFormat("yyyyMMdd").parse("20161127").getTime(); assertEquals("Base value is not properly parsed", expected, parsed); }
@Test(expected = IndexException.class) public void testBaseStringInvalid() { dateMapper().pattern("yyyyMMdd").build("name").base("test", "2014/03/19"); }
@Test public void testBaseTimeUUID() throws ParseException { Long expected = new SimpleDateFormat("yyyyMMdd").parse("20161127").getTime(); DateMapper mapper = dateMapper().pattern("yyyyMMdd").build("name"); Long parsed = mapper.base("test", UUIDGen.getTimeUUID(expected)); assertEquals("Base value is not properly parsed", expected, parsed); }
@Test(expected = IndexException.class) public void testBaseNotTimeUUID() { dateMapper().pattern("yyyyMMdd").build("name").base("name", UUID.randomUUID()); }
@Test public void testExtractAnalyzers() { DateMapper mapper = dateMapper().pattern("yyyyMMdd").build("name"); assertNull("Analyzer must be null", mapper.analyzer); }