ModeShapeDateTimeValueFactory extends AbstractValueFactory<DateTime> implements DateTimeFactory { @Override public DateTime create( String value ) { if (value == null) return null; try { return new ModeShapeDateTime(value.trim()); } catch (java.time.format.DateTimeParseException err) { try { Long longValue = Long.parseLong(value); return new ModeShapeDateTime(longValue); } catch (NumberFormatException e) { throw new ValueFormatException(value, getPropertyType(), GraphI18n.errorConvertingType.text(String.class.getSimpleName(), org.modeshape.jcr.api.value.DateTime.class.getSimpleName(), value), err); } } } ModeShapeDateTimeValueFactory( TextDecoder decoder,
                                          ValueFactories factories ); @Override DateTimeFactory with( ValueFactories valueFactories ); @Override DateTime create( String value ); @Override DateTime create( String value,
                            TextDecoder decoder ); @Override DateTime create( int value ); @Override DateTime create( long value ); @Override DateTime create( boolean value ); @Override DateTime create( float value ); @Override DateTime create( double value ); @Override DateTime create( BigDecimal value ); @Override DateTime create( Calendar value ); @Override DateTime create( Date value ); @Override DateTime create( DateTime value ); @Override DateTime create( Name value ); @Override DateTime create( Path value ); @Override DateTime create( Path.Segment value ); @Override DateTime create( Reference value ); @Override DateTime create( URI value ); @Override DateTime create( UUID value ); @Override DateTime create( NodeKey value ); @Override DateTime create( byte[] value ); @Override DateTime create( BinaryValue value ); @Override DateTime create( InputStream stream ); @Override DateTime create(); @Override DateTime createUtc(); @Override DateTime create( int year,
                            int monthOfYear,
                            int dayOfMonth,
                            int hourOfDay,
                            int minuteOfHour,
                            int secondOfMinute,
                            int millisecondsOfSecond ); @Override DateTime create( DateTime original,
                            long offsetInMillis ); @Override DateTime[] createEmptyArray( int length );  }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateDateFromBoolean() { factory.create(true); }
@Test public void shouldCreateDateFromString() { assertThat(factory.create(TODAY.getString()), is(TODAY)); assertThat(factory.create(LAST_YEAR.getString()), is(LAST_YEAR)); }
@Test public void shouldCreateDateFromStringRegardlessOfLeadingAndTrailingWhitespace() { assertThat(factory.create(" " + TODAY.getString() + " "), is(TODAY)); assertThat(factory.create(" " + LAST_YEAR.getString() + " "), is(LAST_YEAR)); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateDateFromStringThatIsNotInTheStandardFormat() { factory.create("something"); }
@Test public void shouldNotCreateDateFromIntegerValue() { assertThat(factory.create(10000), is(new ModeShapeDateTime(10000))); }
@Test public void shouldNotCreateDateFromLongValue() { assertThat(factory.create(10000l), is((new ModeShapeDateTime(10000l)))); }
@Test public void shouldNotCreateDateFromFloatValue() { assertThat(factory.create(10000.12345f), is(new ModeShapeDateTime(10000))); }
@Test public void shouldNotCreateDateFromDoubleValue() { assertThat(factory.create(10000.12345d), is((new ModeShapeDateTime(10000)))); }
@Test public void shouldCreateDateFromBigDecimal() { assertThat(factory.create(new BigDecimal(10000)), is((new ModeShapeDateTime(10000)))); }
@Test public void shouldCreateDateFromDate() { Calendar value = Calendar.getInstance(); assertThat(factory.create(value.getTime()), is(new ModeShapeDateTime(value.getTime()))); }
@Test public void shouldCreateDateFromCalendar() { Calendar value = Calendar.getInstance(); value.setTimeInMillis(10000); assertThat(factory.create(value), is(new ModeShapeDateTime(value))); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateDateFromName() { factory.create(mock(Name.class)); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateDateFromPath() { factory.create(mock(Path.class)); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateDateFromReference() { factory.create(mock(Reference.class)); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateDateFromUri() throws Exception { factory.create(new URI("http: }
@Test public void shouldCreateDateFromByteArrayContainingUtf8EncodingOfStringWithWellFormedDate() throws Exception { assertThat(factory.create(TODAY.getString().getBytes("UTF-8")), is(TODAY)); assertThat(factory.create(LAST_YEAR.getString().getBytes("UTF-8")), is(LAST_YEAR)); }
@Test public void shouldCreateDateFromInputStreamContainingUtf8EncodingOfStringWithWellFormedDate() throws Exception { assertThat(factory.create(new ByteArrayInputStream(TODAY.getString().getBytes("UTF-8"))), is(TODAY)); assertThat(factory.create(new ByteArrayInputStream(LAST_YEAR.getString().getBytes("UTF-8"))), is(LAST_YEAR)); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateDateFromByteArrayContainingUtf8EncodingOfStringWithContentThatIsNotWellFormedDate() throws Exception { factory.create("something".getBytes("UTF-8")); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateDateFromInputStreamContainingUtf8EncodingOfStringWithContentThatIsNotWellFormedDate() throws Exception { factory.create(new ByteArrayInputStream("something".getBytes("UTF-8"))); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateDateFromReaderContainingStringWithContentThatIsNotWellFormedDate() throws Exception { factory.create(new ByteArrayInputStream("something".getBytes("UTF-8"))); }
@Test public void shouldCreateIteratorOverValuesWhenSuppliedIteratorOfUnknownObjects() { List<String> values = new ArrayList<>(); for (int i = 0; i != 10; ++i) { values.add(new ModeShapeDateTime(10000 + i).toString()); } Iterator<DateTime> iter = factory.create(values.iterator()); Iterator<String> valueIter = values.iterator(); while (iter.hasNext()) { assertThat(iter.next(), is(factory.create(valueIter.next()))); } }