cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From johnth...@apache.org
Subject cayenne git commit: Expression.filterObjects needs to always return a new mutable collection to avoid blindsiding clients
Date Tue, 15 Sep 2015 18:04:02 GMT
Repository: cayenne
Updated Branches:
  refs/heads/master de8a52b2e -> 43a0af870


Expression.filterObjects needs to always return a new mutable collection to avoid blindsiding
clients


Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/43a0af87
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/43a0af87
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/43a0af87

Branch: refs/heads/master
Commit: 43a0af870efcb5dfef197e41bc75519c36e078ab
Parents: de8a52b
Author: John Huss <johnthuss@apache.org>
Authored: Tue Sep 15 11:37:37 2015 -0500
Committer: John Huss <johnthuss@apache.org>
Committed: Tue Sep 15 11:37:37 2015 -0500

----------------------------------------------------------------------
 .../java/org/apache/cayenne/exp/Expression.java  |  2 +-
 .../org/apache/cayenne/CayenneDataObjectIT.java  | 19 ++++++++++++++++---
 2 files changed, 17 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/43a0af87/cayenne-server/src/main/java/org/apache/cayenne/exp/Expression.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/exp/Expression.java b/cayenne-server/src/main/java/org/apache/cayenne/exp/Expression.java
index c22e74e..11bfdb6 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/exp/Expression.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/exp/Expression.java
@@ -485,7 +485,7 @@ public abstract class Expression implements Serializable, XMLSerializable
{
 	@SuppressWarnings("unchecked")
 	public <T> List<T> filterObjects(Collection<T> objects) {
 		if (objects == null || objects.size() == 0) {
-			return Collections.emptyList();
+			return new LinkedList<T>(); // returning Collections.emptyList() could cause random
client exceptions if they try to mutate the resulting list
 		}
 
 		return (List<T>) filter(objects, new LinkedList<T>());

http://git-wip-us.apache.org/repos/asf/cayenne/blob/43a0af87/cayenne-server/src/test/java/org/apache/cayenne/CayenneDataObjectIT.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/CayenneDataObjectIT.java b/cayenne-server/src/test/java/org/apache/cayenne/CayenneDataObjectIT.java
index 8efccd7..01c39b5 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/CayenneDataObjectIT.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/CayenneDataObjectIT.java
@@ -19,9 +19,7 @@
 
 package org.apache.cayenne;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertSame;
+import static org.junit.Assert.*;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -163,4 +161,19 @@ public class CayenneDataObjectIT extends ServerCase {
 		List<Painting> rezult = exp.filterObjects(paintingList);
 		assertEquals(a1, rezult.get(0).getToArtist());
 	}
+	
+	@Test
+	public void testFilterObjectsResultIsMutable() {
+
+		List<Artist> artistList = new ArrayList<Artist>();
+		Artist a = context.newObject(Artist.class);
+		a.setArtistName("Pablo");
+
+		Expression exp = ExpressionFactory.matchExp("artistName", "Mismatch");
+
+		List<Artist> result = exp.filterObjects(artistList);
+		assertTrue(result.isEmpty());
+		result.add(a); // list should be mutable
+		assertTrue(!result.isEmpty());
+	}
 }


Mime
View raw message