You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2019/01/09 17:37:28 UTC

[myfaces-tobago] 02/02: TOBAGO-1972: Sheet sorting: can't sort "Apples with Pears"

This is an automated email from the ASF dual-hosted git repository.

lofwyr pushed a commit to branch tobago-4.x
in repository https://gitbox.apache.org/repos/asf/myfaces-tobago.git

commit 1ee1274111f16cbd8f0579efa2fbf844277013ae
Author: Udo Schnurpfeil <lo...@apache.org>
AuthorDate: Wed Jan 9 18:36:21 2019 +0100

    TOBAGO-1972: Sheet sorting: can't sort "Apples with Pears"
    
    (cherry picked from commit a326499fb79055cb5838054349371b473f8a6e7d)
---
 .../apache/myfaces/tobago/util/BeanComparator.java |   2 +-
 .../tobago/util/BeanComparatorUnitTest.java        | 109 +++++++++++++++++++++
 2 files changed, 110 insertions(+), 1 deletion(-)

diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/util/BeanComparator.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/util/BeanComparator.java
index 5998a8f..68d1dfb 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/util/BeanComparator.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/util/BeanComparator.java
@@ -89,7 +89,7 @@ public class BeanComparator extends ComparatorBase implements Serializable {
     final Object obj2;
     try {
       obj1 = new PropertyDescriptor(property, param1.getClass(), property, null).getReadMethod().invoke(param1);
-      obj2 = new PropertyDescriptor(property, param1.getClass(), property, null).getReadMethod().invoke(param2);
+      obj2 = new PropertyDescriptor(property, param2.getClass(), property, null).getReadMethod().invoke(param2);
     } catch (final Exception e) {
       LOG.error(e.getMessage(), e);
       return 0;
diff --git a/tobago-core/src/test/java/org/apache/myfaces/tobago/util/BeanComparatorUnitTest.java b/tobago-core/src/test/java/org/apache/myfaces/tobago/util/BeanComparatorUnitTest.java
new file mode 100644
index 0000000..d8911dc
--- /dev/null
+++ b/tobago-core/src/test/java/org/apache/myfaces/tobago/util/BeanComparatorUnitTest.java
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+package org.apache.myfaces.tobago.util;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class BeanComparatorUnitTest {
+
+  @Test
+  public void testComparingInstancesOfDifferentClasses() {
+    final List<Fruit> original = new ArrayList<>();
+    original.add(new Apple("Golden Delicious"));
+    original.add(new Apple("Schöner aus Boskoop"));
+    original.add(new Pear("Williams Christ"));
+    original.add(new Pear("Köstliche aus Charneux"));
+
+    final BeanComparator ascendingComparator = new BeanComparator("name", null, false);
+    final List<Fruit> ascending = new ArrayList<>(original);
+    ascending.sort(ascendingComparator);
+
+    Assert.assertEquals("#0", original.get(0), ascending.get(0));
+    Assert.assertEquals("#1", original.get(3), ascending.get(1));
+    Assert.assertEquals("#2", original.get(1), ascending.get(2));
+    Assert.assertEquals("#3", original.get(2), ascending.get(3));
+
+    final BeanComparator descendingComparator = new BeanComparator("name", null, true);
+    final List<Fruit> descending = new ArrayList<>(original);
+    descending.sort(descendingComparator);
+
+    Assert.assertEquals("#0", original.get(2), descending.get(0));
+    Assert.assertEquals("#1", original.get(1), descending.get(1));
+    Assert.assertEquals("#2", original.get(3), descending.get(2));
+    Assert.assertEquals("#3", original.get(0), descending.get(3));
+  }
+
+  public interface Fruit {
+
+    String getName();
+
+    void setName(String name);
+  }
+
+  public static class Apple implements Fruit {
+
+    private String name;
+
+    public Apple(String name) {
+      this.name = name;
+    }
+
+    @Override
+    public String getName() {
+      return name;
+    }
+
+    public void setName(String name) {
+      this.name = name;
+    }
+
+    @Override
+    public String toString() {
+      return getName();
+    }
+  }
+
+  public static class Pear implements Fruit {
+
+    private String name;
+
+    public Pear(String name) {
+      this.name = name;
+    }
+
+    @Override
+    public String getName() {
+      return name;
+    }
+
+    public void setName(String name) {
+      this.name = name;
+    }
+
+    @Override
+    public String toString() {
+      return getName();
+    }
+  }
+}