DecimalValueFactory extends AbstractValueFactory<BigDecimal> { @Override public BigDecimal create( String value ) { if (value == null) return null; try { return new BigDecimal(value.trim()); } catch (NumberFormatException err) { throw new ValueFormatException(value, getPropertyType(), GraphI18n.errorConvertingType.text(String.class.getSimpleName(), BigDecimal.class.getSimpleName(), value), err); } } DecimalValueFactory( TextDecoder decoder,
                                ValueFactories factories ); @Override ValueFactory<BigDecimal> with( ValueFactories valueFactories ); @Override BigDecimal create( String value ); @Override BigDecimal create( String value,
                              TextDecoder decoder ); @Override BigDecimal create( int value ); @Override BigDecimal create( long value ); @Override BigDecimal create( boolean value ); @Override BigDecimal create( float value ); @Override BigDecimal create( double value ); @Override BigDecimal create( BigDecimal value ); @Override BigDecimal create( Calendar value ); @Override BigDecimal create( Date value ); @Override BigDecimal create( DateTime value ); @Override BigDecimal create( Name value ); @Override BigDecimal create( Path value ); @Override BigDecimal create( Path.Segment value ); @Override BigDecimal create( Reference value ); @Override BigDecimal create( URI value ); @Override BigDecimal create( UUID value ); @Override BigDecimal create( NodeKey value ); @Override BigDecimal create( byte[] value ); @Override BigDecimal create( BinaryValue value ); @Override BigDecimal create( InputStream stream ); @Override BigDecimal[] createEmptyArray( int length );  }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateDecimalFromBooleanValue() { factory.create(true); }
@Test public void shouldCreateDecimalFromString() { assertThat(factory.create("1"), is(BigDecimal.valueOf(1))); assertThat(factory.create("-1.0"), is(BigDecimal.valueOf(-1.0d))); assertThat(factory.create("100.000101"), is(BigDecimal.valueOf(100.000101d))); }
@Test public void shouldCreateDecimalFromStringRegardlessOfLeadingAndTrailingWhitespace() { assertThat(factory.create(" 1 "), is(BigDecimal.valueOf(1))); assertThat(factory.create(" -1.0 "), is(BigDecimal.valueOf(-1.0d))); assertThat(factory.create(" 100.000101 "), is(BigDecimal.valueOf(100.000101d))); }
@Test public void shouldCreateDecimalFromIntegerValue() { assertThat(factory.create(1), is(BigDecimal.valueOf(1))); }
@Test public void shouldCreateDecimalFromLongValue() { assertThat(factory.create(1l), is(BigDecimal.valueOf(1l))); }
@Test public void shouldCreateDecimalFromFloatValue() { assertThat(factory.create(1.0f), is(BigDecimal.valueOf(1.0f))); }
@Test public void shouldCreateDecimalFromDoubleValue() { assertThat(factory.create(1.0d), is(BigDecimal.valueOf(1.0d))); }
@Test public void shouldCreateDecimalFromBigDecimal() { BigDecimal value = new BigDecimal(100); assertThat(factory.create(value), is(value)); }
@Test public void shouldCreateDecimalFromDate() { Date value = new Date(); assertThat(factory.create(value), is(BigDecimal.valueOf(value.getTime()))); }
@Test public void shouldCreateDecimalFromCalendar() { Calendar value = Calendar.getInstance(); assertThat(factory.create(value), is(BigDecimal.valueOf(value.getTimeInMillis()))); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateDecimalFromName() { factory.create(mock(Name.class)); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateDecimalFromPath() { factory.create(mock(Path.class)); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateDecimalFromReference() { factory.create(mock(Reference.class)); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateDecimalFromUri() throws Exception { factory.create(new URI("http: }
@Test public void shouldCreateDecimalFromByteArrayContainingUtf8EncodingOfStringWithDecimal() throws Exception { assertThat(factory.create("1".getBytes("UTF-8")), is(BigDecimal.valueOf(1l))); assertThat(factory.create("-1.0".getBytes("UTF-8")), is(BigDecimal.valueOf(-1.d))); assertThat(factory.create("100.000101".getBytes("UTF-8")), is(BigDecimal.valueOf(100.000101d))); }
@Test public void shouldCreateDecimalFromInputStreamContainingUtf8EncodingOfStringWithDecimal() throws Exception { assertThat(factory.create(new ByteArrayInputStream("1".getBytes("UTF-8"))), is(BigDecimal.valueOf(1l))); assertThat(factory.create(new ByteArrayInputStream("-1.0".getBytes("UTF-8"))), is(BigDecimal.valueOf(-1.d))); assertThat(factory.create(new ByteArrayInputStream("100.000101".getBytes("UTF-8"))), is(BigDecimal.valueOf(100.000101d))); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateDecimalFromByteArrayContainingUtf8EncodingOfStringWithContentsOtherThanDecimal() throws Exception { factory.create("something".getBytes("UTF-8")); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateDecimalFromInputStreamContainingUtf8EncodingOfStringWithContentsOtherThanDecimal() throws Exception { factory.create(new ByteArrayInputStream("something".getBytes("UTF-8"))); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateDecimalFromReaderContainingStringWithContentsOtherThanDecimal() { factory.create(new StringReader("something")); }
@Test public void shouldCreateIteratorOverValuesWhenSuppliedIteratorOfUnknownObjects() { List<String> values = new ArrayList<String>(); for (int i = 0; i != 10; ++i) values.add("" + i); Iterator<BigDecimal> iter = factory.create(values.iterator()); Iterator<String> valueIter = values.iterator(); while (iter.hasNext()) { assertThat(iter.next(), is(factory.create(valueIter.next()))); } }