Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package org.springframework.data.mongodb.core.mapping;

import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.config.AbstractIntegrationTests;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.mapping.PagingAndSortingTreeSetTest.Entity;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.test.context.ContextConfiguration;

/**
* @author Guto Bortolozzo
* see DATAMONGO-887
*/
@ContextConfiguration
abstract class PagingAndSortingTreeSetConfiguration extends AbstractIntegrationTests{

@Configuration
static class Config {

@Bean
public SourceRepository sourceRepository(){
return new SourceRepositoryImpl();
}
}

@Autowired protected SourceRepository repository;

@Autowired protected MongoOperations operations;

interface SourceRepository extends PagingAndSortingRepository<Entity, ObjectId> {}

static class SourceRepositoryImpl implements SourceRepository {

@Override
public Iterable<Entity> findAll(Sort sort) {
return null;
}

@Override
public Page<Entity> findAll(Pageable pageable) {
return null;
}

@Override
public <S extends Entity> S save(S entity) {
return null;
}

@Override
public <S extends Entity> Iterable<S> save(Iterable<S> entities) {
return null;
}

@Override
public Entity findOne(ObjectId id) {
return null;
}

@Override
public boolean exists(ObjectId id) {
return false;
}

@Override
public Iterable<Entity> findAll() {
return null;
}

@Override
public Iterable<Entity> findAll(Iterable<ObjectId> ids) {
return null;
}

@Override
public long count() {
return 0;
}

@Override
public void delete(ObjectId id) {}

@Override
public void delete(Entity entity) {}

@Override
public void delete(Iterable<? extends Entity> entities) {}

@Override
public void deleteAll() {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package org.springframework.data.mongodb.core.mapping;

import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.TreeMap;

import org.bson.types.ObjectId;
import org.junit.Test;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.annotation.Id;

/**
* @author Guto Bortolozzo
* see DATAMONGO-887
*/
@Configuration
public class PagingAndSortingTreeSetTest extends PagingAndSortingTreeSetConfiguration{

@Test
public void testExistsInstanceOfPagingRepository(){
assertNotNull(repository);
}

@Test
public void testFindForEntityWithFieldOfTreeMap() {

Entity entity = new Entity();

entity.treeMap.put("foo", new Element());

testEntity(entity, "Entity [treeMap={foo=Element [foo=Bar]}, hashMap={}, hashSet=[]]");
}

@Test
public void testFindForEntityWithFieldOfHashMap() {

Entity entity = new Entity();

entity.hashMap.put("foo", new Element());

testEntity(entity, "Entity [treeMap={}, hashMap={foo=Element [foo=Bar]}, hashSet=[]]");
}

@Test
public void testFindForEntityWithFieldOfHashSet() {

Entity entity = new Entity();

entity.hashSet.add(new Element());

testEntity(entity, "Entity [treeMap={}, hashMap={}, hashSet=[Element [foo=Bar]]]");
}

private void testEntity(Entity entity, String toString) {

operations.insert(entity);

List<Entity> all = operations.findAll(Entity.class);

assertThat(all, hasSize(1));

Entity searched = all.get(0);

assertThat(searched.toString(), is(toString));
assertThat(searched.id, is(entity.id));
}

@Document(collection = "test_collection")
static class Entity {

@Id
String id;
TreeMap<String,Element> treeMap;
HashMap<String, Element> hashMap;
HashSet<Element> hashSet;

public Entity() {
id = ObjectId.get().toString();
treeMap = new TreeMap<String, Element>();
hashMap = new HashMap<String, Element>();
hashSet = new HashSet<Element>();
}

@Override
public String toString() {
return "Entity [treeMap=" + treeMap + ", hashMap=" + hashMap+ ", hashSet=" + hashSet + "]";
}
}

static class Element {
String foo = "Bar";

@Override
public String toString() {
return "Element [foo=" + foo + "]";
}
}
}