diff --git a/jOOQ-test/src/test/java/org/jooq/test/all/testcases/ResultTests.java b/jOOQ-test/src/test/java/org/jooq/test/all/testcases/ResultTests.java index 60b9eacc0e..e3f30fb3ab 100644 --- a/jOOQ-test/src/test/java/org/jooq/test/all/testcases/ResultTests.java +++ b/jOOQ-test/src/test/java/org/jooq/test/all/testcases/ResultTests.java @@ -93,39 +93,25 @@ extends BaseTest { + // Put 1 at the end of everything + Comparator c1 = (o1, o2) -> + (o1 == 1 && o2 != 1) ? 1 : + (o2 == 1 && o1 != 1) ? -1 : + o1.compareTo(o2); - @Override - public int compare(Integer o1, Integer o2) { - - // Put 1 at the end of everything - if (o1 == 1 && o2 != 1) return 1; - if (o2 == 1 && o1 != 1) return -1; - - return o1.compareTo(o2); - } - } - - assertTrue(result == result.sortAsc(TBook_ID(), new C1())); + assertTrue(result == result.sortAsc(TBook_ID(), c1)); assertEquals(asList(2, 3, 4, 1), result.getValues(TBook_ID())); - assertTrue(result == result.sortDesc(TBook_ID(), new C1())); + assertTrue(result == result.sortDesc(TBook_ID(), c1)); assertEquals(asList(1, 4, 3, 2), result.getValues(TBook_ID())); - class C2 implements Comparator { + Comparator c2 = (book1, book2) -> + c1.compare(book1.getValue(TBook_ID()), book2.getValue(TBook_ID())); - private final C1 c1 = new C1(); - - @Override - public int compare(B book1, B book2) { - return c1.compare(book1.getValue(TBook_ID()), book2.getValue(TBook_ID())); - } - } - - assertTrue(result == result.sortAsc(new C2())); + assertTrue(result == result.sortAsc(c2)); assertEquals(asList(2, 3, 4, 1), result.getValues(TBook_ID())); - assertTrue(result == result.sortDesc(new C2())); + assertTrue(result == result.sortDesc(c2)); assertEquals(asList(1, 4, 3, 2), result.getValues(TBook_ID())); } } diff --git a/jOOQ/src/test/java/org/jooq/test/ResultSortTest.java b/jOOQ/src/test/java/org/jooq/test/ResultSortTest.java new file mode 100644 index 0000000000..9c520a937a --- /dev/null +++ b/jOOQ/src/test/java/org/jooq/test/ResultSortTest.java @@ -0,0 +1,92 @@ +/** + * Copyright (c) 2009-2014, Data Geekery GmbH (http://www.datageekery.com) + * All rights reserved. + * + * This work is dual-licensed + * - under the Apache Software License 2.0 (the "ASL") + * - under the jOOQ License and Maintenance Agreement (the "jOOQ License") + * ============================================================================= + * You may choose which license applies to you: + * + * - If you're using this work with Open Source databases, you may choose + * either ASL or jOOQ License. + * - If you're using this work with at least one commercial database, you must + * choose jOOQ License + * + * For more information, please visit http://www.jooq.org/licenses + * + * Apache Software License 2.0: + * ----------------------------------------------------------------------------- + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * jOOQ License and Maintenance Agreement: + * ----------------------------------------------------------------------------- + * Data Geekery grants the Customer the non-exclusive, timely limited and + * non-transferable license to install and use the Software under the terms of + * the jOOQ License and Maintenance Agreement. + * + * This library is distributed with a LIMITED WARRANTY. See the jOOQ License + * and Maintenance Agreement for more details: http://www.jooq.org/licensing + */ + +package org.jooq.test; + +import static org.jooq.test.data.Table1.FIELD_ID1; +import static org.jooq.test.data.Table2.FIELD_ID2; +import static org.jooq.test.data.Table3.FIELD_ID3; + +import org.jooq.Record3; +import org.jooq.Result; + +import org.junit.Test; + + +/** + * A test suite for sorting functionality of {@link Result}. + * + * @author Lukas Eder + */ +public class ResultSortTest extends AbstractTest { + + @Test + public void testSortStability() throws Exception { + Result> original = result(); + Result> result = result(); + + assertEquals(original, result); + + assertEquals(original, result.sortAsc(FIELD_ID1)); + assertEquals(original, result.sortDesc(FIELD_ID1).sortAsc(FIELD_ID1)); + assertEquals(original, result.sortAsc(FIELD_ID3).sortAsc(FIELD_ID2).sortAsc(FIELD_ID1)); + } + + private Result> result() { + Result> result = create.newResult(FIELD_ID1, FIELD_ID2, FIELD_ID3); + + result.add(record(1, 1, 1)); + result.add(record(1, 1, 2)); + result.add(record(1, 1, 3)); + result.add(record(1, 2, 1)); + result.add(record(1, 2, 2)); + result.add(record(1, 2, 3)); + result.add(record(2, 1, 1)); + + return result; + } + + private Record3 record(int i, int j, int k) { + Record3 record = create.newRecord(FIELD_ID1, FIELD_ID2, FIELD_ID3); + record.fromArray(i, j, k); + return record; + } +}