@Test public void shouldDetectInheritedAttributes() { Auditor auditor = new Auditor(); SubClassEntity entity = new SubClassEntity("uuid-1", "Adam", 25); ValueContainer pastState = auditor.inspectEntity(entity); entity.setName("Eva"); entity.setAge(21); entity.setWeight(BigDecimal.ONE); entity.increaseVersion(); ValueContainer futureState = auditor.inspectEntity(entity); final SortedMap<String, String> changes = futureState.compare(pastState); assertThat(changes.size(), is(2)); assertThat(changes.get(SuperClassEntity.NAME), is("Eva")); assertThat(changes.get(SubClassEntity.AGE), is("21")); assertFalse(changes.containsKey(BaseEntity.VERSION)); assertFalse(changes.containsKey(UnauditedMiddleClassEntity.WEIGHT)); }
ValueContainer inspectEntity(HasUuid entity) { DefaultValueContainer result = new DefaultValueContainer(); EntityInspector inspector = new EntityInspector(entity); for (Method currentAttribute : inspector.getAuditedAttributes()) { try { AuditedAttribute auditedAttribute = currentAttribute.getAnnotation(AuditedAttribute.class); AuditedCollection auditedCollection = currentAttribute.getAnnotation(AuditedCollection.class); boolean isCollection = Collection.class.isAssignableFrom(currentAttribute.getReturnType()); if (auditedAttribute != null || !isCollection) { logAttributeChange(result, inspector, currentAttribute, auditedAttribute); } else if (auditedCollection != null || isCollection) { logCollectionChange(result, inspector, currentAttribute, auditedCollection); } } catch (InvocationTargetException | IllegalAccessException e) { throw new AuditlogException(String.format("No changes for entity %s can be detected.", entity.toString()), e); } } return result; }
Auditor implements Serializable { ValueContainer inspectEntity(HasUuid entity) { DefaultValueContainer result = new DefaultValueContainer(); EntityInspector inspector = new EntityInspector(entity); for (Method currentAttribute : inspector.getAuditedAttributes()) { try { AuditedAttribute auditedAttribute = currentAttribute.getAnnotation(AuditedAttribute.class); AuditedCollection auditedCollection = currentAttribute.getAnnotation(AuditedCollection.class); boolean isCollection = Collection.class.isAssignableFrom(currentAttribute.getReturnType()); if (auditedAttribute != null || !isCollection) { logAttributeChange(result, inspector, currentAttribute, auditedAttribute); } else if (auditedCollection != null || isCollection) { logCollectionChange(result, inspector, currentAttribute, auditedCollection); } } catch (InvocationTargetException | IllegalAccessException e) { throw new AuditlogException(String.format("No changes for entity %s can be detected.", entity.toString()), e); } } return result; } }
Auditor implements Serializable { ValueContainer inspectEntity(HasUuid entity) { DefaultValueContainer result = new DefaultValueContainer(); EntityInspector inspector = new EntityInspector(entity); for (Method currentAttribute : inspector.getAuditedAttributes()) { try { AuditedAttribute auditedAttribute = currentAttribute.getAnnotation(AuditedAttribute.class); AuditedCollection auditedCollection = currentAttribute.getAnnotation(AuditedCollection.class); boolean isCollection = Collection.class.isAssignableFrom(currentAttribute.getReturnType()); if (auditedAttribute != null || !isCollection) { logAttributeChange(result, inspector, currentAttribute, auditedAttribute); } else if (auditedCollection != null || isCollection) { logCollectionChange(result, inspector, currentAttribute, auditedCollection); } } catch (InvocationTargetException | IllegalAccessException e) { throw new AuditlogException(String.format("No changes for entity %s can be detected.", entity.toString()), e); } } return result; }  }
Auditor implements Serializable { ValueContainer inspectEntity(HasUuid entity) { DefaultValueContainer result = new DefaultValueContainer(); EntityInspector inspector = new EntityInspector(entity); for (Method currentAttribute : inspector.getAuditedAttributes()) { try { AuditedAttribute auditedAttribute = currentAttribute.getAnnotation(AuditedAttribute.class); AuditedCollection auditedCollection = currentAttribute.getAnnotation(AuditedCollection.class); boolean isCollection = Collection.class.isAssignableFrom(currentAttribute.getReturnType()); if (auditedAttribute != null || !isCollection) { logAttributeChange(result, inspector, currentAttribute, auditedAttribute); } else if (auditedCollection != null || isCollection) { logCollectionChange(result, inspector, currentAttribute, auditedCollection); } } catch (InvocationTargetException | IllegalAccessException e) { throw new AuditlogException(String.format("No changes for entity %s can be detected.", entity.toString()), e); } } return result; }  void register(HasUuid entity); ChangeEvent detectChanges(HasUuid entity); }
Auditor implements Serializable { ValueContainer inspectEntity(HasUuid entity) { DefaultValueContainer result = new DefaultValueContainer(); EntityInspector inspector = new EntityInspector(entity); for (Method currentAttribute : inspector.getAuditedAttributes()) { try { AuditedAttribute auditedAttribute = currentAttribute.getAnnotation(AuditedAttribute.class); AuditedCollection auditedCollection = currentAttribute.getAnnotation(AuditedCollection.class); boolean isCollection = Collection.class.isAssignableFrom(currentAttribute.getReturnType()); if (auditedAttribute != null || !isCollection) { logAttributeChange(result, inspector, currentAttribute, auditedAttribute); } else if (auditedCollection != null || isCollection) { logCollectionChange(result, inspector, currentAttribute, auditedCollection); } } catch (InvocationTargetException | IllegalAccessException e) { throw new AuditlogException(String.format("No changes for entity %s can be detected.", entity.toString()), e); } } return result; }  void register(HasUuid entity); ChangeEvent detectChanges(HasUuid entity);  }