Search in sources :

Example 1 with Document

use of org.springframework.data.mongodb.core.mapping.Document in project spring-data-mongodb by spring-projects.

the class MongoConfigurationSupport method scanForEntities.

/**
 * Scans the given base package for entities, i.e. MongoDB specific types annotated with {@link Document}.
 *
 * @param basePackage must not be {@literal null}.
 * @return
 * @throws ClassNotFoundException
 * @since 1.10
 */
protected Set<Class<?>> scanForEntities(String basePackage) throws ClassNotFoundException {
    if (!StringUtils.hasText(basePackage)) {
        return Collections.emptySet();
    }
    Set<Class<?>> initialEntitySet = new HashSet<Class<?>>();
    if (StringUtils.hasText(basePackage)) {
        ClassPathScanningCandidateComponentProvider componentProvider = new ClassPathScanningCandidateComponentProvider(false);
        componentProvider.addIncludeFilter(new AnnotationTypeFilter(Document.class));
        for (BeanDefinition candidate : componentProvider.findCandidateComponents(basePackage)) {
            initialEntitySet.add(ClassUtils.forName(candidate.getBeanClassName(), MongoConfigurationSupport.class.getClassLoader()));
        }
    }
    return initialEntitySet;
}
Also used : AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) ClassPathScanningCandidateComponentProvider(org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider) Document(org.springframework.data.mongodb.core.mapping.Document) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) HashSet(java.util.HashSet)

Example 2 with Document

use of org.springframework.data.mongodb.core.mapping.Document in project spring-data-mongodb by spring-projects.

the class MappingMongoConverterUnitTests method convertsLocalesOutOfTheBox.

@Test
void convertsLocalesOutOfTheBox() {
    LocaleWrapper wrapper = new LocaleWrapper();
    wrapper.locale = Locale.US;
    org.bson.Document document = new org.bson.Document();
    converter.write(wrapper, document);
    Object localeField = document.get("locale");
    assertThat(localeField).isInstanceOf(String.class);
    assertThat(localeField).isEqualTo("en_US");
    LocaleWrapper read = converter.read(LocaleWrapper.class, document);
    assertThat(read.locale).isEqualTo(Locale.US);
}
Also used : DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Document(org.springframework.data.mongodb.core.mapping.Document) Test(org.junit.jupiter.api.Test)

Example 3 with Document

use of org.springframework.data.mongodb.core.mapping.Document in project spring-data-mongodb by spring-projects.

the class MappingMongoConverterUnitTests method readsClassWithBigDecimal.

// DATACMNS-42, DATAMONGO-171
@Test
void readsClassWithBigDecimal() {
    org.bson.Document document = new org.bson.Document("value", "2.5");
    document.put("map", new org.bson.Document("foo", "2.5"));
    BasicDBList list = new BasicDBList();
    list.add("2.5");
    document.put("collection", list);
    BigDecimalContainer result = converter.read(BigDecimalContainer.class, document);
    assertThat(result.value).isEqualTo(BigDecimal.valueOf(2.5d));
    assertThat(result.map.get("foo")).isEqualTo(BigDecimal.valueOf(2.5d));
    assertThat(result.collection.get(0)).isEqualTo(BigDecimal.valueOf(2.5d));
}
Also used : BasicDBList(com.mongodb.BasicDBList) Document(org.springframework.data.mongodb.core.mapping.Document) Test(org.junit.jupiter.api.Test)

Example 4 with Document

use of org.springframework.data.mongodb.core.mapping.Document in project spring-data-mongodb by spring-projects.

the class MappingMongoConverterUnitTests method shouldReadEntityWithGeoBoxCorrectly.

// DATAMONGO-858
@Test
void shouldReadEntityWithGeoBoxCorrectly() {
    ClassWithGeoBox object = new ClassWithGeoBox();
    object.box = new Box(new Point(1, 2), new Point(3, 4));
    org.bson.Document document = new org.bson.Document();
    converter.write(object, document);
    ClassWithGeoBox result = converter.read(ClassWithGeoBox.class, document);
    assertThat(result).isNotNull();
    assertThat(result.box).isEqualTo(object.box);
}
Also used : Box(org.springframework.data.geo.Box) Point(org.springframework.data.geo.Point) Document(org.springframework.data.mongodb.core.mapping.Document) Test(org.junit.jupiter.api.Test)

Example 5 with Document

use of org.springframework.data.mongodb.core.mapping.Document in project spring-data-mongodb by spring-projects.

the class MappingMongoConverterUnitTests method readsDBRefMap.

// DATAMONGO-424
@Test
void readsDBRefMap() {
    DBRef dbRef = mock(DBRef.class);
    org.bson.Document refMap = new org.bson.Document("foo", dbRef);
    org.bson.Document document = new org.bson.Document("refMap", refMap);
    DBRefWrapper result = converter.read(DBRefWrapper.class, document);
    assertThat(result.refMap.entrySet()).hasSize(1);
    assertThat(result.refMap.values()).contains(dbRef);
}
Also used : DBRef(com.mongodb.DBRef) Document(org.springframework.data.mongodb.core.mapping.Document) Test(org.junit.jupiter.api.Test)

Aggregations

Document (org.springframework.data.mongodb.core.mapping.Document)55 Test (org.junit.jupiter.api.Test)53 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)16 Query (org.springframework.data.mongodb.core.query.Query)16 TextQuery (org.springframework.data.mongodb.core.query.TextQuery)16 Point (org.springframework.data.geo.Point)12 BasicDBList (com.mongodb.BasicDBList)8 BasicDBObject (com.mongodb.BasicDBObject)6 DBObject (com.mongodb.DBObject)5 DBRef (com.mongodb.DBRef)5 Sphere (org.springframework.data.mongodb.core.geo.Sphere)5 ObjectId (org.bson.types.ObjectId)4 Distance (org.springframework.data.geo.Distance)4 LocalDate (org.joda.time.LocalDate)3 MongoException (com.mongodb.MongoException)2 BigInteger (java.math.BigInteger)2 HashSet (java.util.HashSet)2 DataAccessException (org.springframework.dao.DataAccessException)2 Circle (org.springframework.data.geo.Circle)2 Polygon (org.springframework.data.geo.Polygon)2