From 3ec7c158e95bbbfd6d211d694c58c7a4618d13d1 Mon Sep 17 00:00:00 2001 From: sandzhang Date: Mon, 27 Aug 2012 09:48:36 +0800 Subject: [PATCH] fix MapComparator and add testcase --- .../com/alibaba/druid/util/MapComparator.java | 8 +- .../support/http/resources/js/common.js | 2 +- .../alibaba/druid/util/MapComparatorTest.java | 166 ++++++++++++++++++ 3 files changed, 173 insertions(+), 3 deletions(-) create mode 100644 src/test/java/com/alibaba/druid/util/MapComparatorTest.java diff --git a/src/main/java/com/alibaba/druid/util/MapComparator.java b/src/main/java/com/alibaba/druid/util/MapComparator.java index 7e0ff4a20a..878cf8c67c 100644 --- a/src/main/java/com/alibaba/druid/util/MapComparator.java +++ b/src/main/java/com/alibaba/druid/util/MapComparator.java @@ -29,7 +29,7 @@ public class MapComparator implements Compar private boolean isDesc; private K orderByKey; - public MapComparator(K orderByKey, boolean isDesc) { + public MapComparator(K orderByKey, boolean isDesc){ this.orderByKey = orderByKey; this.isDesc = isDesc; } @@ -58,19 +58,23 @@ public class MapComparator implements Compar private Object getValueByKey(Map map, K key) { if (key instanceof String) { String keyStr = (String) key; + if (keyStr.matches(".+\\[[0-9]+\\]")) { Object value = map.get(keyStr.substring(0, keyStr.indexOf("["))); + if (value == null) return null; + Integer index = StringUtils.subStringToInteger(keyStr, "[", "]"); if (value.getClass().isArray() && Array.getLength(value) >= index) { return Array.get(value, index); } + return null; } } return map.get(key); } - public int compare_0(Map o1, Map o2) { + private int compare_0(Map o1, Map o2) { Object v1 = getValueByKey(o1, orderByKey); Object v2 = getValueByKey(o2, orderByKey); diff --git a/src/main/resources/support/http/resources/js/common.js b/src/main/resources/support/http/resources/js/common.js index 87ebb99882..2ced6e4dcd 100644 --- a/src/main/resources/support/http/resources/js/common.js +++ b/src/main/resources/support/http/resources/js/common.js @@ -41,7 +41,7 @@ druid.common = function () { buildFooter : function() { var html = ''; $(document.body).append(html); diff --git a/src/test/java/com/alibaba/druid/util/MapComparatorTest.java b/src/test/java/com/alibaba/druid/util/MapComparatorTest.java new file mode 100644 index 0000000000..f69fe13938 --- /dev/null +++ b/src/test/java/com/alibaba/druid/util/MapComparatorTest.java @@ -0,0 +1,166 @@ +/* + * Copyright 1999-2011 Alibaba Group Holding Ltd. + * + * 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. + */ +package com.alibaba.druid.util; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +import junit.framework.Assert; +import junit.framework.TestCase; + +public class MapComparatorTest extends TestCase { + + private String orderByKey = "orderby"; + + public void test_comparator_date() throws Exception { + test_comparator_date_0(true); + test_comparator_date_0(false); + } + + public void test_comparator_String() throws Exception { + test_comparator_string_0(true); + test_comparator_string_0(false); + } + + public void test_comparator_number() throws Exception { + test_comparator_number_0(true); + test_comparator_number_0(false); + } + + public void test_comparator_array() throws Exception { + test_comparator_array_0(true); + test_comparator_array_0(false); + } + + private void test_comparator_array_0(boolean desc) throws Exception { + String orderByKey = "orderby"; + long now = System.currentTimeMillis(); + MapComparator comparator = new MapComparator(orderByKey + "[0]", desc); + + Map map1 = new HashMap(); + Map map2 = new HashMap(); + + map1.put(orderByKey, new Date[] { new Date(now) }); + map2.put(orderByKey, new Date[] { new Date(now - 1) }); + Assert.assertEquals(!desc, comparator.compare(map1, map2) > 0); + + map2.put(orderByKey, new Date[] { new Date(now) }); + Assert.assertEquals(true, comparator.compare(map1, map2) == 0); + + map2.put(orderByKey, new Date[] { new Date(now + 1) }); + Assert.assertEquals(!desc, comparator.compare(map1, map2) < 0); + + map2.put(orderByKey, null); + Assert.assertEquals(!desc, comparator.compare(map1, map2) > 0); + + map1.put(orderByKey, null); + map2.put(orderByKey, new Date[] { new Date(now) }); + Assert.assertEquals(!desc, comparator.compare(map1, map2) < 0); + + map1.put(orderByKey, null); + map2.put(orderByKey, null); + Assert.assertEquals(true, comparator.compare(map1, map2) == 0); + + } + + private void test_comparator_date_0(boolean desc) throws Exception { + long now = System.currentTimeMillis(); + MapComparator comparator = new MapComparator(orderByKey, desc); + + Map map1 = new HashMap(); + Map map2 = new HashMap(); + + map1.put(orderByKey, new Date(now)); + map2.put(orderByKey, new Date(now - 1)); + Assert.assertEquals(!desc, comparator.compare(map1, map2) > 0); + + map2.put(orderByKey, new Date(now)); + Assert.assertEquals(true, comparator.compare(map1, map2) == 0); + + map2.put(orderByKey, new Date(now + 1)); + Assert.assertEquals(!desc, comparator.compare(map1, map2) < 0); + + map2.put(orderByKey, null); + Assert.assertEquals(!desc, comparator.compare(map1, map2) > 0); + + map1.put(orderByKey, null); + map2.put(orderByKey, new Date(now)); + Assert.assertEquals(!desc, comparator.compare(map1, map2) < 0); + + map1.put(orderByKey, null); + map2.put(orderByKey, null); + Assert.assertEquals(true, comparator.compare(map1, map2) == 0); + + } + + private void test_comparator_string_0(boolean desc) throws Exception { + MapComparator comparator = new MapComparator(orderByKey, desc); + + Map map1 = new HashMap(); + Map map2 = new HashMap(); + + map1.put(orderByKey, "opq"); + map2.put(orderByKey, "xyz"); + Assert.assertEquals(desc, comparator.compare(map1, map2) > 0); + + map2.put(orderByKey, "opq"); + Assert.assertEquals(true, comparator.compare(map1, map2) == 0); + + map2.put(orderByKey, "abc"); + Assert.assertEquals(desc, comparator.compare(map1, map2) < 0); + + map2.put(orderByKey, null); + Assert.assertEquals(!desc, comparator.compare(map1, map2) > 0); + + map1.put(orderByKey, null); + map2.put(orderByKey, "opq"); + Assert.assertEquals(!desc, comparator.compare(map1, map2) < 0); + + map1.put(orderByKey, null); + map2.put(orderByKey, null); + Assert.assertEquals(true, comparator.compare(map1, map2) == 0); + } + + private void test_comparator_number_0(boolean desc) throws Exception { + MapComparator comparator = new MapComparator(orderByKey, desc); + double baseNumber = 100.123; + + Map map1 = new HashMap(); + Map map2 = new HashMap(); + + map1.put(orderByKey, baseNumber); + map2.put(orderByKey, baseNumber - 1); + Assert.assertEquals(!desc, comparator.compare(map1, map2) > 0); + + map2.put(orderByKey, baseNumber); + Assert.assertEquals(true, comparator.compare(map1, map2) == 0); + + map2.put(orderByKey, baseNumber + 1); + Assert.assertEquals(!desc, comparator.compare(map1, map2) < 0); + + map2.put(orderByKey, null); + Assert.assertEquals(!desc, comparator.compare(map1, map2) > 0); + + map1.put(orderByKey, null); + map2.put(orderByKey, baseNumber); + Assert.assertEquals(!desc, comparator.compare(map1, map2) < 0); + + map1.put(orderByKey, null); + map2.put(orderByKey, null); + Assert.assertEquals(true, comparator.compare(map1, map2) == 0); + } +}