Skip to content

Commit aed4bf7

Browse files
committed
tests
1 parent 82df229 commit aed4bf7

7 files changed

Lines changed: 133 additions & 16 deletions

File tree

bugsnag/src/main/java/com/bugsnag/Configuration.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,12 @@ public String[] getDiscardClasses() {
261261
return discardClassPatterns.toArray(new String[0]);
262262
}
263263

264+
/**
265+
* Set which exception classes should be ignored (not sent) by Bugsnag.
266+
* Supports glob-style wildcards: * (matches any characters) and ? (matches single character).
267+
*
268+
* @param discardClasses a list of exception class patterns to ignore
269+
*/
264270
public void setDiscardClasses(String[] discardClasses) {
265271
this.discardClasses.clear();
266272
this.discardClassPatterns.clear();
@@ -293,8 +299,8 @@ private String convertToRegex(String pattern) {
293299

294300
StringBuilder regex = new StringBuilder();
295301
for (int i = 0; i < pattern.length(); i++) {
296-
char c = pattern.charAt(i);
297-
switch (c) {
302+
char ch = pattern.charAt(i);
303+
switch (ch) {
298304
case '*':
299305
regex.append(".*");
300306
break;
@@ -315,10 +321,10 @@ private String convertToRegex(String pattern) {
315321
case '{':
316322
case '}':
317323
case '\\':
318-
regex.append('\\').append(c);
324+
regex.append('\\').append(ch);
319325
break;
320326
default:
321-
regex.append(c);
327+
regex.append(ch);
322328
break;
323329
}
324330
}

bugsnag/src/test/java/com/bugsnag/ConfigurationDiscardClassesTest.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.bugsnag;
22

3+
import static org.junit.Assert.assertEquals;
34
import static org.junit.Assert.assertFalse;
45
import static org.junit.Assert.assertTrue;
5-
import static org.junit.Assert.assertEquals;
66

77
import org.junit.Before;
88
import org.junit.Test;
@@ -22,15 +22,15 @@ public void setUp() {
2222
@Test
2323
public void testExactMatch() {
2424
config.setDiscardClasses(new String[] {"com.example.CustomException"});
25-
25+
2626
assertTrue(config.shouldIgnoreClass("com.example.CustomException"));
2727
assertFalse(config.shouldIgnoreClass("com.example.OtherException"));
2828
}
2929

3030
@Test
3131
public void testWildcardMatch() {
3232
config.setDiscardClasses(new String[] {"com.example.*"});
33-
33+
3434
assertTrue(config.shouldIgnoreClass("com.example.CustomException"));
3535
assertTrue(config.shouldIgnoreClass("com.example.OtherException"));
3636
assertTrue(config.shouldIgnoreClass("com.example."));
@@ -40,7 +40,7 @@ public void testWildcardMatch() {
4040
@Test
4141
public void testMultipleWildcards() {
4242
config.setDiscardClasses(new String[] {"com.*.Exception"});
43-
43+
4444
assertTrue(config.shouldIgnoreClass("com.example.Exception"));
4545
assertTrue(config.shouldIgnoreClass("com.other.Exception"));
4646
assertFalse(config.shouldIgnoreClass("com.example.CustomException"));
@@ -49,7 +49,7 @@ public void testMultipleWildcards() {
4949
@Test
5050
public void testQuestionMarkWildcard() {
5151
config.setDiscardClasses(new String[] {"com.example.Exception?"});
52-
52+
5353
assertTrue(config.shouldIgnoreClass("com.example.Exception1"));
5454
assertTrue(config.shouldIgnoreClass("com.example.ExceptionX"));
5555
assertFalse(config.shouldIgnoreClass("com.example.Exception"));
@@ -63,7 +63,7 @@ public void testMultiplePatterns() {
6363
"com.example.CustomException",
6464
"org.*.SpecialException"
6565
});
66-
66+
6767
assertTrue(config.shouldIgnoreClass("java.io.IOException"));
6868
assertTrue(config.shouldIgnoreClass("java.io.FileNotFoundException"));
6969
assertTrue(config.shouldIgnoreClass("com.example.CustomException"));
@@ -76,16 +76,20 @@ public void testMultiplePatterns() {
7676
public void testGetDiscardClassesReturnsOriginalPatterns() {
7777
String[] patterns = new String[] {"com.example.*", "java.io.IOException"};
7878
config.setDiscardClasses(patterns);
79-
79+
8080
String[] retrieved = config.getDiscardClasses();
8181
assertEquals(2, retrieved.length);
82-
82+
8383
// Check that patterns are returned (not regex)
8484
boolean hasWildcard = false;
8585
boolean hasExact = false;
8686
for (String pattern : retrieved) {
87-
if (pattern.equals("com.example.*")) hasWildcard = true;
88-
if (pattern.equals("java.io.IOException")) hasExact = true;
87+
if (pattern.equals("com.example.*")) {
88+
hasWildcard = true;
89+
}
90+
if (pattern.equals("java.io.IOException")) {
91+
hasExact = true;
92+
}
8993
}
9094
assertTrue(hasWildcard);
9195
assertTrue(hasExact);
@@ -95,15 +99,15 @@ public void testGetDiscardClassesReturnsOriginalPatterns() {
9599
public void testEmptyAndNullPatterns() {
96100
config.setDiscardClasses(new String[] {});
97101
assertFalse(config.shouldIgnoreClass("com.example.Exception"));
98-
102+
99103
config.setDiscardClasses(null);
100104
assertFalse(config.shouldIgnoreClass("com.example.Exception"));
101105
}
102106

103107
@Test
104108
public void testSpecialCharactersAreEscaped() {
105109
config.setDiscardClasses(new String[] {"com.example.Exception$Inner"});
106-
110+
107111
assertTrue(config.shouldIgnoreClass("com.example.Exception$Inner"));
108112
assertFalse(config.shouldIgnoreClass("com.example.ExceptionXInner"));
109113
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<include resource="org/springframework/boot/logging/logback/base.xml" />
4+
5+
<appender name="BUGSNAG" class="com.bugsnag.BugsnagAppender">
6+
<apiKey>a35a2a72bd230ac0aa0f52715bbdc6aa</apiKey>
7+
<releaseStage>production</releaseStage>
8+
<appVersion>1.0.0</appVersion>
9+
10+
<discardClass>java.lang.*</discardClass>
11+
12+
<endpoint>http://localhost:9339/notify</endpoint>
13+
</appender>
14+
15+
<root level="INFO">
16+
<appender-ref ref="BUGSNAG"/>
17+
</root>
18+
</configuration>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.bugsnag.mazerunner.scenarios;
2+
3+
import com.bugsnag.Bugsnag;
4+
5+
/**
6+
* Attempts to send ignored handled exceptions using wildcard patterns to Bugsnag,
7+
* which should not result in any operation.
8+
*/
9+
public class IgnoredExceptionWildcardScenario extends Scenario {
10+
11+
public IgnoredExceptionWildcardScenario(Bugsnag bugsnag) {
12+
super(bugsnag);
13+
}
14+
15+
@Override
16+
public void run() {
17+
// Use wildcard pattern to ignore all RuntimeException and its subclasses
18+
bugsnag.setDiscardClasses("java.lang.*");
19+
20+
// These should all be ignored due to the wildcard pattern
21+
bugsnag.notify(new RuntimeException("Should never appear"));
22+
bugsnag.notify(new IllegalArgumentException("Should never appear"));
23+
bugsnag.notify(new IllegalStateException("Should never appear"));
24+
25+
// This should also be sent but will be ignored due to pattern
26+
try {
27+
throw new NullPointerException("Should never appear");
28+
} catch (Exception e) {
29+
bugsnag.notify(e);
30+
}
31+
}
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.bugsnag.mazerunner.scenarios;
2+
3+
import com.bugsnag.Bugsnag;
4+
5+
/**
6+
* Tests multiple wildcard patterns working together.
7+
* Uses both * and ? wildcards along with exact matches.
8+
*/
9+
public class MultipleWildcardPatternsScenario extends Scenario {
10+
11+
public MultipleWildcardPatternsScenario(Bugsnag bugsnag) {
12+
super(bugsnag);
13+
}
14+
15+
@Override
16+
public void run() {
17+
// Set multiple patterns: wildcards and exact matches
18+
bugsnag.setDiscardClasses(
19+
"java.io.*", // All java.io exceptions
20+
"java.lang.IllegalStateException", // Exact match
21+
"java.lang.Illegal*" // All IllegalXException classes
22+
);
23+
24+
// These should all be ignored
25+
bugsnag.notify(new java.io.IOException("Should be ignored - java.io.*"));
26+
bugsnag.notify(new java.io.FileNotFoundException("Should be ignored - java.io.*"));
27+
bugsnag.notify(new IllegalStateException("Should be ignored - exact match"));
28+
bugsnag.notify(new IllegalArgumentException("Should be ignored - java.lang.Illegal*"));
29+
30+
// This should be sent (not matching any pattern)
31+
bugsnag.notify(new RuntimeException("Should be sent"));
32+
}
33+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Feature: Reports are ignored with wildcard patterns
2+
3+
Scenario: Exception classname ignored with wildcard in plain Java app
4+
When I run "IgnoredExceptionWildcardScenario" with the defaults
5+
Then I should receive no errors
6+
7+
Scenario: Exception classname ignored with wildcard in spring boot app
8+
When I run spring boot "IgnoredExceptionWildcardScenario" with the defaults
9+
Then I should receive no errors
10+
11+
Scenario: Exception classname ignored with wildcard in plain spring app
12+
When I run plain Spring "IgnoredExceptionWildcardScenario" with the defaults
13+
Then I should receive no errors
14+
15+
Scenario: Test logback appender with wildcard pattern for ignored error class
16+
When I run "LogbackScenario" with logback config "ignored_class_wildcard_config.xml"
17+
Then I should receive no errors
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Feature: Multiple wildcard patterns for ignoring reports
2+
3+
Scenario: Multiple wildcard patterns in plain Java app
4+
When I run "MultipleWildcardPatternsScenario" with the defaults
5+
Then I should receive 1 error
6+
And the exception "errorClass" equals "java.lang.RuntimeException"
7+
And the exception "message" equals "Should be sent"

0 commit comments

Comments
 (0)