
com.jpattern.orm.test.version.VersionTest Maven / Gradle / Ivy
package com.jpattern.orm.test.version;
import static org.junit.Assert.*;
import java.sql.Date;
import org.junit.Test;
import com.jpattern.orm.JPO;
import com.jpattern.orm.BaseTestShared;
import com.jpattern.orm.exception.OrmOptimisticLockException;
import com.jpattern.orm.session.Session;
import com.jpattern.orm.test.domain.DataVersionInteger;
import com.jpattern.orm.test.domain.DataVersionJodaDatetime;
import com.jpattern.orm.test.domain.DataVersionLong;
import com.jpattern.orm.test.domain.DataVersionSqlDate;
import com.jpattern.orm.test.domain.DataVersionUtilDate;
import com.jpattern.orm.transaction.ITransaction;
/**
*
* @author cinafr
*
*/
public class VersionTest extends BaseTestShared {
private Session session;
@Override
protected void setUp() throws Exception {
final JPO jpOrm = getJPOrm();
jpOrm.register(DataVersionInteger.class);
jpOrm.register(DataVersionLong.class);
jpOrm.register(DataVersionSqlDate.class);
jpOrm.register(DataVersionUtilDate.class);
jpOrm.register(DataVersionJodaDatetime.class);
this.session = jpOrm.session();
}
@Override
protected void tearDown() throws Exception {
}
@Test
public void testLongNewRecordVersion() throws Exception {
DataVersionLong dataVersion = new DataVersionLong();
dataVersion.setData("dataVersion1");
ITransaction tx = this.session.transaction();
dataVersion = this.session.save(dataVersion);
final long currentVersion = dataVersion.getVersion();
assertEquals(0l, currentVersion);
tx.commit();
tx = this.session.transaction();
dataVersion = this.session.update(dataVersion);
assertEquals(currentVersion+1 , dataVersion.getVersion());
tx.commit();
tx = this.session.transaction();
dataVersion = this.session.update(dataVersion);
assertEquals(currentVersion+2 , dataVersion.getVersion());
tx.commit();
}
@Test
public void testLongNewRecordVersionWithCustomVersionNumber() throws Exception {
DataVersionLong dataVersion = new DataVersionLong();
dataVersion.setData("dataVersion1");
dataVersion.setVersion(1000);
ITransaction tx = this.session.transaction();
dataVersion = this.session.save(dataVersion);
final long currentVersion = dataVersion.getVersion();
assertEquals(0l, currentVersion);
tx.commit();
tx = this.session.transaction();
dataVersion = this.session.update(dataVersion);
assertEquals(currentVersion+1 , dataVersion.getVersion());
tx.commit();
tx = this.session.transaction();
dataVersion = this.session.update(dataVersion);
assertEquals(currentVersion+2 , dataVersion.getVersion());
tx.commit();
}
@Test
public void testLongWrongVersionNumber() throws Exception {
DataVersionLong dataVersion = new DataVersionLong();
dataVersion.setData("dataVersion1");
dataVersion.setVersion(1000);
ITransaction tx = this.session.transaction();
dataVersion = this.session.save(dataVersion);
final long currentVersion = dataVersion.getVersion();
assertEquals(0l, currentVersion);
tx.commit();
tx = this.session.transaction();
dataVersion = this.session.update(dataVersion);
assertEquals(currentVersion+1 , dataVersion.getVersion());
tx.commit();
tx = this.session.transaction();
boolean wrongVersion = false;
try {
dataVersion.setVersion(1000);
dataVersion = this.session.update(dataVersion);
} catch (final OrmOptimisticLockException e) {
e.printStackTrace();
wrongVersion = true;
}
assertTrue(wrongVersion);
tx.commit();
}
@Test
public void testSqlDateNewRecordVersion() throws Exception {
DataVersionSqlDate dataVersion = new DataVersionSqlDate();
dataVersion.setData("dataVersion1");
assertNull( dataVersion.getVersion() );
ITransaction tx = this.session.transaction();
dataVersion = this.session.save(dataVersion);
assertNotNull( dataVersion.getVersion() );
Date currentVersion = dataVersion.getVersion();
tx.commit();
Thread.sleep(100);
tx = this.session.transaction();
dataVersion = this.session.update(dataVersion);
assertTrue( dataVersion.getVersion().getTime() >= currentVersion.getTime());
currentVersion = dataVersion.getVersion();
tx.commit();
Thread.sleep(100);
tx = this.session.transaction();
dataVersion = this.session.update(dataVersion);
assertTrue( dataVersion.getVersion().getTime() >= (currentVersion.getTime()));
tx.commit();
}
@Test
public void testIntegerNewRecordVersion() throws Exception {
DataVersionInteger dataVersion = new DataVersionInteger();
dataVersion.setData("dataVersion1");
assertNull( dataVersion.getVersion() );
ITransaction tx = this.session.transaction();
dataVersion = this.session.save(dataVersion);
final Integer currentVersion = dataVersion.getVersion();
assertEquals( Integer.valueOf(0), currentVersion);
tx.commit();
tx = this.session.transaction();
dataVersion = this.session.update(dataVersion);
assertEquals(Integer.valueOf(currentVersion+1) , dataVersion.getVersion());
tx.commit();
tx = this.session.transaction();
boolean wrongVersion = false;
try {
dataVersion.setVersion(1000);
dataVersion = this.session.update(dataVersion);
} catch (final OrmOptimisticLockException e) {
e.printStackTrace();
wrongVersion = true;
}
assertTrue(wrongVersion);
tx.commit();
}
@Test
public void testUtilDateNewRecordVersion() throws Exception {
DataVersionUtilDate dataVersion = new DataVersionUtilDate();
dataVersion.setData("dataVersion1");
assertNull( dataVersion.getVersion() );
ITransaction tx = this.session.transaction();
dataVersion = this.session.save(dataVersion);
assertNotNull( dataVersion.getVersion() );
java.util.Date currentVersion = dataVersion.getVersion();
tx.commit();
Thread.sleep(100);
tx = this.session.transaction();
dataVersion = this.session.update(dataVersion);
assertTrue( dataVersion.getVersion().getTime() >= currentVersion.getTime());
currentVersion = dataVersion.getVersion();
tx.commit();
Thread.sleep(100);
tx = this.session.transaction();
dataVersion = this.session.update(dataVersion);
assertTrue( dataVersion.getVersion().getTime() >= (currentVersion.getTime()));
tx.commit();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy