Skip to content

Commit

Permalink
BigDecimalのscaleをチェックする処理を追加 (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
siosio authored and kiyohome committed Mar 27, 2017
1 parent 8a6943a commit 9020a3b
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group = 'com.nablarch.framework'
version = '1.1.3'
version = '1.1.4'
description = 'ビーンユーティリティ'

buildscript {
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Apr 27 13:44:01 JST 2016
#Mon Mar 27 10:57:17 JST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import nablarch.core.beans.ConversionException;
import nablarch.core.beans.Converter;
import nablarch.core.util.NumberUtil;

/**
* {@code BigDecimal}型への変換を行う {@link Converter} 。
Expand Down Expand Up @@ -46,7 +47,9 @@ public BigDecimal convert(final Object value) {
}
} else if (value instanceof String) {
try {
return new BigDecimal(value.toString());
final BigDecimal result = new BigDecimal(value.toString());
NumberUtil.verifyBigDecimalScale(result);
return result;
} catch (NumberFormatException e) {
throw new ConversionException(BigDecimal.class, value);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package nablarch.core.beans.converter;

import static org.hamcrest.Matchers.closeTo;
import static org.hamcrest.Matchers.comparesEqualTo;
import static org.junit.Assert.assertThat;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;

import nablarch.core.beans.ConversionException;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

/**
* {@link BigDecimalConverter}のテスト。
*/
public class BigDecimalConverterTest {

@Rule
public ExpectedException expectedException = ExpectedException.none();

private final BigDecimalConverter sut = new BigDecimalConverter();

@Test
public void fromBigDecimal() throws Exception {
final BigDecimal input = new BigDecimal("100.1234");
final BigDecimal actual = sut.convert(input);

assertThat(actual, comparesEqualTo(input));
}

@Test
public void fromBigInteger() throws Exception {
final BigInteger input = new BigInteger("1234554321");
final BigDecimal actual = sut.convert(input);

assertThat(actual, comparesEqualTo(new BigDecimal("1234554321")));
}

@Test
public void fromDouble() throws Exception {
final BigDecimal actual = sut.convert(1.1D);
assertThat(actual, closeTo(new BigDecimal("1.1"), new BigDecimal("0.000001")));
}

@Test
public void fromFloat() throws Exception {
final BigDecimal actual = sut.convert(1.2F);
assertThat(actual, closeTo(new BigDecimal("1.2"), new BigDecimal("0.00001")));
}

@Test
public void fromInteger() throws Exception {
final BigDecimal actual = sut.convert(Integer.MAX_VALUE);
assertThat(actual, comparesEqualTo(new BigDecimal(Integer.MAX_VALUE)));
}

@Test
public void fromLong() throws Exception {
final BigDecimal actual = sut.convert(Long.MAX_VALUE);
assertThat(actual, comparesEqualTo(BigDecimal.valueOf(Long.MAX_VALUE)));
}

@Test
public void fromString() throws Exception {
final BigDecimal actual = sut.convert("100");
assertThat(actual, comparesEqualTo(new BigDecimal("100")));
}

@Test
public void fromExponentialExpression() throws Exception {
final BigDecimal actual = sut.convert("1e2");
assertThat(actual, comparesEqualTo(new BigDecimal("100")));
}

@Test
public void fromBooleanTrue() throws Exception {
final BigDecimal actual = sut.convert(true);
assertThat(actual, comparesEqualTo(BigDecimal.ONE));
}

@Test
public void fromBooleanFalse() throws Exception {
final BigDecimal actual = sut.convert(false);
assertThat(actual, comparesEqualTo(BigDecimal.ZERO));
}

@Test
public void fromStringArray() throws Exception {
final BigDecimal actual = sut.convert(new String[] {"123"});
assertThat(actual, comparesEqualTo(new BigDecimal("123")));
}

@Test
public void fromInvalidString() throws Exception {
expectedException.expect(ConversionException.class);
expectedException.expectMessage("Can't convert あいうえお to BigDecimal.");
sut.convert("あいうえお");
}

@Test
public void fromUnsupportedDataType() throws Exception {
expectedException.expect(ConversionException.class);
expectedException.expectMessage("Can't convert [] to BigDecimal.");
sut.convert(new ArrayList<String>());
}

@Test
public void fromInvalidScale() throws Exception {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Illegal scale(10000): needs to be between(-9999, 9999)");
sut.convert("100e-10000");
}

@Test
public void fromInvalidScaleInArray() throws Exception {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Illegal scale(-99999): needs to be between(-9999, 9999)");
sut.convert(new String[] {"100e99999"});
}
}

0 comments on commit 9020a3b

Please sign in to comment.