<commit_before>package org.lxp.java8.map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.junit.Test;
import org.lxp.vo.Student;

/**
 * @see https://www.cnblogs.com/hiver/p/9156147.html
 */
public class CollectionToMapTest {
    private List<Student> fakeStudent() {
        List<Student> students = new ArrayList<>();
        students.add(new Student("1", "name1", false, 2));
        students.add(new Student("2", "name2", false, 2));
        students.add(new Student("3", "name2", null, 2));
        students.add(new Student("4", "name4", true, 2));
        students.add(new Student(null, "name5", true, 2));
        return students;
    }

    @Test(expected = NullPointerException.class)
    public void shouldThrowNPEWhenGroupingByNullKey() {
        fakeStudent().stream().collect(Collectors.groupingBy(Student::getStudentNo));
    }

    @Test
    public void shouldReturnMapWhenToMapNullKey() {
        Map<String, Student> map = fakeStudent().stream()
                .collect(Collectors.toMap(Student::getStudentNo, Function.identity()));
        assertEquals("{null=Student [studentNo=null, name=name5, gender=true, age=2], "
                + "1=Student [studentNo=1, name=name1, gender=false, age=2], "
                + "2=Student [studentNo=2, name=name2, gender=false, age=2], "
                + "3=Student [studentNo=3, name=name2, gender=null, age=2], "
                + "4=Student [studentNo=4, name=name4, gender=true, age=2]}", map.toString());
    }

    @Test(expected = NullPointerException.class)
    public void shouldReturnMapWhenPartitioningByNullKey() {
        fakeStudent().stream().collect(Collectors.partitioningBy(Student::getGender));
    }

    @Test
    public void shouldThrowIllegalStateExceptionWhenToMapDuplicateKey() {
        Map<String, Student> map = null;
        try {
            map = fakeStudent().stream().collect(Collectors.toMap(Student::getName, Function.identity()));
        } catch (Exception e) {
            assertTrue(e instanceof IllegalStateException);
            assertEquals("Duplicate key Student [studentNo=2, name=name2, gender=false, age=2]", e.getMessage());
        }
        assertNull(map);
    }
<commit_msg>add shouldThrowNPEWhenToMapNullValue<commit_after>package org.lxp.java8.map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.junit.Test;
import org.lxp.vo.Student;

/**
 * @see https://www.cnblogs.com/hiver/p/9156147.html
 */
public class CollectionToMapTest {
    private List<Student> fakeStudent() {
        List<Student> students = new ArrayList<>();
        students.add(new Student("1", "name1", false, 2));
        students.add(new Student("2", "name2", false, 2));
        students.add(new Student("3", "name2", null, 2));
        students.add(new Student("4", "name4", true, 2));
        students.add(new Student(null, "name5", true, 2));
        return students;
    }

    @Test(expected = NullPointerException.class)
    public void shouldThrowNPEWhenGroupingByNullKey() {
        fakeStudent().stream().collect(Collectors.groupingBy(Student::getStudentNo));
    }

    @Test
    public void shouldReturnMapWhenToMapNullKey() {
        Map<String, Student> map = fakeStudent().stream()
                .collect(Collectors.toMap(Student::getStudentNo, Function.identity()));
        assertEquals("{null=Student [studentNo=null, name=name5, gender=true, age=2], "
                + "1=Student [studentNo=1, name=name1, gender=false, age=2], "
                + "2=Student [studentNo=2, name=name2, gender=false, age=2], "
                + "3=Student [studentNo=3, name=name2, gender=null, age=2], "
                + "4=Student [studentNo=4, name=name4, gender=true, age=2]}", map.toString());
    }

    @Test(expected = NullPointerException.class)
    public void shouldThrowNPEWhenToMapNullValue() {
        fakeStudent().stream().collect(Collectors.toMap(Student::getStudentNo, Student::getGender));
    }

    @Test(expected = NullPointerException.class)
    public void shouldReturnMapWhenPartitioningByNullKey() {
        fakeStudent().stream().collect(Collectors.partitioningBy(Student::getGender));
    }

    @Test
    public void shouldThrowIllegalStateExceptionWhenToMapDuplicateKey() {
        Map<String, Student> map = null;
        try {
            map = fakeStudent().stream().collect(Collectors.toMap(Student::getName, Function.identity()));
        } catch (Exception e) {
            assertTrue(e instanceof IllegalStateException);
            assertEquals("Duplicate key Student [studentNo=2, name=name2, gender=false, age=2]", e.getMessage());
        }
        assertNull(map);
    }

{'fraction_non_alphanumeric': 0.14121766193347599, 'fraction_numerical': 0.016728262983855283, 'mean_word_length': 3.546419098143236, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1891195', 'n_tokens_mistral': 1547, 'n_tokens_neox': 1522, 'n_words': 290}