Skip to content

Commit

Permalink
Merge pull request #661 from darkv/ERXQ_patch
Browse files Browse the repository at this point in the history
test for empty array in ERXQ.one() and ERXQ.first()
  • Loading branch information
darkv committed Jun 29, 2015
2 parents cdf3977 + f1cce97 commit 01be1b7
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 24 deletions.
28 changes: 7 additions & 21 deletions Frameworks/Core/ERExtensions/Sources/er/extensions/eof/ERXQ.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,14 @@ public static void filter(NSMutableArray<?> array, EOQualifier qualifier) {
* @throws IllegalStateException if more than one object matched
*/
public static <T> T one(NSArray<T> array, EOQualifier qualifier) {
T object;
if (array == null) {
object = null;
}
else {
T object = null;
if (array != null && !array.isEmpty()) {
NSArray<T> objects = ERXQ.filtered(array, qualifier);
int count = objects.count();
if (count == 0) {
object = null;
}
else if (count == 1) {
if (count == 1) {
object = objects.lastObject();
}
else {
else if (count > 1) {
throw new IllegalStateException("There was more than one object that matched the qualifier '" + qualifier + "'.");
}
}
Expand All @@ -167,20 +161,12 @@ else if (count == 1) {
* @param qualifier
* the qualifier to filter on
* @return one matching object or null
* @throws IllegalStateException if more than one object matched
*/
public static <T> T first(NSArray<T> array, EOQualifier qualifier) {
T object;
if (array == null) {
object = null;
}
else {
T object = null;
if (array != null && !array.isEmpty()) {
NSArray<T> objects = ERXQ.filtered(array, qualifier);
int count = objects.count();
if (count == 0) {
object = null;
}
else {
if (!objects.isEmpty()) {
object = objects.objectAtIndex(0);
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/ERXTest/Sources/er/erxtest/ERXTestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static Test suite() {
suite.addTestSuite(er.extensions.eof.ERXKeyTest.class);

suite.addTestSuite(er.extensions.eof.qualifiers.ERXToManyQualifierTest.class);
suite.addTestSuite(er.extensions.eof.qualifiers.ERXQTest.class);
suite.addTestSuite(er.extensions.eof.ERXQTest.class);

suite.addTestSuite(er.extensions.foundation.ERXArrayUtilitiesTest.class);
suite.addTestSuite(er.extensions.foundation.ERXMutableArrayTest.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package er.extensions.eof.qualifiers;
package er.extensions.eof;

import junit.framework.TestCase;

import com.webobjects.foundation.NSArray;
import com.webobjects.foundation.NSDictionary;
import com.webobjects.foundation.NSMutableArray;
import com.webobjects.foundation.NSMutableDictionary;
import com.webobjects.foundation.NSPropertyListSerialization;

import com.webobjects.eocontrol.EOAndQualifier;
import com.webobjects.eocontrol.EOClassDescription;
import com.webobjects.eocontrol.EOGenericRecord;
import com.webobjects.eocontrol.EOKeyValueQualifier;
import com.webobjects.eocontrol.EOQualifier;

Expand Down Expand Up @@ -96,4 +98,79 @@ public void testMatchingValues() {
assertNotNull(qualifier);
assertTrue(qualifier instanceof EOAndQualifier && ((EOAndQualifier)qualifier).qualifiers().size() == 3);
}

public void testOne() {
NSArray<EOGenericRecord> persons = personArray();
EOGenericRecord firstElement = persons.get(0);

EOGenericRecord result = ERXQ.one(persons, ERXQ.is("firstName", "Bill"));
assertNotNull(result);
assertEquals(firstElement, result);

result = ERXQ.one(persons, ERXQ.is("firstName", "Billl"));
assertNull(result);

result = ERXQ.one(null, ERXQ.is("firstName", "Bill"));
assertNull(result);

result = ERXQ.one(NSArray.<EOGenericRecord>emptyArray(), ERXQ.is("firstName", "Bill"));
assertNull(result);

try {
result = ERXQ.one(persons, null);
fail("missing IllegalStateException");
} catch (IllegalStateException e) {
// got expected exception
}

try {
result = ERXQ.one(persons, ERXQ.is("lastName", "Smith"));
fail("missing IllegalStateException");
} catch (IllegalStateException e) {
// got expected exception
}
}

public void testFirst() {
NSArray<EOGenericRecord> persons = personArray();
EOGenericRecord firstElement = persons.get(0);

EOGenericRecord result = ERXQ.first(persons, ERXQ.is("firstName", "Bill"));
assertNotNull(result);
assertEquals(firstElement, result);

result = ERXQ.first(persons, ERXQ.is("firstName", "Billl"));
assertNull(result);

result = ERXQ.first(null, ERXQ.is("firstName", "Bill"));
assertNull(result);

result = ERXQ.first(NSArray.<EOGenericRecord>emptyArray(), ERXQ.is("firstName", "Bill"));
assertNull(result);

result = ERXQ.first(persons, null);
assertNotNull(result);
assertEquals(firstElement, result);

result = ERXQ.first(persons, ERXQ.is("lastName", "Smith"));
assertNotNull(result);
assertEquals(firstElement, result);
}

private NSArray<EOGenericRecord> personArray() {
NSMutableArray<EOGenericRecord> persons = new NSMutableArray<EOGenericRecord>();
persons.add(createPerson("Bill", "Smith"));
persons.add(createPerson("Bob", "Anything"));
persons.add(createPerson("John", "Smith"));
persons.add(createPerson("Peter", "Pan"));
return persons.immutableClone();
}

private EOGenericRecord createPerson(String firstName, String lastName) {
EOClassDescription classDescription = EOClassDescription.classDescriptionForEntityName("Person");
EOGenericRecord person = new EOGenericRecord(classDescription);
person.takeValueForKey(firstName, "firstName");
person.takeValueForKey(lastName, "lastName");
return person;
}
}

0 comments on commit 01be1b7

Please sign in to comment.