#349 - ENH: Add @WhoCreated @WhoModified ... with CurrentUserProvider interface

This commit is contained in:
Robin Bygrave committed 2015-07-24 12:33:04 +12:00
1 parent a1ff9034b6
commit eae06059c9
21 files changed
+495 -93

No files matched your search

@@ -0,0 +1,32 @@
package com.avaje.ebean;
import com.avaje.ebean.config.CurrentUserProvider;
/**
* Returns the current user typically from a Thread local or similar context.
*/
public class MyCurrentUserProvider implements CurrentUserProvider {
public static final String DEFAULT = "42789";
/**
* Do not do this yourself - this is for testing purposes.
*/
private static String userId = DEFAULT;
@Override
public Object currentUser() {
// just hardcoding here for testing
return userId;
}
public static void setUserId(String value) {
userId = value;
}
public static void resetToDefault() {
userId = DEFAULT;
}
}
@@ -0,0 +1,46 @@
package com.avaje.tests.generated;
import com.avaje.ebean.BaseTestCase;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.MyCurrentUserProvider;
import com.avaje.tests.model.EWhoProps;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class TestGeneratedWho extends BaseTestCase {
@Test
public void insertUpdate() {
EWhoProps bean = new EWhoProps();
bean.setName("one");
MyCurrentUserProvider.setUserId("INSERT_WHO_1");
Ebean.save(bean);
assertEquals("one", bean.getName());
assertEquals("INSERT_WHO_1", bean.getWhoCreated());
assertEquals("INSERT_WHO_1", bean.getWhoModified());
assertNotNull(bean.getWhenCreated());
assertNotNull(bean.getWhenModified());
MyCurrentUserProvider.setUserId("UPDATE_WHO_1");
bean.setName("two");
Ebean.save(bean);
assertEquals("two", bean.getName());
assertEquals("INSERT_WHO_1", bean.getWhoCreated());
assertEquals("UPDATE_WHO_1", bean.getWhoModified());
MyCurrentUserProvider.setUserId("UPDATE_WHO_2");
bean.setName("three");
Ebean.save(bean);
assertEquals("INSERT_WHO_1", bean.getWhoCreated());
assertEquals("UPDATE_WHO_2", bean.getWhoModified());
MyCurrentUserProvider.resetToDefault();
}
}
@@ -0,0 +1,91 @@
package com.avaje.tests.model;
import com.avaje.ebean.annotation.WhenCreated;
import com.avaje.ebean.annotation.WhenModified;
import com.avaje.ebean.annotation.WhoCreated;
import com.avaje.ebean.annotation.WhoModified;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Version;
import java.sql.Timestamp;
@Entity
public class EWhoProps {
@Id
Long id;
String name;
@Version
Long version;
@WhenCreated
Timestamp whenCreated;
@WhenModified
Timestamp whenModified;
@WhoCreated
String whoCreated;
@WhoModified
String whoModified;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
public Timestamp getWhenCreated() {
return whenCreated;
}
public void setWhenCreated(Timestamp whenCreated) {
this.whenCreated = whenCreated;
}
public Timestamp getWhenModified() {
return whenModified;
}
public void setWhenModified(Timestamp whenModified) {
this.whenModified = whenModified;
}
public String getWhoCreated() {
return whoCreated;
}
public void setWhoCreated(String whoCreated) {
this.whoCreated = whoCreated;
}
public String getWhoModified() {
return whoModified;
}
public void setWhoModified(String whoModified) {
this.whoModified = whoModified;
}
}