1

I have a class Article that has a field ean that is annotated with the org.hibernate.validator.constraints.EAN.

How can I generate valid ean13 values for doing unit tests on a bunch of Articles?

mat_boy
  • 12,998
  • 22
  • 72
  • 116

2 Answers2

2

Please find an example below that tests valid / invalid EAN codes :

The following site was used to get a valid EAN 13 code : http://www.gomaro.ch/lecheck.htm

Maybe you would be interested by Barcode4j API too (for instance) : http://barcode4j.sourceforge.net/

package ean;

import static org.junit.Assert.assertTrue;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import org.hibernate.validator.constraints.EAN;
import org.junit.Before;
import org.junit.Test;

/**
 * Test EAN 13 constraint. EAN 13 = 12 digits + 1 check digit.
 *
 */
public class testEAN13 {

    private static Validator validator;

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

    @Test
    public void validEAN() {
        Article product = new Article("7894561330231");
        Set<ConstraintViolation<Article>> violations = validator.validate(product);
        assertTrue(violations.isEmpty());
    }

    @Test
    public void invalidEAN() {
        Article product = new Article("7894561330235");
        Set<ConstraintViolation<Article>> violations = validator.validate(product);
        assertTrue(violations.size() == 1);
    }

    class Article {

        @EAN
        private final String ean;

        private Article(String ean) {
            this.ean = ean;
        }
    }
}

Maven required dependencies :

<dependencies>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.2.2.Final</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.el</artifactId>
        <version>3.0.0</version>
        <scope>test</scope>
    </dependency>

</dependencies>

Note : Hibernate Validator requires Expression Language dependency.

André Blaszczyk
  • 750
  • 4
  • 17
  • Well, once that I set the @EAN validation constraint on the object, if I run an integration test and I try to persist the entity I get an exception by default. So I don't need to test again the validation. It comes from another library. What I need is a generator for EAN13 codes in Java, but seems that I have to use an external tool. Thanks for the effort. – mat_boy Nov 01 '15 at 09:01
0

Ok, I created an EAN13 generator after some research over the Internet

public class EANGenerator {

    private static long _12_DIGITS = 100000000000L;

    private EANGenerator() {
    }

    /**
     * Generate the next EAN13 code
     *
     * @return
     */
    public static String getEAN13() {
        final String eanWithoutDigit = Long.toString(_12_DIGITS++);
        assertThat(eanWithoutDigit, length(equalTo(12)));

        int sum = 0;
        for (int i = 0; i < eanWithoutDigit.length(); i++) {
            sum = sum + (Integer.parseInt(String.valueOf(eanWithoutDigit.charAt(i))) * (i % 2 == 0 ? 1 : 3));
        }
        int checkDigit = sum % 10 == 0 ? 0 : (10-(sum % 10));
        return eanWithoutDigit + checkDigit;
    }

}

It suffices to call the static method from this helper class to get the next EAN13. Clearly, calling this for a very very large number of times will provide an exception to be thrown because of the number of digits to be checked (from 12 to 13)

mat_boy
  • 12,998
  • 22
  • 72
  • 116