View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.util;
15  
16  import static org.junit.Assert.assertTrue;
17  
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.Collection;
21  import java.util.List;
22  
23  import org.junit.Test;
24  
25  /**
26   * Unit tests for {@link StringCollectionUtil}.
27   *
28   * @author Carl Harris
29   */
30  public class StringCollectionUtilTest {
31  
32      @Test
33      public void testRetainMatchingWithNoPatterns() throws Exception {
34          Collection<String> values = stringToList("A");
35          StringCollectionUtil.retainMatching(values);
36          assertTrue(values.contains("A"));
37      }
38  
39      @Test
40      public void testRetainMatchingWithMatchingPattern() throws Exception {
41          Collection<String> values = stringToList("A");
42          StringCollectionUtil.retainMatching(values, "A");
43          assertTrue(values.contains("A"));
44      }
45  
46      @Test
47      public void testRetainMatchingWithNoMatchingPattern() throws Exception {
48          Collection<String> values = stringToList("A");
49          StringCollectionUtil.retainMatching(values, "B");
50          assertTrue(values.isEmpty());
51      }
52  
53      @Test
54      public void testRemoveMatchingWithNoPatterns() throws Exception {
55          Collection<String> values = stringToList("A");
56          StringCollectionUtil.removeMatching(values);
57          assertTrue(values.contains("A"));
58      }
59  
60      @Test
61      public void testRemoveMatchingWithMatchingPattern() throws Exception {
62          Collection<String> values = stringToList("A");
63          StringCollectionUtil.removeMatching(values, "A");
64          assertTrue(values.isEmpty());
65      }
66  
67      @Test
68      public void testRemoveMatchingWithNoMatchingPattern() throws Exception {
69          Collection<String> values = stringToList("A");
70          StringCollectionUtil.removeMatching(values, "B");
71          assertTrue(values.contains("A"));
72      }
73  
74      private List<String> stringToList(String... values) {
75          List<String> result = new ArrayList<String>(values.length);
76          result.addAll(Arrays.asList(values));
77          return result;
78      }
79  
80  }