1. 序章

チュートリアルJavaBean検証の基本では、基本的な java x 検証をさまざまなタイプに適用する方法を説明しました。このチュートリアルでは、使用に焦点を当てます。 java x BigDecimalによる検証。

2. BigDecimalインスタンスの検証

残念ながら、 BigDecimalでは、従来の@Minまたは@Maxjavaxアノテーションを使用できません。

幸いなことに、それらを操作するための専用の注釈セットがあります。

  • @DecimalMin

  • @Digits

  • @DecimalMax

BigDecimal は、精度が高いため、財務計算の最初の選択肢です。

タイプBigDecimalのフィールドを持つInvoiceクラスを見てみましょう。

public class Invoice {

    @DecimalMin(value = "0.0", inclusive = false)
    @Digits(integer=3, fraction=2)
    private BigDecimal price;
    private String description;

    public Invoice(BigDecimal price, String description) {
        this.price = price;
        this.description = description;
    }
}

2.1. @DecimalMin

注釈付き要素は、指定された最小値以上の値である必要があります。 @DecimalMin には、指定された最小値かどうかを示す属性includeがあります。包括的または排他的です。

2.2. @DecimalMax

@DecimalMax は、@DecimalMinに相当します。 注釈付き要素は、指定された最大値以下の値である必要があります。 @DecimalMax には、指定された最大値が包括的であるか排他的であるかを指定する包括的属性があります。

また、@Minおよび@Maxは、long値のみを受け入れます。 @DecimalMinおよび@DecimalMaxでは、値を string 形式で指定できます。これは、任意の数値タイプにすることができます。

2.3. @Digits

多くの場合、10進数整数部分と分数部分の桁数を検証する必要があります。

@Digitアノテーションには、整数と分数の2つの属性があり、整数部分の許可桁数と数値分数部分を指定します。 。

公式ドキュメントに従って、 integer を使用すると、この数値に受け入れられる整数の最大数を指定できます。

同様に、 fraction 属性を使用すると、この数値に受け入れられる小数桁の最大数を指定できます。

2.4. テストケース

これらのアノテーションの動作を見てみましょう。

まず、検証に従って無効な価格の請求書を作成するテストを追加し、検証が失敗することを確認します。

public class InvoiceUnitTest {

    private static Validator validator;

    @BeforeClass
    public static void setupValidatorInstance() {
        validator = Validation.buildDefaultValidatorFactory().getValidator();
    }

    @Test
    public void whenMoreThanThreeIntegerDigits_thenShouldGiveConstraintViolations() {
        Invoice invoice = new Invoice(new BigDecimal("1021.21"), "Book purchased");
        Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
        assertThat(violations).hasSize(1);
        assertThat(violations)
            .extracting("message")
            .containsOnly("numeric value out of bounds (<3 digits>.<2 digits> expected)");
    }
}

次に、正しい価格で検証を確認しましょう。

@Test
public void whenLessThanThreeIntegerDigits_thenShouldNotGiveConstraintViolations() {
    Invoice invoice = new Invoice(new BigDecimal("10.21"), "Book purchased");
    Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
    assertThat(violations).isEmpty();
}

同様の方法で、小数部分の検証がどのように機能するかを見てみましょう。

@Test
public void whenTwoFractionDigits_thenShouldNotGiveConstraintViolations() {
    Invoice invoice = new Invoice(new BigDecimal("99.99"), "Book purchased");
    Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
    assertThat(violations).isEmpty();
}

@Test
public void whenMoreThanTwoFractionDigits_thenShouldGiveConstraintViolations() {
    Invoice invoice = new Invoice(new BigDecimal("99.999"), "Book purchased");
    Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
    assertThat(violations).hasSize(1);
    assertThat(violations)
        .extracting("message")
        .containsOnly("numeric value out of bounds (<3 digits>.<2 digits> expected)");
}

0.00に等しい価格は、制約に違反するはずです。

@Test
public void whenPriceIsZero_thenShouldGiveConstraintViolations() {
    Invoice invoice = new Invoice(new BigDecimal("0.00"), "Book purchased");
    Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
    assertThat(violations).hasSize(1);
    assertThat(violations)
        .extracting("message")
        .containsOnly("must be greater than 0.0");
}

最後に、価格がゼロより大きい場合を見てみましょう。

@Test
public void whenPriceIsGreaterThanZero_thenShouldNotGiveConstraintViolations() {
    Invoice invoice = new Invoice(new BigDecimal("100.50"), "Book purchased");
    Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
    assertThat(violations).isEmpty();
}

3. 結論

この記事では、BigDecimalでjavax検証を使用する方法を説明しました。

すべてのコードスニペットはGitHubで見つけることができます。