Skip to content

Commit

Permalink
#703 Setting a value on a deleted model does not cause a "frozen" exc…
Browse files Browse the repository at this point in the history
…eption
  • Loading branch information
ipolevoy committed Apr 8, 2018
1 parent 6c2fb46 commit a2b92f4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
*/
public class FrozenException extends RuntimeException {
public FrozenException(Model model) {
super("Model frozen: " + model);
super("Model frozen, must thaw() before using: " + model);
}
}
3 changes: 3 additions & 0 deletions activejdbc/src/main/java/org/javalite/activejdbc/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,9 @@ public void set(String[] attributeNames, Object[] values) {
* @return reference to self, so you can string these methods one after another.
*/
public <T extends Model> T set(String attributeName, Object value) {
if(isFrozen()){
throw new FrozenException(this);
}
Converter<Object, Object> converter = modelRegistryLocal().converterForValue(attributeName, value, Object.class);
return setRaw(attributeName, converter != null ? converter.convert(value) : value);
}
Expand Down
13 changes: 10 additions & 3 deletions activejdbc/src/test/java/org/javalite/activejdbc/ModelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -452,23 +452,30 @@ public void testFrozen(){

a.delete();

expect(new ExceptionExpectation(FrozenException.class) {
expect(new ExceptionExpectation<FrozenException>(FrozenException.class) {
@Override
public void exec() {
a.saveIt();
}
});

expect(new ExceptionExpectation(FrozenException.class) {
expect(new ExceptionExpectation<FrozenException>(FrozenException.class) {
@Override
public void exec() {
u.add(a);
}
});

expect(new ExceptionExpectation<FrozenException>(FrozenException.class) {
@Override
public void exec() {
a.set("state", "AZ");
}
});

a.thaw();

expect(new DifferenceExpectation(u.getAll(Address.class).size()) {
expect(new DifferenceExpectation<Object>(u.getAll(Address.class).size()) {
@Override
public Object exec() {
u.add(a);
Expand Down

0 comments on commit a2b92f4

Please sign in to comment.