1818import static org .junit .jupiter .api .Assertions .assertThrows ;
1919import org .approvaltests .reporters .UseReporter ;
2020import org .approvaltests .reporters .AutoApproveReporter ;
21- import org .approvaltests .reporters .BeyondCompareReporter ;
2221
2322public class DateScrubberTest
2423{
@@ -109,30 +108,27 @@ void testUtilDate()
109108 @ Test
110109 void testAddScrubberWithValidRegexAndExample ()
111110 {
112- // Clear any existing custom scrubbers
113- DateScrubber .clearCustomScrubbers ();
114- // Add a custom scrubber with valid regex and example
115- DateScrubber .addScrubber ("2023-Dec-25" , "\\ d{4}-[A-Za-z]{3}-\\ d{2}" );
116- // Test that the custom scrubber works
117- DateScrubber scrubber = DateScrubber .getScrubberFor ("2024-Jan-15" );
118- assertEquals ("[Date1]" , scrubber .scrub ("2024-Jan-15" ));
111+ try
112+ {
113+ DateScrubber .addScrubber ("2023-Dec-25" , "\\ d{4}-[A-Za-z]{3}-\\ d{2}" );
114+ DateScrubber scrubber = DateScrubber .getScrubberFor ("2024-Jan-15" );
115+ assertEquals ("[Date1]" , scrubber .scrub ("2024-Jan-15" ));
116+ }
117+ finally
118+ {
119+ DateScrubber .clearCustomScrubbers ();
120+ }
119121 }
120122 @ Test
121123 void testAddScrubberWithInvalidRegex ()
122124 {
123- // Clear any existing custom scrubbers
124- DateScrubber .clearCustomScrubbers ();
125- // Test that invalid regex throws an exception
126125 assertThrows (IllegalArgumentException .class , () -> {
127126 DateScrubber .addScrubber ("2023-Dec-25" , "[invalid regex" );
128127 });
129128 }
130129 @ Test
131130 void testAddScrubberWithRegexThatDoesntMatchExample ()
132131 {
133- // Clear any existing custom scrubbers
134- DateScrubber .clearCustomScrubbers ();
135- // Test that regex that doesn't match example throws an exception
136132 assertThrows (IllegalArgumentException .class , () -> {
137133 DateScrubber .addScrubber ("2023-Dec-25" , "\\ d{2}-\\ d{2}-\\ d{4}" );
138134 });
@@ -150,70 +146,86 @@ void testAddScrubberDisplaysMessage()
150146 To suppress this message, use:
151147 DateScrubber.addScrubber("<date format>", "<regex>", false)
152148 """ ;
153- // Clear any existing custom scrubbers
154- DateScrubber .clearCustomScrubbers ();
155- // Capture system output to verify message is displayed
156- try (ConsoleOutput console = new ConsoleOutput ())
149+ try
157150 {
158- DateScrubber .addScrubber ("2023-Dec-25" , "\\ d{4}-[A-Za-z]{3}-\\ d{2}" );
159- console .verifyOutput (new Options ().inline (expected ));
151+ try (ConsoleOutput console = new ConsoleOutput ())
152+ {
153+ DateScrubber .addScrubber ("2023-Dec-25" , "\\ d{4}-[A-Za-z]{3}-\\ d{2}" );
154+ console .verifyOutput (new Options ().inline (expected ));
155+ }
156+ }
157+ finally
158+ {
159+ DateScrubber .clearCustomScrubbers ();
160160 }
161161 }
162162 @ Test
163163 void testAddScrubberSuppressMessage ()
164164 {
165- // Clear any existing custom scrubbers
166- DateScrubber .clearCustomScrubbers ();
167- // Capture system output to verify message is NOT displayed
168- try (ConsoleOutput console = new ConsoleOutput ())
165+ try
169166 {
170- DateScrubber .addScrubber ("2023-Dec-25" , "\\ d{4}-[A-Za-z]{3}-\\ d{2}" , false );
171- String output = console .getOutput ();
172- assertEquals ("" , output );
167+ try (ConsoleOutput console = new ConsoleOutput ())
168+ {
169+ DateScrubber .addScrubber ("2023-Dec-25" , "\\ d{4}-[A-Za-z]{3}-\\ d{2}" , false );
170+ String output = console .getOutput ();
171+ assertEquals ("" , output );
172+ }
173+ }
174+ finally
175+ {
176+ DateScrubber .clearCustomScrubbers ();
173177 }
174178 }
175179 @ Test
176180 void testCustomScrubberIntegrationWithGetScrubberFor ()
177181 {
178- // Clear any existing custom scrubbers
179- DateScrubber .clearCustomScrubbers ();
180- // Add a custom scrubber - use a unique pattern that doesn't conflict with built-ins
181- DateScrubber .addScrubber ("2023-Dec-25" , "\\ d{4}-[A-Za-z]{3}-\\ d{2}" , false );
182- // Test that getScrubberFor finds the custom scrubber
183- DateScrubber scrubber = DateScrubber .getScrubberFor ("2024-Jan-15" );
184- assertEquals ("[Date1]" , scrubber .scrub ("2024-Jan-15" ));
185- // Test that it works with different examples matching the pattern
186- assertEquals ("[Date1]" , scrubber .scrub ("2025-Mar-30" ));
187- assertEquals ("Meeting on [Date1] at noon" , scrubber .scrub ("Meeting on 2024-Jan-15 at noon" ));
188- assertEquals ("Due date: [Date1]" , scrubber .scrub ("Due date: 2025-Mar-30" ));
182+ try
183+ {
184+ DateScrubber .addScrubber ("2023-Dec-25" , "\\ d{4}-[A-Za-z]{3}-\\ d{2}" , false );
185+ DateScrubber scrubber = DateScrubber .getScrubberFor ("2024-Jan-15" );
186+ assertEquals ("[Date1]" , scrubber .scrub ("2024-Jan-15" ));
187+ assertEquals ("[Date1]" , scrubber .scrub ("2025-Mar-30" ));
188+ assertEquals ("Meeting on [Date1] at noon" , scrubber .scrub ("Meeting on 2024-Jan-15 at noon" ));
189+ assertEquals ("Due date: [Date1]" , scrubber .scrub ("Due date: 2025-Mar-30" ));
190+ }
191+ finally
192+ {
193+ DateScrubber .clearCustomScrubbers ();
194+ }
189195 }
190196 @ Test
191197 void testClearCustomScrubbers ()
192198 {
193- // Add a custom scrubber
194- DateScrubber .addScrubber ("2023-Dec-25" , "\\ d{4}-[A-Za-z]{3}-\\ d{2}" , false );
195- // Verify it works
196- DateScrubber scrubber = DateScrubber .getScrubberFor ("2024-Jan-15" );
197- assertEquals ("[Date1]" , scrubber .scrub ("2024-Jan-15" ));
198- // Clear custom scrubbers
199- DateScrubber .clearCustomScrubbers ();
200- // Verify the custom scrubber no longer works
201- assertThrows (Exception .class , () -> {
202- DateScrubber .getScrubberFor ("2024-Jan-15" );
203- });
199+ try
200+ {
201+ DateScrubber .addScrubber ("2023-Dec-25" , "\\ d{4}-[A-Za-z]{3}-\\ d{2}" , false );
202+ DateScrubber scrubber = DateScrubber .getScrubberFor ("2024-Jan-15" );
203+ assertEquals ("[Date1]" , scrubber .scrub ("2024-Jan-15" ));
204+ DateScrubber .clearCustomScrubbers ();
205+ assertThrows (Exception .class , () -> {
206+ DateScrubber .getScrubberFor ("2024-Jan-15" );
207+ });
208+ }
209+ finally
210+ {
211+ DateScrubber .clearCustomScrubbers ();
212+ }
204213 }
205214 @ Test
206215 void testMultipleCustomScrubbers ()
207216 {
208- // Clear any existing custom scrubbers
209- DateScrubber .clearCustomScrubbers ();
210- // Add multiple custom scrubbers
211- DateScrubber .addScrubber ("2023-Dec-25" , "\\ d{4}-[A-Za-z]{3}-\\ d{2}" , false );
212- DateScrubber .addScrubber ("25/Dec/2023" , "\\ d{2}/[A-Za-z]{3}/\\ d{4}" , false );
213- // Test both custom scrubbers work
214- DateScrubber scrubber1 = DateScrubber .getScrubberFor ("2024-Jan-15" );
215- assertEquals ("[Date1]" , scrubber1 .scrub ("2024-Jan-15" ));
216- DateScrubber scrubber2 = DateScrubber .getScrubberFor ("15/Jan/2024" );
217- assertEquals ("[Date1]" , scrubber2 .scrub ("15/Jan/2024" ));
217+ try
218+ {
219+ DateScrubber .addScrubber ("2023-Dec-25" , "\\ d{4}-[A-Za-z]{3}-\\ d{2}" , false );
220+ DateScrubber .addScrubber ("25/Dec/2023" , "\\ d{2}/[A-Za-z]{3}/\\ d{4}" , false );
221+ DateScrubber scrubber1 = DateScrubber .getScrubberFor ("2024-Jan-15" );
222+ assertEquals ("[Date1]" , scrubber1 .scrub ("2024-Jan-15" ));
223+ DateScrubber scrubber2 = DateScrubber .getScrubberFor ("15/Jan/2024" );
224+ assertEquals ("[Date1]" , scrubber2 .scrub ("15/Jan/2024" ));
225+ }
226+ finally
227+ {
228+ DateScrubber .clearCustomScrubbers ();
229+ }
218230 }
219231}
0 commit comments