LongValueFactory extends AbstractValueFactory<Long> { @Override public Long create( String value ) { if (value == null) return null; try { return Long.valueOf(value.trim()); } catch (NumberFormatException err) { throw new ValueFormatException(value, getPropertyType(), GraphI18n.errorConvertingType.text(String.class.getSimpleName(), Long.class.getSimpleName(), value), err); } } LongValueFactory( TextDecoder decoder,
                             ValueFactories factories ); @Override ValueFactory<Long> with( ValueFactories valueFactories ); @Override Long create( String value ); @Override Long create( String value,
                        TextDecoder decoder ); @Override Long create( int value ); @Override Long create( long value ); @Override Long create( boolean value ); @Override Long create( float value ); @Override Long create( double value ); @Override Long create( BigDecimal value ); @Override Long create( Calendar value ); @Override Long create( Date value ); @Override Long create( DateTime value ); @Override Long create( Name value ); @Override Long create( Path value ); @Override Long create( Path.Segment value ); @Override Long create( Reference value ); @Override Long create( URI value ); @Override Long create( UUID value ); @Override Long create( NodeKey value ); @Override Long create( byte[] value ); @Override Long create( BinaryValue value ); @Override Long create( InputStream stream ); @Override Long[] createEmptyArray( int length );  }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateDoubleFromBooleanValue() { factory.create(true); }
@Test public void shouldCreateLongFromString() { assertThat(factory.create("1"), is(Long.valueOf(1))); assertThat(factory.create("-10"), is(Long.valueOf(-10))); assertThat(factory.create("100000101"), is(Long.valueOf(100000101))); }
@Test public void shouldCreateLongFromStringRegardlessOfLeadingAndTrailingWhitespace() { assertThat(factory.create(" 1 "), is(Long.valueOf(1))); assertThat(factory.create(" -10 "), is(Long.valueOf(-10))); assertThat(factory.create(" 100000101 "), is(Long.valueOf(100000101))); }
@Test public void shouldNotCreateLongFromIntegerValue() { assertThat(factory.create(1), is(1l)); }
@Test public void shouldNotCreateLongFromLongValue() { assertThat(factory.create(1l), is(1l)); }
@Test public void shouldNotCreateLongFromFloatValue() { assertThat(factory.create(1.0f), is(1l)); assertThat(factory.create(1.023f), is(1l)); assertThat(factory.create(1.923f), is(1l)); }
@Test public void shouldNotCreateLongFromDoubleValue() { assertThat(factory.create(1.0122d), is(1l)); }
@Test public void shouldCreateLongFromBigDecimal() { BigDecimal value = new BigDecimal(100); assertThat(factory.create(value), is(value.longValue())); }
@Test public void shouldCreateLongFromDate() { Date value = new Date(); assertThat(factory.create(value), is(Long.valueOf(value.getTime()))); }
@Test public void shouldCreateLongFromCalendar() { Calendar value = Calendar.getInstance(); assertThat(factory.create(value), is(Long.valueOf(value.getTimeInMillis()))); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateLongFromName() { factory.create(mock(Name.class)); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateLongFromPath() { factory.create(mock(Path.class)); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateLongFromReference() { factory.create(mock(Reference.class)); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateLongFromUri() throws Exception { factory.create(new URI("http: }
@Test public void shouldCreateLongFromByteArrayContainingUtf8EncodingOfStringWithLong() throws Exception { assertThat(factory.create("0".getBytes("UTF-8")), is(0l)); assertThat(factory.create("10".getBytes("UTF-8")), is(10l)); assertThat(factory.create("-103".getBytes("UTF-8")), is(-103l)); assertThat(factory.create("1003044".getBytes("UTF-8")), is(1003044l)); }
@Test public void shouldCreateLongFromInputStreamContainingUtf8EncodingOfStringWithLong() throws Exception { assertThat(factory.create(new ByteArrayInputStream("0".getBytes("UTF-8"))), is(0l)); assertThat(factory.create(new ByteArrayInputStream("10".getBytes("UTF-8"))), is(10l)); assertThat(factory.create(new ByteArrayInputStream("-103".getBytes("UTF-8"))), is(-103l)); assertThat(factory.create(new ByteArrayInputStream("1003044".getBytes("UTF-8"))), is(1003044l)); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateLongFromByteArrayContainingUtf8EncodingOfStringWithContentsOtherThanLong() throws Exception { factory.create("something".getBytes("UTF-8")); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateLongFromInputStreamContainingUtf8EncodingOfStringWithContentsOtherThanLong() throws Exception { factory.create(new ByteArrayInputStream("something".getBytes("UTF-8"))); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateLongFromReaderContainingStringWithContentsOtherThanLong() throws Exception { factory.create(new ByteArrayInputStream("something".getBytes("UTF-8"))); }
@Test public void shouldCreateIteratorOverValuesWhenSuppliedIteratorOfUnknownObjects() { List<String> values = new ArrayList<String>(); for (int i = 0; i != 10; ++i) values.add(" " + i); Iterator<Long> iter = factory.create(values.iterator()); Iterator<String> valueIter = values.iterator(); while (iter.hasNext()) { assertThat(iter.next(), is(factory.create(valueIter.next()))); } }