HHH-3843 - Refactoring, test simplified

This commit is contained in:
Lukasz Antoniak 2011-10-22 11:20:46 +02:00
parent 86482c630b
commit 6a89afccb6
4 changed files with 11 additions and 122 deletions

View File

@ -38,7 +38,6 @@ import org.hibernate.tuple.entity.EntityMetamodel;
/**
* @author Adam Warski (adam at warski dot org)
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
public class AuditProcess implements BeforeTransactionCompletionProcess {
private final RevisionInfoGenerator revisionInfoGenerator;
@ -80,8 +79,8 @@ public class AuditProcess implements BeforeTransactionCompletionProcess {
String entityName = vwu.getEntityName();
Pair<String, Object> usedIdsKey = Pair.make(entityName, entityId);
AuditWorkUnit other = getAlreadyScheduledWorkUnit(usedIdsKey);
if (other != null) {
if (usedIds.containsKey(usedIdsKey)) {
AuditWorkUnit other = usedIds.get(usedIdsKey);
AuditWorkUnit result = vwu.dispatch(other);
if (result != other) {
@ -100,25 +99,6 @@ public class AuditProcess implements BeforeTransactionCompletionProcess {
}
}
/**
* Checks if another work unit associated with the same entity hierarchy and identifier has already been scheduled.
* @param idKey Work unit's identifier.
* @return Corresponding work unit or {@code null} if no satisfying result was found.
*/
private AuditWorkUnit getAlreadyScheduledWorkUnit(Pair<String, Object> idKey) {
EntityMetamodel entityMetamodel = session.getFactory().getEntityPersister(idKey.getFirst()).getEntityMetamodel();
String rootEntityName = entityMetamodel.getRootName();
EntityMetamodel rootEntityMetamodel = session.getFactory().getEntityPersister(rootEntityName).getEntityMetamodel();
// Checking all possible subtypes, supertypes and the actual class.
for (Object entityName : rootEntityMetamodel.getSubclassEntityNames()) {
Pair<String, Object> key = Pair.make((String) entityName, idKey.getSecond());
if (usedIds.containsKey(key)) {
return usedIds.get(key);
}
}
return null;
}
private void executeInSession(Session session) {
// Making sure the revision data is persisted.
Object currentRevisionData = getCurrentRevisionData(session, true);

View File

@ -17,10 +17,6 @@ import java.util.Arrays;
@TestForIssue(jiraKey = "HHH-3843")
public class ParentReferencingChildTest extends AbstractEntityTest {
Person expLukaszRev1 = null;
Person expLukaszRev2 = null;
Person expAdamRev4 = null;
Role expDirectorRev3 = null;
Role expAdminRev2 = null;
Role expAdminRev1 = null;
public void configure(Ejb3Configuration cfg) {
@ -37,11 +33,11 @@ public class ParentReferencingChildTest extends AbstractEntityTest {
// Revision 1
em.getTransaction().begin();
Person lukasz = new Person();
lukasz.setName("Lukasz");
lukasz.setName("Lukasz");
lukasz.setGroup("IT");
em.persist(lukasz);
Role admin = new Role();
admin.setName("Admin");
admin.setName("Admin");
admin.setGroup("Confidential");
lukasz.getRoles().add(admin);
admin.getMembers().add(lukasz);
@ -50,106 +46,32 @@ public class ParentReferencingChildTest extends AbstractEntityTest {
expLukaszRev1 = new Person(lukasz.getId(), "IT", "Lukasz");
expAdminRev1 = new Role(admin.getId(), "Confidential", "Admin");
// Revision 2
em.getTransaction().begin();
lukasz = em.find(Person.class, lukasz.getId());
lukasz.setGroup("Senior IT");
lukasz.setName("Lukasz Antoniak");
admin = em.find(Role.class, admin.getId());
admin.setGroup("Very Confidential");
em.getTransaction().commit();
expAdminRev2 = new Role(admin.getId(), "Very Confidential", "Admin");
expLukaszRev2 = new Person(lukasz.getId(), "Senior IT", "Lukasz Antoniak");
// Revision 3
em.getTransaction().begin();
lukasz = em.find(Person.class, lukasz.getId());
Role director = new Role();
director.setName("Director");
director.getMembers().add(lukasz);
em.persist(director);
lukasz.getRoles().add(director);
em.getTransaction().commit();
expDirectorRev3 = new Role(director.getId(), null, "Director");
// Revision 4
em.getTransaction().begin();
Person adam = new Person();
adam.setName("Adam");
adam.setGroup("CEO");
em.persist(adam);
director = em.find(Role.class, director.getId());
director.getMembers().add(adam);
adam.getRoles().add(director);
em.getTransaction().commit();
expAdamRev4 = new Person(adam.getId(), "CEO", "Adam");
// Revision 5
em.getTransaction().begin();
adam = em.find(Person.class, adam.getId());
admin = em.find(Role.class, admin.getId());
admin.getMembers().add(adam);
em.getTransaction().commit();
// Revision 6
em.getTransaction().begin();
adam = em.find(Person.class, adam.getId());
adam.setName("Adam Warski");
em.getTransaction().commit();
}
@Test
public void testRevisionsCounts() {
Assert.assertEquals(Arrays.asList(1, 2, 3), getAuditReader().getRevisions(Person.class, expLukaszRev1.getId()));
Assert.assertEquals(Arrays.asList(1, 2, 3), getAuditReader().getRevisions(RightsSubject.class, expLukaszRev1.getId()));
Assert.assertEquals(Arrays.asList(1), getAuditReader().getRevisions(Person.class, expLukaszRev1.getId()));
Assert.assertEquals(Arrays.asList(1), getAuditReader().getRevisions(RightsSubject.class, expLukaszRev1.getId()));
Assert.assertEquals(Arrays.asList(4, 5, 6), getAuditReader().getRevisions(Person.class, expAdamRev4.getId()));
Assert.assertEquals(Arrays.asList(4, 5, 6), getAuditReader().getRevisions(RightsSubject.class, expAdamRev4.getId()));
Assert.assertEquals(Arrays.asList(1, 2, 5), getAuditReader().getRevisions(Role.class, expAdminRev1.getId()));
Assert.assertEquals(Arrays.asList(1, 2, 5), getAuditReader().getRevisions(RightsSubject.class, expAdminRev1.getId()));
Assert.assertEquals(Arrays.asList(3, 4), getAuditReader().getRevisions(Role.class, expDirectorRev3.getId()));
Assert.assertEquals(Arrays.asList(3, 4), getAuditReader().getRevisions(RightsSubject.class, expDirectorRev3.getId()));
}
@Test
public void testHistoryOfAdam() {
Person adamRev4 = getAuditReader().find(Person.class, expAdamRev4.getId(), 4);
RightsSubject rightsSubjectRev5 = getAuditReader().find(RightsSubject.class, expAdamRev4.getId(), 5);
Assert.assertEquals(expAdamRev4, adamRev4);
Assert.assertEquals(TestTools.makeSet(expDirectorRev3), adamRev4.getRoles());
Assert.assertEquals(TestTools.makeSet(expDirectorRev3, expAdminRev2), rightsSubjectRev5.getRoles());
Assert.assertEquals(Arrays.asList(1), getAuditReader().getRevisions(Role.class, expAdminRev1.getId()));
Assert.assertEquals(Arrays.asList(1), getAuditReader().getRevisions(RightsSubject.class, expAdminRev1.getId()));
}
@Test
public void testHistoryOfLukasz() {
Person lukaszRev1 = getAuditReader().find(Person.class, expLukaszRev1.getId(), 1);
Person lukaszRev2 = getAuditReader().find(Person.class, expLukaszRev1.getId(), 2);
RightsSubject rightsSubjectRev3 = getAuditReader().find(RightsSubject.class, expLukaszRev1.getId(), 3);
Person lukaszRev3 = getAuditReader().find(Person.class, expLukaszRev1.getId(), 3);
RightsSubject rightsSubjectLukaszRev1 = getAuditReader().find(RightsSubject.class, expLukaszRev1.getId(), 1);
Assert.assertEquals(expLukaszRev1, lukaszRev1);
Assert.assertEquals(expLukaszRev2, lukaszRev2);
Assert.assertEquals(TestTools.makeSet(expAdminRev1), lukaszRev1.getRoles());
Assert.assertEquals(TestTools.makeSet(expAdminRev2, expDirectorRev3), rightsSubjectRev3.getRoles());
Assert.assertEquals(TestTools.makeSet(expAdminRev2, expDirectorRev3), lukaszRev3.getRoles());
Assert.assertEquals(TestTools.makeSet(expAdminRev1), rightsSubjectLukaszRev1.getRoles());
}
@Test
public void testHistoryOfAdmin() {
Role adminRev1 = getAuditReader().find(Role.class, expAdminRev1.getId(), 1);
Role adminRev2 = getAuditReader().find(Role.class, expAdminRev1.getId(), 2);
Role adminRev5 = getAuditReader().find(Role.class, expAdminRev1.getId(), 5);
Assert.assertEquals(expAdminRev1, adminRev1);
Assert.assertEquals(expAdminRev2, adminRev2);
Assert.assertEquals(TestTools.makeSet(expLukaszRev1), adminRev1.getMembers());
Assert.assertEquals(TestTools.makeSet(expLukaszRev2, expAdamRev4), adminRev5.getMembers());
}
}

View File

@ -9,7 +9,6 @@ import javax.persistence.Table;
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
@Entity
@Table(name = "APP_PERSON")
@Audited
public class Person extends RightsSubject {
private String name;

View File

@ -10,16 +10,12 @@ import java.util.Set;
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
@Entity
@Table(name = "APP_RIGHTSSUBJCT")
@Inheritance(strategy = InheritanceType.JOINED)
@Audited
public class RightsSubject {
@Id
@GeneratedValue
private Long id;
@Version
private Long version;
@Column(name = "APP_GROUP")
private String group;
@ -57,7 +53,7 @@ public class RightsSubject {
@Override
public String toString() {
return "RightsSubject(id = " + id + ", version = " + version + ", group = " + group + ")";
return "RightsSubject(id = " + id + ", group = " + group + ")";
}
public Long getId() {
@ -68,14 +64,6 @@ public class RightsSubject {
this.id = id;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
public Set<Role> getRoles() {
return roles;
}