001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.util;
015
016import static org.junit.Assert.assertTrue;
017
018import java.util.ArrayList;
019import java.util.Arrays;
020import java.util.Collection;
021import java.util.List;
022
023import org.junit.Test;
024
025/**
026 * Unit tests for {@link StringCollectionUtil}.
027 *
028 * @author Carl Harris
029 */
030public class StringCollectionUtilTest {
031
032    @Test
033    public void testRetainMatchingWithNoPatterns() throws Exception {
034        Collection<String> values = stringToList("A");
035        StringCollectionUtil.retainMatching(values);
036        assertTrue(values.contains("A"));
037    }
038
039    @Test
040    public void testRetainMatchingWithMatchingPattern() throws Exception {
041        Collection<String> values = stringToList("A");
042        StringCollectionUtil.retainMatching(values, "A");
043        assertTrue(values.contains("A"));
044    }
045
046    @Test
047    public void testRetainMatchingWithNoMatchingPattern() throws Exception {
048        Collection<String> values = stringToList("A");
049        StringCollectionUtil.retainMatching(values, "B");
050        assertTrue(values.isEmpty());
051    }
052
053    @Test
054    public void testRemoveMatchingWithNoPatterns() throws Exception {
055        Collection<String> values = stringToList("A");
056        StringCollectionUtil.removeMatching(values);
057        assertTrue(values.contains("A"));
058    }
059
060    @Test
061    public void testRemoveMatchingWithMatchingPattern() throws Exception {
062        Collection<String> values = stringToList("A");
063        StringCollectionUtil.removeMatching(values, "A");
064        assertTrue(values.isEmpty());
065    }
066
067    @Test
068    public void testRemoveMatchingWithNoMatchingPattern() throws Exception {
069        Collection<String> values = stringToList("A");
070        StringCollectionUtil.removeMatching(values, "B");
071        assertTrue(values.contains("A"));
072    }
073
074    private List<String> stringToList(String... values) {
075        List<String> result = new ArrayList<String>(values.length);
076        result.addAll(Arrays.asList(values));
077        return result;
078    }
079
080}