diff --git a/.travis.yml b/.travis.yml index 8e5340be2..43fa8a0d2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ jdk: install: mvn compile -Dmaven.javadoc.skip=true -script: mvn test -Dtest=UnitTestSuite && mvn cobertura:cobertura +script: mvn test -Dtest=UnitTestSuite after_success: - bash <(curl -s https://codecov.io/bash) diff --git a/pom.xml b/pom.xml index e2ca47893..8936c7b10 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.baidu.hugegraph hugegraph-common - 1.5.4 + 1.5.5 hugegraph-common https://github.com/hugegraph/hugegraph-common @@ -198,23 +198,34 @@ - 1.5.4.0 + 1.5.5.0 - org.codehaus.mojo - cobertura-maven-plugin - 2.7 - - - html - xml - - - + org.jacoco + jacoco-maven-plugin + 0.8.2 + + + pre-unit-test + + prepare-agent + + + + post-unit-test + test + + report + + + ${project.build.directory} + + + diff --git a/src/main/java/com/baidu/hugegraph/util/CollectionUtil.java b/src/main/java/com/baidu/hugegraph/util/CollectionUtil.java index 2c091a34c..ca8ef5eb7 100644 --- a/src/main/java/com/baidu/hugegraph/util/CollectionUtil.java +++ b/src/main/java/com/baidu/hugegraph/util/CollectionUtil.java @@ -22,9 +22,12 @@ import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; +import java.util.Map; import java.util.Set; public final class CollectionUtil { @@ -65,7 +68,7 @@ public static boolean prefixOf(List prefix, List all) { return true; } - public static boolean allUnique(Collection collection){ + public static boolean allUnique(Collection collection) { return collection.stream().allMatch(new HashSet<>()::add); } @@ -163,4 +166,36 @@ public static boolean hasIntersection(Set first, Set second) { } return false; } + + public static , V> Map sortByKey( + Map map, boolean incr) { + List> list = new ArrayList<>(map.entrySet()); + if (incr) { + list.sort(Map.Entry.comparingByKey()); + } else { + list.sort(Collections.reverseOrder(Map.Entry.comparingByKey())); + } + + Map result = new LinkedHashMap<>(); + for (Map.Entry entry : list) { + result.put(entry.getKey(), entry.getValue()); + } + return result; + } + + public static > Map sortByValue( + Map map, boolean incr) { + List> list = new ArrayList<>(map.entrySet()); + if (incr) { + list.sort(Map.Entry.comparingByValue()); + } else { + list.sort(Collections.reverseOrder(Map.Entry.comparingByValue())); + } + + Map result = new LinkedHashMap<>(); + for (Map.Entry entry : list) { + result.put(entry.getKey(), entry.getValue()); + } + return result; + } } diff --git a/src/main/java/com/baidu/hugegraph/version/CommonVersion.java b/src/main/java/com/baidu/hugegraph/version/CommonVersion.java index aa9c14f7c..e8d5f7a58 100644 --- a/src/main/java/com/baidu/hugegraph/version/CommonVersion.java +++ b/src/main/java/com/baidu/hugegraph/version/CommonVersion.java @@ -27,5 +27,5 @@ public class CommonVersion { // The second parameter of Version.of() is for all-in-one JAR public static final Version VERSION = Version.of(CommonVersion.class, - "1.5.4"); + "1.5.5"); } diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties deleted file mode 100644 index 3e9f7e155..000000000 --- a/src/main/resources/log4j.properties +++ /dev/null @@ -1,24 +0,0 @@ -log4j.rootLogger=INFO, console, file - -log4j.logger.org.apache.cassandra=INFO -log4j.logger.org.apache.hadoop=INFO -log4j.logger.com.datastax.driver=WARN -log4j.logger.io.netty=INFO -log4j.logger.org.apache.commons=INFO -log4j.logger.com.baidu.hugegraph=INFO - -# Console Logger. -log4j.appender.console=org.apache.log4j.ConsoleAppender -log4j.appender.console.Threshold=DEBUG -log4j.appender.console.layout=org.apache.log4j.PatternLayout -log4j.appender.console.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} %-5r\ - [%t] [%-5p] %c %x - %m%n - -# File Logger -log4j.appender.file=org.apache.log4j.DailyRollingFileAppender -log4j.appender.file.File=logs/hugegraph-common.log -log4j.appender.file.Threshold=INFO -log4j.appender.file.DatePattern='-'yyyy-MM-dd'.log' -log4j.appender.file.layout=org.apache.log4j.PatternLayout -log4j.appender.file.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} %-5r\ - [%t] [%-5p] %c %x - %m%n diff --git a/src/test/java/com/baidu/hugegraph/unit/UnitTestSuite.java b/src/test/java/com/baidu/hugegraph/unit/UnitTestSuite.java index c178649b0..143772dd2 100644 --- a/src/test/java/com/baidu/hugegraph/unit/UnitTestSuite.java +++ b/src/test/java/com/baidu/hugegraph/unit/UnitTestSuite.java @@ -31,6 +31,7 @@ import com.baidu.hugegraph.unit.util.BytesTest; import com.baidu.hugegraph.unit.util.CollectionUtilTest; import com.baidu.hugegraph.unit.util.HashUtilTest; +import com.baidu.hugegraph.unit.util.InsertionOrderUtilTest; import com.baidu.hugegraph.unit.util.VersionUtilTest; @RunWith(Suite.class) @@ -46,6 +47,7 @@ BytesTest.class, CollectionUtilTest.class, HashUtilTest.class, + InsertionOrderUtilTest.class, VersionUtilTest.class }) public class UnitTestSuite { diff --git a/src/test/java/com/baidu/hugegraph/unit/util/CollectionUtilTest.java b/src/test/java/com/baidu/hugegraph/unit/util/CollectionUtilTest.java index c9be43fdb..ec0e1b7e5 100644 --- a/src/test/java/com/baidu/hugegraph/unit/util/CollectionUtilTest.java +++ b/src/test/java/com/baidu/hugegraph/unit/util/CollectionUtilTest.java @@ -21,16 +21,89 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; import org.junit.Test; import com.baidu.hugegraph.testutil.Assert; +import com.baidu.hugegraph.unit.BaseUnitTest; import com.baidu.hugegraph.util.CollectionUtil; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; -public class CollectionUtilTest { +public class CollectionUtilTest extends BaseUnitTest { + + @Test + public void testToList() { + Object array1 = new Integer[]{1, 2, 3}; + Assert.assertEquals(ImmutableList.of(1, 2, 3), + CollectionUtil.toList(array1)); + + Object array2 = new String[]{"1", "2", "3"}; + Assert.assertEquals(ImmutableList.of("1", "2", "3"), + CollectionUtil.toList(array2)); + + Assert.assertThrows(NullPointerException.class, () -> { + CollectionUtil.toList(null); + }); + + Assert.assertThrows(IllegalArgumentException.class, () -> { + CollectionUtil.toList("123"); + }); + } + + @Test + public void testPrefixOf() { + List list = ImmutableList.of(1, 2, 3); + + List list1 = ImmutableList.of(); + Assert.assertTrue(CollectionUtil.prefixOf(list1, list)); + + List list2 = ImmutableList.of(1, 2); + Assert.assertTrue(CollectionUtil.prefixOf(list2, list)); + + List list3 = ImmutableList.of(1, 2, 3); + Assert.assertTrue(CollectionUtil.prefixOf(list3, list)); + + List list4 = ImmutableList.of(1, 2, 3, 4); + Assert.assertFalse(CollectionUtil.prefixOf(list4, list)); + } + + @Test + public void testAllUnique() { + List list = ImmutableList.of(); + Assert.assertTrue(CollectionUtil.allUnique(list)); + + list = ImmutableList.of(1, 2, 3, 2, 3); + Assert.assertFalse(CollectionUtil.allUnique(list)); + + list = ImmutableList.of(1, 2, 3, 4, 5); + Assert.assertTrue(CollectionUtil.allUnique(list)); + + list = ImmutableList.of(1, 1, 1, 1, 1); + Assert.assertFalse(CollectionUtil.allUnique(list)); + } + + @Test + public void testSubSet() { + Set originSet = ImmutableSet.of(1, 2, 3, 4, 5); + + Set subSet = CollectionUtil.subSet(originSet, 1, 1); + Assert.assertEquals(ImmutableSet.of(), subSet); + + subSet = CollectionUtil.subSet(originSet, 2, 4); + Assert.assertEquals(ImmutableSet.of(3, 4), subSet); + + subSet = CollectionUtil.subSet(originSet, 2, 5); + Assert.assertEquals(ImmutableSet.of(3, 4, 5), subSet); + + subSet = CollectionUtil.subSet(originSet, 0, 5); + Assert.assertEquals(ImmutableSet.of(1, 2, 3, 4, 5), subSet); + } @Test public void testUnion() { @@ -137,4 +210,84 @@ public void testHasIntersectionBetweenSetAndSet() { second.add(1); Assert.assertTrue(CollectionUtil.hasIntersection(first, second)); } + + @Test + public void testMapSortByStringKey() { + Map unordered = new HashMap<>(); + unordered.put("D", 1); + unordered.put("B", 2); + unordered.put("E", 3); + unordered.put("A", 4); + unordered.put("C", 5); + + Map incrOrdered = CollectionUtil.sortByKey(unordered, + true); + Assert.assertEquals(ImmutableList.of("A", "B", "C", "D", "E"), + ImmutableList.copyOf(incrOrdered.keySet())); + + Map decrOrdered = CollectionUtil.sortByKey(unordered, + false); + Assert.assertEquals(ImmutableList.of("E", "D", "C", "B", "A"), + ImmutableList.copyOf(decrOrdered.keySet())); + } + + @Test + public void testMapSortByIntegerKey() { + Map unordered = new HashMap<>(); + unordered.put(4, "A"); + unordered.put(2, "B"); + unordered.put(5, "C"); + unordered.put(1, "D"); + unordered.put(3, "E"); + + Map incrOrdered = CollectionUtil.sortByKey(unordered, + true); + Assert.assertEquals(ImmutableList.of(1, 2, 3, 4, 5), + ImmutableList.copyOf(incrOrdered.keySet())); + + Map decrOrdered = CollectionUtil.sortByKey(unordered, + false); + Assert.assertEquals(ImmutableList.of(5, 4, 3, 2, 1), + ImmutableList.copyOf(decrOrdered.keySet())); + } + + @Test + public void testMapSortByIntegerValue() { + Map unordered = new HashMap<>(); + unordered.put("A", 4); + unordered.put("B", 2); + unordered.put("C", 5); + unordered.put("D", 1); + unordered.put("E", 3); + + Map incrOrdered = CollectionUtil.sortByValue(unordered, + true); + Assert.assertEquals(ImmutableList.of(1, 2, 3, 4, 5), + ImmutableList.copyOf(incrOrdered.values())); + + Map decrOrdered = CollectionUtil.sortByValue(unordered, + false); + Assert.assertEquals(ImmutableList.of(5, 4, 3, 2, 1), + ImmutableList.copyOf(decrOrdered.values())); + } + + @Test + public void testMapSortByStringValue() { + Map unordered = new HashMap<>(); + unordered.put(1, "D"); + unordered.put(2, "B"); + unordered.put(3, "E"); + unordered.put(4, "A"); + unordered.put(5, "C"); + + Map incrOrdered = CollectionUtil.sortByValue(unordered, + true); + Assert.assertEquals(ImmutableList.of("A", "B", "C", "D", "E"), + ImmutableList.copyOf(incrOrdered.values())); + + Map decrOrdered = CollectionUtil.sortByValue(unordered, + false); + Assert.assertEquals(ImmutableList.of("E", "D", "C", "B", "A"), + ImmutableList.copyOf(decrOrdered.values())); + } } diff --git a/src/test/java/com/baidu/hugegraph/unit/util/InsertionOrderUtilTest.java b/src/test/java/com/baidu/hugegraph/unit/util/InsertionOrderUtilTest.java new file mode 100644 index 000000000..3298c5ae2 --- /dev/null +++ b/src/test/java/com/baidu/hugegraph/unit/util/InsertionOrderUtilTest.java @@ -0,0 +1,73 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * 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 com.baidu.hugegraph.unit.util; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.Test; + +import com.baidu.hugegraph.testutil.Assert; +import com.baidu.hugegraph.unit.BaseUnitTest; +import com.baidu.hugegraph.util.InsertionOrderUtil; +import com.google.common.collect.ImmutableList; + +public class InsertionOrderUtilTest extends BaseUnitTest { + + @Test + public void testSet() { + Set set = InsertionOrderUtil.newSet(); + set.add(4); + set.add(2); + set.add(5); + set.add(1); + set.add(3); + + Assert.assertEquals(ImmutableList.of(4, 2, 5, 1, 3), + ImmutableList.copyOf(set)); + } + + @Test + public void testList() { + List list = InsertionOrderUtil.newList(); + list.add(4); + list.add(2); + list.add(5); + list.add(1); + list.add(3); + + Assert.assertEquals(ImmutableList.of(4, 2, 5, 1, 3), + ImmutableList.copyOf(list)); + } + + @Test + public void testMap() { + Map map = InsertionOrderUtil.newMap(); + map.put(4, 4); + map.put(2, 2); + map.put(5, 5); + map.put(1, 1); + map.put(3, 3); + + Assert.assertEquals(ImmutableList.of(4, 2, 5, 1, 3), + ImmutableList.copyOf(map.keySet())); + } +}