com.feilong.core.util.CollectionsUtilTest.java Source code

Java tutorial

Introduction

Here is the source code for com.feilong.core.util.CollectionsUtilTest.java

Source

/*
 * Copyright (C) 2008 feilong
 *
 * 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.feilong.core.util;

import static java.util.Collections.emptyList;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections4.ListUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.PredicateUtils;
import org.apache.commons.collections4.Transformer;
import org.apache.commons.collections4.TransformerUtils;
import org.apache.commons.collections4.functors.ComparatorPredicate.Criterion;
import org.apache.commons.collections4.functors.EqualPredicate;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.feilong.core.util.predicate.BeanPredicateUtil;
import com.feilong.test.User;
import com.feilong.test.UserAddress;
import com.feilong.test.UserInfo;
import com.feilong.tools.jsonlib.JsonUtil;

import static com.feilong.core.bean.ConvertUtil.toArray;
import static com.feilong.core.bean.ConvertUtil.toList;

/**
 * The Class CollectionUtilTest.
 * 
 * @author <a href="http://feitianbenyue.iteye.com/">feilong</a>
 */
public class CollectionsUtilTest {

    /** The Constant LOGGER. */
    private static final Logger LOGGER = LoggerFactory.getLogger(CollectionsUtilTest.class);

    /**
     * Test remove.
     */
    @Test
    public void testRemove() {
        List<String> list = new ArrayList<String>() {

            /**
             * 
             */
            private static final long serialVersionUID = -9002323146501447769L;

            {
                add("xinge");
                add("feilong1");
                add("feilong2");
                add("feilong2");
            }
        };

        List<String> removeList = CollectionsUtil.remove(list, "feilong2");
        assertThat(removeList, hasItems("xinge", "feilong1"));
        assertThat(list, hasItems("xinge", "feilong1", "feilong2", "feilong2"));
    }

    /**
     * Test add all ignore null.
     */
    @Test
    public void testAddAllIgnoreNull() {
        List<String> list = toList("xinge", "feilong1");
        assertEquals(false, CollectionsUtil.addAllIgnoreNull(list, null));
    }

    /**
     * Test add all ignore null2.
     */
    @Test
    public void testAddAllIgnoreNull2() {
        List<String> list = toList("xinge", "feilong1");
        boolean addAllIgnoreNull = CollectionsUtil.addAllIgnoreNull(list, toList("xinge", "feilong1"));
        assertEquals(true, addAllIgnoreNull);
        assertThat(list, hasItems("xinge", "feilong1", "xinge", "feilong1"));
    }

    /**
     * Test add all ignore null1.
     */
    @Test(expected = NullPointerException.class)
    public void testAddAllIgnoreNull1() {
        CollectionsUtil.addAllIgnoreNull(null, null);
    }

    /**
     * Test partition.
     */
    @Test
    public void testPartition() {
        List<String> list = toList("xinge", "feilong1", "feilong2");
        LOGGER.debug("list:{}", JsonUtil.format(ListUtils.partition(list, 2)));
    }

    /**
     * Test collect.
     */
    @Test
    public void testCollect() {
        List<String> list = toList("xinge", "feilong1", "feilong2", "feilong2");

        Transformer<String, Object> nullTransformer = TransformerUtils.nullTransformer();
        List<Object> collect = CollectionsUtil.collect(list, nullTransformer);

        Object[] objects = { null, null, null, null };
        assertThat(collect, hasItems(objects));
    }

    /**
     * Test collect1.
     */
    @Test
    public void testCollect1() {
        List<String> collect1 = CollectionsUtil.collect((List<Long>) null,
                TransformerUtils.stringValueTransformer());
        assertEquals(emptyList(), collect1);
    }

    /**
     * Test collect5.
     */
    @Test
    public void testCollect5() {
        List<String> collect1 = CollectionsUtil.collect(new ArrayList<Long>(),
                TransformerUtils.stringValueTransformer());
        assertEquals(emptyList(), collect1);
    }

    /**
     * Test collect2.
     */
    @Test
    public void testCollect2() {
        List<User> list = toList(//
                new User("", 23), new User("", 24), new User("", 25));

        Transformer<User, String> invokerTransformer = TransformerUtils.invokerTransformer("getName");
        List<String> collect1 = CollectionsUtil.collect(list, invokerTransformer);

        assertThat(collect1, hasItems("", "", ""));
    }

    /**
     * Test collect3.
     */
    @Test
    public void testCollect3() {
        List<User> list = toList(//
                new User("", 23), new User("", 24), new User("", 25));

        List<String> collect1 = CollectionsUtil.collect(list, TransformerUtils.constantTransformer("jintian"));
        assertThat(collect1, hasItems("jintian", "jintian", "jintian"));
    }

    /**
     * Removes the duplicate.
     */
    @Test
    public void testRemoveDuplicate() {
        List<String> list = toList("feilong1", "feilong2", "feilong2", "feilong3");

        List<String> removeDuplicate = CollectionsUtil.removeDuplicate(list);

        assertSame(3, removeDuplicate.size());
        assertThat(removeDuplicate, hasItems("feilong1", "feilong2", "feilong3"));

        assertSame(4, list.size());
        assertThat(list, hasItems("feilong1", "feilong2", "feilong2", "feilong3"));

        assertEquals(emptyList(), CollectionsUtil.removeDuplicate(null));
    }

    /**
     * Test remove.
     */
    @Test
    public void testIndexOf() {
        List<User> list = toList(//
                new User("", 23), new User("", 24), new User("", 25));

        assertEquals(0, CollectionsUtil.indexOf(list, "name", ""));
        assertEquals(1, CollectionsUtil.indexOf(list, "age", 24));
        assertEquals(-1, CollectionsUtil.indexOf(null, "age", 24));
        assertEquals(-1, CollectionsUtil.indexOf(new ArrayList<User>(), "age", 24));
    }

    /**
     * Test group one.
     */
    @Test
    public void testGroupOne() {
        User zhangfei = new User("", 23);
        User liubei25 = new User("", 25);
        User liubei30 = new User("", 30);
        List<User> list = toList(zhangfei, liubei25, liubei30);
        Map<String, User> map = CollectionsUtil.groupOne(list, "name");

        assertThat(map.keySet(), is(hasSize(2)));
        assertThat(map, allOf(//
                hasEntry("", zhangfei), hasEntry("", liubei25), not(hasEntry("", liubei30))));
    }

    /**
     * Test group.
     */
    @Test
    public void testGroup() {
        List<User> list = toList(//
                new User("", 23), new User("", 25), new User("", 25));

        Map<String, List<User>> map = CollectionsUtil.group(list, "name");
        LOGGER.debug(JsonUtil.format(map));
    }

    /**
     * Test group2.
     */
    @Test
    public void testGroup2() {
        User zhangfei28 = new User("", 28);
        User liubei32 = new User("", 32);
        User liubei30 = new User("", 30);
        List<User> list = toList(//
                new User("", 10), zhangfei28, liubei32, liubei30, new User("", 10));

        Predicate<User> comparatorPredicate = BeanPredicateUtil.comparatorPredicate("age", 20, Criterion.LESS);
        Map<String, List<User>> map = CollectionsUtil.group(list, "name", comparatorPredicate);
        LOGGER.debug(JsonUtil.format(map));

        assertThat(map, allOf(//
                hasKey(""), hasKey(""), hasEntry(is(""), hasItem(zhangfei28)),
                hasEntry(is(""), hasItems(liubei32, liubei30))));
        assertSame(2, map.size());

    }

    /**
     * Test select.
     */
    @Test
    public void testSelect() {
        User zhangfei = new User("", 23);
        User guanyu = new User("", 24);
        User liubei = new User("", 25);
        List<User> list = toList(zhangfei, guanyu, liubei);
        assertThat(CollectionsUtil.select(list, "name", toList("", "")),
                allOf(hasItem(zhangfei), hasItem(liubei), not(hasItem(guanyu))));
    }

    /**
     * Test find.
     */
    @Test
    public void testFind() {
        User zhangfei = new User("", 23);
        User guanyu24 = new User("", 24);
        User liubei = new User("", 25);
        User guanyu50 = new User("", 50);
        List<User> list = toList(zhangfei, guanyu24, liubei, guanyu50);

        assertThat(CollectionsUtil.find(list, "name", ""), is(equalTo(guanyu24)));
    }

    /**
     * Test find2.
     */
    @Test
    public void testFind2() {
        User guanyu30 = new User("", 30);
        List<User> list = toList(//
                new User("", 23), new User("", 24), new User("", 25), guanyu30);
        Predicate<User> predicate = PredicateUtils.andPredicate(BeanPredicateUtil.equalPredicate("name", ""),
                BeanPredicateUtil.equalPredicate("age", 30));

        assertEquals(guanyu30, CollectionsUtil.find(list, predicate));
    }

    /**
     * Test select value.
     */
    @Test
    public void testSelectValue() {
        User zhangfei = new User("", 23);
        User guanyu = new User("", 24);
        User liubei = new User("", 25);
        List<User> list = toList(zhangfei, guanyu, liubei, guanyu);

        assertThat(CollectionsUtil.select(list, "name", ""),
                allOf(hasItem(guanyu), hasItem(guanyu), not(hasItem(zhangfei)), not(hasItem(liubei))));
    }

    /**
     * Test select array.
     */
    @Test
    public void testSelectArray() {
        User zhangfei = new User("", 23);
        User guanyu = new User("", 30);
        User liubei = new User("", 25);
        List<User> list = toList(zhangfei, guanyu, liubei);

        List<User> select = CollectionsUtil.select(list, "name", "", "");
        assertThat(select, allOf(hasItem(liubei), hasItem(guanyu), not(hasItem(zhangfei))));
    }

    @Test
    public void testSelectPredicate1() {
        List<Long> list = toList(1L, 1L, 2L, 3L);
        assertThat(CollectionsUtil.select(list, new EqualPredicate<Long>(1L)), contains(1L, 1L));
        assertEquals(emptyList(), CollectionsUtil.select(null, new EqualPredicate<Long>(1L)));
    }

    /**
     * Test remove all.
     */
    @Test
    public void testRemoveAll() {
        User zhangfei = new User("", 23);
        User guanyu = new User("", 24);
        User liubei = new User("", 25);
        List<User> list = toList(zhangfei, guanyu, liubei);

        List<User> removeAll = CollectionsUtil.removeAll(list, "name", toList("", ""));

        assertThat(removeAll, allOf(hasItem(guanyu), not(hasItem(zhangfei)), not(hasItem(liubei))));
        assertThat(list, allOf(hasItem(zhangfei), hasItem(liubei), hasItem(guanyu)));

    }

    /**
     * Test remove all1.
     */
    @Test
    public void testRemoveAll1() {
        User zhangfei = new User("", 23);
        User guanyu = new User("", 24);
        User liubei = new User("", 25);
        List<User> list = toList(zhangfei, guanyu, liubei);
        LOGGER.debug(JsonUtil.format(CollectionsUtil.removeAll(list, "name", "")));
        LOGGER.debug(JsonUtil.format(CollectionsUtil.removeAll(list, "name", "", "")));
    }

    /**
     * Test select rejected1.
     */
    @Test
    public void testSelectRejected1() {
        User zhangfei = new User("", 23);
        User guanyu = new User("", 24);
        User liubei = new User("", 25);
        List<User> list = toList(zhangfei, guanyu, liubei);

        List<User> selectRejected = CollectionsUtil.selectRejected(list, "name", "", "");
        assertSame(1, selectRejected.size());
        assertThat(selectRejected.get(0), hasProperty("name", equalTo("")));
    }

    /**
     * Test select rejected.
     */
    @Test
    public void testSelectRejected() {
        User zhangfei = new User("", 23);
        User guanyu = new User("", 24);
        User liubei = new User("", 25);
        List<User> list = toList(zhangfei, guanyu, liubei);

        List<User> selectRejected = CollectionsUtil.selectRejected(list, "name", toList("", ""));
        assertThat(selectRejected, hasSize(1));
        assertThat(selectRejected, allOf(//
                hasItem(guanyu), not(hasItem(zhangfei)), not(hasItem(liubei))));
    }

    //******************************************************************************************************

    /**
     * Test get property value map.
     */
    @Test
    public void testGetPropertyValueMap() {
        User zhangfei = new User("", 23);
        User guanyu = new User("", 24);
        User liubei = new User("", 25);
        List<User> list = toList(zhangfei, guanyu, liubei);

        Map<String, Integer> map = CollectionsUtil.getPropertyValueMap(list, "name", "age");

        assertThat(map, allOf(//
                hasEntry("", 23), hasEntry("", 24), hasEntry("", 25)));
    }

    /**
     * Test get property value list.
     */
    @Test
    public void testGetPropertyValueList() {
        List<User> list = toList(//
                new User(2L), new User(5L), new User(5L));

        List<Long> resultList = CollectionsUtil.getPropertyValueList(list, "id");
        assertThat(resultList, contains(2L, 5L, 5L));

        resultList.add(7L);
        resultList.add(8L);

        assertThat(resultList, contains(2L, 5L, 5L, 7L, 8L));
    }

    /**
     * Test get property value list1.
     */
    @Test
    public void testGetPropertyValueList1() {
        UserAddress userAddress = new UserAddress();
        userAddress.setAddress("?");
        List<UserAddress> userAddresseList = toList(userAddress);

        //*******************************************************
        Map<String, String> attrMap = new HashMap<String, String>();
        attrMap.put("", "?");
        attrMap.put("?", "");
        attrMap.put("?", "");

        //*******************************************************
        UserInfo userInfo1 = new UserInfo();
        userInfo1.setAge(28);

        //*******************************************************

        User user1 = new User(2L);

        user1.setLoves(toArray("sanguo1", "xiaoshuo1"));
        user1.setUserInfo(userInfo1);
        user1.setAttrMap(attrMap);
        user1.setUserAddresseList(userAddresseList);

        //*****************************************************
        UserInfo userInfo2 = new UserInfo();
        userInfo2.setAge(null);

        User user2 = new User(3L);
        user2.setLoves(toArray("sanguo2", "xiaoshuo2"));
        user2.setUserInfo(userInfo2);
        user2.setAttrMap(attrMap);
        user2.setUserAddresseList(userAddresseList);

        List<User> userList = toList(user1, user2);

        //
        List<String> fieldValueList1 = CollectionsUtil.getPropertyValueList(userList, "loves[1]");
        LOGGER.debug(JsonUtil.format(fieldValueList1));

        //?
        List<Integer> fieldValueList2 = CollectionsUtil.getPropertyValueList(userList, "userInfo.age");
        LOGGER.debug(JsonUtil.format(fieldValueList2));

        //Map
        List<Integer> attrList = CollectionsUtil.getPropertyValueList(userList, "attrMap()");
        LOGGER.debug(JsonUtil.format(attrList));

        //?
        List<String> addressList = CollectionsUtil.getPropertyValueList(userList, "userAddresseList[0]");
        LOGGER.debug(JsonUtil.format(addressList));
    }

    /**
     * Test get property value set.
     */
    @Test
    public void testGetPropertyValueSet() {
        List<User> list = toList(//
                new User(2L), new User(5L), new User(5L));

        Set<Long> set = CollectionsUtil.getPropertyValueSet(list, "id");
        assertThat(set, contains(2L, 5L));
    }
}