/*
 * 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.collectionsutiltest;

import static java.util.Collections.emptyMap;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.hasEntry;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

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

import org.junit.Test;

import com.feilong.core.util.CollectionsUtil;
import com.feilong.store.member.User;

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

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

    /**
     * Test group.
     */
    @Test
    public void testGroup(){
        User zhangfei = new User("张飞", 23);
        User liubei25 = new User("刘备", 25);
        User liubei30 = new User("刘备", 30);
        List<User> list = toList(zhangfei, liubei25, liubei30);

        Map<String, List<User>> map = CollectionsUtil.group(list, "name");

        assertEquals(2, map.size());
        assertThat(map, allOf(hasEntry("张飞", toList(zhangfei)), hasEntry("刘备", toList(liubei25, liubei30))));
    }

    /**
     * Test group null collection.
     */
    @Test
    public void testGroupNullCollection(){
        assertEquals(emptyMap(), CollectionsUtil.group(null, "name"));
    }

    /**
     * Test group empty collection.
     */
    @Test
    public void testGroupEmptyCollection(){
        assertEquals(emptyMap(), CollectionsUtil.group(new ArrayList<>(), "name"));
    }

    /**
     * Test group null property name.
     */
    //*****
    @Test(expected = NullPointerException.class)
    public void testGroupNullPropertyName(){
        List<User> list = toList(new User("张飞", 23), new User("刘备", 25), new User("刘备", 25));
        CollectionsUtil.group(list, (String) null);
    }

    /**
     * Test group empty property name.
     */
    @Test(expected = IllegalArgumentException.class)
    public void testGroupEmptyPropertyName(){
        List<User> list = toList(new User("张飞", 23), new User("刘备", 25), new User("刘备", 25));
        CollectionsUtil.group(list, "");
    }

    /**
     * Test group blank property name.
     */
    @Test(expected = IllegalArgumentException.class)
    public void testGroupBlankPropertyName(){
        List<User> list = toList(new User("张飞", 23), new User("刘备", 25), new User("刘备", 25));
        CollectionsUtil.group(list, " ");
    }
}

@@ -1772,32 +1772,621 @@
 i30))));%0A    %7D%0A%0A
+    @Test%0A    public void testGroupInt()%7B%0A        User zhangfei = new User(%22%E5%BC%A0%E9%A3%9E%22);%0A        zhangfei.setAgeInt(25);%0A%0A        User hanxiangdi18 = new User(%22%E6%B1%89%E7%8C%AE%E5%B8%9D%22);%0A        hanxiangdi18.setAgeInt(18);%0A%0A        User zhugeliang18 = new User(%22%E8%AF%B8%E8%91%9B%E4%BA%AE%22);%0A        zhugeliang18.setAgeInt(18);%0A%0A        List%3CUser%3E list = toList(zhangfei, hanxiangdi18, zhugeliang18);%0A%0A        Map%3CInteger, List%3CUser%3E%3E map = CollectionsUtil.group(list, %22ageInt%22);%0A%0A        assertEquals(2, map.size());%0A        assertThat(map, allOf(hasEntry(25, toList(zhangfei)), hasEntry(18, toList(hanxiangdi18, zhugeliang18))));%0A    %7D%0A%0A
     /**%0A     * T

add CollectionsUtilGroupWithPropertyNameTest.testGroupInt() fix #590