package com.vladmihalcea.book.hpjp.hibernate.concurrency.version;

import com.vladmihalcea.book.hpjp.util.AbstractTest;
import org.junit.Test;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Version;

import static org.junit.Assert.assertEquals;

/**
 * @author Vlad Mihalcea
 */
public class DefaultMinValueShortVersionTest extends AbstractTest {

    @Override
    protected Class<?>[] entities() {
        return new Class<?>[]{
                Product.class
        };
    }

    @Test
    public void testOptimisticLocking() {

        doInJPA(entityManager -> {
            entityManager.persist(
                new Product()
                    .setId(1L)
                    .setQuantity(10)
                    .setVersion(Short.MAX_VALUE)
            );
        });

        doInJPA(entityManager -> {
            Product product = entityManager.find(Product.class, 1L);

            assertEquals(Short.MAX_VALUE, product.getVersion());

            product.setQuantity(9);
        });

        doInJPA(entityManager -> {
            Product product = entityManager.find(Product.class, 1L);

            assertEquals(Short.MIN_VALUE, product.getVersion());
        });
    }

    @Entity(name = "Product")
    @Table(name = "product")
    public static class Product {

        @Id
        private Long id;

        private int quantity;

        @Version
        private short version;

        public Long getId() {
            return id;
        }

        public Product setId(Long id) {
            this.id = id;
            return this;
        }

        public int getQuantity() {
            return quantity;
        }

        public Product setQuantity(int quantity) {
            this.quantity = quantity;
            return this;
        }

        public short getVersion() {
            return version;
        }

        public Product setVersion(short version) {
            this.version = version;
            return this;
        }
    }
}

@@ -113,16 +113,75 @@
 ctTest;%0A
+import com.vladmihalcea.book.hpjp.util.providers.Database;%0A
 import o
@@ -600,24 +600,112 @@
   %7D;%0A    %7D%0A%0A
+    @Override%0A    protected Database database() %7B%0A        return Database.MYSQL;%0A    %7D%0A%0A
     @Test%0A  

Add test for MySQL version overflowing