Skip to content

Commit

Permalink
[Issue 615] Updated QueryForSubtypeTest to use assertThat, and simpli…
Browse files Browse the repository at this point in the history
…fied the tests, although they do the same things.
  • Loading branch information
trishagee committed Dec 12, 2014
1 parent b648340 commit f8b793e
Showing 1 changed file with 26 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,26 @@

import java.util.ArrayList;

import static org.junit.Assert.assertTrue;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mongodb.morphia.query.FilterOperator.EQUAL;
import static org.mongodb.morphia.query.FilterOperator.SIZE;
import static org.mongodb.morphia.query.QueryValidator.isCompatibleForOperator;

/**
* For issue #615.
*
*
* @author jbyler
*/
public class QueryForSubtypeTest extends TestBase {

private MappedClass jobMappedClass;

interface User { }
interface User {
}

@Entity
static class LocalUser implements User {
static class UserImpl implements User {
@Id
@SuppressWarnings("unused")
private ObjectId id;
Expand All @@ -50,50 +55,36 @@ static class Job {
}

@Before
public void commonSetup() throws Exception {
Mapper mapper = new Mapper();
jobMappedClass = mapper.getMappedClass(Job.class);
public void setUp() {
super.setUp();
jobMappedClass = new Mapper().getMappedClass(Job.class);
}

@Test
public void testClassIsCompatibleWithInterface() {
MappedField mf = jobMappedClass.getMappedField("owner");
public void testImplementingClassIsCompatibleWithInterface() {
MappedField user = jobMappedClass.getMappedField("owner");

boolean compatible = QueryValidator.isCompatibleForOperator(
mf,
mf.getType(),
FilterOperator.EQUAL,
new LocalUser(),
new ArrayList<ValidationFailure>());
boolean compatible = isCompatibleForOperator(user, User.class, EQUAL, new UserImpl(), new ArrayList<ValidationFailure>());

assertTrue("LocalUser should be compatible for field of type User", compatible);
assertThat(compatible, is(true));
}

@Test
public void testSizeOfArrayList() {
MappedField mf = jobMappedClass.getMappedField("attributes");
public void testIntSizeShouldBeCompatibleWithArrayList() {
MappedField attributes = jobMappedClass.getMappedField("attributes");

boolean compatible = QueryValidator.isCompatibleForOperator(
mf,
mf.getType(),
FilterOperator.SIZE,
2,
new ArrayList<ValidationFailure>());
boolean compatible = isCompatibleForOperator(attributes, ArrayList.class, SIZE, 2, new ArrayList<ValidationFailure>());

assertTrue("$size 2 should be compatible for field of type ArrayList", compatible);
assertThat(compatible, is(true));
}

@Test
public void testSubclassOfKey() {
MappedField mf = jobMappedClass.getMappedField("owner");

boolean compatible = QueryValidator.isCompatibleForOperator(
mf,
mf.getType(),
FilterOperator.EQUAL,
new Key<User>(User.class, 212L) {},
new ArrayList<ValidationFailure>()); // anonymous subclass of Key
public void testSubclassOfKeyShouldBeCompatibleWithFieldUser() {
MappedField user = jobMappedClass.getMappedField("owner");
Key<User> anonymousKeySubclass = new Key<User>(User.class, 212L) { };

boolean compatible = isCompatibleForOperator(user, User.class, EQUAL, anonymousKeySubclass, new ArrayList<ValidationFailure>());

assertTrue("Subclass of Key<User> should be compatible for field of type User", compatible);
assertThat(compatible, is(true));
}
}

0 comments on commit f8b793e

Please sign in to comment.