Skip to content

Commit

Permalink
Add test for TestReflectiveModificationUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave committed Oct 23, 2024
1 parent c868d8a commit f6da332
Showing 1 changed file with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package io.ebean.xtest.base;

import io.ebean.BeanState;
import io.ebean.DB;
import io.ebean.bean.EntityBean;
import io.ebean.bean.EntityBeanIntercept;
import io.ebean.test.LoggedSql;
import io.ebean.xtest.BaseTestCase;
import org.junit.jupiter.api.Test;
import org.tests.model.converstation.User;

import java.lang.reflect.Field;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

class TestReflectiveModificationUpdate extends BaseTestCase {

@Test
void statelessUpdate_via_reflection() throws NoSuchFieldException, IllegalAccessException {
// populate db with a user
User seed = new User();
seed.setName("someName");
seed.setEmail("[email protected]");
seed.save();

Field field = User.class.getDeclaredField("email");
field.setAccessible(true);

// our bean to perform stateless update
User user = new User();
user.setId(seed.getId());
user.setName("mod");

field.set(user, "[email protected]");

// need to set property loaded state to include in the stateless update
BeanState beanState = DB.beanState(user);
beanState.setPropertyLoaded("email", true);

LoggedSql.start();
user.update();

List<String> sql = LoggedSql.stop();
assertThat(sql).hasSize(1);
assertThat(sql.get(0)).contains("update c_user set name=?, email=?, when_modified=? where id=?");
}

@Test
void normalUpdate_via_reflection() throws NoSuchFieldException, IllegalAccessException {
// populate db with a user
User seed = new User();
seed.setName("someName");
seed.setEmail("[email protected]");
seed.save();

Field field = User.class.getDeclaredField("email");
field.setAccessible(true);

// fetching the bean from database, so a "normal update"
User user = DB.find(User.class, seed.getId());
user.setName("mod");

// ideally we get the old value first (if there are change listeners etc)
Object oldValue = field.get(user);

// reflectively modify
field.set(user, "[email protected]");

// BeanState beanState = DB.beanState(user);
// need to use EntityBeanIntercept rather than BeanState
// so this isn't great !!
EntityBean eb = (EntityBean) user;
EntityBeanIntercept ebi = eb._ebean_getIntercept();
int pos = ebi.findProperty("email");

// EITHER ideally mark as dirty with the original value
//ebi.setChangedPropertyValue(pos, true, oldValue);

// OR just mark as changed
ebi.markPropertyAsChanged(pos);

LoggedSql.start();
user.update();

List<String> sql = LoggedSql.stop();
assertThat(sql).hasSize(1);
assertThat(sql.get(0)).contains("update c_user set name=?, email=?, version=?, when_modified=? where id=? and version=?");
}
}

0 comments on commit f6da332

Please sign in to comment.