MentaBean

Compare Revisions

Ignore whitespace Rev 212 → Rev 213

/trunk/src/test/java/org/mentabean/jdbc/UpdateDiffTest.java
3,7 → 3,11
import static org.junit.Assert.*;
 
import java.sql.Connection;
import java.util.LinkedList;
import java.util.List;
 
import junit.framework.Assert;
 
import org.junit.Test;
import org.mentabean.BeanConfig;
import org.mentabean.BeanManager;
14,7 → 18,7
 
public class UpdateDiffTest extends AbstractBeanSessionTest {
 
public static class User implements Cloneable {
static class User implements Cloneable {
 
private int id;
private String name;
72,7 → 76,7
}
}
 
public static class Group implements Cloneable {
static class Group implements Cloneable {
 
private int id;
private String name;
84,6 → 88,11
public Group(int id) {
this.id = id;;
}
public Group(int id, String name) {
this.id = id;;
this.name = name;
}
 
public Group() {}
 
99,14 → 108,88
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Group) {
return id == ((Group) obj).id;
}
return false;
}
@Override
public int hashCode() {
return id;
}
 
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
static class TypeTest {
private long id;
private int intPrimitive;
private double doublePrimitive;
private Double doubleWrapper;
private Integer intWrapper;
private String string;
private Group group;
public long getId() {
return id;
}
public TypeTest setId(long id) {
this.id = id;
return this;
}
public int getIntPrimitive() {
return intPrimitive;
}
public TypeTest setIntPrimitive(int intPrimitive) {
this.intPrimitive = intPrimitive;
return this;
}
public double getDoublePrimitive() {
return doublePrimitive;
}
public TypeTest setDoublePrimitive(double doublePrimitive) {
this.doublePrimitive = doublePrimitive;
return this;
}
public Double getDoubleWrapper() {
return doubleWrapper;
}
public TypeTest setDoubleWrapper(Double doubleWrapper) {
this.doubleWrapper = doubleWrapper;
return this;
}
public Integer getIntWrapper() {
return intWrapper;
}
public TypeTest setIntWrapper(Integer intWrapper) {
this.intWrapper = intWrapper;
return this;
}
public String getString() {
return string;
}
public TypeTest setString(String string) {
this.string = string;
return this;
}
public Group getGroup() {
return group;
}
public TypeTest setGroup(Group group) {
this.group = group;
return this;
}
}
 
private BeanManager configureManager() {
private BeanManager configureManager1() {
 
BeanManager manager = new BeanManager();
 
127,6 → 210,24
 
return manager;
}
private BeanManager configureManager2() {
BeanManager manager = new BeanManager();
TypeTest t = PropertiesProxy.create(TypeTest.class);
BeanConfig conf = new BeanConfig(TypeTest.class, "test")
.pk(t.getId(), DBTypes.AUTOINCREMENT)
.field(t.getIntPrimitive(), DBTypes.INTEGER)
.field(t.getIntWrapper(), DBTypes.INTEGER)
.field(t.getDoublePrimitive(), DBTypes.DOUBLE)
.field(t.getDoubleWrapper(), DBTypes.DOUBLE)
.field(t.getGroup().getId(), "idgroups", DBTypes.INTEGER)
.field(t.getString(), DBTypes.STRING);
manager.addBeanConfig(conf);
return manager;
}
 
@Test
public void test() throws Exception {
135,7 → 236,7
try {
BeanSession session = new H2BeanSession(configureManager(), conn);
BeanSession session = new H2BeanSession(configureManager1(), conn);
session.createTables();
 
Group g1 = new Group("Common");
215,5 → 316,104
SQLUtils.close(conn);
}
}
@Test
public void testDifferences() throws Exception {
 
Connection conn = getConnection();
 
try {
 
BeanSession session = new H2BeanSession(configureManager2(), conn);
session.createTables();
 
Group g1 = new Group(1, "Group one");
Group g2 = new Group(2, "Group two");
TypeTest newObj = new TypeTest()
.setDoublePrimitive(0.6)
.setDoubleWrapper(0.6)
.setIntPrimitive(1)
.setIntWrapper(1)
.setGroup(g1)
.setString("Test one");
TypeTest oldObj = new TypeTest()
.setDoublePrimitive(0.7)
.setDoubleWrapper(0.6)
.setIntPrimitive(1)
.setIntWrapper(1)
.setGroup(g1)
.setString("Test one");
List<String> diffs = new LinkedList<String>();
TypeTest diff = session.compareDifferences(newObj, oldObj, diffs);
Assert.assertNotNull(diff);
Assert.assertEquals(diff.getDoublePrimitive(), newObj.getDoublePrimitive());
Assert.assertEquals(0, diffs.size());
diffs.clear();
oldObj.setDoublePrimitive(0.6);
diff = session.compareDifferences(newObj, oldObj, diffs);
Assert.assertNull(diff);
Assert.assertEquals(0, diffs.size());
 
diffs.clear();
newObj.setGroup(g2);
diff = session.compareDifferences(newObj, oldObj, diffs);
Assert.assertNotNull(diff);
Assert.assertEquals(0, diffs.size());
Assert.assertEquals(g2, diff.getGroup());
diffs.clear();
newObj.setGroup(null);
diff = session.compareDifferences(newObj, oldObj, diffs);
Assert.assertEquals(1, diffs.size());
Assert.assertEquals("group.id", diffs.get(0));
diffs.clear();
newObj.setGroup(g2);
newObj.setDoublePrimitive(0);
newObj.setString(null);
diff = session.compareDifferences(newObj, oldObj, diffs);
Assert.assertEquals(g2, diff.getGroup());
Assert.assertEquals(2, diffs.size());
Assert.assertEquals("doublePrimitive", diffs.get(0));
Assert.assertEquals("string", diffs.get(1));
diffs.clear();
newObj.setGroup(oldObj.getGroup());
newObj.setDoublePrimitive(0.000000001d);
oldObj.setDoublePrimitive(0.000000001d);
newObj.setString(oldObj.getString());
diff = session.compareDifferences(newObj, oldObj, diffs);
Assert.assertEquals(0, diffs.size());
Assert.assertNull(diff);
diffs.clear();
oldObj.setDoublePrimitive(0);
diff = session.compareDifferences(newObj, oldObj, diffs);
Assert.assertNotNull(diff);
Assert.assertEquals(newObj.getDoublePrimitive(), diff.getDoublePrimitive());
diffs.clear();
newObj.setDoublePrimitive(0);
oldObj.setDoublePrimitive(0);
diff = session.compareDifferences(newObj, oldObj, diffs);
Assert.assertNull(diff);
diffs.clear();
newObj.setIntPrimitive(0);
diff = session.compareDifferences(newObj, oldObj, diffs);
Assert.assertNotNull(diff);
Assert.assertEquals(0, diff.getIntPrimitive());
Assert.assertEquals(1, diffs.size());
Assert.assertEquals("intPrimitive", diffs.get(0));
 
} finally {
 
SQLUtils.close(conn);
}
}
 
}
/trunk/src/main/java/org/mentabean/jdbc/AnsiSQLBeanSession.java
1285,12 → 1285,11
 
} else if (returnType.isPrimitive() && !returnType.equals(boolean.class) && !returnType.equals(char.class) && value instanceof Number) {
 
// if number consider everything different than zero to be
// set...
// if number consider everything different than zero to be set...
 
final Number n = (Number) value;
 
if (n.intValue() != 0) {
if (n.doubleValue() != 0d) {
return true;
}