Skip to content

Commit aaf3d50

Browse files
author
Igor Polevoy
committed
Merge pull request #387 from Garagoth/master
#381: Fixed test method names, enchanced tests a bit
2 parents a45595e + 3c2ea47 commit aaf3d50

File tree

2 files changed

+77
-30
lines changed

2 files changed

+77
-30
lines changed

activejdbc/src/main/java/org/javalite/activejdbc/Model.java

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public abstract class Model extends CallbackSupport implements Externalizable {
5656
private final static Logger logger = LoggerFactory.getLogger(Model.class);
5757

5858
private Map<String, Object> attributes = new CaseInsensitiveMap<Object>();
59-
private final CaseInsensitiveSet dirtyAttributes = new CaseInsensitiveSet();
59+
private final Set<String> dirtyAttributeNames = new CaseInsensitiveSet();
6060
private boolean frozen = false;
6161
private MetaModel metaModelLocal;
6262
private ModelRegistry modelRegistryLocal;
@@ -143,11 +143,11 @@ public static MetaModel getMetaModel() {
143143
}
144144

145145
protected Map<String, Object> getAttributes() {
146-
return attributes;
146+
return Collections.unmodifiableMap(attributes);
147147
}
148148

149-
protected CaseInsensitiveSet getDirtyAttributes() {
150-
return dirtyAttributes;
149+
protected Set<String> dirtyAttributeNames() {
150+
return Collections.unmodifiableSet(dirtyAttributeNames);
151151
}
152152

153153
/**
@@ -160,7 +160,7 @@ protected CaseInsensitiveSet getDirtyAttributes() {
160160
*/
161161
public <T extends Model> T fromMap(Map input) {
162162
hydrate(input);
163-
dirtyAttributes.addAll(input.keySet());
163+
dirtyAttributeNames.addAll(input.keySet());
164164
return (T) this;
165165
}
166166

@@ -287,16 +287,17 @@ private <T extends Model> T setRaw(String attributeName, Object value) {
287287
getMetaModelLocal().checkAttributeOrAssociation(attributeName);
288288

289289
attributes.put(attributeName, value);
290-
dirtyAttributes.add(attributeName);
290+
dirtyAttributeNames.add(attributeName);
291291
return (T) this;
292292
}
293293

294294
/**
295295
* Will return true if any attribute of this instance was changed after latest load/save.
296+
* (Instance state differs from state in DB)
296297
* @return true if this instance was modified.
297298
*/
298299
public boolean isModified() {
299-
return !dirtyAttributes.isEmpty();
300+
return !dirtyAttributeNames.isEmpty();
300301
}
301302

302303
/**
@@ -309,6 +310,14 @@ public boolean isFrozen(){
309310
return frozen;
310311
}
311312

313+
/**
314+
* Synonym for {@link #isModified()}.
315+
*
316+
* @return true if this instance was modified.
317+
*/
318+
public boolean modified() {
319+
return isModified();
320+
}
312321

313322
/**
314323
* Returns names of all attributes from this model.
@@ -1138,27 +1147,27 @@ public void setParent(Model parent) {
11381147
* @param other target model.
11391148
*/
11401149
public void copyTo(Model other) {
1150+
other.copyFrom(this);
1151+
}
1152+
1153+
/**
1154+
* Copies all attribute values (except for ID, created_at and updated_at) from other instance to this one.
1155+
*
1156+
* @param other source model.
1157+
*/
1158+
public void copyFrom(Model other) {
11411159
if (!getMetaModelLocal().getTableName().equals(other.getMetaModelLocal().getTableName())) {
11421160
throw new IllegalArgumentException("can only copy between the same types");
11431161
}
1144-
1162+
Map<String, Object> otherAttributes = other.getAttributes();
11451163
for (String name : getMetaModelLocal().getAttributeNamesSkipId()) {
1146-
other.getAttributes().put(name, get(name));
1147-
other.getDirtyAttributes().add(name);
1164+
attributes.put(name, otherAttributes.get(name));
1165+
dirtyAttributeNames.add(name);
11481166
// Why not use setRaw() here? Does the same and avoids duplication of code... (Garagoth)
11491167
// other.setRaw(name, getRaw(name));
11501168
}
11511169
}
11521170

1153-
/**
1154-
* Copies all attribute values (except for ID, created_at and updated_at) from this instance to the other.
1155-
*
1156-
* @param other target model.
1157-
*/
1158-
public void copyFrom(Model other) {
1159-
other.copyTo(this);
1160-
}
1161-
11621171
/**
11631172
* This method should be called from all instance methods for performance.
11641173
*
@@ -1196,7 +1205,7 @@ public void refresh() {
11961205
"this ID does not exist anymore. Stale model: " + this);
11971206
}
11981207
fresh.copyTo(this);
1199-
dirtyAttributes.clear();
1208+
dirtyAttributeNames.clear();
12001209
}
12011210

12021211
/**
@@ -2425,7 +2434,7 @@ public void reset() {
24252434
*/
24262435
public void thaw(){
24272436
attributes.put(getIdName(), null);
2428-
dirtyAttributes.addAll(attributes.keySet());
2437+
dirtyAttributeNames.addAll(attributes.keySet());
24292438
frozen = false;
24302439
}
24312440

@@ -2551,7 +2560,7 @@ public boolean insert() {
25512560
attributes.put(metaModel.getVersionColumn(), 1);
25522561
}
25532562

2554-
dirtyAttributes.clear(); // Clear all dirty attribute names as all were inserted. What about versionColumn ?
2563+
dirtyAttributeNames.clear(); // Clear all dirty attribute names as all were inserted. What about versionColumn ?
25552564
fireAfterCreate();
25562565

25572566
return done;
@@ -2582,7 +2591,7 @@ private boolean update() {
25822591
MetaModel metaModel = getMetaModelLocal();
25832592
StringBuilder query = new StringBuilder().append("UPDATE ").append(metaModel.getTableName()).append(" SET ");
25842593
Set<String> attributeNames = metaModel.getAttributeNamesSkipGenerated(manageTime);
2585-
attributeNames.retainAll(dirtyAttributes);
2594+
attributeNames.retainAll(dirtyAttributeNames);
25862595
if(attributeNames.size() > 0) {
25872596
join(query, attributeNames, " = ?, ");
25882597
query.append(" = ?");
@@ -2624,7 +2633,7 @@ private boolean update() {
26242633
if(metaModel.cached()){
26252634
QueryCache.instance().purgeTableCache(metaModel.getTableName());
26262635
}
2627-
dirtyAttributes.clear();
2636+
dirtyAttributeNames.clear();
26282637
fireAfterUpdate();
26292638
return updated > 0;
26302639
}

activejdbc/src/test/java/org/javalite/activejdbc/Defect381_UpdateModifiedOnlyTest.java

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,36 @@
2828
public class Defect381_UpdateModifiedOnlyTest extends ActiveJDBCTest {
2929

3030
@Test
31-
public void isModifiedWhenModified() {
31+
public void shouldBeModifiedWhenModified() {
3232
Programmer prg = Programmer.createIt("first_name", "John");
3333
the(prg).shouldNotBe("new");
3434
the(prg).shouldNotBe("modified");
3535
prg.set("last_name", "Doe");
3636
the(prg).shouldBe("modified");
37+
the(prg.dirtyAttributeNames()).shouldContain("last_name");
38+
the(prg.dirtyAttributeNames().size()).shouldBeEqual(1);
39+
3740
prg.saveIt();
3841
the(prg).shouldNotBe("modified");
42+
the(prg.dirtyAttributeNames().size()).shouldBeEqual(0);
3943
}
4044

4145
@Test
42-
public void isModifiedAfterCreated() {
46+
public void shouldBeModifiedAfterCreated() {
4347
Programmer prg = Programmer.create("first_name", "John");
4448
the(prg).shouldBe("new");
4549
the(prg).shouldBe("modified");
4650
prg.set("last_name", "Doe");
4751
the(prg).shouldBe("modified");
52+
the(prg.dirtyAttributeNames()).shouldBeEqual(prg.getAttributes().keySet());
53+
4854
prg.saveIt();
4955
the(prg).shouldNotBe("modified");
56+
the(prg.dirtyAttributeNames().size()).shouldBeEqual(0);
5057
}
5158

5259
@Test
53-
public void isModifiedAfterFroMap() {
60+
public void shouldBeModifiedAfterFromMap() {
5461
Programmer prg = Programmer.createIt("first_name", "John");
5562
the(prg).shouldNotBe("new");
5663
the(prg).shouldNotBe("modified");
@@ -64,7 +71,7 @@ public void isModifiedAfterFroMap() {
6471
}
6572

6673
@Test
67-
public void isModifiedAfterFind() {
74+
public void shouldNotBeModifiedAfterFind() {
6875
Programmer prg = Programmer.createIt("first_name", "John", "last_name", "Doe");
6976
the(prg).shouldNotBe("new");
7077
the(prg).shouldNotBe("modified");
@@ -76,7 +83,14 @@ public void isModifiedAfterFind() {
7683
the(prgFromDB.get("last_name")).shouldBeEqual("Doe");
7784

7885
prgFromDB.set("first_name", "Jane");
86+
prgFromDB.set("last_name", "Moe");
7987
the(prgFromDB).shouldBe("modified");
88+
the(prgFromDB.dirtyAttributeNames()).shouldContain("first_name");
89+
the(prgFromDB.dirtyAttributeNames()).shouldContain("last_name");
90+
the(prgFromDB.dirtyAttributeNames()).shouldContain("First_Name");
91+
the(prgFromDB.dirtyAttributeNames()).shouldContain("LAST_NAME");
92+
the(prgFromDB.dirtyAttributeNames().size()).shouldBeEqual(2);
93+
8094
prgFromDB.saveIt();
8195
the(prgFromDB).shouldNotBe("modified");
8296

@@ -86,7 +100,7 @@ public void isModifiedAfterFind() {
86100
}
87101

88102
@Test
89-
public void isModifiedAfterDeletaAndThaw() {
103+
public void shouldBeModifiedAfterDeletaAndThaw() {
90104
Programmer prg = Programmer.createIt("first_name", "John", "last_name", "Doe");
91105
the(prg).shouldNotBe("new");
92106
the(prg).shouldNotBe("modified");
@@ -97,10 +111,34 @@ public void isModifiedAfterDeletaAndThaw() {
97111
prg.thaw();
98112
the(prg).shouldBe("new");
99113
the(prg).shouldBe("modified");
100-
the(prg.getDirtyAttributes()).shouldBeEqual(prg.getAttributes().keySet());
114+
the(prg.dirtyAttributeNames()).shouldBeEqual(prg.getAttributes().keySet());
101115

102116
prg.saveIt();
103117
the(prg).shouldNotBe("new");
104118
the(prg).shouldNotBe("modified");
105119
}
120+
121+
@Test
122+
public void shouldBeModifiedAfterCopyTo() {
123+
Programmer prg = Programmer.createIt("first_name", "John", "last_name", "Doe");
124+
the(prg).shouldNotBe("new");
125+
the(prg).shouldNotBe("modified");
126+
127+
Programmer prg2 = Programmer.createIt("first_name", "Jane", "last_name", "Doe");
128+
the(prg).shouldNotBe("new");
129+
the(prg).shouldNotBe("modified");
130+
131+
prg.copyTo(prg2);
132+
the(prg).shouldNotBe("new");
133+
the(prg2).shouldBe("modified");
134+
}
135+
136+
@Test
137+
public void shouldBeCaseInsensitive() {
138+
Programmer prg = new Programmer();
139+
prg.set("FIRST_NAME", "John");
140+
prg.set("First_Name", "John");
141+
prg.set("first_name", "John");
142+
the(prg.dirtyAttributeNames().size()).shouldBeEqual(1);
143+
}
106144
}

0 commit comments

Comments
 (0)