MentaBean

Compare Revisions

Ignore whitespace Rev 197 → Rev 198

/trunk/src/test/java/org/mentabean/jdbc/UpdateDiffTest.java
New file
0,0 → 1,219
package org.mentabean.jdbc;
 
import static org.junit.Assert.*;
 
import java.sql.Connection;
 
import org.junit.Test;
import org.mentabean.BeanConfig;
import org.mentabean.BeanManager;
import org.mentabean.BeanSession;
import org.mentabean.DBTypes;
import org.mentabean.util.PropertiesProxy;
import org.mentabean.util.SQLUtils;
 
public class UpdateDiffTest extends AbstractBeanSessionTest {
 
public static class User implements Cloneable {
 
private int id;
private String name;
private Integer age;
private boolean active;
private Group group;
 
public User() {}
 
public User(int id) {
this.id = id;
}
 
public User(String name, Integer age, boolean active, Group group) {
super();
this.name = name;
this.age = age;
this.group = group;
}
 
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
 
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
 
public static class Group implements Cloneable {
 
private int id;
private String name;
 
public Group(String name) {
this.name = name;
}
 
public Group(int id) {
this.id = id;;
}
 
public Group() {}
 
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
 
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
 
private BeanManager configureManager() {
 
BeanManager manager = new BeanManager();
 
User userProxy = PropertiesProxy.create(User.class);
BeanConfig userCfg = new BeanConfig(User.class, "users")
.pk(userProxy.getId(), DBTypes.AUTOINCREMENT)
.field(userProxy.getGroup().getId(), "idgroups", DBTypes.INTEGER)
.field(userProxy.getName(), DBTypes.STRING.size(0))
.field(userProxy.getAge(), DBTypes.INTEGER)
.field(userProxy.isActive(), DBTypes.BOOLEAN);
manager.addBeanConfig(userCfg);
 
Group groupProxy = PropertiesProxy.create(Group.class);
BeanConfig groupCfg = new BeanConfig(Group.class, "groups")
.pk(groupProxy.getId(), DBTypes.AUTOINCREMENT)
.field(groupProxy.getName(), DBTypes.STRING.size(0));
manager.addBeanConfig(groupCfg);
 
return manager;
}
 
@Test
public void test() throws Exception {
 
Connection conn = getConnection();
try {
BeanSession session = new H2BeanSession(configureManager(), conn);
session.createTables();
 
Group g1 = new Group("Common");
Group g2 = new Group("Super");
Group g3 = new Group("Admin");
session.insert(g1);
session.insert(g2);
session.insert(g3);
 
User u1 = new User("John", 40, true, g1);
User u2 = new User("Ralph", 30, false, g2);
User u3 = new User("Matt", 20, true, g3);
session.insert(u1);
session.insert(u2);
session.insert(u3);
 
User ralph = new User(2);
session.load(ralph);
assertEquals("Ralph", ralph.getName());
 
User clone = (User) ralph.clone();
assertNotNull(clone.getGroup());
assertEquals(2, clone.getGroup().getId());
 
int updated = session.updateDiff(clone, ralph);
assertEquals(0, updated);
 
clone.setGroup(null);
updated = session.updateDiff(clone, ralph);
assertEquals(1, updated);
clone = session.createBasicInstance(clone);
session.load(clone);
assertNull(clone.getGroup());
assertEquals("Ralph", clone.getName());
assertFalse(clone.isActive());
 
User old = (User) clone.clone();
clone.setGroup(g3);
updated = session.updateDiff(clone, old);
assertEquals(1, updated);
clone = session.createBasicInstance(clone);
session.load(clone);
assertNotNull(clone.getGroup());
assertEquals("Ralph", clone.getName());
 
old = (User) clone.clone();
clone.setGroup(new Group());
clone.setAge(0);
clone.setActive(true);
updated = session.updateDiff(clone, old);
assertEquals(1, updated);
clone = session.createBasicInstance(clone);
session.load(clone);
assertNull(clone.getGroup());
assertEquals(new Integer(0), clone.getAge());
assertTrue(clone.isActive());
 
old = (User) clone.clone();
clone.setAge(null);
updated = session.updateDiff(clone, old);
assertEquals(1, updated);
clone = session.loadUnique(clone);
assertNull(clone.getAge());
 
old = (User) clone.clone();
clone.setAge(10);
clone.setActive(false);
updated = session.updateDiff(clone, old);
assertEquals(1, updated);
clone = session.createBasicInstance(clone);
session.load(clone);
assertEquals(new Integer(10), clone.getAge());
assertFalse(clone.isActive());
 
} finally {
SQLUtils.close(conn);
}
}
 
}
Property changes:
Added: svn:mime-type
+ text/plain
/trunk/src/main/java/org/mentabean/jdbc/AnsiSQLBeanSession.java
1644,21 → 1644,30
field = fields.next();
final Method m = findMethodToGet(bean, field.getName());
Method m = findMethodToGet(bean, field.getName());
boolean isNestedProperty = field.getName().contains(".");
if (m == null) {
if (!isNestedProperty) {
throw new BeanException("Cannot find method to get field from bean: " + field.getName());
} else {
continue; // nested property not set!
int index = field.getName().lastIndexOf(".")+1;
Object deepest = getDeepestBean(bean, field.getName().substring(0, index-1), true);
String lastField = field.getName().substring(index);
m = findMethodToGet(deepest, lastField);
}
}
 
final Class<? extends Object> returnType = m.getReturnType();
valueBean = getValueFromBean(bean, field.getName(), m);
valueBean = getValueFromBean(bean, field.getName());
 
if (field.isPK()) {
1667,7 → 1676,7
continue;
}
valueAnother = getValueFromBean(another, field.getName(), m);
valueAnother = getValueFromBean(another, field.getName());
if (!isSet(valueBean, returnType) && !isSet(valueAnother, returnType)) {