Package de.bwaldvogel.mongo.backend

Source Code of de.bwaldvogel.mongo.backend.DocumentComparatorTest

package de.bwaldvogel.mongo.backend;

import static org.fest.assertions.Assertions.assertThat;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import org.bson.BSONObject;
import org.bson.BasicBSONObject;
import org.junit.Test;

public class DocumentComparatorTest {

    @Test
    public void testCompareSingleKey() {
        DocumentComparator comparator = new DocumentComparator(new BasicBSONObject("a", 1));

        List<BSONObject> list = new ArrayList<BSONObject>();
        list.add(new BasicBSONObject("a", 10));
        list.add(new BasicBSONObject("a", 15));
        list.add(new BasicBSONObject("a", 5));
        list.add(new BasicBSONObject("b", 1));

        Collections.sort(list, comparator);
        assertThat(list).containsExactly(new BasicBSONObject("b", 1), //
                new BasicBSONObject("a", 5), //
                new BasicBSONObject("a", 10), //
                new BasicBSONObject("a", 15));
    }

    @Test
    public void testCompareMultiKey() {
        DocumentComparator comparator = new DocumentComparator(new BasicBSONObject("a", 1).append("b", -1));

        List<BSONObject> list = new ArrayList<BSONObject>();
        list.add(new BasicBSONObject("a", 15).append("b", 3));
        list.add(new BasicBSONObject("a", 15).append("b", 2));
        list.add(new BasicBSONObject("a", 5));
        list.add(new BasicBSONObject("b", 1));
        list.add(new BasicBSONObject("b", 2));
        list.add(new BasicBSONObject("b", 3));

        Random rnd = new Random(4711);

        for (int i = 0; i < 10; i++) {
            Collections.shuffle(list, rnd);

            Collections.sort(list, comparator);
            assertThat(list).containsExactly(new BasicBSONObject("b", 3), //
                    new BasicBSONObject("b", 2), //
                    new BasicBSONObject("b", 1), //
                    new BasicBSONObject("a", 5), //
                    new BasicBSONObject("a", 15).append("b", 3), //
                    new BasicBSONObject("a", 15).append("b", 2));
        }
    }

    @Test
    public void testCompareCompoundKey() throws Exception {
        DocumentComparator comparator = new DocumentComparator(new BasicBSONObject("a.b", 1).append("c", -1));

        BSONObject a = new BasicBSONObject("a", new BasicBSONObject("b", 10));
        BSONObject b = new BasicBSONObject("a", new BasicBSONObject("b", 15));
        BSONObject c = new BasicBSONObject("a", new BasicBSONObject("b", 15)).append("x", 70);

        assertThat(comparator.compare(a, b)).isLessThan(0);
        assertThat(comparator.compare(b, a)).isGreaterThan(0);
        assertThat(comparator.compare(b, c)).isZero();

    }
}
TOP

Related Classes of de.bwaldvogel.mongo.backend.DocumentComparatorTest

TOP
Copyright © 2018 www.massapi.com. 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.