You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IgnoreListTest.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.parser.common;
  23. import java.util.ArrayList;
  24. import java.util.Arrays;
  25. import java.util.List;
  26. import org.junit.Test;
  27. import static org.junit.Assert.*;
  28. public class IgnoreListTest {
  29. private final String[][] tests = {
  30. {"a@b.c", "a@b\\.c"},
  31. {"*chris*", ".*chris.*"},
  32. {"c???s", "c...s"},
  33. {"c*?*", "c.*..*"},
  34. {"foo?", "foo."},
  35. {".^$[]\\(){}|+", "\\.\\^\\$\\[\\]\\\\\\(\\)\\{\\}\\|\\+"},
  36. };
  37. private final String[] illegals = {
  38. "a+",
  39. "a*",
  40. "a.{4}",
  41. "a|b",
  42. "a?",
  43. "foo\\",
  44. "a\\?",
  45. "a\\*",
  46. };
  47. @Test
  48. public void testToRegex() {
  49. for (String[] test : tests) {
  50. final String convert1 = IgnoreList.simpleToRegex(test[0]);
  51. assertEquals(test[1], convert1);
  52. }
  53. }
  54. @Test
  55. public void testToSimple() {
  56. for (String[] test : tests) {
  57. final String convert2 = IgnoreList.regexToSimple(test[1]);
  58. assertEquals(test[0], convert2);
  59. }
  60. }
  61. @Test
  62. public void testIllegals() {
  63. for (String test : illegals) {
  64. boolean except = false;
  65. try {
  66. IgnoreList.regexToSimple(test);
  67. } catch (UnsupportedOperationException ex) {
  68. except = true;
  69. }
  70. assertTrue(except);
  71. }
  72. }
  73. @Test
  74. public void testConstructor() {
  75. final List<String> items = Arrays.asList("abc", "def");
  76. final IgnoreList list = new IgnoreList(items);
  77. assertEquals(items, list.getRegexList());
  78. }
  79. @Test
  80. public void testAddSimple() {
  81. final IgnoreList list = new IgnoreList();
  82. for (String[] test : tests) {
  83. list.addSimple(test[0]);
  84. assertTrue(list.getRegexList().contains(test[1]));
  85. }
  86. }
  87. @Test
  88. public void testCanConvert() {
  89. final IgnoreList list = new IgnoreList();
  90. assertTrue(list.canConvert());
  91. list.addSimple("abc!def@ghi");
  92. assertTrue(list.canConvert());
  93. list.add(illegals[0]);
  94. assertFalse(list.canConvert());
  95. }
  96. @Test
  97. public void testGetSimpleList() throws UnsupportedOperationException {
  98. final IgnoreList list = new IgnoreList();
  99. final List<String> items = new ArrayList<>();
  100. for (String[] test : tests) {
  101. items.add(test[0]);
  102. list.add(test[1]);
  103. }
  104. assertEquals(items, list.getSimpleList());
  105. }
  106. }