Package org.mongodb.morphia.mapping

Examples of org.mongodb.morphia.mapping.Mapper


    }

    @Test
    public void shouldNotAllowTypeThatDoesNotMatchKeyTypeValue() {
        // expect
        MappedClass mappedClass = new MappedClass(SimpleEntity.class, new Mapper());
        MappedField mappedField = mappedClass.getMappedField("name");
        assertThat(QueryValidator.isCompatibleForOperator(mappedField, String.class, EQUAL,
                                                          new Key<Number>(Integer.class, new ObjectId()),
                                                          new ArrayList<ValidationFailure>()), is(false));
    }
View Full Code Here


    }

    @Test
    public void shouldNotAllowNonKeyTypeWithKeyValue() {
        // expect
        MappedClass mappedClass = new MappedClass(EntityWithListsAndArrays.class, new Mapper());
        MappedField mappedField = mappedClass.getMappedField("listOfIntegers");
        assertThat(QueryValidator.isCompatibleForOperator(mappedField, SimpleEntity.class, EQUAL, new Key<String>("kind", new ObjectId()),
                                                          new ArrayList<ValidationFailure>()), is(false));
    }
View Full Code Here

    }

    @Test
    public void shouldAllowValuesOfList() {
        // expect
        MappedClass mappedClass = new MappedClass(SimpleEntity.class, new Mapper());
        MappedField mappedField = mappedClass.getMappedField("name");
        assertThat(QueryValidator.isCompatibleForOperator(mappedField, List.class, EQUAL, new ArrayList<String>(),
                                                          new ArrayList<ValidationFailure>()), is(true));
    }
View Full Code Here

    }

    @Test
    public void shouldRejectTypesAndValuesThatDoNotMatch() {
        // expect
        MappedClass mappedClass = new MappedClass(SimpleEntity.class, new Mapper());
        MappedField mappedField = mappedClass.getMappedField("name");
        assertThat(QueryValidator.isCompatibleForOperator(mappedField, String.class, EQUAL, 1, new ArrayList<ValidationFailure>()),
                   is(false));
    }
View Full Code Here

                                             op,
                                             value,
                                             validateNames,
                                             validateTypes);

        final Mapper mapper = query.getDatastore().getMapper();

        MappedClass mc = null;
        try {
            if (value != null && !ReflectionUtils.isPropertyType(value.getClass())
                && !ReflectionUtils.implementsInterface(value.getClass(), Iterable.class)) {
                if (mf != null && !mf.isTypeMongoCompatible()) {
                    mc = mapper.getMappedClass((mf.isSingleValue()) ? mf.getType() : mf.getSubClass());
                } else {
                    mc = mapper.getMappedClass(value);
                }
            }
        } catch (Exception e) {
            //Ignore these. It is likely they related to mapping validation that is unimportant for queries (the query will
            // fail/return-empty anyway)
            LOG.debug("Error during mapping of filter criteria: ", e);
        }

        Object mappedValue = mapper.toMongoObject(mf, mc, value);

        final Class<?> type = (mappedValue == null) ? null : mappedValue.getClass();

        //convert single values into lists for $in/$nin
        if (type != null && (op == FilterOperator.IN || op == FilterOperator.NOT_IN)
View Full Code Here

    @Test
    public void testMapping() {
        final BasicDAO<User, ObjectId> messageDAO = new BasicDAO<User, ObjectId>(User.class, getDs());
        Assert.assertNotNull(messageDAO);
       
        Mapper mapper = new Mapper();

        User user = new User();
        user.id = 1;

        user.userObject = "just a String";
        DBObject dbObject = mapper.toDBObject(user);
        Object object = mapper.fromDBObject(User.class, dbObject, new DefaultEntityCache());
        Assert.assertEquals(user.userObject, ((User) object).userObject);
       
        user.userObject = 33;
        dbObject = mapper.toDBObject(user);
        object = mapper.fromDBObject(User.class, dbObject, new DefaultEntityCache());
        Assert.assertEquals(user.userObject, ((User) object).userObject);

        user.userObject = 33.3;
        dbObject = mapper.toDBObject(user);
        object = mapper.fromDBObject(User.class, dbObject, new DefaultEntityCache());
        Assert.assertEquals(user.userObject, ((User) object).userObject);
    }
View Full Code Here

    }

    @Test
    public void testToMongoObjectCorrectlyMapsSerializableFieldForIssue591() {
        // given
        Mapper mapper = new Mapper();

        User user = new User();
        user.id = 1;
        user.userObject = new SerializableObject();

        MappedClass mc = new MappedClass(User.class, mapper);
        MappedField mf = mc.getMappedField("userObject");

        // when
        Object dbValue = mapper.toMongoObject(mf, null, user.userObject);
        Class<byte[]> byteArrayClass = byte[].class;

        // then
        assertThat(dbValue, is(instanceOf(byteArrayClass)));
    }
View Full Code Here

    }

    @Test
    public void testToMongoObjectCorrectlyMapsSerializableListOfObjectsForIssue591() {
        // given
        Mapper mapper = new Mapper();

        ListEntity user = new ListEntity();
        user.id = 1;
        List<Object> list = new ArrayList<Object>();
        list.add("value");
        user.list = list;

        MappedClass mc = new MappedClass(ListEntity.class, mapper);
        MappedField mf = mc.getMappedField("list");

        // when
        Object dbValue = mapper.toMongoObject(mf, null, user.list);
        Class<byte[]> byteArrayClass = byte[].class;

        // then
        assertThat(dbValue, is(instanceOf(byteArrayClass)));
    }
View Full Code Here

    }

    @Test
    public void testCanMapSerializableObject() {
        // given
        Mapper mapper = new Mapper();
        User user = new User();
        user.id = 1;
        user.userObject = new SerializableObject();

        // when
        DBObject dbObject = mapper.toDBObject(user);
        User object = mapper.fromDBObject(User.class, dbObject, new DefaultEntityCache());

        // then
        assertThat(object.userObject, is(user.userObject));
    }
View Full Code Here

        }
    }

    @Test
    public void testExternalMapping() throws Exception {
        final Mapper mapper = getMorphia().getMapper();
        final CloneMapper helper = new CloneMapper(mapper);
        helper.map(Skeleton.class, EntityWithNoAnnotations.class);
        final MappedClass mc = mapper.getMappedClass(EntityWithNoAnnotations.class);
        mc.update();
        assertNotNull(mc.getIdField());
        assertNotNull(mc.getEntityAnnotation());
        assertEquals("special", mc.getEntityAnnotation().value());
View Full Code Here

TOP

Related Classes of org.mongodb.morphia.mapping.Mapper

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.