|
| 1 | +package com.bugsnag; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | + |
| 7 | +import org.junit.Before; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import java.util.regex.Pattern; |
| 11 | + |
| 12 | +/** |
| 13 | + * Test for Configuration.shouldIgnoreClass with regex pattern matching |
| 14 | + */ |
| 15 | +public class ConfigurationDiscardClassesTest { |
| 16 | + |
| 17 | + private Configuration config; |
| 18 | + |
| 19 | + @Before |
| 20 | + public void setUp() { |
| 21 | + config = new Configuration("test-api-key"); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + public void testExactMatch() { |
| 26 | + config.setDiscardClasses(new Pattern[] {Pattern.compile("com.example.CustomException")}); |
| 27 | + |
| 28 | + assertTrue(config.shouldIgnoreClass("com.example.CustomException")); |
| 29 | + assertFalse(config.shouldIgnoreClass("com.example.OtherException")); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testWildcardMatch() { |
| 34 | + config.setDiscardClasses(new Pattern[] {Pattern.compile("com\\.example\\..*")}); |
| 35 | + |
| 36 | + assertTrue(config.shouldIgnoreClass("com.example.CustomException")); |
| 37 | + assertTrue(config.shouldIgnoreClass("com.example.OtherException")); |
| 38 | + assertTrue(config.shouldIgnoreClass("com.example.")); |
| 39 | + assertFalse(config.shouldIgnoreClass("com.other.Exception")); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testMultipleWildcards() { |
| 44 | + config.setDiscardClasses(new Pattern[] {Pattern.compile("com\\..*\\.Exception")}); |
| 45 | + |
| 46 | + assertTrue(config.shouldIgnoreClass("com.example.Exception")); |
| 47 | + assertTrue(config.shouldIgnoreClass("com.other.Exception")); |
| 48 | + assertFalse(config.shouldIgnoreClass("com.example.CustomException")); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testQuestionMarkWildcard() { |
| 53 | + config.setDiscardClasses(new Pattern[] {Pattern.compile("com\\.example\\.Exception.")}); |
| 54 | + |
| 55 | + assertTrue(config.shouldIgnoreClass("com.example.Exception1")); |
| 56 | + assertTrue(config.shouldIgnoreClass("com.example.ExceptionX")); |
| 57 | + assertFalse(config.shouldIgnoreClass("com.example.Exception")); |
| 58 | + assertFalse(config.shouldIgnoreClass("com.example.Exception12")); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testMultiplePatterns() { |
| 63 | + config.setDiscardClasses(new Pattern[] { |
| 64 | + Pattern.compile("java\\.io\\..*"), |
| 65 | + Pattern.compile("com\\.example\\.CustomException"), |
| 66 | + Pattern.compile("org\\..*\\.SpecialException") |
| 67 | + }); |
| 68 | + |
| 69 | + assertTrue(config.shouldIgnoreClass("java.io.IOException")); |
| 70 | + assertTrue(config.shouldIgnoreClass("java.io.FileNotFoundException")); |
| 71 | + assertTrue(config.shouldIgnoreClass("com.example.CustomException")); |
| 72 | + assertTrue(config.shouldIgnoreClass("org.apache.SpecialException")); |
| 73 | + assertTrue(config.shouldIgnoreClass("org.springframework.SpecialException")); |
| 74 | + assertFalse(config.shouldIgnoreClass("com.example.OtherException")); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testGetDiscardClassesReturnsOriginalPatterns() { |
| 79 | + Pattern[] patterns = new Pattern[] { |
| 80 | + Pattern.compile("com\\.example\\..*"), |
| 81 | + Pattern.compile("java\\.io\\.IOException") |
| 82 | + }; |
| 83 | + config.setDiscardClasses(patterns); |
| 84 | + |
| 85 | + Pattern[] retrieved = config.getDiscardClasses(); |
| 86 | + assertEquals(2, retrieved.length); |
| 87 | + |
| 88 | + // Check that patterns are returned |
| 89 | + boolean hasWildcard = false; |
| 90 | + boolean hasExact = false; |
| 91 | + for (Pattern pattern : retrieved) { |
| 92 | + if (pattern.pattern().equals("com\\.example\\..*")) { |
| 93 | + hasWildcard = true; |
| 94 | + } |
| 95 | + if (pattern.pattern().equals("java\\.io\\.IOException")) { |
| 96 | + hasExact = true; |
| 97 | + } |
| 98 | + } |
| 99 | + assertTrue(hasWildcard); |
| 100 | + assertTrue(hasExact); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testEmptyAndNullPatterns() { |
| 105 | + config.setDiscardClasses(new Pattern[] {}); |
| 106 | + assertFalse(config.shouldIgnoreClass("com.example.Exception")); |
| 107 | + |
| 108 | + config.setDiscardClasses(null); |
| 109 | + assertFalse(config.shouldIgnoreClass("com.example.Exception")); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void testSpecialCharactersAreEscaped() { |
| 114 | + // In regex, $ is a special character (end of line), so it needs to be escaped |
| 115 | + config.setDiscardClasses(new Pattern[] {Pattern.compile("com\\.example\\.Exception\\$Inner")}); |
| 116 | + |
| 117 | + assertTrue(config.shouldIgnoreClass("com.example.Exception$Inner")); |
| 118 | + assertFalse(config.shouldIgnoreClass("com.example.ExceptionXInner")); |
| 119 | + } |
| 120 | +} |
0 commit comments